From 766911e402cd11e9fe8be609a5a88e9429bf18ea Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 26 Apr 2020 16:48:48 +0200 Subject: [PATCH] Fixed items not having the player's race when dropped and ensure the values are stored/loaded again --- .../ability/EarthPonyStompAbility.java | 13 +++++---- .../entity/ItemEntityCapabilities.java | 27 ++++++++++++++----- .../unicopia/mixin/MixinItemEntity.java | 23 +++++++++++++--- .../unicopia/mixin/MixinLivingEntity.java | 13 +++++++++ .../unicopia/mixin/MixinPlayerEntity.java | 8 +++--- 5 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java index c1463ec1..759058d9 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java @@ -22,7 +22,6 @@ import net.minecraft.block.Blocks; import net.minecraft.block.LeavesBlock; import net.minecraft.block.LogBlock; import net.minecraft.entity.EntityContext; -import net.minecraft.entity.EntityType; import net.minecraft.entity.ItemEntity; import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.damage.DamageSource; @@ -336,12 +335,12 @@ public class EarthPonyStompAbility implements Ability { if (state.getBlock() instanceof LeavesBlock && w.getBlockState(pos.down()).isAir()) { WorldEvent.DESTROY_BLOCK.play(w, pos, state); - - ItemEntity item = new ItemEntity(EntityType.ITEM, w); - item.setPos(pos.getX() + w.random.nextFloat(), pos.getY() - 0.5, pos.getZ() + w.random.nextFloat()); - item.setStack(TreeType.get(log).pickRandomStack()); - - drops.add(item); + drops.add(new ItemEntity(w, + pos.getX() + w.random.nextFloat(), + pos.getY() - 0.5, + pos.getZ() + w.random.nextFloat(), + TreeType.get(log).pickRandomStack() + )); } PosHelper.all(pos, p -> { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/ItemEntityCapabilities.java b/src/main/java/com/minelittlepony/unicopia/entity/ItemEntityCapabilities.java index 9e39ec0d..3a19923c 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/ItemEntityCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/ItemEntityCapabilities.java @@ -4,27 +4,40 @@ import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.ducks.IItemEntity; import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.data.DataTracker; +import net.minecraft.entity.data.TrackedData; +import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.util.ActionResult; public class ItemEntityCapabilities implements RaceContainer, Owned { - - private Race race = Race.HUMAN; + private static final TrackedData ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.INTEGER); private final ItemEntity owner; + private Race serverRace; public ItemEntityCapabilities(ItemEntity owner) { this.owner = owner; + owner.getDataTracker().startTracking(ITEM_RACE, Race.HUMAN.ordinal()); } @Override public void onUpdate() { - } @Override public boolean beforeUpdate() { + + if (!owner.world.isClient) { + Race race = getSpecies(); + if (race != serverRace) { + serverRace = race; + setSpecies(Race.HUMAN); + setSpecies(race); + } + } + ItemStack stack = owner.getStack(); if (!stack.isEmpty() && stack.getItem() instanceof TickableItem) { @@ -36,23 +49,23 @@ public class ItemEntityCapabilities implements RaceContainer, Owned< @Override public Race getSpecies() { - return race; + return Race.fromId(getOwner().getDataTracker().get(ITEM_RACE)); } @Override public void setSpecies(Race race) { - this.race = race; + getOwner().getDataTracker().set(ITEM_RACE, race.ordinal()); } @Override public void toNBT(CompoundTag compound) { - compound.putString("owner_species", race.name()); + compound.putString("owner_species", getSpecies().name()); } @Override public void fromNBT(CompoundTag compound) { - race = Race.fromName(compound.getString("owner_species")); + setSpecies(Race.fromName(compound.getString("owner_species"))); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinItemEntity.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinItemEntity.java index 76d07dc4..7b51d191 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinItemEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/MixinItemEntity.java @@ -11,11 +11,12 @@ import com.minelittlepony.unicopia.entity.ItemEntityCapabilities; import net.minecraft.entity.Entity; import net.minecraft.entity.ItemEntity; +import net.minecraft.nbt.CompoundTag; @Mixin(ItemEntity.class) abstract class MixinItemEntity extends Entity implements IItemEntity { - private final ItemEntityCapabilities caster = create(); + private ItemEntityCapabilities caster; private MixinItemEntity() { super(null, null); } @@ -26,19 +27,34 @@ abstract class MixinItemEntity extends Entity implements IItemEntity { @Override public ItemEntityCapabilities get() { + if (caster == null) { + caster = create(); + } return caster; } @Inject(method = "tick()V", at = @At("HEAD"), cancellable = true) private void beforeTick(CallbackInfo info) { - if (caster.beforeUpdate()) { + if (get().beforeUpdate()) { info.cancel(); } } @Inject(method = "tick()V", at = @At("RETURN")) private void afterTick(CallbackInfo info) { - caster.onUpdate(); + get().onUpdate(); + } + + @Inject(method = "writeCustomDataToTag(Lnet/minecraft/nbt/CompoundTag;)V", at = @At("HEAD")) + private void onWriteCustomDataToTag(CompoundTag tag, CallbackInfo info) { + if (tag.contains("unicopia_caster")) { + get().fromNBT(tag.getCompound("unicopia_caster")); + } + } + + @Inject(method = "readCustomDataFromTag(Lnet/minecraft/nbt/CompoundTag;)V", at = @At("HEAD")) + private void onReadCustomDataFromTag(CompoundTag tag, CallbackInfo info) { + tag.put("unicopia_caster", get().toNBT()); } @Accessor("age") @@ -48,4 +64,5 @@ abstract class MixinItemEntity extends Entity implements IItemEntity { @Accessor("pickupDelay") @Override public abstract int getPickupDelay(); + } diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java index 6d747da3..3cba78ba 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java @@ -12,6 +12,7 @@ import com.minelittlepony.unicopia.entity.LivingEntityCapabilities; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; +import net.minecraft.nbt.CompoundTag; @Mixin(LivingEntity.class) abstract class MixinLivingEntity extends Entity implements PonyContainer { @@ -71,6 +72,18 @@ abstract class MixinLivingEntity extends Entity implements PonyContainer info) { - PonyContainer.of(info.getReturnValue()).ifPresent(o -> { - o.get().setSpecies(get().getSpecies()); + at = @At("RETURN")) + private void onDropItem(ItemStack itemStack_1, boolean scatter, boolean retainOwnership, CallbackInfoReturnable info) { + PonyContainer.of(info.getReturnValue()).ifPresent(container -> { + container.get().setSpecies(get().getSpecies()); }); }