Fixed spells dissapearing when reloading the world, and respawn the spell entity if it dies

This commit is contained in:
Sollace 2021-03-05 20:52:49 +02:00
parent fc8c68c100
commit 4c1f5a4ba5
4 changed files with 69 additions and 20 deletions

View file

@ -42,25 +42,24 @@ public abstract class AbstractPlacedSpell extends AbstractSpell implements Attac
if (dimension == null) { if (dimension == null) {
dimension = source.getWorld().getRegistryKey().getValue(); dimension = source.getWorld().getRegistryKey().getValue();
setDirty(true); setDirty(true);
} else if (!source.getWorld().getRegistryKey().getValue().equals(dimension)) {
return false;
}
if (!source.isClient() && !castEntity.isPresent(source.getWorld())) { if (!castEntity.isPresent(source.getWorld())) {
CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getWorld()); CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getWorld());
Vec3d pos = source.getOriginVector(); Vec3d pos = castEntity.getPosition().orElse(source.getOriginVector());
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, 0, 0); entity.updatePositionAndAngles(pos.x, pos.y, pos.z, 0, 0);
entity.setSpell(this); entity.setSpell(this);
entity.setMaster(source.getMaster()); entity.setMaster(source.getMaster());
entity.world.spawnEntity(entity); entity.world.spawnEntity(entity);
castEntity.set(entity); castEntity.set(entity);
setDirty(true);
} }
} }
if (!source.getWorld().getRegistryKey().getValue().equals(dimension)) { return !isDead();
return false;
}
}
return true;
} }
public boolean onGroundTick(Caster<?> source) { public boolean onGroundTick(Caster<?> source) {
@ -81,13 +80,13 @@ public abstract class AbstractPlacedSpell extends AbstractSpell implements Attac
if (dimension != null) { if (dimension != null) {
compound.putString("dimension", dimension.toString()); compound.putString("dimension", dimension.toString());
} }
compound.put("owner", castEntity.toNBT()); compound.put("castEntity", castEntity.toNBT());
} }
@Override @Override
public void fromNBT(CompoundTag compound) { public void fromNBT(CompoundTag compound) {
super.fromNBT(compound); super.fromNBT(compound);
System.out.println("Loaded!");
if (compound.contains("dimension")) { if (compound.contains("dimension")) {
dimension = new Identifier(compound.getString("dimension")); dimension = new Identifier(compound.getString("dimension"));
} }

View file

@ -14,6 +14,7 @@ import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Quaternion; import net.minecraft.util.math.Quaternion;
import net.minecraft.util.math.Vec3d;
public class RunesParticle extends OrientedBillboardParticle implements Attachment { public class RunesParticle extends OrientedBillboardParticle implements Attachment {
@ -56,6 +57,8 @@ public class RunesParticle extends OrientedBillboardParticle implements Attachme
velocityX = 0; velocityX = 0;
velocityY = 0; velocityY = 0;
velocityZ = 0; velocityZ = 0;
Vec3d pos = caster.getOriginVector();
setPos(pos.x, pos.y, pos.z);
} }
@Override @Override

View file

@ -21,6 +21,7 @@ import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Packet; import net.minecraft.network.Packet;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
public class CastSpellEntity extends Entity implements Caster<LivingEntity> { public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
@ -37,6 +38,10 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
private final EntityReference<LivingEntity> owner = new EntityReference<>(); private final EntityReference<LivingEntity> owner = new EntityReference<>();
private BlockPos lastPos;
private int orphanedTicks;
public CastSpellEntity(EntityType<?> type, World world) { public CastSpellEntity(EntityType<?> type, World world) {
super(type, world); super(type, world);
} }
@ -54,13 +59,29 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
return; 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(); LivingEntity master = getMaster();
if (master == null || master.removed) { if (master == null || master.removed) {
if (orphanedTicks-- > 0) {
return;
}
remove(); remove();
return; return;
} }
orphanedTicks = 0;
if (!Caster.of(master).filter(c -> { if (!Caster.of(master).filter(c -> {
UUID spellId = dataTracker.get(SPELL).orElse(null); UUID spellId = dataTracker.get(SPELL).orElse(null);
@ -76,6 +97,14 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
} }
} }
@Override
public void remove() {
super.remove();
if (world.isClient) {
world.getLightingProvider().checkBlock(getBlockPos());
}
}
@Override @Override
public void setMaster(LivingEntity owner) { public void setMaster(LivingEntity owner) {
this.owner.set(owner); this.owner.set(owner);
@ -119,6 +148,10 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
@Override @Override
protected void writeCustomDataToTag(CompoundTag tag) { protected void writeCustomDataToTag(CompoundTag tag) {
tag.put("owner", owner.toNBT()); tag.put("owner", owner.toNBT());
dataTracker.get(SPELL).ifPresent(spellId -> {
tag.putUuid("spellId", spellId);
});
} }
@Override @Override
@ -126,8 +159,11 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
if (tag.contains("owner")) { if (tag.contains("owner")) {
owner.fromNBT(tag.getCompound("owner")); owner.fromNBT(tag.getCompound("owner"));
} }
if (tag.contains("spellId")) {
dataTracker.set(SPELL, Optional.ofNullable(tag.getUuid("spellId")));
}
orphanedTicks = 60;
} }
@Override @Override
public Packet<?> createSpawnPacket() { public Packet<?> createSpawnPacket() {

View file

@ -1,5 +1,6 @@
package com.minelittlepony.unicopia.entity; package com.minelittlepony.unicopia.entity;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -9,6 +10,7 @@ import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
public class EntityReference<T extends Entity> implements NbtSerialisable { public class EntityReference<T extends Entity> implements NbtSerialisable {
@ -16,13 +18,20 @@ public class EntityReference<T extends Entity> implements NbtSerialisable {
private UUID uuid; private UUID uuid;
private int clientId; private int clientId;
private Optional<Vec3d> pos = Optional.empty();
public void set(@Nullable T entity) { public void set(@Nullable T entity) {
if (entity != null) { if (entity != null) {
uuid = entity.getUuid(); uuid = entity.getUuid();
clientId = entity.getEntityId(); clientId = entity.getEntityId();
pos = Optional.of(entity.getPos());
} }
} }
public Optional<Vec3d> getPosition() {
return pos;
}
public boolean isPresent(World world) { public boolean isPresent(World world) {
T entity = get(world); T entity = get(world);
return entity != null && !entity.removed; return entity != null && !entity.removed;
@ -47,14 +56,16 @@ public class EntityReference<T extends Entity> implements NbtSerialisable {
if (uuid != null) { if (uuid != null) {
tag.putUuid("uuid", uuid); tag.putUuid("uuid", uuid);
} }
pos.ifPresent(p -> {
tag.put("pos", NbtSerialisable.writeVector(p));
});
tag.putInt("clientId", clientId); tag.putInt("clientId", clientId);
} }
@Override @Override
public void fromNBT(CompoundTag tag) { public void fromNBT(CompoundTag tag) {
if (tag.containsUuid("uuid")) { uuid = tag.containsUuid("uuid") ? tag.getUuid("uuid") : null;
uuid = tag.getUuid("uuid"); pos = tag.contains("pos") ? Optional.ofNullable(NbtSerialisable.readVector(tag.getList("pos", 6))) : Optional.empty();
}
clientId = tag.getInt("clientId"); clientId = tag.getInt("clientId");
} }
} }