2021-03-04 21:35:49 +01:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.Affinity;
|
|
|
|
import com.minelittlepony.unicopia.ability.magic.Caster;
|
|
|
|
import com.minelittlepony.unicopia.ability.magic.Levelled;
|
2021-03-05 18:22:09 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.SpellContainer;
|
2021-12-22 12:12:51 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.SpellContainer.Operation;
|
2021-11-05 14:18:32 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.Situation;
|
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
|
2021-03-04 21:35:49 +01:00
|
|
|
import com.minelittlepony.unicopia.network.Channel;
|
|
|
|
import com.minelittlepony.unicopia.network.MsgSpawnProjectile;
|
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.EntityType;
|
|
|
|
import net.minecraft.entity.LivingEntity;
|
|
|
|
import net.minecraft.entity.data.DataTracker;
|
|
|
|
import net.minecraft.entity.data.TrackedData;
|
|
|
|
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
|
2021-08-04 15:38:03 +02:00
|
|
|
import net.minecraft.nbt.NbtCompound;
|
2021-03-04 21:35:49 +01:00
|
|
|
import net.minecraft.network.Packet;
|
2021-03-06 13:27:52 +01:00
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.text.TranslatableText;
|
2021-03-04 21:35:49 +01:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2022-01-01 18:14:37 +01:00
|
|
|
public class CastSpellEntity extends LightEmittingEntity implements Caster<LivingEntity> {
|
2021-03-04 21:35:49 +01:00
|
|
|
private static final TrackedData<Float> GRAVITY = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
|
|
|
private static final TrackedData<Optional<UUID>> SPELL = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID);
|
|
|
|
|
|
|
|
private static final LevelStore LEVELS = Levelled.fixed(0);
|
|
|
|
|
|
|
|
private final EntityPhysics<CastSpellEntity> physics = new EntityPhysics<>(this, GRAVITY);
|
|
|
|
|
2021-12-21 16:28:47 +01:00
|
|
|
private final SpellContainer spell = new SpellContainer.Delegate() {
|
|
|
|
@Override
|
|
|
|
public SpellContainer delegate() {
|
|
|
|
return Caster.of(getMaster()).map(Caster::getSpellSlot).orElse(SpellContainer.EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void put(Spell spell) {
|
|
|
|
getDataTracker().set(SPELL, Optional.ofNullable(spell).map(Spell::getUuid));
|
|
|
|
SpellContainer.Delegate.super.put(spell);
|
|
|
|
}
|
2021-12-31 14:40:08 +01:00
|
|
|
|
|
|
|
@Override
|
2021-12-31 23:00:18 +01:00
|
|
|
public boolean clear() {
|
|
|
|
return getDataTracker().get(SPELL).map(id -> {
|
|
|
|
return delegate().removeIf(spell -> spell.getUuid().equals(id), true);
|
|
|
|
}).orElse(false);
|
2021-12-31 14:40:08 +01:00
|
|
|
}
|
2021-12-21 16:28:47 +01:00
|
|
|
};
|
|
|
|
|
2021-12-27 01:15:48 +01:00
|
|
|
private final EntityReference<LivingEntity> owner = new EntityReference<>();
|
2021-03-05 19:52:49 +01:00
|
|
|
|
|
|
|
private int orphanedTicks;
|
|
|
|
|
2021-03-04 21:35:49 +01:00
|
|
|
public CastSpellEntity(EntityType<?> type, World world) {
|
|
|
|
super(type, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initDataTracker() {
|
|
|
|
getDataTracker().startTracking(SPELL, Optional.empty());
|
|
|
|
}
|
|
|
|
|
2021-12-27 01:15:48 +01:00
|
|
|
@Override
|
|
|
|
public int getLightLevel() {
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
|
2021-03-06 13:27:52 +01:00
|
|
|
@Override
|
|
|
|
public Text getName() {
|
|
|
|
Entity master = getMaster();
|
|
|
|
if (master != null) {
|
|
|
|
return new TranslatableText("entity.unicopia.cast_spell.by", master.getName());
|
|
|
|
}
|
|
|
|
return super.getName();
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:35:49 +01:00
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
super.tick();
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
if (isRemoved()) {
|
2021-03-04 21:35:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LivingEntity master = getMaster();
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
if (master == null || master.isRemoved()) {
|
2021-03-05 19:52:49 +01:00
|
|
|
if (orphanedTicks-- > 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-22 12:12:51 +01:00
|
|
|
discard();
|
2021-03-04 21:35:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-05 19:52:49 +01:00
|
|
|
orphanedTicks = 0;
|
|
|
|
|
2021-12-31 14:40:08 +01:00
|
|
|
if (dataTracker.get(SPELL).filter(spellId -> {
|
2021-12-22 12:12:51 +01:00
|
|
|
return getSpellSlot().forEach(spell -> {
|
2021-12-26 10:13:32 +01:00
|
|
|
return spell.getUuid().equals(spellId) ? Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)) : Operation.SKIP;
|
2021-12-22 12:12:51 +01:00
|
|
|
}, true);
|
2021-12-31 14:40:08 +01:00
|
|
|
}).isEmpty()) {
|
2021-12-22 12:12:51 +01:00
|
|
|
discard();
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setMaster(LivingEntity owner) {
|
2021-03-04 22:30:43 +01:00
|
|
|
this.owner.set(owner);
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-27 01:15:48 +01:00
|
|
|
public LivingEntity getMaster() {
|
|
|
|
return owner.get(((Entity)this).world);
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-27 01:15:48 +01:00
|
|
|
public Entity getEntity() {
|
|
|
|
return this;
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public LevelStore getLevel() {
|
|
|
|
return Caster.of(getMaster()).map(Caster::getLevel).orElse(LEVELS);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Affinity getAffinity() {
|
|
|
|
return getSpellSlot().get(true).map(Spell::getAffinity).orElse(Affinity.NEUTRAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Physics getPhysics() {
|
|
|
|
return physics;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-05 18:22:09 +01:00
|
|
|
public SpellContainer getSpellSlot() {
|
2021-12-21 16:28:47 +01:00
|
|
|
return spell;
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 11:26:54 +01:00
|
|
|
@Override
|
|
|
|
public boolean subtractEnergyCost(double amount) {
|
|
|
|
return Caster.of(getMaster()).filter(c -> c.subtractEnergyCost(amount)).isPresent();
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:35:49 +01:00
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
protected void writeCustomDataToNbt(NbtCompound tag) {
|
2021-03-04 22:30:43 +01:00
|
|
|
tag.put("owner", owner.toNBT());
|
2021-03-05 19:52:49 +01:00
|
|
|
dataTracker.get(SPELL).ifPresent(spellId -> {
|
|
|
|
tag.putUuid("spellId", spellId);
|
|
|
|
});
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
protected void readCustomDataFromNbt(NbtCompound tag) {
|
2021-03-04 22:30:43 +01:00
|
|
|
if (tag.contains("owner")) {
|
|
|
|
owner.fromNBT(tag.getCompound("owner"));
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
2021-12-27 01:15:48 +01:00
|
|
|
orphanedTicks = 60;
|
2021-03-05 19:52:49 +01:00
|
|
|
if (tag.contains("spellId")) {
|
|
|
|
dataTracker.set(SPELL, Optional.ofNullable(tag.getUuid("spellId")));
|
|
|
|
}
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Packet<?> createSpawnPacket() {
|
|
|
|
return Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this));
|
|
|
|
}
|
|
|
|
}
|