Keep track of entities summoned and kill then when the spell is cancelled

This commit is contained in:
Sollace 2021-03-06 14:53:40 +02:00
parent da5f64e199
commit 9c19184910
2 changed files with 58 additions and 0 deletions

View file

@ -1,9 +1,11 @@
package com.minelittlepony.unicopia.ability.magic.spell;
import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.Lists;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.entity.EntityReference;
import com.minelittlepony.unicopia.util.WorldEvent;
import com.minelittlepony.unicopia.util.shape.Shape;
import com.minelittlepony.unicopia.util.shape.Sphere;
@ -11,6 +13,8 @@ import com.minelittlepony.unicopia.util.shape.Sphere;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.ZombieEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
@ -25,6 +29,8 @@ public class NecromancySpell extends AbstractPlacedSpell {
EntityType.ZOMBIFIED_PIGLIN
);
private final List<EntityReference<LivingEntity>> summonedEntities = new ArrayList<>();
protected NecromancySpell(SpellType<?> type) {
super(type);
}
@ -70,9 +76,27 @@ public class NecromancySpell extends AbstractPlacedSpell {
}
}
summonedEntities.removeIf(ref -> !ref.isPresent(source.getWorld()));
return true;
}
@Override
public void onDestroyed(Caster<?> caster) {
super.onDestroyed(caster);
LivingEntity master = caster.getMaster();
summonedEntities.forEach(ref -> {
ref.ifPresent(caster.getWorld(), e -> {
if (master != null) {
master.dealDamage(master, e);
}
if (caster.getWorld().random.nextInt(2000) != 0) {
e.setHealth(0);
}
});
});
}
protected void spawnMonster(Caster<?> source, Vec3d pos) {
int index = (int)MathHelper.nextDouble(source.getWorld().random, 0, spawns.size());
LivingEntity zombie = spawns.get(index).create(source.getWorld());
@ -85,5 +109,32 @@ public class NecromancySpell extends AbstractPlacedSpell {
source.getWorld().syncWorldEvent(WorldEvent.ZOMBIE_BREAK_WOODEN_DOOR, zombie.getBlockPos(), 0);
source.getWorld().spawnEntity(zombie);
EntityReference<LivingEntity> ref = new EntityReference<>();
ref.set(zombie);
summonedEntities.add(ref);
}
@Override
public void toNBT(CompoundTag compound) {
super.toNBT(compound);
if (summonedEntities.size() > 0) {
ListTag list = new ListTag();
summonedEntities.forEach(ref -> list.add(ref.toNBT()));
compound.put("summonedEntities", list);
}
}
@Override
public void fromNBT(CompoundTag compound) {
super.fromNBT(compound);
if (compound.contains("summonedEntities")) {
summonedEntities.clear();
compound.getList("summonedEntities", 10).forEach(tag -> {
EntityReference<LivingEntity> ref = new EntityReference<>();
ref.fromNBT((CompoundTag)tag);
summonedEntities.add(ref);
});
}
}
}

View file

@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.entity;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import javax.annotation.Nullable;
@ -37,6 +38,12 @@ public class EntityReference<T extends Entity> implements NbtSerialisable {
return entity != null && !entity.removed;
}
public void ifPresent(World world, Consumer<T> consumer) {
if (isPresent(world)) {
consumer.accept(get(world));
}
}
@SuppressWarnings("unchecked")
@Nullable
public T get(World world) {