From 43521e76d99a78e101e4ebb4b7d00dfff534e5da Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Thu, 9 Jan 2025 18:32:23 +0100 Subject: [PATCH 01/32] move trackedlocations to subcraft and back to the parent on release Would be better to change this to use the same object but TrackedLocation is not mutable (cherry picked from commit c1a4e3b54e6741d2ef688c6403ecb21a2095fc9b) --- .../listener/CraftPilotListener.java | 88 ++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index 5901cdace..5070f7571 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -1,17 +1,28 @@ package net.countercraft.movecraft.listener; +import com.google.common.base.Predicates; import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.TrackedLocation; import net.countercraft.movecraft.craft.Craft; +import net.countercraft.movecraft.craft.SubCraft; import net.countercraft.movecraft.events.CraftPilotEvent; +import net.countercraft.movecraft.events.CraftReleaseEvent; +import net.countercraft.movecraft.util.hitboxes.HitBox; +import org.bukkit.NamespacedKey; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; +import java.util.Map; +import java.util.Set; +import java.util.function.Predicate; + public class CraftPilotListener implements Listener { - @EventHandler(ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onCraftPilot(@NotNull CraftPilotEvent event) { // Walk through all signs and set a UUID in there final Craft craft = event.getCraft(); @@ -30,6 +41,81 @@ public void onCraftPilot(@NotNull CraftPilotEvent event) { craft.markTileStateWithUUID(tile); tile.update(); } + + // Tracked locations => Modify correctly with subcrafts + if (craft instanceof SubCraft subCraft && subCraft.getParent() != null) { + carryOverTrackedLocations(subCraft, subCraft.getParent()); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onCraftRelease(@NotNull CraftReleaseEvent event) { + if (event.getReason() != CraftReleaseEvent.Reason.SUB_CRAFT) { + return; + } + Craft released = event.getCraft(); + if (!(released instanceof SubCraft)) { + return; + } + SubCraft subCraft = (SubCraft) released; + if (subCraft.getParent() == null) { + return; + } + + // Attention: SquadronCrafts are also subcrafts! We need to make sure that they are at least intersecting the parent when we add back the tracked locations + final HitBox parentHitBox = subCraft.getParent().getHitBox(); + final HitBox subCraftHitBox = subCraft.getHitBox(); + + if (!parentHitBox.inBounds(subCraftHitBox.getMinX(), subCraftHitBox.getMinY(), subCraftHitBox.getMinZ())) { + if (!parentHitBox.inBounds(subCraftHitBox.getMaxX(), subCraftHitBox.getMaxY(), subCraftHitBox.getMaxZ())) { + // Subcraft cant possible be within its parent anymore + return; + } + } + + transferTrackedLocations(subCraft, subCraft.getParent(), Predicates.alwaysTrue()); + } + + private static void carryOverTrackedLocations(SubCraft subCraft, Craft parent) { + if (parent.getWorld() != subCraft.getWorld()) { + return; + } + + transferTrackedLocations(parent, subCraft, (trackedLocation) -> { + MovecraftLocation absolute = trackedLocation.getAbsoluteLocation(); + if (subCraft.getHitBox().inBounds(absolute)) { + if (subCraft.getHitBox().contains(absolute)) { + return true; + } + } + return false; + }); + } + + /* + * Transfers TrackedLocations from a craft A to a craft B with a optional filter. + * This MOVES the tracked locations, so keep that in mind + */ + private static void transferTrackedLocations(final Craft a, final Craft b, Predicate filterArgument) { + final MovecraftLocation bMidPoint = b.getHitBox().getMidPoint(); + + for (Map.Entry> entry : a.getTrackedLocations().entrySet()) { + Set bTrackedLocations = b.getTrackedLocations().getOrDefault(entry.getKey(), Set.of()); + Set aTrackedLocations = entry.getValue(); + + if (aTrackedLocations.isEmpty()) { + continue; + } + + + aTrackedLocations.removeIf(trackedLocation -> { + if (filterArgument.test(trackedLocation)) { + TrackedLocation trackedLocationB = new TrackedLocation(b, trackedLocation.getAbsoluteLocation().subtract(bMidPoint)); + return bTrackedLocations.add(trackedLocationB); + } + return false; + }); + } } } From bea513c0a58f03569fba34896297f2a7b7858b19 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Thu, 9 Jan 2025 18:40:46 +0100 Subject: [PATCH 02/32] Ugly hack to allow transfering it to a different craft (cherry picked from commit 62f4fb1479aa10eaf3f32b38feb305f9a41ec449) --- .../movecraft/TrackedLocation.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index d61d74cab..7af886a24 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -1,12 +1,13 @@ package net.countercraft.movecraft; import net.countercraft.movecraft.craft.Craft; +import net.countercraft.movecraft.craft.SubCraft; import net.countercraft.movecraft.util.MathUtils; import org.jetbrains.annotations.NotNull; public class TrackedLocation { private MovecraftLocation offSet; - private final Craft craft; + private Craft craft; /** * Creates a new TrackedLocation instance which tracks a location about a craft's midpoint. @@ -15,9 +16,7 @@ public class TrackedLocation { * location to the craft's central hitbox. */ public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location) { - this.craft = craft; - MovecraftLocation midPoint = craft.getHitBox().getMidPoint(); - offSet = location.subtract(midPoint); + this.reset(craft, location); } /** @@ -44,4 +43,26 @@ public MovecraftLocation getAbsoluteLocation() { public MovecraftLocation getOffSet() { return offSet; } + + /** + * NEVER USE THIS UNLESS ABSOLUTELY NECESSARY + * @param craft + * @param location + */ + public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { + if (this.craft != null) { + if (!( + // From parent to subcraft + (craft instanceof SubCraft subCraft && subCraft.getParent() == this.craft) + // From subcraft back to parent + || (this.craft instanceof SubCraft subCraft2 && subCraft2.getParent() == craft) + )) { + throw new IllegalStateException("Only ever call this when transferring from or to subcraft!"); + } + } + this.craft = craft; + MovecraftLocation midPoint = craft.getHitBox().getMidPoint(); + offSet = location.subtract(midPoint); + } + } From 6ee8f0c7c6027d88cafe91a266da8a980d0216f0 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Thu, 9 Jan 2025 18:40:58 +0100 Subject: [PATCH 03/32] Transfer the same *object* (cherry picked from commit abeb7ed0b5398d2897a4ab13187ff9d06dc663b0) --- .../countercraft/movecraft/listener/CraftPilotListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index 5070f7571..75112ba07 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -110,8 +110,8 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi aTrackedLocations.removeIf(trackedLocation -> { if (filterArgument.test(trackedLocation)) { - TrackedLocation trackedLocationB = new TrackedLocation(b, trackedLocation.getAbsoluteLocation().subtract(bMidPoint)); - return bTrackedLocations.add(trackedLocationB); + trackedLocation.reset(b, trackedLocation.getAbsoluteLocation()); + return bTrackedLocations.add(trackedLocation); } return false; }); From 9b9369779e1c530c680dd4929b75683b3c0544e3 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 12 Jan 2025 17:56:39 +0100 Subject: [PATCH 04/32] update trackedlocation map to concurrent map --- .../main/java/net/countercraft/movecraft/craft/BaseCraft.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java index 2eec10dfd..bd3afab8c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java @@ -41,6 +41,7 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; @@ -79,7 +80,7 @@ public abstract class BaseCraft implements Craft { private String name = ""; @NotNull private MovecraftLocation lastTranslation = new MovecraftLocation(0, 0, 0); - private Map> trackedLocations = new HashMap<>(); + private Map> trackedLocations = new ConcurrentHashMap<>(); @NotNull private final CraftDataTagContainer dataTagContainer; From 1ed6851b7c36c396b597c9484bda06b74ba4e0f5 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 12 Jan 2025 20:21:16 +0100 Subject: [PATCH 05/32] for some odd reason, the pilot event for subcraft fires twice sometimes (cherry picked from commit 353711bf9f9abace4be72031c766b1e348ba9228) --- .../main/java/net/countercraft/movecraft/TrackedLocation.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 7af886a24..0aae9dce8 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -50,6 +50,9 @@ public MovecraftLocation getOffSet() { * @param location */ public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { + if (craft == this.craft) { + return; + } if (this.craft != null) { if (!( // From parent to subcraft From 49a1bf4d2a2d8cb0ad5933f0ec95de62d9d3002b Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 12 Jan 2025 20:33:55 +0100 Subject: [PATCH 06/32] use computeIfAbsent and HashSet in favor of Set.of() (cherry picked from commit a32024123d080f7f89fb1ffcf7acc6550cd9cda7) --- .../countercraft/movecraft/listener/CraftPilotListener.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index 75112ba07..81dcc26b8 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -16,6 +16,7 @@ import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.function.Predicate; @@ -100,8 +101,8 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi final MovecraftLocation bMidPoint = b.getHitBox().getMidPoint(); for (Map.Entry> entry : a.getTrackedLocations().entrySet()) { - Set bTrackedLocations = b.getTrackedLocations().getOrDefault(entry.getKey(), Set.of()); - Set aTrackedLocations = entry.getValue(); + final Set bTrackedLocations = b.getTrackedLocations().computeIfAbsent(entry.getKey(), k -> new HashSet<>()); + final Set aTrackedLocations = entry.getValue(); if (aTrackedLocations.isEmpty()) { continue; From b4bcb8438a170bbf97ba1c226e49ab26f9eca68a Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 18:48:28 +0100 Subject: [PATCH 07/32] reinforce transfer logic (cherry picked from commit 65e1209576917b3fb95a53113a6d7b734c8efee3) --- .../listener/CraftPilotListener.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index 81dcc26b8..396bde6fe 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -8,6 +8,7 @@ import net.countercraft.movecraft.events.CraftPilotEvent; import net.countercraft.movecraft.events.CraftReleaseEvent; import net.countercraft.movecraft.util.hitboxes.HitBox; +import org.bukkit.Location; import org.bukkit.NamespacedKey; import org.bukkit.block.Block; import org.bukkit.block.Sign; @@ -16,9 +17,7 @@ import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.Predicate; public class CraftPilotListener implements Listener { @@ -45,7 +44,19 @@ public void onCraftPilot(@NotNull CraftPilotEvent event) { // Tracked locations => Modify correctly with subcrafts if (craft instanceof SubCraft subCraft && subCraft.getParent() != null) { - carryOverTrackedLocations(subCraft, subCraft.getParent()); + final Craft parent = subCraft.getParent(); + if (parent.getWorld() != subCraft.getWorld()) { + return; + } + transferTrackedLocations(parent, subCraft, (trackedLocation) -> { + MovecraftLocation absolute = trackedLocation.getAbsoluteLocation(); + if (subCraft.getHitBox().inBounds(absolute)) { + if (subCraft.getHitBox().contains(absolute)) { + return true; + } + } + return false; + }); } } @@ -77,22 +88,6 @@ public void onCraftRelease(@NotNull CraftReleaseEvent event) { transferTrackedLocations(subCraft, subCraft.getParent(), Predicates.alwaysTrue()); } - private static void carryOverTrackedLocations(SubCraft subCraft, Craft parent) { - if (parent.getWorld() != subCraft.getWorld()) { - return; - } - - transferTrackedLocations(parent, subCraft, (trackedLocation) -> { - MovecraftLocation absolute = trackedLocation.getAbsoluteLocation(); - if (subCraft.getHitBox().inBounds(absolute)) { - if (subCraft.getHitBox().contains(absolute)) { - return true; - } - } - return false; - }); - } - /* * Transfers TrackedLocations from a craft A to a craft B with a optional filter. * This MOVES the tracked locations, so keep that in mind @@ -108,14 +103,19 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi continue; } - - aTrackedLocations.removeIf(trackedLocation -> { + List transferred = new ArrayList<>(); + aTrackedLocations.forEach(trackedLocation -> { if (filterArgument.test(trackedLocation)) { - trackedLocation.reset(b, trackedLocation.getAbsoluteLocation()); - return bTrackedLocations.add(trackedLocation); + final MovecraftLocation absoluteLocation = trackedLocation.getAbsoluteLocation(); + trackedLocation.reset(b, absoluteLocation); + if (!(bTrackedLocations.add(trackedLocation))) { + trackedLocation.reset(a, absoluteLocation); + } else { + transferred.add(trackedLocation); + } } - return false; }); + aTrackedLocations.removeAll(transferred); } } From 2492f1e0321516e049de5b3068c6896e7ec63c8f Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 19:17:22 +0100 Subject: [PATCH 08/32] Fix trackedlocation rotation Old logic rotated the vector around the wrong point. Its safer to recalculate the new absolute location and calculate the offset from that (cherry picked from commit 771094b5e5eb8c03b46fc65f125fc976e0f96f79) --- .../java/net/countercraft/movecraft/TrackedLocation.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 0aae9dce8..52f11fc75 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -24,7 +24,11 @@ public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location * @param rotation A clockwise or counter-clockwise direction to rotate. */ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { - offSet = MathUtils.rotateVec(rotation, getAbsoluteLocation().subtract(origin)); + MovecraftLocation absolute = this.getAbsoluteLocation(); + MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(origin)); + + MovecraftLocation newAbsolute = origin.add(vector); + offSet = offSet.add(newAbsolute.subtract(absolute)); } /** From 299c1608369d85246712bf5ecb800f514ac3bd4c Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 19:18:07 +0100 Subject: [PATCH 09/32] comment out code => this will rotate ALL aprent craft's trackedlocations around the rotation point, REGARDLESS of if they are part of the subcraft or not (cherry picked from commit 7e5563677e474d4ff79ca3d459feb809e7b67e80) --- .../countercraft/movecraft/async/rotation/RotationTask.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 586aea1a0..d9cd615fe 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -166,13 +166,13 @@ protected void execute() { // Rotates the craft's tracked locations, and all parent craft's. Craft temp = craft; - do { - for (Set locations : craft.getTrackedLocations().values()) { + //do { + for (Set locations : temp.getTrackedLocations().values()) { for (TrackedLocation location : locations) { location.rotate(rotation, originPoint); } } - } while (temp instanceof SubCraft && (temp = ((SubCraft) temp).getParent()) != null); + //} while (temp instanceof SubCraft && (temp = ((SubCraft) temp).getParent()) != null); updates.add(new CraftRotateCommand(getCraft(),originPoint, rotation)); //rotate entities in the craft From 3c8d3007d19a2f07dbfed5b3b32a2321a14700f8 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 19:30:36 +0100 Subject: [PATCH 10/32] just to be safe and to keep it working with normal rotations (cherry picked from commit 501dcd379d44c6f6ab87a12c479811160edd7627) --- .../java/net/countercraft/movecraft/TrackedLocation.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 52f11fc75..7f4e4b408 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -28,7 +28,7 @@ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(origin)); MovecraftLocation newAbsolute = origin.add(vector); - offSet = offSet.add(newAbsolute.subtract(absolute)); + reset(this.craft, newAbsolute); } /** @@ -72,4 +72,8 @@ public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { offSet = location.subtract(midPoint); } + public void reset(@NotNull MovecraftLocation location) { + this.reset(this.craft, location); + } + } From 873035cb9edfb7afb2a8dea37d42f2e1f4ddd145 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 19:42:51 +0100 Subject: [PATCH 11/32] transfer the object if possible to keep compatibility with moving subcrafts such as squadrons (cherry picked from commit 289ddbbba7d607ec2452855aea908d9c69a73a56) --- .../listener/CraftPilotListener.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index 396bde6fe..b24469b72 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -56,7 +56,7 @@ public void onCraftPilot(@NotNull CraftPilotEvent event) { } } return false; - }); + }, true); } } @@ -85,14 +85,14 @@ public void onCraftRelease(@NotNull CraftReleaseEvent event) { } } - transferTrackedLocations(subCraft, subCraft.getParent(), Predicates.alwaysTrue()); + transferTrackedLocations(subCraft, subCraft.getParent(), Predicates.alwaysTrue(), true); } /* * Transfers TrackedLocations from a craft A to a craft B with a optional filter. * This MOVES the tracked locations, so keep that in mind */ - private static void transferTrackedLocations(final Craft a, final Craft b, Predicate filterArgument) { + private static void transferTrackedLocations(final Craft a, final Craft b, Predicate filterArgument, boolean move) { final MovecraftLocation bMidPoint = b.getHitBox().getMidPoint(); for (Map.Entry> entry : a.getTrackedLocations().entrySet()) { @@ -103,15 +103,21 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi continue; } + // Commented out code: previous attempt to actually transfer the tracked locations, which technically is unnecessary unless for subcrafts like squadrons that actually move! List transferred = new ArrayList<>(); aTrackedLocations.forEach(trackedLocation -> { if (filterArgument.test(trackedLocation)) { - final MovecraftLocation absoluteLocation = trackedLocation.getAbsoluteLocation(); - trackedLocation.reset(b, absoluteLocation); - if (!(bTrackedLocations.add(trackedLocation))) { - trackedLocation.reset(a, absoluteLocation); + if (move) { + + final MovecraftLocation absoluteLocation = trackedLocation.getAbsoluteLocation(); + trackedLocation.reset(b, absoluteLocation); + if (!(bTrackedLocations.add(trackedLocation))) { + trackedLocation.reset(a, absoluteLocation); + } else { + transferred.add(trackedLocation); + } } else { - transferred.add(trackedLocation); + bTrackedLocations.add(trackedLocation); } } }); From fd33db1b2b601ca599889a1cde4b223855f0fbe9 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 20:14:56 +0100 Subject: [PATCH 12/32] actually run the code Tested: Calculation wise, the correct result is put out, but either the debugger is inaccurate or something else is happening... (cherry picked from commit 5085cbf8f17415fcef396c31c16906421689ca80) --- .../java/net/countercraft/movecraft/TrackedLocation.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 7f4e4b408..1be487e0e 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -28,7 +28,10 @@ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(origin)); MovecraftLocation newAbsolute = origin.add(vector); - reset(this.craft, newAbsolute); + // Ugly hack, but necessary + Craft actualCraft = this.craft; + this.craft = null; + reset(actualCraft, newAbsolute); } /** From 7effbfbe29c15cb940fb6e10521227266e79b384 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 21:23:51 +0100 Subject: [PATCH 13/32] temporary "debug" stuff (cherry picked from commit 166438cce4a13c25eaf34dcf35f36ad6fb4aa329) --- .../countercraft/movecraft/async/rotation/RotationTask.java | 3 +++ .../countercraft/movecraft/listener/CraftPilotListener.java | 5 ++++- .../java/net/countercraft/movecraft/TrackedLocation.java | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index d9cd615fe..5a405cf82 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -169,7 +169,10 @@ protected void execute() { //do { for (Set locations : temp.getTrackedLocations().values()) { for (TrackedLocation location : locations) { + System.out.println("ROTATING"); + System.out.println("Original absolute: " + location.getAbsoluteLocation()); location.rotate(rotation, originPoint); + System.out.println("ROTATING END"); } } //} while (temp instanceof SubCraft && (temp = ((SubCraft) temp).getParent()) != null); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index b24469b72..859e720c4 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -108,14 +108,17 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi aTrackedLocations.forEach(trackedLocation -> { if (filterArgument.test(trackedLocation)) { if (move) { - final MovecraftLocation absoluteLocation = trackedLocation.getAbsoluteLocation(); + System.out.println("TRANSFERRING"); + System.out.println("Original absolute: " + absoluteLocation.toString()); trackedLocation.reset(b, absoluteLocation); if (!(bTrackedLocations.add(trackedLocation))) { trackedLocation.reset(a, absoluteLocation); } else { transferred.add(trackedLocation); } + System.out.println("Absolute After Transfer: " + trackedLocation.getAbsoluteLocation().toString()); + System.out.println("TRANSFERRING END"); } else { bTrackedLocations.add(trackedLocation); } diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 1be487e0e..dee1d1348 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -24,10 +24,13 @@ public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location * @param rotation A clockwise or counter-clockwise direction to rotate. */ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { + // TODO: SOMEHOW "absolute" and "origin" can end up to be the same thing?! WTF?! MovecraftLocation absolute = this.getAbsoluteLocation(); + System.out.println("Absolute: " + absolute.toString()); MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(origin)); MovecraftLocation newAbsolute = origin.add(vector); + System.out.println("New absolute: " + newAbsolute.toString()); // Ugly hack, but necessary Craft actualCraft = this.craft; this.craft = null; @@ -72,6 +75,8 @@ public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { } this.craft = craft; MovecraftLocation midPoint = craft.getHitBox().getMidPoint(); + System.out.println("Location: " + location.toString()); + System.out.println("Midpoint: " + midPoint.toString()); offSet = location.subtract(midPoint); } From b9878227c3c00f3b1b86223a554ebe86bdca71db Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 21:36:12 +0100 Subject: [PATCH 14/32] no need to call reset() (cherry picked from commit d45567e645f33dfcc93e81423b566b96e138a6b7) --- .../java/net/countercraft/movecraft/TrackedLocation.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index dee1d1348..e470c892f 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -32,9 +32,9 @@ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { MovecraftLocation newAbsolute = origin.add(vector); System.out.println("New absolute: " + newAbsolute.toString()); // Ugly hack, but necessary - Craft actualCraft = this.craft; - this.craft = null; - reset(actualCraft, newAbsolute); + MovecraftLocation craftMidPoint = this.craft.getHitBox().getMidPoint(); + this.offSet = newAbsolute.subtract(craftMidPoint); + System.out.println("New absolute after recalculating: " + this.getAbsoluteLocation().toString()); } /** From 19ff28deae430ebd16c94cc09036d0a8771c0e18 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 17 Jan 2025 23:15:24 +0100 Subject: [PATCH 15/32] throw exception when the effective location changes while transferring (cherry picked from commit 436a2e5feb77fed948e751d5320f22540e9edf1a) --- .../countercraft/movecraft/listener/CraftPilotListener.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index 859e720c4..ae3ce584c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -118,6 +118,9 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi transferred.add(trackedLocation); } System.out.println("Absolute After Transfer: " + trackedLocation.getAbsoluteLocation().toString()); + if (!absoluteLocation.equals(trackedLocation.getAbsoluteLocation())) { + throw new IllegalStateException("Somehow the previous and transferred absolute locations are NOT the same! This should NEVER happen!"); + } System.out.println("TRANSFERRING END"); } else { bTrackedLocations.add(trackedLocation); From e2f7a03e324bad5e001154c9823870c45feb1a3f Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:46:32 +0100 Subject: [PATCH 16/32] make trackedlocations a bit more stupid it does not need to know the craft. Also just moving around the cached coordinates is more reliable as it does not rely on a changing center of the craft (cherry picked from commit 8fe3979777300c632b2ced57aae1de6e490ee22e) --- .../movecraft/TrackedLocation.java | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index e470c892f..0aae41b96 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -1,22 +1,37 @@ package net.countercraft.movecraft; import net.countercraft.movecraft.craft.Craft; -import net.countercraft.movecraft.craft.SubCraft; import net.countercraft.movecraft.util.MathUtils; import org.jetbrains.annotations.NotNull; public class TrackedLocation { - private MovecraftLocation offSet; - private Craft craft; + + private int x; + private int y; + private int z; /** * Creates a new TrackedLocation instance which tracks a location about a craft's midpoint. - * @param craft The craft that's that tied to the location. * @param location The absolute position to track. This location will be stored as a relative * location to the craft's central hitbox. */ + public TrackedLocation(@NotNull MovecraftLocation location) { + reinit(location); + } + + @Deprecated(forRemoval = true) public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location) { - this.reset(craft, location); + this(location); + } + + protected void reinit(@NotNull MovecraftLocation location) { + reinit(location.getX(), location.getY(), location.getZ()); + } + + protected void reinit(final int x, final int y, final int z) { + this.x = x; + this.y = y; + this.z = z; } /** @@ -28,30 +43,33 @@ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { MovecraftLocation absolute = this.getAbsoluteLocation(); System.out.println("Absolute: " + absolute.toString()); MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(origin)); + //MovecraftLocation vector = absolute.subtract(origin); + //int x = rotation == MovecraftRotation.ANTICLOCKWISE ? vector.getZ() : -vector.getZ(); + //int z = rotation == MovecraftRotation.ANTICLOCKWISE ? -vector.getX() : vector.getX(); + //vector = new MovecraftLocation(x, vector.getY(), z); MovecraftLocation newAbsolute = origin.add(vector); System.out.println("New absolute: " + newAbsolute.toString()); // Ugly hack, but necessary - MovecraftLocation craftMidPoint = this.craft.getHitBox().getMidPoint(); - this.offSet = newAbsolute.subtract(craftMidPoint); + //MovecraftLocation craftMidPoint = this.craft.getHitBox().getMidPoint(); + //this.offSet = newAbsolute.subtract(craftMidPoint); + reinit(newAbsolute); + System.out.println("New absolute after recalculating: " + this.getAbsoluteLocation().toString()); } - /** - * Gets the stored absolute location. - * @return Returns the absolute location instead of a vector. - */ - public MovecraftLocation getAbsoluteLocation() { - MovecraftLocation midPoint = craft.getHitBox().getMidPoint(); - return offSet.add(midPoint); + public void translate(final int dx, final int dy, final int dz) { + this.x += dx; + this.y += dy; + this.z += dz; } /** - * Gets the stored location as a position vector relative to the midpoint. + * Gets the stored absolute location. * @return Returns the absolute location instead of a vector. */ - public MovecraftLocation getOffSet() { - return offSet; + public MovecraftLocation getAbsoluteLocation() { + return new MovecraftLocation(this.x, this. y, this.z); } /** @@ -60,7 +78,7 @@ public MovecraftLocation getOffSet() { * @param location */ public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { - if (craft == this.craft) { + /*if (craft == this.craft) { return; } if (this.craft != null) { @@ -77,11 +95,8 @@ public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { MovecraftLocation midPoint = craft.getHitBox().getMidPoint(); System.out.println("Location: " + location.toString()); System.out.println("Midpoint: " + midPoint.toString()); - offSet = location.subtract(midPoint); - } - - public void reset(@NotNull MovecraftLocation location) { - this.reset(this.craft, location); + offSet = location.subtract(midPoint);*/ + reinit(location); } } From de7d7e8461c16602c343dc7ebfb6cb5003da2544 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:46:44 +0100 Subject: [PATCH 17/32] move tracked locations (cherry picked from commit ab9322bf5d034718a7fe441cf73f6d09529a2f1f) --- .../async/translation/TranslationTask.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java index a1a9390b6..144db0b62 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java @@ -3,6 +3,7 @@ import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftChunk; import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.TrackedLocation; import net.countercraft.movecraft.async.AsyncTask; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.ChunkManager; @@ -318,6 +319,8 @@ protected void execute() throws InterruptedException, ExecutionException { } } + updateTrackedLocations(craft, dx, dy, dz); + if (!collisionBox.isEmpty() && craft.getType().getBoolProperty(CraftType.CRUISE_ON_PILOT)) { CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.EMPTY, false); for (MovecraftLocation location : oldHitBox) { @@ -339,6 +342,16 @@ protected void execute() throws InterruptedException, ExecutionException { captureYield(harvestedBlocks); } + protected void updateTrackedLocations(Craft craft, int dx, int dy, int dz) { + craft.getTrackedLocations().values().forEach(trackedLocations -> { + trackedLocations.forEach( + trackedLocation -> { + trackedLocation.translate(dx, dy, dz); + } + ); + }); + } + private void preventsTorpedoRocketsPilots() { if (!craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES) || (craft instanceof SinkingCraft From 8d312f1693602c361a63629d9e8a0541e18db679 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:49:12 +0100 Subject: [PATCH 18/32] clean up (cherry picked from commit 41514c60508007431ce48dbe00da50f9f093d96a) --- .../listener/CraftPilotListener.java | 10 ++---- .../movecraft/TrackedLocation.java | 35 +++---------------- 2 files changed, 8 insertions(+), 37 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index ae3ce584c..eeaed477a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -8,7 +8,6 @@ import net.countercraft.movecraft.events.CraftPilotEvent; import net.countercraft.movecraft.events.CraftReleaseEvent; import net.countercraft.movecraft.util.hitboxes.HitBox; -import org.bukkit.Location; import org.bukkit.NamespacedKey; import org.bukkit.block.Block; import org.bukkit.block.Sign; @@ -108,20 +107,17 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi aTrackedLocations.forEach(trackedLocation -> { if (filterArgument.test(trackedLocation)) { if (move) { + // Technically this (the reset call) is not necessary, but we will keep it here for potential extensions by third party addons final MovecraftLocation absoluteLocation = trackedLocation.getAbsoluteLocation(); - System.out.println("TRANSFERRING"); - System.out.println("Original absolute: " + absoluteLocation.toString()); - trackedLocation.reset(b, absoluteLocation); + trackedLocation.reset(absoluteLocation); if (!(bTrackedLocations.add(trackedLocation))) { - trackedLocation.reset(a, absoluteLocation); + trackedLocation.reset(absoluteLocation); } else { transferred.add(trackedLocation); } - System.out.println("Absolute After Transfer: " + trackedLocation.getAbsoluteLocation().toString()); if (!absoluteLocation.equals(trackedLocation.getAbsoluteLocation())) { throw new IllegalStateException("Somehow the previous and transferred absolute locations are NOT the same! This should NEVER happen!"); } - System.out.println("TRANSFERRING END"); } else { bTrackedLocations.add(trackedLocation); } diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 0aae41b96..dc72c04dd 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -39,23 +39,11 @@ protected void reinit(final int x, final int y, final int z) { * @param rotation A clockwise or counter-clockwise direction to rotate. */ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { - // TODO: SOMEHOW "absolute" and "origin" can end up to be the same thing?! WTF?! MovecraftLocation absolute = this.getAbsoluteLocation(); - System.out.println("Absolute: " + absolute.toString()); MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(origin)); - //MovecraftLocation vector = absolute.subtract(origin); - //int x = rotation == MovecraftRotation.ANTICLOCKWISE ? vector.getZ() : -vector.getZ(); - //int z = rotation == MovecraftRotation.ANTICLOCKWISE ? -vector.getX() : vector.getX(); - //vector = new MovecraftLocation(x, vector.getY(), z); MovecraftLocation newAbsolute = origin.add(vector); - System.out.println("New absolute: " + newAbsolute.toString()); - // Ugly hack, but necessary - //MovecraftLocation craftMidPoint = this.craft.getHitBox().getMidPoint(); - //this.offSet = newAbsolute.subtract(craftMidPoint); reinit(newAbsolute); - - System.out.println("New absolute after recalculating: " + this.getAbsoluteLocation().toString()); } public void translate(final int dx, final int dy, final int dz) { @@ -77,25 +65,12 @@ public MovecraftLocation getAbsoluteLocation() { * @param craft * @param location */ + @Deprecated(forRemoval = true) public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { - /*if (craft == this.craft) { - return; - } - if (this.craft != null) { - if (!( - // From parent to subcraft - (craft instanceof SubCraft subCraft && subCraft.getParent() == this.craft) - // From subcraft back to parent - || (this.craft instanceof SubCraft subCraft2 && subCraft2.getParent() == craft) - )) { - throw new IllegalStateException("Only ever call this when transferring from or to subcraft!"); - } - } - this.craft = craft; - MovecraftLocation midPoint = craft.getHitBox().getMidPoint(); - System.out.println("Location: " + location.toString()); - System.out.println("Midpoint: " + midPoint.toString()); - offSet = location.subtract(midPoint);*/ + reset(location); + } + + public void reset(@NotNull MovecraftLocation location) { reinit(location); } From df0b6908aeb348b0ef6ed6b05e16aa7ae8ae3739 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:50:30 +0100 Subject: [PATCH 19/32] remove debug println's (cherry picked from commit 2792c4816188e9a2a8475b5aff6458fb6f94f283) --- .../countercraft/movecraft/async/rotation/RotationTask.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 5a405cf82..d9cd615fe 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -169,10 +169,7 @@ protected void execute() { //do { for (Set locations : temp.getTrackedLocations().values()) { for (TrackedLocation location : locations) { - System.out.println("ROTATING"); - System.out.println("Original absolute: " + location.getAbsoluteLocation()); location.rotate(rotation, originPoint); - System.out.println("ROTATING END"); } } //} while (temp instanceof SubCraft && (temp = ((SubCraft) temp).getParent()) != null); From ae18c94af119c42c4ec26d685cfc4fb23e7e3c90 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:39:23 +0100 Subject: [PATCH 20/32] Add craftOrigin helper object --- .../countercraft/movecraft/craft/Craft.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java index a1a44ee69..eddd8517e 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java @@ -40,7 +40,9 @@ import org.bukkit.block.data.BlockData; import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.NotNull; +import org.joml.Vector3i; +import javax.naming.Name; import java.util.*; public interface Craft { @@ -51,12 +53,74 @@ public interface Craft { CraftDataTagKey> MOVEBLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "moveblocks"), craft -> new Counter<>()); CraftDataTagKey NON_NEGLIGIBLE_BLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "non-negligible-blocks"), Craft::getOrigBlockCount); CraftDataTagKey NON_NEGLIGIBLE_SOLID_BLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "non-negligible-solid-blocks"), Craft::getOrigBlockCount); + CraftDataTagKey CRAFT_ORIGIN = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "craft-origin"), CraftOrigin::new); // Java disallows private or protected fields in interfaces, this is a workaround class Hidden { // Concurrent so we don't have problems when accessing async (useful for addon plugins that want to do stuff async, for example NPC crafts with complex off-thread pathfinding) protected static final Map uuidToCraft = Collections.synchronizedMap(new WeakHashMap<>()); } + + public class CraftOrigin { + + private int x; + private int y; + private int z; + + public CraftOrigin(final @NotNull Craft craft) { + + } + + public void translate(int dx, int dy, int dz) { + this.x += dx; + this.y += dy; + this.z += dz; + } + + public void rotate(final MovecraftLocation rotationPoint, final MovecraftRotation rotation) { + MovecraftLocation oldAbsolute = this.getLocation(); + MovecraftLocation vector = oldAbsolute.subtract(rotationPoint); + MovecraftLocation vectorRotated = MathUtils.rotateVec(rotation, vector); + MovecraftLocation newAbsolute = rotationPoint.add(vectorRotated); + this.copyValues(newAbsolute); + } + + public void copyValues(final MovecraftLocation location) { + this.x = location.getX(); + this.y = location.getY(); + this.z = location.getZ(); + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public int getZ() { + return z; + } + + public void setZ(int z) { + this.z = z; + } + + public MovecraftLocation getLocation() { + return new MovecraftLocation(x, y, z); + } + + } + public static Craft getCraftByUUID(final UUID uuid) { return Hidden.uuidToCraft.getOrDefault(uuid, null); } @@ -293,4 +357,8 @@ public default void removeUUIDMarkFromTile(TileState tile) { } Map> getTrackedLocations(); + + public default CraftOrigin getCraftOrigin() { + return this.getDataTag(CRAFT_ORIGIN); + } } From 41c4ca377fb8d606a80c629b367c94a0087e350a Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:47:00 +0100 Subject: [PATCH 21/32] update trackedLocation implementation to be based on the enw reference location --- .../movecraft/TrackedLocation.java | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index dc72c04dd..659923603 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -4,24 +4,25 @@ import net.countercraft.movecraft.util.MathUtils; import org.jetbrains.annotations.NotNull; +import java.lang.ref.WeakReference; + public class TrackedLocation { - private int x; - private int y; - private int z; + private int dx; + private int dy; + private int dz; + + private WeakReference craft; /** * Creates a new TrackedLocation instance which tracks a location about a craft's midpoint. + * @param craft The craft this trackedlocation belongs to * @param location The absolute position to track. This location will be stored as a relative * location to the craft's central hitbox. */ - public TrackedLocation(@NotNull MovecraftLocation location) { - reinit(location); - } - - @Deprecated(forRemoval = true) public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location) { - this(location); + this.craft = new WeakReference<>(craft); + reinit(location); } protected void reinit(@NotNull MovecraftLocation location) { @@ -29,9 +30,11 @@ protected void reinit(@NotNull MovecraftLocation location) { } protected void reinit(final int x, final int y, final int z) { - this.x = x; - this.y = y; - this.z = z; + Craft craft = this.getCraft(); + Craft.CraftOrigin origin = craft.getCraftOrigin(); + this.dx = x - origin.getX(); + this.dy = y - origin.getY(); + this.dz = z - origin.getZ(); } /** @@ -46,18 +49,19 @@ public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { reinit(newAbsolute); } - public void translate(final int dx, final int dy, final int dz) { - this.x += dx; - this.y += dy; - this.z += dz; - } - /** * Gets the stored absolute location. * @return Returns the absolute location instead of a vector. */ public MovecraftLocation getAbsoluteLocation() { - return new MovecraftLocation(this.x, this. y, this.z); + Craft craft = this.getCraft(); + Craft.CraftOrigin origin = craft.getCraftOrigin(); + + int x = origin.getX() + this.dx; + int y = origin.getX() + this.dy; + int z = origin.getX() + this.dz; + + return new MovecraftLocation(x, y, z); } /** @@ -65,13 +69,16 @@ public MovecraftLocation getAbsoluteLocation() { * @param craft * @param location */ - @Deprecated(forRemoval = true) public void reset(@NotNull Craft craft, @NotNull MovecraftLocation location) { - reset(location); + this.craft = new WeakReference<>(craft); + reinit(location); } - public void reset(@NotNull MovecraftLocation location) { - reinit(location); + public Craft getCraft() { + if (this.craft.get() == null) { + throw new RuntimeException("Craft of tracked location is null! This indicates that the craft object was destroyed but somehow the tracked location is still around!"); + } + return this.craft.get(); } } From 073059f7254583a644ef40cdd3b67acbbd7e1fbf Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:48:29 +0100 Subject: [PATCH 22/32] update implementation of translate and rotate for subcrafts --- .../movecraft/async/rotation/RotationTask.java | 1 + .../async/translation/TranslationTask.java | 13 ++----------- .../movecraft/listener/CraftPilotListener.java | 4 ++-- .../net/countercraft/movecraft/TrackedLocation.java | 6 +++--- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index d9cd615fe..8efabce69 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -166,6 +166,7 @@ protected void execute() { // Rotates the craft's tracked locations, and all parent craft's. Craft temp = craft; + // recursion through all subcrafts is not necessary as the trackedlocations are transferred to the subcraft //do { for (Set locations : temp.getTrackedLocations().values()) { for (TrackedLocation location : locations) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java index 144db0b62..690799e05 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java @@ -319,7 +319,8 @@ protected void execute() throws InterruptedException, ExecutionException { } } - updateTrackedLocations(craft, dx, dy, dz); + // Update the reference location for trackedlocations + craft.getCraftOrigin().translate(dx, dy, dz); if (!collisionBox.isEmpty() && craft.getType().getBoolProperty(CraftType.CRUISE_ON_PILOT)) { CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.EMPTY, false); @@ -342,16 +343,6 @@ protected void execute() throws InterruptedException, ExecutionException { captureYield(harvestedBlocks); } - protected void updateTrackedLocations(Craft craft, int dx, int dy, int dz) { - craft.getTrackedLocations().values().forEach(trackedLocations -> { - trackedLocations.forEach( - trackedLocation -> { - trackedLocation.translate(dx, dy, dz); - } - ); - }); - } - private void preventsTorpedoRocketsPilots() { if (!craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES) || (craft instanceof SinkingCraft diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index eeaed477a..e02048a77 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -109,9 +109,9 @@ private static void transferTrackedLocations(final Craft a, final Craft b, Predi if (move) { // Technically this (the reset call) is not necessary, but we will keep it here for potential extensions by third party addons final MovecraftLocation absoluteLocation = trackedLocation.getAbsoluteLocation(); - trackedLocation.reset(absoluteLocation); + trackedLocation.reset(b, absoluteLocation); if (!(bTrackedLocations.add(trackedLocation))) { - trackedLocation.reset(absoluteLocation); + trackedLocation.reset(a, absoluteLocation); } else { transferred.add(trackedLocation); } diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 659923603..402c8cde7 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -41,11 +41,11 @@ protected void reinit(final int x, final int y, final int z) { * Rotates the stored location. * @param rotation A clockwise or counter-clockwise direction to rotate. */ - public void rotate(MovecraftRotation rotation, MovecraftLocation origin) { + public void rotate(MovecraftRotation rotation, MovecraftLocation rotationPoint) { MovecraftLocation absolute = this.getAbsoluteLocation(); - MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(origin)); + MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(rotationPoint)); - MovecraftLocation newAbsolute = origin.add(vector); + MovecraftLocation newAbsolute = rotationPoint.add(vector); reinit(newAbsolute); } From f3af15c62ab7843fdb7b797afd5890f2a103d261 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:55:58 +0100 Subject: [PATCH 23/32] forgot to rotate origin too (cherry picked from commit eb10b6f5131e9ba06b507cdd598e30e413a146fb) --- .../net/countercraft/movecraft/async/rotation/RotationTask.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 8efabce69..04ea7eb02 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -165,6 +165,8 @@ protected void execute() { } // Rotates the craft's tracked locations, and all parent craft's. + // First, rotate the refernce point! + craft.getCraftOrigin().rotate(originPoint, rotation); Craft temp = craft; // recursion through all subcrafts is not necessary as the trackedlocations are transferred to the subcraft //do { From 5adcaebda5dcc50f50564e4dc905701a0ae8f351 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 26 Jan 2025 20:02:59 +0100 Subject: [PATCH 24/32] correct typo (cherry picked from commit 11fe20a5b50b6f05fe33d864bf8020ae89dfc036) --- .../main/java/net/countercraft/movecraft/TrackedLocation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 402c8cde7..0850bf4a0 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -58,8 +58,8 @@ public MovecraftLocation getAbsoluteLocation() { Craft.CraftOrigin origin = craft.getCraftOrigin(); int x = origin.getX() + this.dx; - int y = origin.getX() + this.dy; - int z = origin.getX() + this.dz; + int y = origin.getY() + this.dy; + int z = origin.getZ() + this.dz; return new MovecraftLocation(x, y, z); } From 3f6425cf4586a928bb05ad89ab5cd02c9d6f04ce Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 26 Jan 2025 20:13:47 +0100 Subject: [PATCH 25/32] careful, the order here is very important => Origin needs to be rotated *last* (cherry picked from commit def82a6fab5f8f884855e7f0e3d88c932b7feca3) --- .../countercraft/movecraft/async/rotation/RotationTask.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 04ea7eb02..6d29f2dd6 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -165,8 +165,6 @@ protected void execute() { } // Rotates the craft's tracked locations, and all parent craft's. - // First, rotate the refernce point! - craft.getCraftOrigin().rotate(originPoint, rotation); Craft temp = craft; // recursion through all subcrafts is not necessary as the trackedlocations are transferred to the subcraft //do { @@ -176,6 +174,8 @@ protected void execute() { } } //} while (temp instanceof SubCraft && (temp = ((SubCraft) temp).getParent()) != null); + // Lastly, rotate the refernce point as the trackedlocations need the center at the old location while it rotates!! + craft.getCraftOrigin().rotate(originPoint, rotation); updates.add(new CraftRotateCommand(getCraft(),originPoint, rotation)); //rotate entities in the craft From 8150cef83ceff015aad5d85f004b7e659d72f7f0 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 26 Jan 2025 22:37:13 +0100 Subject: [PATCH 26/32] fix rotation on trackedlocations (cherry picked from commit a26f3500c72eb206dedbeaccd95182e7ca85ffdb) --- .../movecraft/async/rotation/RotationTask.java | 5 ++--- .../net/countercraft/movecraft/TrackedLocation.java | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 6d29f2dd6..711989196 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -165,17 +165,16 @@ protected void execute() { } // Rotates the craft's tracked locations, and all parent craft's. + craft.getCraftOrigin().rotate(originPoint, rotation); Craft temp = craft; // recursion through all subcrafts is not necessary as the trackedlocations are transferred to the subcraft //do { for (Set locations : temp.getTrackedLocations().values()) { for (TrackedLocation location : locations) { - location.rotate(rotation, originPoint); + location.rotate(rotation); } } //} while (temp instanceof SubCraft && (temp = ((SubCraft) temp).getParent()) != null); - // Lastly, rotate the refernce point as the trackedlocations need the center at the old location while it rotates!! - craft.getCraftOrigin().rotate(originPoint, rotation); updates.add(new CraftRotateCommand(getCraft(),originPoint, rotation)); //rotate entities in the craft diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index 0850bf4a0..e515d669b 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -41,12 +41,12 @@ protected void reinit(final int x, final int y, final int z) { * Rotates the stored location. * @param rotation A clockwise or counter-clockwise direction to rotate. */ - public void rotate(MovecraftRotation rotation, MovecraftLocation rotationPoint) { - MovecraftLocation absolute = this.getAbsoluteLocation(); - MovecraftLocation vector = MathUtils.rotateVec(rotation, absolute.subtract(rotationPoint)); + public void rotate(MovecraftRotation rotation) { + MovecraftLocation newVector = MathUtils.rotateVec(rotation, new MovecraftLocation(this.dx, this.dy, this.dz)); - MovecraftLocation newAbsolute = rotationPoint.add(vector); - reinit(newAbsolute); + this.dx = newVector.getX(); + this.dy = newVector.getY(); + this.dz = newVector.getZ(); } /** From 714316f0774776415746fc2b07df9f5dc246b8b1 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Mon, 27 Jan 2025 12:59:34 +0100 Subject: [PATCH 27/32] Use MovecraftLocation in TrackedLocation Not necessary to use ints here as it is just a vector --- .../movecraft/TrackedLocation.java | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index e515d669b..ac44ac3e5 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -8,9 +8,7 @@ public class TrackedLocation { - private int dx; - private int dy; - private int dz; + private MovecraftLocation vector; private WeakReference craft; @@ -26,15 +24,9 @@ public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location } protected void reinit(@NotNull MovecraftLocation location) { - reinit(location.getX(), location.getY(), location.getZ()); - } - - protected void reinit(final int x, final int y, final int z) { Craft craft = this.getCraft(); Craft.CraftOrigin origin = craft.getCraftOrigin(); - this.dx = x - origin.getX(); - this.dy = y - origin.getY(); - this.dz = z - origin.getZ(); + this.vector = location.subtract(origin); } /** @@ -42,11 +34,7 @@ protected void reinit(final int x, final int y, final int z) { * @param rotation A clockwise or counter-clockwise direction to rotate. */ public void rotate(MovecraftRotation rotation) { - MovecraftLocation newVector = MathUtils.rotateVec(rotation, new MovecraftLocation(this.dx, this.dy, this.dz)); - - this.dx = newVector.getX(); - this.dy = newVector.getY(); - this.dz = newVector.getZ(); + this.vector = MathUtils.rotateVec(rotation, new MovecraftLocation(this.dx, this.dy, this.dz)); } /** @@ -57,11 +45,7 @@ public MovecraftLocation getAbsoluteLocation() { Craft craft = this.getCraft(); Craft.CraftOrigin origin = craft.getCraftOrigin(); - int x = origin.getX() + this.dx; - int y = origin.getY() + this.dy; - int z = origin.getZ() + this.dz; - - return new MovecraftLocation(x, y, z); + return this.vector.add(origin); } /** From e0bfea0660323cc567eb12ecbfc865582794ddf5 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:04:26 +0100 Subject: [PATCH 28/32] Use MovecraftLocation in CraftOrigin Untested, but should work --- .../countercraft/movecraft/craft/Craft.java | 45 +++---------------- 1 file changed, 5 insertions(+), 40 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java index eddd8517e..186334920 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java @@ -63,60 +63,25 @@ class Hidden { public class CraftOrigin { - private int x; - private int y; - private int z; + private MovecraftLocation location; public CraftOrigin(final @NotNull Craft craft) { - + this.location = craft.getHitbox().getMidPoint(); } public void translate(int dx, int dy, int dz) { - this.x += dx; - this.y += dy; - this.z += dz; + this.location = this.location.translate(dx, dy, dz); } public void rotate(final MovecraftLocation rotationPoint, final MovecraftRotation rotation) { MovecraftLocation oldAbsolute = this.getLocation(); MovecraftLocation vector = oldAbsolute.subtract(rotationPoint); MovecraftLocation vectorRotated = MathUtils.rotateVec(rotation, vector); - MovecraftLocation newAbsolute = rotationPoint.add(vectorRotated); - this.copyValues(newAbsolute); - } - - public void copyValues(final MovecraftLocation location) { - this.x = location.getX(); - this.y = location.getY(); - this.z = location.getZ(); - } - - public int getX() { - return x; - } - - public void setX(int x) { - this.x = x; - } - - public int getY() { - return y; - } - - public void setY(int y) { - this.y = y; - } - - public int getZ() { - return z; - } - - public void setZ(int z) { - this.z = z; + this.location = rotationPoint.add(vectorRotated); } public MovecraftLocation getLocation() { - return new MovecraftLocation(x, y, z); + return this.location; } } From 4a018e5459d3f2a22b4e428ee74ba419609f4d59 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:07:29 +0100 Subject: [PATCH 29/32] Simplify lambda --- .../movecraft/listener/CraftPilotListener.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index e02048a77..466556828 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -47,15 +47,7 @@ public void onCraftPilot(@NotNull CraftPilotEvent event) { if (parent.getWorld() != subCraft.getWorld()) { return; } - transferTrackedLocations(parent, subCraft, (trackedLocation) -> { - MovecraftLocation absolute = trackedLocation.getAbsoluteLocation(); - if (subCraft.getHitBox().inBounds(absolute)) { - if (subCraft.getHitBox().contains(absolute)) { - return true; - } - } - return false; - }, true); + transferTrackedLocations(parent, subCraft, (trackedLocation) -> subCraft.getHitBox().inBounds(trackedLocation.getAbsoluteLocation()) && subCraft.getHitBox().contains(trackedLocation.getAbsoluteLocation()), true); } } From 97387eeece90eaef2a9d70ac7c873edf969ec906 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Fri, 31 Jan 2025 10:18:51 +0100 Subject: [PATCH 30/32] fixes and use movecraftlocation as origin now --- .../async/rotation/RotationTask.java | 4 ++- .../async/translation/TranslationTask.java | 2 +- .../movecraft/TrackedLocation.java | 9 ++---- .../countercraft/movecraft/craft/Craft.java | 31 +++---------------- 4 files changed, 11 insertions(+), 35 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 711989196..d7a0a318e 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -165,7 +165,9 @@ protected void execute() { } // Rotates the craft's tracked locations, and all parent craft's. - craft.getCraftOrigin().rotate(originPoint, rotation); + MovecraftLocation oldOrigin = craft.getCraftOrigin(); + MovecraftLocation vectorRotated = MathUtils.rotateVec(rotation, oldOrigin.subtract(originPoint)); + craft.setDataTag(Craft.CRAFT_ORIGIN, originPoint.add(vectorRotated)); Craft temp = craft; // recursion through all subcrafts is not necessary as the trackedlocations are transferred to the subcraft //do { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java index 690799e05..01218eb34 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java @@ -320,7 +320,7 @@ protected void execute() throws InterruptedException, ExecutionException { } // Update the reference location for trackedlocations - craft.getCraftOrigin().translate(dx, dy, dz); + craft.setDataTag(Craft.CRAFT_ORIGIN, craft.getCraftOrigin().translate(dx, dy, dz)); if (!collisionBox.isEmpty() && craft.getType().getBoolProperty(CraftType.CRUISE_ON_PILOT)) { CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.EMPTY, false); diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java index a10e397a3..d00157e93 100644 --- a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -27,8 +27,7 @@ public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location protected void reinit(@NotNull MovecraftLocation location) { Craft craft = this.getCraft(); - Craft.CraftOrigin origin = craft.getCraftOrigin(); - this.vector = location.subtract(origin); + this.vector = location.subtract(craft.getCraftOrigin()); } /** @@ -51,7 +50,7 @@ public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location * @param rotation A clockwise or counter-clockwise direction to rotate. */ public void rotate(MovecraftRotation rotation) { - this.vector = MathUtils.rotateVec(rotation, new MovecraftLocation(this.dx, this.dy, this.dz)); + this.vector = MathUtils.rotateVec(rotation, this.vector); } /** @@ -60,9 +59,7 @@ public void rotate(MovecraftRotation rotation) { */ public MovecraftLocation getAbsoluteLocation() { Craft craft = this.getCraft(); - Craft.CraftOrigin origin = craft.getCraftOrigin(); - - return this.vector.add(origin); + return this.vector.add(craft.getCraftOrigin()); } /** diff --git a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java index 186334920..2fdf71c54 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java @@ -53,7 +53,9 @@ public interface Craft { CraftDataTagKey> MOVEBLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "moveblocks"), craft -> new Counter<>()); CraftDataTagKey NON_NEGLIGIBLE_BLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "non-negligible-blocks"), Craft::getOrigBlockCount); CraftDataTagKey NON_NEGLIGIBLE_SOLID_BLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "non-negligible-solid-blocks"), Craft::getOrigBlockCount); - CraftDataTagKey CRAFT_ORIGIN = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "craft-origin"), CraftOrigin::new); + CraftDataTagKey CRAFT_ORIGIN = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "craft-origin"), craft -> { + return craft.getHitBox().getMidPoint(); + }); // Java disallows private or protected fields in interfaces, this is a workaround class Hidden { @@ -61,31 +63,6 @@ class Hidden { protected static final Map uuidToCraft = Collections.synchronizedMap(new WeakHashMap<>()); } - public class CraftOrigin { - - private MovecraftLocation location; - - public CraftOrigin(final @NotNull Craft craft) { - this.location = craft.getHitbox().getMidPoint(); - } - - public void translate(int dx, int dy, int dz) { - this.location = this.location.translate(dx, dy, dz); - } - - public void rotate(final MovecraftLocation rotationPoint, final MovecraftRotation rotation) { - MovecraftLocation oldAbsolute = this.getLocation(); - MovecraftLocation vector = oldAbsolute.subtract(rotationPoint); - MovecraftLocation vectorRotated = MathUtils.rotateVec(rotation, vector); - this.location = rotationPoint.add(vectorRotated); - } - - public MovecraftLocation getLocation() { - return this.location; - } - - } - public static Craft getCraftByUUID(final UUID uuid) { return Hidden.uuidToCraft.getOrDefault(uuid, null); } @@ -323,7 +300,7 @@ public default void removeUUIDMarkFromTile(TileState tile) { Map> getTrackedLocations(); - public default CraftOrigin getCraftOrigin() { + public default MovecraftLocation getCraftOrigin() { return this.getDataTag(CRAFT_ORIGIN); } } From bf29d8fadaa5d9dc68213a71a696014f7b148134 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Wed, 19 Mar 2025 13:30:15 +0100 Subject: [PATCH 31/32] Simplify lambda --- api/src/main/java/net/countercraft/movecraft/craft/Craft.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java index 2fdf71c54..d776af48a 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java @@ -53,9 +53,7 @@ public interface Craft { CraftDataTagKey> MOVEBLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "moveblocks"), craft -> new Counter<>()); CraftDataTagKey NON_NEGLIGIBLE_BLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "non-negligible-blocks"), Craft::getOrigBlockCount); CraftDataTagKey NON_NEGLIGIBLE_SOLID_BLOCKS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "non-negligible-solid-blocks"), Craft::getOrigBlockCount); - CraftDataTagKey CRAFT_ORIGIN = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "craft-origin"), craft -> { - return craft.getHitBox().getMidPoint(); - }); + CraftDataTagKey CRAFT_ORIGIN = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "craft-origin"), craft -> craft.getHitBox().getMidPoint()); // Java disallows private or protected fields in interfaces, this is a workaround class Hidden { From 8a6e6a50ef230c895a37e7bfa68c6f0209a5c8c5 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Wed, 19 Mar 2025 13:31:06 +0100 Subject: [PATCH 32/32] Remove unnecessary imports --- api/src/main/java/net/countercraft/movecraft/craft/Craft.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java index d776af48a..b90272780 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java @@ -40,9 +40,7 @@ import org.bukkit.block.data.BlockData; import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.NotNull; -import org.joml.Vector3i; -import javax.naming.Name; import java.util.*; public interface Craft {