Cleanup and replace some more references to getMaster()

This commit is contained in:
Sollace 2022-12-19 17:27:24 +01:00
parent bf5649a966
commit daa508ba73
12 changed files with 62 additions and 75 deletions

View file

@ -21,16 +21,6 @@ public interface Owned<E extends Entity> {
@Nullable @Nullable
E getMaster(); E getMaster();
/**
* Gets the unique entity id of the entity that holds this object.
* <p>
* Since {@link Owned#getMaster()} will only return if the owner is loaded, use this to perform checks
* in the owner's absence.
*/
default Optional<UUID> getMasterId() {
return Optional.of(getMaster()).map(Entity::getUuid);
}
/** /**
* Updates the owner of this object. * Updates the owner of this object.
*/ */
@ -45,6 +35,20 @@ public interface Owned<E extends Entity> {
setMaster(sibling.getMaster()); setMaster(sibling.getMaster());
} }
default boolean hasMaster() {
return getMaster() != null;
}
/**
* Gets the unique entity id of the entity that holds this object.
* <p>
* Since {@link Owned#getMaster()} will only return if the owner is loaded, use this to perform checks
* in the owner's absence.
*/
default Optional<UUID> getMasterId() {
return Optional.of(getMaster()).map(Entity::getUuid);
}
default boolean isOwnedBy(@Nullable Object owner) { default boolean isOwnedBy(@Nullable Object owner) {
return owner instanceof Entity e return owner instanceof Entity e
&& getMasterId().isPresent() && getMasterId().isPresent()

View file

@ -35,6 +35,11 @@ public interface WeaklyOwned<E extends Entity> extends Owned<E> {
} }
} }
@Override
default void setMaster(E master) {
getMasterReference().set(master);
}
default World getReferenceWorld() { default World getReferenceWorld() {
return ((Entity)this).getEntityWorld(); return ((Entity)this).getEntityWorld();
} }
@ -45,11 +50,6 @@ public interface WeaklyOwned<E extends Entity> extends Owned<E> {
return getMasterReference().get(getReferenceWorld()); return getMasterReference().get(getReferenceWorld());
} }
@Override
default void setMaster(E master) {
getMasterReference().set(master);
}
@Override @Override
default Optional<UUID> getMasterId() { default Optional<UUID> getMasterId() {
return getMasterReference().getId(); return getMasterReference().getId();

View file

@ -87,7 +87,7 @@ public class WorldRenderDelegate {
matrices.push(); matrices.push();
if (pony.getPhysics().isGravityNegative()) { if (pony.getPhysics().isGravityNegative()) {
matrices.translate(0, -((ItemImpl) pony).getMaster().getHeight() * 1.1, 0); matrices.translate(0, -((ItemImpl) pony).asEntity().getHeight() * 1.1, 0);
} }
return false; return false;

View file

@ -77,6 +77,11 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster<Livin
return owner; return owner;
} }
@Override
public World getReferenceWorld() {
return WeaklyOwned.super.getReferenceWorld();
}
@Override @Override
public Entity getEntity() { public Entity getEntity() {
return this; return this;
@ -150,9 +155,4 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster<Livin
public Packet<ClientPlayPacketListener> createSpawnPacket() { public Packet<ClientPlayPacketListener> createSpawnPacket() {
return Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this)); return Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this));
} }
@Override
public World getReferenceWorld() {
return WeaklyOwned.super.getReferenceWorld();
}
} }

View file

