From e465a218396f459173998c01e236feb02704da12 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 8 Apr 2024 16:53:01 +0100 Subject: [PATCH] Minor fixes to Ether and some owned style changes --- .../com/minelittlepony/unicopia/Owned.java | 2 +- .../minelittlepony/unicopia/WeaklyOwned.java | 4 ++-- .../magic/spell/effect/NecromancySpell.java | 2 +- .../magic/spell/effect/PortalSpell.java | 22 +++++++++---------- .../unicopia/entity/EntityReference.java | 2 +- .../unicopia/server/world/Ether.java | 14 +++++++++++- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/Owned.java b/src/main/java/com/minelittlepony/unicopia/Owned.java index 47c5403c..41c55d02 100644 --- a/src/main/java/com/minelittlepony/unicopia/Owned.java +++ b/src/main/java/com/minelittlepony/unicopia/Owned.java @@ -53,7 +53,7 @@ public interface Owned { } default boolean hasCommonOwner(Owned sibling) { - return getMasterId().isPresent() && getMasterId().equals(sibling.getMasterId()); + return getMasterId().equals(sibling.getMasterId()); } interface Mutable { diff --git a/src/main/java/com/minelittlepony/unicopia/WeaklyOwned.java b/src/main/java/com/minelittlepony/unicopia/WeaklyOwned.java index 514c22fd..504191bf 100644 --- a/src/main/java/com/minelittlepony/unicopia/WeaklyOwned.java +++ b/src/main/java/com/minelittlepony/unicopia/WeaklyOwned.java @@ -42,8 +42,8 @@ public interface WeaklyOwned extends Owned, WorldConvertabl @Override @SuppressWarnings("unchecked") default void setMaster(Owned sibling) { - if (sibling instanceof WeaklyOwned) { - getMasterReference().copyFrom(((WeaklyOwned)sibling).getMasterReference()); + if (sibling instanceof WeaklyOwned w) { + getMasterReference().copyFrom(w.getMasterReference()); } else { setMaster(sibling.getMaster()); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/NecromancySpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/NecromancySpell.java index 1e4243a6..dbf79bab 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/NecromancySpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/NecromancySpell.java @@ -197,7 +197,7 @@ public class NecromancySpell extends AbstractAreaEffectSpell implements Projecti } Equine.of(minion).filter(eq -> eq instanceof Creature).ifPresent(eq -> { - ((Creature)eq).setMaster(source.getMaster()); + ((Creature)eq).setMaster(source); if (source.asWorld().random.nextFloat() < source.getCorruption().getScaled(1)) { ((Creature)eq).setDiscorded(true); } 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 82a66ad9..fea666b7 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 @@ -57,7 +57,7 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme } public boolean isLinked() { - return teleportationTarget.getTarget().isPresent(); + return teleportationTarget.isSet(); } public Optional> getTarget() { @@ -85,7 +85,7 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme } @SuppressWarnings("unchecked") - private Optional> getTarget(Caster source) { + private Optional> getDestination(Caster source) { return getTarget().map(target -> Ether.get(source.asWorld()).get((SpellType)getType(), target, targetPortalId)); } @@ -98,13 +98,12 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme @Override public boolean tick(Caster source, Situation situation) { if (situation == Situation.GROUND) { - if (source.isClient()) { source.spawnParticles(particleArea, 5, pos -> { source.addParticle(ParticleTypes.ELECTRIC_SPARK, pos, Vec3d.ZERO); }); } else { - getTarget().ifPresent(target -> { + teleportationTarget.getTarget().ifPresent(target -> { if (Ether.get(source.asWorld()).get(getType(), target, targetPortalId) == null) { Unicopia.LOGGER.debug("Lost sibling, breaking connection to " + target.uuid()); teleportationTarget.set(null); @@ -113,16 +112,17 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme } }); - getTarget(source).ifPresentOrElse( + getDestination(source).ifPresentOrElse( entry -> tickWithTargetLink(source, entry), () -> findLink(source) ); } - var entry = Ether.get(source.asWorld()).getOrCreate(this, source); + Ether ether = Ether.get(source.asWorld()); + var entry = ether.getOrCreate(this, source); entry.pitch = pitch; entry.yaw = yaw; - Ether.get(source.asWorld()).markDirty(); + ether.markDirty(); } return !isDead(); @@ -185,8 +185,7 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme } Ether.get(source.asWorld()).anyMatch(getType(), entry -> { - if (entry.isAvailable() && !entry.entity.referenceEquals(source.asEntity()) && entry.entity.isSet()) { - entry.setTaken(true); + if (!entry.entity.referenceEquals(source.asEntity()) && entry.claim()) { teleportationTarget.copyFrom(entry.entity); targetPortalId = entry.getSpellId(); setDirty(); @@ -216,9 +215,8 @@ public class PortalSpell extends AbstractSpell implements PlaceableSpell.Placeme @Override protected void onDestroyed(Caster caster) { - Ether ether = Ether.get(caster.asWorld()); - ether.remove(getType(), caster); - getTarget(caster).ifPresent(e -> e.setTaken(false)); + Ether.get(caster.asWorld()).remove(getType(), caster); + getDestination(caster).ifPresent(Ether.Entry::release); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java index ad6a4d4c..c401c1ce 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java @@ -22,7 +22,7 @@ import net.minecraft.world.World; /** * An indirect reference to an entity by its unique id. - * Used to store the 'owner' reference for certain objects that allows them to\ + * Used to store the 'owner' reference for certain objects that allows them to * remember who they belong to even when the entity has been unloaded. * * Will also remember the position and certain attributes of the owner. diff --git a/src/main/java/com/minelittlepony/unicopia/server/world/Ether.java b/src/main/java/com/minelittlepony/unicopia/server/world/Ether.java index 8d2e309c..d60b7607 100644 --- a/src/main/java/com/minelittlepony/unicopia/server/world/Ether.java +++ b/src/main/java/com/minelittlepony/unicopia/server/world/Ether.java @@ -211,7 +211,7 @@ public class Ether extends PersistentState { } public boolean isAvailable() { - return !isDead() && !taken; + return !isDead() && !taken && entity.isSet(); } public void setTaken(boolean taken) { @@ -219,6 +219,18 @@ public class Ether extends PersistentState { markDirty(); } + public void release() { + setTaken(false); + } + + public boolean claim() { + if (isAvailable()) { + setTaken(true); + return true; + } + return false; + } + @Nullable public T getSpell() { if (removed) {