Fixed items not having the player's race when dropped and ensure the values are stored/loaded again

This commit is contained in:
Sollace 2020-04-26 16:48:48 +02:00
parent c731d515c4
commit 766911e402
5 changed files with 63 additions and 21 deletions

View file

@ -22,7 +22,6 @@ import net.minecraft.block.Blocks;
import net.minecraft.block.LeavesBlock; import net.minecraft.block.LeavesBlock;
import net.minecraft.block.LogBlock; import net.minecraft.block.LogBlock;
import net.minecraft.entity.EntityContext; import net.minecraft.entity.EntityContext;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity; import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.damage.DamageSource;
@ -336,12 +335,12 @@ public class EarthPonyStompAbility implements Ability<Multi> {
if (state.getBlock() instanceof LeavesBlock && w.getBlockState(pos.down()).isAir()) { if (state.getBlock() instanceof LeavesBlock && w.getBlockState(pos.down()).isAir()) {
WorldEvent.DESTROY_BLOCK.play(w, pos, state); WorldEvent.DESTROY_BLOCK.play(w, pos, state);
drops.add(new ItemEntity(w,
ItemEntity item = new ItemEntity(EntityType.ITEM, w); pos.getX() + w.random.nextFloat(),
item.setPos(pos.getX() + w.random.nextFloat(), pos.getY() - 0.5, pos.getZ() + w.random.nextFloat()); pos.getY() - 0.5,
item.setStack(TreeType.get(log).pickRandomStack()); pos.getZ() + w.random.nextFloat(),
TreeType.get(log).pickRandomStack()
drops.add(item); ));
} }
PosHelper.all(pos, p -> { PosHelper.all(pos, p -> {

View file

@ -4,27 +4,40 @@ import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ducks.IItemEntity; import com.minelittlepony.unicopia.ducks.IItemEntity;
import net.minecraft.entity.ItemEntity; 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.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
public class ItemEntityCapabilities implements RaceContainer<ItemEntity>, Owned<ItemEntity> { public class ItemEntityCapabilities implements RaceContainer<ItemEntity>, Owned<ItemEntity> {
private static final TrackedData<Integer> ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.INTEGER);
private Race race = Race.HUMAN;
private final ItemEntity owner; private final ItemEntity owner;
private Race serverRace;
public ItemEntityCapabilities(ItemEntity owner) { public ItemEntityCapabilities(ItemEntity owner) {
this.owner = owner; this.owner = owner;
owner.getDataTracker().startTracking(ITEM_RACE, Race.HUMAN.ordinal());
} }
@Override @Override
public void onUpdate() { public void onUpdate() {
} }
@Override @Override
public boolean beforeUpdate() { public boolean beforeUpdate() {
if (!owner.world.isClient) {
Race race = getSpecies();
if (race != serverRace) {
serverRace = race;
setSpecies(Race.HUMAN);
setSpecies(race);
}
}
ItemStack stack = owner.getStack(); ItemStack stack = owner.getStack();
if (!stack.isEmpty() && stack.getItem() instanceof TickableItem) { if (!stack.isEmpty() && stack.getItem() instanceof TickableItem) {
@ -36,23 +49,23 @@ public class ItemEntityCapabilities implements RaceContainer<ItemEntity>, Owned<
@Override @Override
public Race getSpecies() { public Race getSpecies() {
return race; return Race.fromId(getOwner().getDataTracker().get(ITEM_RACE));
} }
@Override @Override
public void setSpecies(Race race) { public void setSpecies(Race race) {
this.race = race; getOwner().getDataTracker().set(ITEM_RACE, race.ordinal());
} }
@Override @Override
public void toNBT(CompoundTag compound) { public void toNBT(CompoundTag compound) {
compound.putString("owner_species", race.name()); compound.putString("owner_species", getSpecies().name());
} }
@Override @Override
public void fromNBT(CompoundTag compound) { public void fromNBT(CompoundTag compound) {
race = Race.fromName(compound.getString("owner_species")); setSpecies(Race.fromName(compound.getString("owner_species")));
} }
@Override @Override

View file

@ -11,11 +11,12 @@ import com.minelittlepony.unicopia.entity.ItemEntityCapabilities;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity; import net.minecraft.entity.ItemEntity;
import net.minecraft.nbt.CompoundTag;
@Mixin(ItemEntity.class) @Mixin(ItemEntity.class)
abstract class MixinItemEntity extends Entity implements IItemEntity { abstract class MixinItemEntity extends Entity implements IItemEntity {
private final ItemEntityCapabilities caster = create(); private ItemEntityCapabilities caster;
private MixinItemEntity() { super(null, null); } private MixinItemEntity() { super(null, null); }
@ -26,19 +27,34 @@ abstract class MixinItemEntity extends Entity implements IItemEntity {
@Override @Override
public ItemEntityCapabilities get() { public ItemEntityCapabilities get() {
if (caster == null) {
caster = create();
}
return caster; return caster;
} }
@Inject(method = "tick()V", at = @At("HEAD"), cancellable = true) @Inject(method = "tick()V", at = @At("HEAD"), cancellable = true)
private void beforeTick(CallbackInfo info) { private void beforeTick(CallbackInfo info) {
if (caster.beforeUpdate()) { if (get().beforeUpdate()) {
info.cancel(); info.cancel();
} }
} }
@Inject(method = "tick()V", at = @At("RETURN")) @Inject(method = "tick()V", at = @At("RETURN"))
private void afterTick(CallbackInfo info) { 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") @Accessor("age")
@ -48,4 +64,5 @@ abstract class MixinItemEntity extends Entity implements IItemEntity {
@Accessor("pickupDelay") @Accessor("pickupDelay")
@Override @Override
public abstract int getPickupDelay(); public abstract int getPickupDelay();
} }

View file

@ -12,6 +12,7 @@ import com.minelittlepony.unicopia.entity.LivingEntityCapabilities;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.nbt.CompoundTag;
@Mixin(LivingEntity.class) @Mixin(LivingEntity.class)
abstract class MixinLivingEntity extends Entity implements PonyContainer<Ponylike> { abstract class MixinLivingEntity extends Entity implements PonyContainer<Ponylike> {
@ -71,6 +72,18 @@ abstract class MixinLivingEntity extends Entity implements PonyContainer<Ponylik
LivingEntityCapabilities.boostrap(); LivingEntityCapabilities.boostrap();
} }
@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());
}
// ---------- temporary // ---------- temporary
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Inject(method = "isClimbing()Z", at = @At("HEAD"), cancellable = true) @Inject(method = "isClimbing()Z", at = @At("HEAD"), cancellable = true)

View file

@ -52,10 +52,10 @@ abstract class MixinPlayerEntity extends LivingEntity implements PonyContainer<P
} }
@Inject(method = "dropItem(Lnet/minecraft/item/ItemStack;ZZ)Lnet/minecraft/entity/ItemEntity;", @Inject(method = "dropItem(Lnet/minecraft/item/ItemStack;ZZ)Lnet/minecraft/entity/ItemEntity;",
at = @At("HEAD")) at = @At("RETURN"))
private void onDropItem(ItemStack itemStack_1, boolean a, boolean b, CallbackInfoReturnable<ItemEntity> info) { private void onDropItem(ItemStack itemStack_1, boolean scatter, boolean retainOwnership, CallbackInfoReturnable<ItemEntity> info) {
PonyContainer.of(info.getReturnValue()).ifPresent(o -> { PonyContainer.of(info.getReturnValue()).ifPresent(container -> {
o.get().setSpecies(get().getSpecies()); container.get().setSpecies(get().getSpecies());
}); });
} }