Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracked Locations on Crafts #616

Merged
merged 14 commits into from
Jul 28, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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.AsyncTask;
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.Craft;
Expand Down Expand Up @@ -51,6 +52,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 @@ -167,6 +169,12 @@ protected void execute() {
parentCraft.getFluidLocations().addAll(newFluidList);
}

//Rotates the craft's tracked locations.
for (Set<TrackedLocation> locations : craft.getTrackedLocations().values()) {
for (TrackedLocation location : locations) {
location.rotate(rotation, originPoint);
}
}

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 @@ -58,8 +59,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 All @@ -25,6 +26,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
Expand Down Expand Up @@ -78,6 +80,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<>();

public BaseCraft(@NotNull CraftType type, @NotNull World world) {
this.type = type;
Expand Down Expand Up @@ -598,4 +601,7 @@ public void setTotalFuel(double fuel) {
public double getTotalFuel() {
return totalFuel;
}

@Override
public Map<NamespacedKey, Set<TrackedLocation>> getTrackedLocations() {return trackedLocations;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 midPoint = craft.getHitBox().getMidPoint();
offSet = MathUtils.rotateVec(rotation, getAbsoluteLocation().subtract(midPoint));
}

/**
* 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;
}
}
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.type.CraftType;
import net.countercraft.movecraft.processing.MovecraftWorld;
import net.countercraft.movecraft.util.Counter;
Expand All @@ -28,12 +29,11 @@
import net.kyori.adventure.audience.Audience;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -263,4 +263,6 @@ default void setLastDZ(int dZ){}
double getTotalFuel ();

void setTotalFuel (double fuel);

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