From 4c1f5a4ba52b2156da4aac714965a712a54a8bae Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 5 Mar 2021 20:52:49 +0200 Subject: [PATCH] Fixed spells dissapearing when reloading the world, and respawn the spell entity if it dies --- .../magic/spell/AbstractPlacedSpell.java | 31 ++++++++------- .../client/particle/RunesParticle.java | 3 ++ .../unicopia/entity/CastSpellEntity.java | 38 ++++++++++++++++++- .../unicopia/entity/EntityReference.java | 17 +++++++-- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractPlacedSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractPlacedSpell.java index 023080cc..e570b462 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractPlacedSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractPlacedSpell.java @@ -42,25 +42,24 @@ public abstract class AbstractPlacedSpell extends AbstractSpell implements Attac if (dimension == null) { dimension = source.getWorld().getRegistryKey().getValue(); setDirty(true); - - if (!source.isClient() && !castEntity.isPresent(source.getWorld())) { - CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getWorld()); - Vec3d pos = source.getOriginVector(); - entity.updatePositionAndAngles(pos.x, pos.y, pos.z, 0, 0); - entity.setSpell(this); - entity.setMaster(source.getMaster()); - entity.world.spawnEntity(entity); - - castEntity.set(entity); - } + } else if (!source.getWorld().getRegistryKey().getValue().equals(dimension)) { + return false; } - if (!source.getWorld().getRegistryKey().getValue().equals(dimension)) { - return false; + if (!castEntity.isPresent(source.getWorld())) { + CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getWorld()); + Vec3d pos = castEntity.getPosition().orElse(source.getOriginVector()); + entity.updatePositionAndAngles(pos.x, pos.y, pos.z, 0, 0); + entity.setSpell(this); + entity.setMaster(source.getMaster()); + entity.world.spawnEntity(entity); + + castEntity.set(entity); + setDirty(true); } } - return true; + return !isDead(); } public boolean onGroundTick(Caster source) { @@ -81,13 +80,13 @@ public abstract class AbstractPlacedSpell extends AbstractSpell implements Attac if (dimension != null) { compound.putString("dimension", dimension.toString()); } - compound.put("owner", castEntity.toNBT()); + compound.put("castEntity", castEntity.toNBT()); } @Override public void fromNBT(CompoundTag compound) { super.fromNBT(compound); - + System.out.println("Loaded!"); if (compound.contains("dimension")) { dimension = new Identifier(compound.getString("dimension")); } diff --git a/src/main/java/com/minelittlepony/unicopia/client/particle/RunesParticle.java b/src/main/java/com/minelittlepony/unicopia/client/particle/RunesParticle.java index d3e3b8a1..211c4f1e 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/particle/RunesParticle.java +++ b/src/main/java/com/minelittlepony/unicopia/client/particle/RunesParticle.java @@ -14,6 +14,7 @@ import net.minecraft.client.world.ClientWorld; import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Quaternion; +import net.minecraft.util.math.Vec3d; public class RunesParticle extends OrientedBillboardParticle implements Attachment { @@ -56,6 +57,8 @@ public class RunesParticle extends OrientedBillboardParticle implements Attachme velocityX = 0; velocityY = 0; velocityZ = 0; + Vec3d pos = caster.getOriginVector(); + setPos(pos.x, pos.y, pos.z); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java index 0f369db5..244d2c97 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java @@ -21,6 +21,7 @@ import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.Packet; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; public class CastSpellEntity extends Entity implements Caster { @@ -37,6 +38,10 @@ public class CastSpellEntity extends Entity implements Caster { private final EntityReference owner = new EntityReference<>(); + private BlockPos lastPos; + + private int orphanedTicks; + public CastSpellEntity(EntityType type, World world) { super(type, world); } @@ -54,13 +59,29 @@ public class CastSpellEntity extends Entity implements Caster { return; } + if (world.isClient) { + BlockPos currentPos = getBlockPos(); + + world.getLightingProvider().addLightSource(currentPos, 11); + + if (lastPos != null && !currentPos.equals(lastPos)) { + world.getLightingProvider().checkBlock(lastPos); + } + + lastPos = currentPos; + } LivingEntity master = getMaster(); if (master == null || master.removed) { + if (orphanedTicks-- > 0) { + return; + } remove(); return; } + orphanedTicks = 0; + if (!Caster.of(master).filter(c -> { UUID spellId = dataTracker.get(SPELL).orElse(null); @@ -76,6 +97,14 @@ public class CastSpellEntity extends Entity implements Caster { } } + @Override + public void remove() { + super.remove(); + if (world.isClient) { + world.getLightingProvider().checkBlock(getBlockPos()); + } + } + @Override public void setMaster(LivingEntity owner) { this.owner.set(owner); @@ -119,6 +148,10 @@ public class CastSpellEntity extends Entity implements Caster { @Override protected void writeCustomDataToTag(CompoundTag tag) { tag.put("owner", owner.toNBT()); + dataTracker.get(SPELL).ifPresent(spellId -> { + tag.putUuid("spellId", spellId); + }); + } @Override @@ -126,9 +159,12 @@ public class CastSpellEntity extends Entity implements Caster { if (tag.contains("owner")) { owner.fromNBT(tag.getCompound("owner")); } + if (tag.contains("spellId")) { + dataTracker.set(SPELL, Optional.ofNullable(tag.getUuid("spellId"))); + } + orphanedTicks = 60; } - @Override public Packet createSpawnPacket() { return Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this)); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java index fa39b8c2..41b9af56 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java @@ -1,5 +1,6 @@ package com.minelittlepony.unicopia.entity; +import java.util.Optional; import java.util.UUID; import javax.annotation.Nullable; @@ -9,6 +10,7 @@ import com.minelittlepony.unicopia.util.NbtSerialisable; import net.minecraft.entity.Entity; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; public class EntityReference implements NbtSerialisable { @@ -16,13 +18,20 @@ public class EntityReference implements NbtSerialisable { private UUID uuid; private int clientId; + private Optional pos = Optional.empty(); + public void set(@Nullable T entity) { if (entity != null) { uuid = entity.getUuid(); clientId = entity.getEntityId(); + pos = Optional.of(entity.getPos()); } } + public Optional getPosition() { + return pos; + } + public boolean isPresent(World world) { T entity = get(world); return entity != null && !entity.removed; @@ -47,14 +56,16 @@ public class EntityReference implements NbtSerialisable { if (uuid != null) { tag.putUuid("uuid", uuid); } + pos.ifPresent(p -> { + tag.put("pos", NbtSerialisable.writeVector(p)); + }); tag.putInt("clientId", clientId); } @Override public void fromNBT(CompoundTag tag) { - if (tag.containsUuid("uuid")) { - uuid = tag.getUuid("uuid"); - } + uuid = tag.containsUuid("uuid") ? tag.getUuid("uuid") : null; + pos = tag.contains("pos") ? Optional.ofNullable(NbtSerialisable.readVector(tag.getList("pos", 6))) : Optional.empty(); clientId = tag.getInt("clientId"); } }