diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java index faf26441..8107ef9d 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java @@ -20,11 +20,12 @@ import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.RegistryWrapper.WrapperLookup; import net.minecraft.util.Identifier; +import net.minecraft.util.Util; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; public class PlacementControlSpell extends AbstractSpell implements OrientedSpell { - private final DataTracker.Entry placedEntityId = dataTracker.startTracking(TrackableDataType.UUID, null); + private final DataTracker.Entry placedEntityId = dataTracker.startTracking(TrackableDataType.UUID, Util.NIL_UUID); private final DataTracker.Entry>> dimension = dataTracker.startTracking(TrackableDataType.ofRegistryKey(), Optional.empty()); private final DataTracker.Entry> position = dataTracker.startTracking(TrackableDataType.OPTIONAL_VECTOR, Optional.empty()); private final DataTracker.Entry> orientation = dataTracker.startTracking(TrackableDataType.OPTIONAL_VECTOR, Optional.empty()); @@ -82,7 +83,7 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel public boolean tick(Caster source, Situation situation) { if (!source.isClient()) { - if (placedEntityId.get() == null) { + if (Util.NIL_UUID.equals(placedEntityId.getOrDefault(Util.NIL_UUID))) { if (dimension.get().isEmpty()) { setDimension(source.asWorld().getRegistryKey()); } @@ -111,7 +112,7 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel @Nullable private Ether.Entry getConnection(Caster source) { - return delegate == null || placedEntityId.get() == null ? null : getWorld(source) + return delegate == null || Util.NIL_UUID.equals(placedEntityId.getOrDefault(Util.NIL_UUID)) ? null : getWorld(source) .map(world -> Ether.get(world).get(getDelegate().getTypeAndTraits().type(), placedEntityId.get(), delegate.getUuid())) .orElse(null); } @@ -127,8 +128,9 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel position.get().ifPresent(pos -> compound.put("position", NbtSerialisable.writeVector(pos))); orientation.get().ifPresent(o -> compound.put("orientation", NbtSerialisable.writeVector(o))); dimension.get().ifPresent(d -> compound.putString("dimension", d.getValue().toString())); - if (placedEntityId.get() != null) { - compound.putUuid("placedEntityId", placedEntityId.get()); + @Nullable UUID placeEntityUuid = placedEntityId.getOrDefault(Util.NIL_UUID); + if (!Util.NIL_UUID.equals(placeEntityUuid)) { + compound.putUuid("placedEntityId", placeEntityUuid); } } @@ -136,7 +138,7 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel public void fromNBT(NbtCompound compound, WrapperLookup lookup) { super.fromNBT(compound, lookup); delegate = Spell.readNbt(compound.getCompound("spell"), lookup); - placedEntityId.set(compound.containsUuid("placedEntityId") ? compound.getUuid("placedEntityId") : null); + placedEntityId.set(compound.containsUuid("placedEntityId") ? compound.getUuid("placedEntityId") : Util.NIL_UUID); position.set(compound.contains("position") ? Optional.of(NbtSerialisable.readVector(compound.getList("position", NbtElement.DOUBLE_TYPE))) : Optional.empty()); orientation.set(compound.contains("orientation") ? Optional.of(NbtSerialisable.readVector(compound.getList("orientation", NbtElement.DOUBLE_TYPE))) : Optional.empty()); dimension.set(compound.contains("dimension", NbtElement.STRING_TYPE) ? Optional.ofNullable(Identifier.tryParse(compound.getString("dimension"))).map(id -> RegistryKey.of(RegistryKeys.WORLD, id)) : Optional.empty()); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java index edd37b52..18331330 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java @@ -84,7 +84,7 @@ public class PortalSpell extends AbstractSpell implements PlacementControlSpell. @SuppressWarnings("unchecked") private Ether.Entry getDestination(Caster source) { - return Util.NIL_UUID.equals(targetPortalId.get()) ? null : getDestinationReference() + return Util.NIL_UUID.equals(targetPortalId.getOrDefault(Util.NIL_UUID)) ? null : getDestinationReference() .getTarget() .map(target -> Ether.get(source.asWorld()).get((SpellType)getType(), target.uuid(), targetPortalId.get())) .filter(destination -> destination.isClaimedBy(getUuid())) @@ -229,7 +229,10 @@ public class PortalSpell extends AbstractSpell implements PlacementControlSpell. @Override public void toNBT(NbtCompound compound, WrapperLookup lookup) { super.toNBT(compound, lookup); - compound.putUuid("targetPortalId", targetPortalId.get()); + @Nullable UUID otherPortalUuid = targetPortalId.getOrDefault(Util.NIL_UUID); + if (!Util.NIL_UUID.equals(otherPortalUuid)) { + compound.putUuid("targetPortalId", otherPortalUuid); + } compound.put("teleportationTarget", teleportationTarget.toNBT(lookup)); compound.putFloat("pitch", getPitch()); compound.putFloat("yaw", getYaw()); diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java index 72ea46b3..8fe9b7bd 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java @@ -140,6 +140,11 @@ public class DataTracker { return tracker.get(this); } + public T getOrDefault(T def) { + T t = get(); + return t == null ? def : t; + } + public void set(T t) { tracker.set(this, t); }