New API methods, refactor position object to use static creators

This commit is contained in:
William278 committed 2023-03-22 19:55:59 +00:00
1 parent 42adc13425
commit 6b8c515b61
42 files changed
+587 -232

No files matched your search

@@ -13,19 +13,30 @@ public class Home extends SavedPosition {
private final User owner;
private boolean isPublic;
public Home(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server,
@NotNull PositionMeta positionMeta, @NotNull UUID uuid, @NotNull User owner, boolean isPublic) {
private Home(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server,
@NotNull PositionMeta positionMeta, @NotNull UUID uuid, @NotNull User owner, boolean isPublic) {
super(x, y, z, yaw, pitch, world, server, positionMeta, uuid);
this.owner = owner;
this.setPublic(isPublic);
}
public Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) {
private Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) {
super(position, meta);
this.owner = owner;
this.setPublic(false);
}
@NotNull
public static Home from(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server,
@NotNull PositionMeta positionMeta, @NotNull UUID uuid, @NotNull User owner, boolean isPublic) {
return new Home(x, y, z, yaw, pitch, world, server, positionMeta, uuid, owner, isPublic);
}
@NotNull
public static Home from(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) {
return new Home(position, meta, owner);
}
/**
* The {@link User} who owns this home
*/
@@ -1,30 +1,26 @@
package net.william278.huskhomes.position;
import com.google.gson.annotations.Expose;
import org.jetbrains.annotations.NotNull;
/**
* Represents a local position on this server
*/
public class Location {
@Expose
private double x;
@Expose
private double y;
@Expose
private double z;
@Expose
private float yaw;
@Expose
private float pitch;
@Expose
private World world;
public Location(double x, double y, double z, @NotNull World world) {
this.setX(x);
this.setY(y);
this.setZ(z);
this.setYaw(0);
this.setPitch(0);
this.setWorld(world);
}
public Location(double x, double y, double z, float yaw, float pitch,
@NotNull World world) {
protected Location(double x, double y, double z, float yaw, float pitch, @NotNull World world) {
this.setX(x);
this.setY(y);
this.setZ(z);
@@ -36,6 +32,16 @@ public class Location {
public Location() {
}
@NotNull
public static Location at(double x, double y, double z, float yaw, float pitch, @NotNull World world) {
return new Location(x, y, z, yaw, pitch, world);
}
@NotNull
public static Location at(double x, double y, double z, @NotNull World world) {
return Location.at(x, y, z, 0, 0, world);
}
/**
* Update the position to match that of another position
*
@@ -1,5 +1,6 @@
package net.william278.huskhomes.position;
import com.google.gson.annotations.Expose;
import net.william278.huskhomes.teleport.Target;
import org.jetbrains.annotations.NotNull;
@@ -8,15 +9,15 @@ import org.jetbrains.annotations.NotNull;
*/
public class Position extends Location implements Target {
@Expose
private String server;
public Position(double x, double y, double z, float yaw, float pitch,
@NotNull World world, @NotNull String server) {
protected Position(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server) {
super(x, y, z, yaw, pitch, world);
this.setServer(server);
}
public Position(@NotNull Location location, @NotNull String server) {
protected Position(@NotNull Location location, @NotNull String server) {
super(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), location.getWorld());
this.setServer(server);
}
@@ -25,6 +26,21 @@ public class Position extends Location implements Target {
private Position() {
}
@NotNull
public static Position at(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server) {
return new Position(x, y, z, yaw, pitch, world, server);
}
@NotNull
public static Position at(double x, double y, double z, @NotNull World world, @NotNull String server) {
return Position.at(x, y, z, 0, 0, world, server);
}
@NotNull
public static Position at(@NotNull Location location, @NotNull String server) {
return new Position(location, server);
}
@Override
public void update(@NotNull Position newPosition) {
super.update(newPosition);
@@ -8,7 +8,6 @@ import org.jetbrains.annotations.Nullable;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* Represents metadata about a {@link Position}, used in {@link SavedPosition} implementations
@@ -16,24 +15,27 @@ import java.util.Optional;
public class PositionMeta {
private String name;
@Nullable
private String description;
private Map<String, String> tags;
private Instant creationTime;
public PositionMeta(@NotNull String name, @NotNull String description, @NotNull Instant creationTime,
@Nullable String serializedTags) {
private PositionMeta(@NotNull String name, @NotNull String description,
@NotNull Instant creationTime, @Nullable String serializedTags) {
this.setName(name);
this.setDescription(description);
this.setCreationTime(creationTime);
this.setTags(deserializeTags(serializedTags));
}
public PositionMeta(@NotNull String name, @NotNull String description) {
this.setName(name);
this.setDescription(description);
this.setCreationTime(Instant.now());
this.setTags(new HashMap<>());
@NotNull
public static PositionMeta from(@NotNull String name, @NotNull String description,
@NotNull Instant creationTime, @Nullable String serializedTags) {
return new PositionMeta(name, description, creationTime, serializedTags);
}
@NotNull
public static PositionMeta create(@NotNull String name, @NotNull String description) {
return PositionMeta.from(name, description, Instant.now(), "");
}
/**
@@ -42,6 +44,7 @@ public class PositionMeta {
* @param serializedTags The JSON string to deserialize
* @return The deserialized {@link Map}
*/
@NotNull
private static Map<String, String> deserializeTags(@Nullable String serializedTags) {
try {
if (serializedTags == null || serializedTags.isBlank()) {
@@ -89,7 +92,7 @@ public class PositionMeta {
*/
@NotNull
public String getDescription() {
return Optional.ofNullable(description).orElse("");
return description;
}
public void setDescription(@Nullable String description) {
@@ -12,9 +12,8 @@ public abstract class SavedPosition extends Position implements Comparable<Saved
private PositionMeta meta;
private final UUID uuid;
protected SavedPosition(double x, double y, double z, float yaw, float pitch,
@NotNull World world, @NotNull String server,
@NotNull PositionMeta meta, @NotNull UUID uuid) {
protected SavedPosition(double x, double y, double z, float yaw, float pitch, @NotNull World world,
@NotNull String server, @NotNull PositionMeta meta, @NotNull UUID uuid) {
super(x, y, z, yaw, pitch, world, server);
this.setMeta(meta);
this.uuid = uuid;
@@ -1,63 +0,0 @@
package net.william278.huskhomes.position;
import net.william278.annotaml.YamlFile;
import net.william278.annotaml.YamlKey;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
/**
* Represents a server on a proxied network
*/
@YamlFile(header = """
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Server ID cache. Must match ┃
┃ server name in proxy config. ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛""")
public class Server {
/**
* Default server identifier
*/
@NotNull
public static String getDefaultServerName() {
try {
final Path serverDirectory = Path.of(System.getProperty("user.dir"));
return serverDirectory.getFileName().toString().trim();
} catch (Exception e) {
return "server";
}
}
@YamlKey("server_name")
private String name = getDefaultServerName();
public Server(@NotNull String name) {
this.setName(name);
}
@SuppressWarnings("unused")
private Server() {
}
@Override
public boolean equals(@NotNull Object other) {
// If the name of this server matches another, the servers are the same.
if (other instanceof Server server) {
return server.getName().equalsIgnoreCase(this.getName());
}
return super.equals(other);
}
/**
* Proxy-defined name of this server
*/
@NotNull
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -11,9 +11,8 @@ public class Warp extends SavedPosition {
private static final String PERMISSION_PREFIX = "huskhomes.command.warp.";
public Warp(double x, double y, double z, float yaw, float pitch,
@NotNull World world, @NotNull String server,
@NotNull PositionMeta positionMeta, @NotNull UUID uuid) {
private Warp(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server,
@NotNull PositionMeta positionMeta, @NotNull UUID uuid) {
super(x, y, z, yaw, pitch, world, server, positionMeta, uuid);
}
@@ -23,10 +22,19 @@ public class Warp extends SavedPosition {
* @param position The {@link Position} to save as a warp
* @param meta {@link PositionMeta} information about this position
*/
public Warp(@NotNull Position position, @NotNull PositionMeta meta) {
private Warp(@NotNull Position position, @NotNull PositionMeta meta) {
super(position, meta);
}
public static Warp from(double x, double y, double z, float yaw, float pitch, @NotNull World world,
@NotNull String server, @NotNull PositionMeta positionMeta, @NotNull UUID uuid) {
return new Warp(x, y, z, yaw, pitch, world, server, positionMeta, uuid);
}
public static Warp from(@NotNull Position position, @NotNull PositionMeta meta) {
return new Warp(position, meta);
}
@NotNull
public static String getPermission(@NotNull String warpName) {
return PERMISSION_PREFIX + warpName.toLowerCase();
@@ -16,12 +16,7 @@ public class World {
@Nullable
private Environment environment;
public World(@NotNull String name, @NotNull UUID uuid) {
this.setName(name);
this.setUuid(uuid);
}
public World(@NotNull String name, @NotNull UUID uuid, @Nullable Environment environment) {
private World(@NotNull String name, @NotNull UUID uuid, @Nullable Environment environment) {
this.setName(name);
this.setUuid(uuid);
this.setEnvironment(environment);
@@ -31,14 +26,25 @@ public class World {
public World() {
}
@NotNull
public static World from(@NotNull String name, @NotNull UUID uuid, @NotNull Environment environment) {
return new World(name, uuid, environment);
}
@NotNull
public static World from(@NotNull String name, @NotNull UUID uuid) {
return new World(name, uuid, null);
}
/**
* The name of this world, as defined by the world directory name
*/
@NotNull
public String getName() {
return name;
}
public void setName(String name) {
public void setName(@NotNull String name) {
this.name = name;
}
@@ -50,13 +56,15 @@ public class World {
return uuid;
}
public void setUuid(UUID uuid) {
public void setUuid(@NotNull UUID uuid) {
this.uuid = uuid;
}
/**
* Environment of the world ({@link Environment#OVERWORLD}, {@link Environment#NETHER}, {@link Environment#THE_END},
* or {@link Environment#CUSTOM})
* <p>
* Will return {@link Environment#OVERWORLD} if the environment is null
*/
@NotNull
public Environment getEnvironment() {