2020-09-22 15:11:20 +02:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-06-26 11:44:47 +02:00
|
|
|
import com.minelittlepony.unicopia.Owned;
|
2020-04-15 14:22:03 +02:00
|
|
|
import com.minelittlepony.unicopia.Race;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
|
|
|
import net.minecraft.entity.ItemEntity;
|
2020-04-26 16:48:48 +02:00
|
|
|
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
|
|
|
|
2020-09-22 15:11:20 +02:00
|
|
|
public class ItemImpl implements Equine<ItemEntity>, Owned<ItemEntity> {
|
2020-04-26 16:48:48 +02:00
|
|
|
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;
|
2020-04-26 16:48:48 +02:00
|
|
|
private Race serverRace;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-05-10 17:18:45 +02:00
|
|
|
private final Physics physics = new EntityPhysics<>(this);
|
|
|
|
|
|
|
|
public ItemImpl(ItemEntity owner) {
|
2020-01-16 12:35:46 +01:00
|
|
|
this.owner = owner;
|
2020-04-26 16:48:48 +02:00
|
|
|
owner.getDataTracker().startTracking(ITEM_RACE, Race.HUMAN.ordinal());
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-10 17:18:45 +02:00
|
|
|
public void tick() {
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-27 11:05:22 +01:00
|
|
|
public boolean beforeUpdate() {
|
2020-04-26 16:48:48 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-10 17:18:45 +02:00
|
|
|
@Override
|
|
|
|
public Physics getPhysics() {
|
|
|
|
return physics;
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:35:46 +01:00
|
|
|
@Override
|
|
|
|
public Race getSpecies() {
|
2020-04-26 16:48:48 +02:00
|
|
|
return Race.fromId(getOwner().getDataTracker().get(ITEM_RACE));
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSpecies(Race race) {
|
2020-04-26 16:48:48 +02:00
|
|
|
getOwner().getDataTracker().set(ITEM_RACE, race.ordinal());
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void toNBT(CompoundTag compound) {
|
2020-04-26 16:48:48 +02:00
|
|
|
compound.putString("owner_species", getSpecies().name());
|
2020-05-10 17:18:45 +02:00
|
|
|
physics.toNBT(compound);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromNBT(CompoundTag compound) {
|
2020-04-26 16:48:48 +02:00
|
|
|
setSpecies(Race.fromName(compound.getString("owner_species")));
|
2020-05-10 17:18:45 +02:00
|
|
|
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
|
|
|
}
|