Unicopia/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java

94 lines
2.4 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
2020-01-16 12:35:46 +01:00
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ducks.IItemEntity;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
2020-01-27 11:05:22 +01:00
import net.minecraft.item.ItemStack;
2020-01-16 12:35:46 +01:00
import net.minecraft.nbt.CompoundTag;
2020-01-27 11:05:22 +01:00
import net.minecraft.util.ActionResult;
2020-01-16 12:35:46 +01:00
public class ItemImpl implements Ponylike<ItemEntity>, Owned<ItemEntity> {
private static final TrackedData<Integer> ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.INTEGER);
2020-01-16 12:35:46 +01:00
private final ItemEntity owner;
private Race serverRace;
2020-01-16 12:35:46 +01:00
private final Physics physics = new EntityPhysics<>(this);
public ItemImpl(ItemEntity owner) {
2020-01-16 12:35:46 +01:00
this.owner = owner;
owner.getDataTracker().startTracking(ITEM_RACE, Race.HUMAN.ordinal());
2020-01-16 12:35:46 +01:00
}
@Override
public void tick() {
2020-01-16 12:35:46 +01:00
}
@Override
2020-01-27 11:05:22 +01:00
public boolean beforeUpdate() {
if (!owner.world.isClient) {
Race race = getSpecies();
if (race != serverRace) {
serverRace = race;
setSpecies(Race.HUMAN);
setSpecies(race);
}
}
2020-01-27 11:05:22 +01:00
ItemStack stack = owner.getStack();
2020-01-16 12:35:46 +01:00
2020-01-27 11:05:22 +01:00
if (!stack.isEmpty() && stack.getItem() instanceof TickableItem) {
return ((TickableItem)stack.getItem()).onGroundTick((IItemEntity)owner) == ActionResult.SUCCESS;
}
return false;
2020-01-16 12:35:46 +01:00
}
@Override
public Physics getPhysics() {
return physics;
}
2020-01-16 12:35:46 +01:00
@Override
public Race getSpecies() {
return Race.fromId(getOwner().getDataTracker().get(ITEM_RACE));
2020-01-16 12:35:46 +01:00
}
@Override
public void setSpecies(Race race) {
getOwner().getDataTracker().set(ITEM_RACE, race.ordinal());
2020-01-16 12:35:46 +01:00
}
@Override
public void toNBT(CompoundTag compound) {
compound.putString("owner_species", getSpecies().name());
physics.toNBT(compound);
2020-01-16 12:35:46 +01:00
}
@Override
public void fromNBT(CompoundTag compound) {
setSpecies(Race.fromName(compound.getString("owner_species")));
physics.fromNBT(compound);
2020-01-16 12:35:46 +01:00
}
@Override
public void setOwner(ItemEntity owner) {
}
@Override
public ItemEntity getOwner() {
return owner;
}
2020-01-27 11:05:22 +01:00
public interface TickableItem {
ActionResult onGroundTick(IItemEntity entity);
}
2020-01-16 12:35:46 +01:00
}