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 22a7032bd..9e9453f11 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 @@ -21,11 +21,13 @@ import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.MovecraftRotation; +import net.countercraft.movecraft.TrackedLocation; import net.countercraft.movecraft.async.AsyncTask; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.SinkingCraft; +import net.countercraft.movecraft.craft.SubCraft; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.events.CraftRotateEvent; import net.countercraft.movecraft.events.CraftTeleportEntityEvent; @@ -51,6 +53,7 @@ import org.bukkit.inventory.InventoryView; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; import static net.countercraft.movecraft.util.MathUtils.withinWorldBorder; @@ -176,6 +179,15 @@ protected void execute() { parentCraft.getFluidLocations().addAll(newFluidList); } + // Rotates the craft's tracked locations, and all parent craft's. + Craft temp = craft; + do { + for (Set locations : craft.getTrackedLocations().values()) { + for (TrackedLocation location : locations) { + location.rotate(rotation, originPoint); + } + } + } while (temp instanceof SubCraft && (temp = ((SubCraft) temp).getParent()) != null); updates.add(new CraftRotateCommand(getCraft(),originPoint, rotation)); //rotate entities in the craft 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 e2713343f..068f5d474 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; @@ -57,8 +58,8 @@ import java.util.Collection; import java.util.EnumSet; import java.util.HashSet; +import java.util.Iterator; import java.util.List; -import java.util.Random; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.ThreadLocalRandom; 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 d83475f77..23065f2eb 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java @@ -4,6 +4,7 @@ import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.MovecraftRotation; +import net.countercraft.movecraft.TrackedLocation; import net.countercraft.movecraft.async.rotation.RotationTask; import net.countercraft.movecraft.async.translation.TranslationTask; import net.countercraft.movecraft.config.Settings; @@ -66,6 +67,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 final CraftDataTagContainer dataTagContainer = new CraftDataTagContainer(); @@ -554,4 +556,7 @@ public boolean equals(Object obj) { public int hashCode() { return this.getUUID().hashCode(); } + + @Override + public Map> getTrackedLocations() {return trackedLocations;} } diff --git a/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java new file mode 100644 index 000000000..d61d74cab --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/TrackedLocation.java @@ -0,0 +1,47 @@ +package net.countercraft.movecraft; + +import net.countercraft.movecraft.craft.Craft; +import net.countercraft.movecraft.util.MathUtils; +import org.jetbrains.annotations.NotNull; + +public class TrackedLocation { + private MovecraftLocation offSet; + private final Craft craft; + + /** + * 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 Craft craft, @NotNull MovecraftLocation location) { + this.craft = craft; + MovecraftLocation midPoint = craft.getHitBox().getMidPoint(); + offSet = location.subtract(midPoint); + } + + /** + * Rotates the stored 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)); + } + + /** + * 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); + } + + /** + * Gets the stored location as a position vector relative to the midpoint. + * @return Returns the absolute location instead of a vector. + */ + public MovecraftLocation getOffSet() { + return offSet; + } +} 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 0a48cb138..1e9e61555 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java @@ -20,6 +20,7 @@ import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.MovecraftRotation; +import net.countercraft.movecraft.TrackedLocation; import net.countercraft.movecraft.craft.datatag.CraftDataTagContainer; import net.countercraft.movecraft.craft.datatag.CraftDataTagKey; import net.countercraft.movecraft.craft.type.CraftType; @@ -39,12 +40,7 @@ import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.Collections; -import java.util.WeakHashMap; +import java.util.*; public interface Craft { CraftDataTagKey> CONTACTS = CraftDataTagContainer.tryRegisterTagKey(new NamespacedKey("movecraft", "contacts"), craft -> new ArrayList<>(0)); @@ -308,4 +304,6 @@ public default void markTileStateWithUUID(TileState tile) { public default void removeUUIDMarkFromTile(TileState tile) { tile.getPersistentDataContainer().remove(MathUtils.KEY_CRAFT_UUID); } + + Map> getTrackedLocations(); }