mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-23 21:38:00 +01:00
Fixed spells dissapearing when reloading the world, and respawn the spell entity if it dies
This commit is contained in:
parent
fc8c68c100
commit
4c1f5a4ba5
4 changed files with 69 additions and 20 deletions
|
@ -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"));
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<LivingEntity> {
|
||||
|
@ -37,6 +38,10 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
|
|||
|
||||
private final EntityReference<LivingEntity> 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<LivingEntity> {
|
|||
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<LivingEntity> {
|
|||
}
|
||||
}
|
||||
|
||||
@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<LivingEntity> {
|
|||
@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<LivingEntity> {
|
|||
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));
|
||||
|
|
|
@ -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<T extends Entity> implements NbtSerialisable {
|
||||
|
@ -16,13 +18,20 @@ public class EntityReference<T extends Entity> implements NbtSerialisable {
|
|||
private UUID uuid;
|
||||
private int clientId;
|
||||
|
||||
private Optional<Vec3d> 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<Vec3d> getPosition() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public boolean isPresent(World world) {
|
||||
T entity = get(world);
|
||||
return entity != null && !entity.removed;
|
||||
|
@ -47,14 +56,16 @@ public class EntityReference<T extends Entity> 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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue