More refactor work, encapsulate classes

This commit is contained in:
William278 committed 2023-03-02 15:00:02 +00:00
1 parent 3e1ec09487
commit 1cebdac9b8
51 files changed
+756 -621

No files matched your search

@@ -10,16 +10,8 @@ import java.util.UUID;
*/
public class Home extends SavedPosition {
/**
* The {@link User} who owns this home
*/
@NotNull
public final User owner;
/**
* {@code true} if this home is public
*/
public boolean isPublic;
private final User owner;
private boolean isPublic;
public Home(double x, double y, double z, float yaw, float pitch,
@NotNull World world, @NotNull Server server,
@@ -27,13 +19,31 @@ public class Home extends SavedPosition {
@NotNull User owner, boolean isPublic) {
super(x, y, z, yaw, pitch, world, server, positionMeta, uuid);
this.owner = owner;
this.isPublic = isPublic;
this.setPublic(isPublic);
}
public Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) {
super(position, meta);
this.owner = owner;
this.isPublic = false;
this.setPublic(false);
}
/**
* The {@link User} who owns this home
*/
@NotNull
public User getOwner() {
return owner;
}
/**
* {@code true} if this home is public
*/
public boolean isPublic() {
return isPublic;
}
public void setPublic(boolean aPublic) {
isPublic = aPublic;
}
}
@@ -7,56 +7,98 @@ import org.jetbrains.annotations.NotNull;
*/
public class Location {
/**
* The double-precision defined x coordinate of the location
*/
public double x;
/**
* The double-precision defined y coordinate of the location
*/
public double y;
/**
* The double-precision defined z coordinate of the location
*/
public double z;
/**
* The float-precision defined yaw facing of the location
*/
public float yaw;
/**
* The float-precision defined pitch facing of the location
*/
public float pitch;
/**
* The {@link World} the location is on
*/
public World world;
private double x;
private double y;
private double z;
private float yaw;
private float pitch;
private World world;
public Location(double x, double y, double z, @NotNull World world) {
this.x = x;
this.y = y;
this.z = z;
this.yaw = 0;
this.pitch = 0;
this.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) {
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
this.world = world;
this.setX(x);
this.setY(y);
this.setZ(z);
this.setYaw(yaw);
this.setPitch(pitch);
this.setWorld(world);
}
public Location() {
}
/**
* The double-precision defined x coordinate of the location
*/
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
/**
* The double-precision defined y coordinate of the location
*/
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
/**
* The double-precision defined z coordinate of the location
*/
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
/**
* The float-precision defined yaw facing of the location
*/
public float getYaw() {
return yaw;
}
public void setYaw(float yaw) {
this.yaw = yaw;
}
/**
* The float-precision defined pitch facing of the location
*/
public float getPitch() {
return pitch;
}
public void setPitch(float pitch) {
this.pitch = pitch;
}
/**
* The {@link World} the location is on
*/
public World getWorld() {
return world;
}
public void setWorld(World world) {
this.world = world;
}
}
@@ -10,24 +10,21 @@ import java.util.UUID;
*/
public class Position extends Location {
/**
* The {@link Server} the position is on
*/
public Server server;
private Server server;
public Position(double x, double y, double z, float yaw, float pitch,
@NotNull World world, @NotNull Server server) {
super(x, y, z, yaw, pitch, world);
this.server = server;
this.setServer(server);
}
public Position(@NotNull Location location, @NotNull Server server) {
super(location.x, location.y, location.z, location.yaw, location.pitch, location.world);
this.server = server;
super(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), location.getWorld());
this.setServer(server);
}
@SuppressWarnings("unused")
public Position() {
private Position() {
}
/**
@@ -36,13 +33,13 @@ public class Position extends Location {
* @param newPosition The position to update to
*/
public void update(@NotNull Position newPosition) {
this.x = newPosition.x;
this.y = newPosition.y;
this.z = newPosition.z;
this.yaw = newPosition.yaw;
this.pitch = newPosition.pitch;
this.world = newPosition.world;
this.server = newPosition.server;
this.setX(newPosition.getX());
this.setY(newPosition.getY());
this.setZ(newPosition.getZ());
this.setYaw(newPosition.getYaw());
this.setPitch(newPosition.getPitch());
this.setWorld(newPosition.getWorld());
this.setServer(newPosition.getServer());
}
/**
@@ -60,12 +57,12 @@ public class Position extends Location {
// Parse, handle relatives, and return, catching NumberFormatExceptions and returning an empty optional
try {
final double x = parseCoordinate(args[0], relativeTo.x);
final double y = parseCoordinate(args[1], relativeTo.y);
final double z = parseCoordinate(args[2], relativeTo.z);
final World world = args.length > 3 ? new World(args[3], UUID.randomUUID()) : relativeTo.world;
final Server server = args.length > 4 ? new Server(args[4]) : relativeTo.server;
return Optional.of(new Position(x, y, z, relativeTo.yaw, relativeTo.pitch, world, server));
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 Server server = args.length > 4 ? new Server(args[4]) : relativeTo.getServer();
return Optional.of(new Position(x, y, z, relativeTo.getYaw(), relativeTo.getPitch(), world, server));
} catch (NumberFormatException ignored) {
return Optional.empty();
}
@@ -91,4 +88,15 @@ public class Position extends Location {
return Double.parseDouble(value);
}
}
/**
* The {@link Server} the position is on
*/
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
}
@@ -8,49 +8,32 @@ 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
*/
public class PositionMeta {
/**
* The name of a position
*/
@NotNull
public String name;
/**
* A description of a position
*/
@NotNull
public String description;
/**
* Map of metadata tags for a position
*/
@NotNull
public Map<String, String> tags;
/**
* The time the position was created
*/
@NotNull
public Instant creationTime;
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) {
this.name = name;
this.description = description;
this.creationTime = creationTime;
this.tags = deserializeTags(serializedTags);
this.setName(name);
this.setDescription(description);
this.setCreationTime(creationTime);
this.setTags(deserializeTags(serializedTags));
}
public PositionMeta(@NotNull String name, @NotNull String description) {
this.name = name;
this.description = description;
this.creationTime = Instant.now();
this.tags = new HashMap<>();
this.setName(name);
this.setDescription(description);
this.setCreationTime(Instant.now());
this.setTags(new HashMap<>());
}
/**
@@ -80,14 +63,59 @@ public class PositionMeta {
@Nullable
public String getSerializedTags() {
try {
if (tags.isEmpty()) {
if (getTags().isEmpty()) {
return null;
}
return new Gson().toJson(tags);
return new Gson().toJson(getTags());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* The name of a position
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* A description of a position
*/
@NotNull
public String getDescription() {
return Optional.ofNullable(description).orElse("");
}
public void setDescription(@Nullable String description) {
this.description = description;
}
/**
* Map of metadata tags for a position
*/
public Map<String, String> getTags() {
return tags;
}
public void setTags(Map<String, String> tags) {
this.tags = tags;
}
/**
* The time the position was created
*/
@NotNull
public Instant getCreationTime() {
return creationTime;
}
public void setCreationTime(Instant creationTime) {
this.creationTime = creationTime;
}
}
@@ -9,23 +9,14 @@ import java.util.UUID;
*/
public abstract class SavedPosition extends Position implements Comparable<SavedPosition> {
/**
* Metadata about this position (name, description)
*/
@NotNull
public PositionMeta meta;
/**
* A unique ID representing this position
*/
@NotNull
public final UUID uuid;
private PositionMeta meta;
private final UUID uuid;
protected SavedPosition(double x, double y, double z, float yaw, float pitch,
@NotNull World world, @NotNull Server server,
@NotNull PositionMeta meta, @NotNull UUID uuid) {
super(x, y, z, yaw, pitch, world, server);
this.meta = meta;
this.setMeta(meta);
this.uuid = uuid;
}
@@ -36,15 +27,43 @@ public abstract class SavedPosition extends Position implements Comparable<Saved
* @param meta {@link PositionMeta} information about this position
*/
protected SavedPosition(@NotNull Position position, @NotNull PositionMeta meta) {
super(position.x, position.y, position.z, position.yaw, position.pitch,
position.world, position.server);
this.meta = meta;
super(position.getX(), position.getY(), position.getZ(), position.getYaw(), position.getPitch(),
position.getWorld(), position.getServer());
this.setMeta(meta);
this.uuid = UUID.randomUUID();
}
// Compare based on names for alphabetical sorting
@Override
public int compareTo(@NotNull SavedPosition o) {
return this.meta.name.compareTo(o.meta.name);
return this.getMeta().getName().compareTo(o.getMeta().getName());
}
/**
* Metadata about this position (name, description)
*/
@NotNull
public PositionMeta getMeta() {
return meta;
}
public void setMeta(@NotNull PositionMeta meta) {
this.meta = meta;
}
/**
* The name of this position. Shortcut for {@link #getMeta()}.{@link PositionMeta#getName()}
*/
@NotNull
public String getName() {
return getMeta().getName();
}
/**
* A unique ID representing this position
*/
@NotNull
public UUID getUuid() {
return uuid;
}
}
@@ -17,27 +17,25 @@ import java.nio.file.Path;
public class Server {
/**
* Default (unknown) server identifier
* Default server identifier
*/
@NotNull
public static Server getDefault() {
final Server server = new Server();
try {
final Path serverDirectory = Path.of(System.getProperty("user.dir"));
server.name = serverDirectory.getFileName().toString().trim();
server.setName(serverDirectory.getFileName().toString().trim());
} catch (Exception e) {
server.name = "server";
server.setName("server");
}
return server;
}
/**
* Proxy-defined name of this server
*/
@YamlKey("server_name")
public String name = getDefault().name;
private String name = getDefault().getName();
public Server(@NotNull String name) {
this.name = name;
this.setName(name);
}
private Server() {
@@ -47,9 +45,20 @@ public class Server {
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.name.equalsIgnoreCase(this.name);
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;
}
}
@@ -48,7 +48,7 @@ public class Warp extends SavedPosition {
* @return true if the user has permission to teleport to this warp
*/
public boolean hasPermission(boolean restrictWarps, @NotNull OnlineUser user) {
return hasPermission(restrictWarps, user, meta.name);
return hasPermission(restrictWarps, user, getMeta().getName());
}
/**
@@ -3,6 +3,7 @@ package net.william278.huskhomes.position;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.UUID;
/**
@@ -10,38 +11,62 @@ import java.util.UUID;
*/
public class World {
private String name;
private UUID uuid;
@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) {
this.setName(name);
this.setUuid(uuid);
this.setEnvironment(environment);
}
@SuppressWarnings("unused")
public World() {
}
/**
* The name of this world, as defined by the world directory name
*/
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* UUID of this world, as defined by the {@code uid.dat} file in the world directory
*/
public UUID uuid;
@NotNull
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
/**
* Environment of the world ({@link Environment#OVERWORLD}, {@link Environment#NETHER}, {@link Environment#THE_END},
* or {@link Environment#CUSTOM})
*/
@Nullable
public Environment environment;
public World(@NotNull String name, @NotNull UUID uuid) {
this.name = name;
this.uuid = uuid;
@NotNull
public Environment getEnvironment() {
return Optional.ofNullable(environment).orElse(Environment.OVERWORLD);
}
public World(@NotNull String name, @NotNull UUID uuid, @Nullable Environment environment) {
this.name = name;
this.uuid = uuid;
public void setEnvironment(@Nullable Environment environment) {
this.environment = environment;
}
@SuppressWarnings("unused")
public World() {
}
/**
* Identifies the environment of the world
*/
@@ -55,7 +80,7 @@ public class World {
@Override
public boolean equals(@NotNull Object obj) {
if (obj instanceof World world) {
return world.uuid.equals(uuid);
return world.getUuid().equals(getUuid());
}
return super.equals(obj);
}