@ -44,7 +44,7 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
private final EntityPhysics<LivingEntity> physics; private final EntityPhysics<LivingEntity> physics;
private final EntityReference<LivingEntity> master = new EntityReference<>(); private final EntityReference<LivingEntity> owner = new EntityReference<>();
@Nullable @Nullable
private GoalSelector goals; private GoalSelector goals;
@ -58,21 +58,21 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
public Creature(LivingEntity entity) { public Creature(LivingEntity entity) {
super(entity, EFFECT); super(entity, EFFECT);
physics = new EntityPhysics<>(entity, GRAVITY); physics = new EntityPhysics<>(entity, GRAVITY);
entity.getDataTracker().startTracking(MASTER, master.toNBT()); entity.getDataTracker().startTracking(MASTER, owner.toNBT());
entity.getDataTracker().startTracking(EATING, 0); entity.getDataTracker().startTracking(EATING, 0);
} }
@Override @Override
public void setMaster(LivingEntity owner) { public void setMaster(LivingEntity owner) {
master.set(owner); this.owner.set(owner);
entity.getDataTracker().set(MASTER, master.toNBT()); entity.getDataTracker().set(MASTER, this.owner.toNBT());
if (targets != null && owner != null) { if (targets != null && owner != null) {
initMinionAi(); initMinionAi();
} }
} }
public boolean isMinion() { public boolean isMinion() {
return master.getId().isPresent(); return owner.getId().isPresent();
} }
@Override @Override
@ -84,13 +84,13 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
@NotNull @NotNull
public LivingEntity getMaster() { public LivingEntity getMaster() {
NbtCompound data = entity.getDataTracker().get(MASTER); NbtCompound data = entity.getDataTracker().get(MASTER);
master.fromNBT(data); owner.fromNBT(data);
return master.getOrEmpty(getReferenceWorld()).orElse(entity); return owner.getOrEmpty(getReferenceWorld()).orElse(entity);
} }
@Override @Override
public EntityReference<LivingEntity> getMasterReference() { public EntityReference<LivingEntity> getMasterReference() {
return master; return owner;
} }
public Optional<GoalSelector> getTargets() { public Optional<GoalSelector> getTargets() {
@ -116,7 +116,7 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
goals.add(3, eatMuffinGoal); goals.add(3, eatMuffinGoal);
} }
if (master.isPresent(getReferenceWorld())) { if (owner.isPresent(getReferenceWorld())) {
initMinionAi(); initMinionAi();
} }
@ -229,7 +229,7 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
getSpellSlot().get(true).ifPresent(effect -> { getSpellSlot().get(true).ifPresent(effect -> {
compound.put("effect", Spell.writeNbt(effect)); compound.put("effect", Spell.writeNbt(effect));
}); });
compound.put("master", master.toNBT()); compound.put("master", owner.toNBT());
physics.toNBT(compound); physics.toNBT(compound);
} }
@ -240,8 +240,8 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect"))); getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
} }
if (compound.contains("master", NbtElement.COMPOUND_TYPE)) { if (compound.contains("master", NbtElement.COMPOUND_TYPE)) {
master.fromNBT(compound.getCompound("master")); owner.fromNBT(compound.getCompound("master"));
if (master.isPresent(getReferenceWorld()) && targets != null) { if (owner.isPresent(getReferenceWorld()) && targets != null) {
initMinionAi(); initMinionAi();
} }
} }

View file

