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.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<Multi> {
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 -> {

View file

@ -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<ItemEntity>, Owned<ItemEntity> {
private Race race = Race.HUMAN;
private static final TrackedData<Integer> 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<ItemEntity>, 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

View file

@ -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();
}

View file

@ -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<Ponylike> {
@ -71,6 +72,18 @@ abstract class MixinLivingEntity extends Entity implements PonyContainer<Ponylik
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
@SuppressWarnings("deprecation")
@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;",
at = @At("HEAD"))
private void onDropItem(ItemStack itemStack_1, boolean a, boolean b, CallbackInfoReturnable<ItemEntity> 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<ItemEntity> info) {
PonyContainer.of(info.getReturnValue()).ifPresent(container -> {
container.get().setSpecies(get().getSpecies());
});
}