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

123 lines
3.2 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.magic.Affinity;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.magic.Affine;
import com.minelittlepony.unicopia.magic.AttachedMagicEffect;
import com.minelittlepony.unicopia.magic.Caster;
import com.minelittlepony.unicopia.magic.MagicEffect;
import com.minelittlepony.unicopia.magic.spell.SpellRegistry;
import com.minelittlepony.unicopia.network.EffectSync;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.nbt.CompoundTag;
2020-04-15 18:12:00 +02:00
public class LivingEntityCapabilities implements RaceContainer<LivingEntity>, Caster<LivingEntity> {
2020-01-16 12:35:46 +01:00
private static final TrackedData<CompoundTag> EFFECT = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.TAG_COMPOUND);
public static void boostrap() {}
private final EffectSync effectDelegate = new EffectSync(this, EFFECT);
private final LivingEntity entity;
public LivingEntityCapabilities(LivingEntity entity) {
this.entity = entity;
entity.getDataTracker().startTracking(EFFECT, new CompoundTag());
}
@Override
public Race getSpecies() {
return Race.HUMAN;
}
@Override
public void setSpecies(Race race) {
}
@Override
2020-04-15 18:12:00 +02:00
public void setEffect(MagicEffect effect) {
2020-01-16 12:35:46 +01:00
effectDelegate.set(effect);
}
@Override
2020-04-15 18:12:00 +02:00
public <T extends MagicEffect> T getEffect(Class<T> type, boolean update) {
2020-01-16 12:35:46 +01:00
return effectDelegate.get(type, update);
}
@Override
public boolean hasEffect() {
return effectDelegate.has();
}
@Override
public void onUpdate() {
if (hasEffect()) {
2020-04-15 18:12:00 +02:00
AttachedMagicEffect effect = getEffect(AttachedMagicEffect.class, true);
2020-01-16 12:35:46 +01:00
if (effect != null) {
if (entity.getEntityWorld().isClient()) {
effect.renderOnPerson(this);
}
if (!effect.updateOnPerson(this)) {
setEffect(null);
}
}
}
}
@Override
public void onDimensionalTravel(int destinationDimension) {
}
@Override
public void setOwner(LivingEntity owner) {
}
@Override
public LivingEntity getOwner() {
return entity;
}
@Override
public int getCurrentLevel() {
return 0;
}
@Override
public void setCurrentLevel(int level) {
}
@Override
public Affinity getAffinity() {
2020-04-15 18:12:00 +02:00
if (getOwner() instanceof Affine) {
return ((Affine)getOwner()).getAffinity();
2020-01-16 12:35:46 +01:00
}
return Affinity.NEUTRAL;
}
@Override
public void toNBT(CompoundTag compound) {
2020-04-15 18:12:00 +02:00
MagicEffect effect = getEffect();
2020-01-16 12:35:46 +01:00
if (effect != null) {
compound.put("effect", SpellRegistry.instance().serializeEffectToNBT(effect));
}
}
@Override
public void fromNBT(CompoundTag compound) {
if (compound.containsKey("effect")) {
setEffect(SpellRegistry.instance().createEffectFromNBT(compound.getCompound("effect")));
}
}
}