Fixup plan expansion and RTP engine

This commit is contained in:
William278 committed 2023-03-20 15:10:21 +00:00
1 parent 8b809b6b5b
commit 547870e6c1
7 files changed
+142 -194

No files matched your search

@@ -3,9 +3,6 @@ package net.william278.huskhomes.position;
import net.william278.huskhomes.teleport.Target;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.UUID;
/**
* Represents a position - a {@link Location} somewhere on the proxy network or server
*/
@@ -34,53 +31,6 @@ public class Position extends Location implements Target {
this.setServer(newPosition.getServer());
}
/**
* Parses the position from a set of values
*
* @param args The values to parse
* @param relativeTo The {@link Position} to use as a reference for relative coordinates
* @return The position if it could be parsed, otherwise an empty optional
*/
public static Optional<Position> parse(@NotNull String[] args, @NotNull Position relativeTo) {
// Validate argument length
if (args.length < 3 || args.length > 5) {
return Optional.empty();
}
// Parse, handle relatives, and return, catching NumberFormatExceptions and returning an empty optional
try {
final double x = parseCoordinate(args[0], relativeTo.getX());
final double y = parseCoordinate(args[1], relativeTo.getY());
final double z = parseCoordinate(args[2], relativeTo.getZ());
final World world = args.length > 3 ? new World(args[3], UUID.randomUUID()) : relativeTo.getWorld();
final String server = args.length > 4 ? args[4] : relativeTo.getServer();
return Optional.of(new Position(x, y, z, relativeTo.getYaw(), relativeTo.getPitch(), world, server));
} catch (NumberFormatException ignored) {
return Optional.empty();
}
}
/**
* Parses a coordinate double value from a string
*
* @param value The string to parse
* @param currentValue The current value of the coordinate, for handling relatives
* @return The parsed coordinate
* @throws NumberFormatException If the string could not be parsed
*/
private static double parseCoordinate(@NotNull String value, double currentValue) throws NumberFormatException {
// Future: Consider supporting ^ ^ ^ (position-relative-to-facing)
if (value.startsWith("~")) {
if (value.length() == 1) {
return currentValue;
} else {
return currentValue + Double.parseDouble(value.substring(1));
}
} else {
return Double.parseDouble(value);
}
}
/**
* The name of the server the position is on
*/
@@ -92,4 +42,13 @@ public class Position extends Location implements Target {
public void setServer(@NotNull String server) {
this.server = server;
}
@Override
public String toString() {
return "x: " + (int) getX() + ", " +
"y: " + (int) getY() + ", " +
"z: " + (int) getZ() + " " +
"(" + getWorld().getName() + " / " + getServer() + ")";
}
}