Skip to content

Commit

Permalink
Tracked Locations on Crafts (#616)
Browse files Browse the repository at this point in the history
* add trackedlocations

* reimplement TrackedLocation to specification

* use built-in methods for vector manipulation

* Fix merge

* Fix build

* Process tracked locations on all parent crafts

* Update RotationTask.java

---------

Co-authored-by: TylerS1066 <TylerS1066@users.noreply.github.com>
  • Loading branch information
goodroach and TylerS1066 authored Jul 28, 2024
1 parent 82d9749 commit 6085cc5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<TrackedLocation> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,7 @@ public abstract class BaseCraft implements Craft {
private String name = "";
@NotNull
private MovecraftLocation lastTranslation = new MovecraftLocation(0, 0, 0);
private Map<NamespacedKey, Set<TrackedLocation>> trackedLocations = new HashMap<>();

private final CraftDataTagContainer dataTagContainer = new CraftDataTagContainer();

Expand Down Expand Up @@ -555,4 +557,7 @@ public boolean equals(Object obj) {
public int hashCode() {
return this.getUUID().hashCode();
}

@Override
public Map<NamespacedKey, Set<TrackedLocation>> getTrackedLocations() {return trackedLocations;}
}
47 changes: 47 additions & 0 deletions api/src/main/java/net/countercraft/movecraft/TrackedLocation.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 4 additions & 6 deletions api/src/main/java/net/countercraft/movecraft/craft/Craft.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<List<Craft>> CONTACTS = CraftDataTagContainer.tryRegisterTagKey(new NamespacedKey("movecraft", "contacts"), craft -> new ArrayList<>(0));
Expand Down Expand Up @@ -308,4 +304,6 @@ public default void markTileStateWithUUID(TileState tile) {
public default void removeUUIDMarkFromTile(TileState tile) {
tile.getPersistentDataContainer().remove(MathUtils.KEY_CRAFT_UUID);
}

Map<NamespacedKey, Set<TrackedLocation>> getTrackedLocations();
}

0 comments on commit 6085cc5

Please sign in to comment.