@ -2,18 +2,12 @@ package com.minelittlepony.unicopia.entity;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.NotNull; import com.minelittlepony.unicopia.*;
import com.minelittlepony.unicopia.Owned;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.UTags;
import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.item.enchantment.UEnchantments;
import com.minelittlepony.unicopia.util.VecHelper; import com.minelittlepony.unicopia.util.VecHelper;
import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity; import net.minecraft.entity.*;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.MovementType;
import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.data.TrackedDataHandlerRegistry;
@ -28,7 +22,7 @@ import net.minecraft.util.ActionResult;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random; import net.minecraft.util.math.random.Random;
public class ItemImpl implements Equine<ItemEntity>, Owned<ItemEntity> { public class ItemImpl implements Equine<ItemEntity> {
private static final TrackedData<String> ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.STRING); private static final TrackedData<String> ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.STRING);
static final TrackedData<Float> ITEM_GRAVITY = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.FLOAT); static final TrackedData<Float> ITEM_GRAVITY = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.FLOAT);
@ -158,17 +152,6 @@ public class ItemImpl implements Equine<ItemEntity>, Owned<ItemEntity> {
physics.fromNBT(compound); physics.fromNBT(compound);
} }
@Override
public void setMaster(ItemEntity owner) {
}
@Override
@NotNull
public ItemEntity getMaster() {
return asEntity();
}
@Override @Override
public ItemEntity asEntity() { public ItemEntity asEntity() {
return entity; return entity;
@ -215,11 +198,11 @@ public class ItemImpl implements Equine<ItemEntity>, Owned<ItemEntity> {
} }
default float getFollowDistance(IItemEntity entity) { default float getFollowDistance(IItemEntity entity) {
return 6 * (1 + EnchantmentHelper.getLevel(UEnchantments.CLINGY, entity.get().getMaster().getStack())); return 6 * (1 + EnchantmentHelper.getLevel(UEnchantments.CLINGY, entity.get().asEntity().getStack()));
} }
default float getFollowSpeed(IItemEntity entity) { default float getFollowSpeed(IItemEntity entity) {
return Math.min(1, 0.02F * (1 + EnchantmentHelper.getLevel(UEnchantments.CLINGY, entity.get().getMaster().getStack()))); return Math.min(1, 0.02F * (1 + EnchantmentHelper.getLevel(UEnchantments.CLINGY, entity.get().asEntity().getStack())));
} }
default void interactWithPlayer(IItemEntity entity, PlayerEntity player) { default void interactWithPlayer(IItemEntity entity, PlayerEntity player) {

View file

@ -4,7 +4,6 @@ import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.USounds; import com.minelittlepony.unicopia.USounds;
@ -99,16 +98,6 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
return armour; return armour;
} }
@Override
public void setMaster(LivingEntity owner) {
}
@Override
@NotNull
public LivingEntity getMaster() {
return asEntity();
}
/** /**
* @deprecated use asEntity() * @deprecated use asEntity()
*/ */

View file

@ -42,7 +42,7 @@ public class CorruptInfluenceStatusEffect extends StatusEffect {
} }
if (nearby > 1) { if (nearby > 1) {
if (Equine.of(entity).filter(eq -> eq instanceof Owned && ((Owned<?>)eq).getMaster() != null).isPresent()) { if (Equine.of(entity).filter(eq -> eq instanceof Owned<?> o && o.hasMaster()).isPresent()) {
return; return;
} }
@ -59,7 +59,9 @@ public class CorruptInfluenceStatusEffect extends StatusEffect {
clone.copyPositionAndRotation(entity); clone.copyPositionAndRotation(entity);
Equine.of(clone).ifPresent(eq -> { Equine.of(clone).ifPresent(eq -> {
((Owned<Entity>)eq).setMaster(mob); if (eq instanceof Owned) {
((Owned<Entity>)eq).setMaster(mob);
}
}); });
mob.world.spawnEntity(clone); mob.world.spawnEntity(clone);

View file

@ -297,6 +297,14 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
return asEntity(); return asEntity();
} }
/**
* @deprecated Pony cannot belong to other entities
*/
@Override
@Deprecated
public void setMaster(@Nullable LivingEntity owner) {
}
public void onSpawn() { public void onSpawn() {
if (entity.world instanceof ServerWorld sw if (entity.world instanceof ServerWorld sw
&& getObservedSpecies() == Race.BAT && getObservedSpecies() == Race.BAT

View file

@ -23,7 +23,7 @@ public class AppleItem {
} }
private static ActionResult onGroundTick(IItemEntity item) { private static ActionResult onGroundTick(IItemEntity item) {
ItemEntity entity = item.get().getMaster(); ItemEntity entity = item.get().asEntity();
if (!entity.isRemoved() && item.getPickupDelay() == 0 && item.getAge() > 2030 && entity.world.random.nextInt(150) < 10) { if (!entity.isRemoved() && item.getPickupDelay() == 0 && item.getAge() > 2030 && entity.world.random.nextInt(150) < 10) {

View file

@ -39,7 +39,7 @@ public class JarItem extends ProjectileItem implements ItemImpl.GroundTickCallba
@Override @Override
public ActionResult onGroundTick(IItemEntity item) { public ActionResult onGroundTick(IItemEntity item) {
ItemEntity entity = item.get().getMaster(); ItemEntity entity = item.get().asEntity();
entity.setInvulnerable(true); entity.setInvulnerable(true);

View file

@ -7,6 +7,7 @@ import java.util.function.Function;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.Affinity; import com.minelittlepony.unicopia.Affinity;
import com.minelittlepony.unicopia.Owned;
import com.minelittlepony.unicopia.ability.magic.Affine; import com.minelittlepony.unicopia.ability.magic.Affine;
import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.Levelled; import com.minelittlepony.unicopia.ability.magic.Levelled;
@ -52,7 +53,7 @@ import net.minecraft.world.World;
* *
* Can also carry a spell if needed. * Can also carry a spell if needed.
*/ */
public class MagicProjectileEntity extends ThrownItemEntity implements Caster<LivingEntity> { public class MagicProjectileEntity extends ThrownItemEntity implements Caster<LivingEntity>, Owned<LivingEntity> {
private static final TrackedData<Float> DAMAGE = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT); private static final TrackedData<Float> DAMAGE = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT);
private static final TrackedData<Float> GRAVITY = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT); private static final TrackedData<Float> GRAVITY = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT);
private static final TrackedData<Boolean> HYDROPHOBIC = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.BOOLEAN); private static final TrackedData<Boolean> HYDROPHOBIC = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
@ -106,16 +107,16 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Li
setOwner(owner); setOwner(owner);
} }
public void setHomingTarget(@Nullable Entity target) {
homingTarget.set(target);
}
@Override @Override
@Nullable @Nullable
public LivingEntity getMaster() { public LivingEntity getMaster() {
return (LivingEntity)getOwner(); return (LivingEntity)getOwner();
} }
public void setHomingTarget(@Nullable Entity target) {
homingTarget.set(target);
}
@Override @Override
public LevelStore getLevel() { public LevelStore getLevel() {
return Caster.of(getMaster()).map(Caster::getLevel).orElse(Levelled.EMPTY); return Caster.of(getMaster()).map(Caster::getLevel).orElse(Levelled.EMPTY);