2021-03-04 21:35:49 +01:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
|
|
|
|
2022-09-17 22:11:24 +02:00
|
|
|
import com.minelittlepony.unicopia.*;
|
2021-03-04 21:35:49 +01:00
|
|
|
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;
|
2022-01-01 21:08:50 +01:00
|
|
|
import com.minelittlepony.unicopia.network.datasync.EffectSync;
|
2021-03-04 21:35:49 +01:00
|
|
|
|
|
|
|
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;
|
2022-12-18 22:07:24 +01:00
|
|
|
import net.minecraft.network.listener.ClientPlayPacketListener;
|
2021-03-06 13:27:52 +01:00
|
|
|
import net.minecraft.text.Text;
|
2021-03-04 21:35:49 +01:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2022-12-19 20:45:02 +01:00
|
|
|
public class CastSpellEntity extends LightEmittingEntity implements Caster<CastSpellEntity>, WeaklyOwned.Mutable<LivingEntity> {
|
2021-03-04 21:35:49 +01:00
|
|
|
private static final TrackedData<Float> GRAVITY = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
2022-03-26 20:34:15 +01:00
|
|
|
private static final TrackedData<NbtCompound> EFFECT = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.NBT_COMPOUND);
|
2021-03-04 21:35:49 +01:00
|
|
|
|
|
|
|
private final EntityPhysics<CastSpellEntity> physics = new EntityPhysics<>(this, GRAVITY);
|
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
private final EffectSync effectDelegate = new EffectSync(this, EFFECT);
|
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
|
|
|
|
2022-09-17 22:11:24 +02:00
|
|
|
private LevelStore level = Levelled.EMPTY;
|
|
|
|
private LevelStore corruption = Levelled.EMPTY;
|
|
|
|
|
2021-03-04 21:35:49 +01:00
|
|
|
public CastSpellEntity(EntityType<?> type, World world) {
|
|
|
|
super(type, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initDataTracker() {
|
2022-01-01 21:08:50 +01:00
|
|
|
getDataTracker().startTracking(EFFECT, new NbtCompound());
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-06-25 00:19:55 +02:00
|
|
|
return Text.translatable("entity.unicopia.cast_spell.by", master.getName());
|
2021-03-06 13:27:52 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-12 23:15:17 +02:00
|
|
|
if (!getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)), world.isClient)) {
|
2021-12-22 12:12:51 +01:00
|
|
|
discard();
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-01-01 21:08:50 +01:00
|
|
|
public EntityReference<LivingEntity> getMasterReference() {
|
|
|
|
return owner;
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 17:27:24 +01:00
|
|
|
@Override
|
2022-12-19 18:13:15 +01:00
|
|
|
public CastSpellEntity asEntity() {
|
2021-12-27 01:15:48 +01:00
|
|
|
return this;
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:11:24 +02:00
|
|
|
public void setCaster(Caster<?> caster) {
|
|
|
|
this.level = Levelled.copyOf(caster.getLevel());
|
|
|
|
this.corruption = Levelled.copyOf(caster.getCorruption());
|
|
|
|
setMaster(caster);
|
|
|
|
}
|
|
|
|
|
2022-12-19 20:45:02 +01:00
|
|
|
@Override
|
|
|
|
public LivingEntity getMaster() {
|
|
|
|
return WeaklyOwned.Mutable.super.getMaster();
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:35:49 +01:00
|
|
|
@Override
|
|
|
|
public LevelStore getLevel() {
|
2022-09-17 22:11:24 +02:00
|
|
|
return level;
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 22:57:19 +02:00
|
|
|
@Override
|
|
|
|
public LevelStore getCorruption() {
|
2022-09-17 22:11:24 +02:00
|
|
|
return corruption;
|
2022-09-01 22:57:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 21:35:49 +01:00
|
|
|
@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() {
|
2022-01-01 21:08:50 +01:00
|
|
|
return effectDelegate;
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 11:26:54 +01:00
|
|
|
@Override
|
|
|
|
public boolean subtractEnergyCost(double amount) {
|
2022-01-01 23:43:21 +01:00
|
|
|
if (getMaster() == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Caster.of(getMaster())
|
|
|
|
.filter(c -> c.subtractEnergyCost(amount))
|
|
|
|
.isPresent();
|
2021-03-06 11:26:54 +01:00
|
|
|
}
|
|
|
|
|
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());
|
2022-09-17 22:11:24 +02:00
|
|
|
tag.put("level", level.toNbt());
|
|
|
|
tag.put("corruption", corruption.toNbt());
|
2022-01-01 21:08:50 +01:00
|
|
|
getSpellSlot().get(true).ifPresent(effect -> {
|
|
|
|
tag.put("effect", Spell.writeNbt(effect));
|
2021-03-05 19:52:49 +01:00
|
|
|
});
|
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
|
|
|
}
|
2022-01-01 21:08:50 +01:00
|
|
|
if (tag.contains("effect")) {
|
|
|
|
getSpellSlot().put(Spell.readNbt(tag.getCompound("effect")));
|
2021-03-05 19:52:49 +01:00
|
|
|
}
|
2022-09-17 22:11:24 +02:00
|
|
|
level = Levelled.fromNbt(tag.getCompound("level"));
|
|
|
|
corruption = Levelled.fromNbt(tag.getCompound("corruption"));
|
2021-03-04 21:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-12-18 22:07:24 +01:00
|
|
|
public Packet<ClientPlayPacketListener> createSpawnPacket() {
|
2021-03-04 21:35:49 +01:00
|
|
|
return Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this));
|
|
|
|
}
|
|
|
|
}
|