mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Simplify message serialization and payload scheme
This commit is contained in:
1 parent
2325fd9220
commit
25975a4de6
5 files changed
+156
-100
No files matched your search
@@ -1,20 +1,14 @@
|
||||
package net.william278.huskhomes.position;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import net.william278.huskhomes.messenger.MessageSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Represents a position - a {@link Location} somewhere on the proxy network or server
|
||||
*/
|
||||
public class Position extends Location implements MessageSerializable {
|
||||
public class Position extends Location {
|
||||
|
||||
/**
|
||||
* The {@link Server} the position is on
|
||||
@@ -27,81 +21,48 @@ public class Position extends Location implements MessageSerializable {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public Position() {
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
|
||||
/**
|
||||
* Parses the position from a set of values
|
||||
*
|
||||
* @param values The values to parse
|
||||
* @return The position if it could be parsed, otherwise an empty optional
|
||||
*/
|
||||
public static Optional<Position> parse(@NotNull Position currentPosition, @NotNull String... values) {
|
||||
// Validate argument length
|
||||
if (values.length < 3 || values.length > 5) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// Parse, handle relatives, and return, catching NumberFormatExceptions and returning an empty optional
|
||||
try {
|
||||
final double x = parseCoordinate(values[0], currentPosition.x);
|
||||
final double y = parseCoordinate(values[1], currentPosition.y);
|
||||
final double z = parseCoordinate(values[2], currentPosition.z);
|
||||
final World world = values.length > 3 ? new World(values[3], UUID.randomUUID()) : currentPosition.world;
|
||||
final Server server = values.length > 4 ? new Server(values[4]) : currentPosition.server;
|
||||
return Optional.of(new Position(x, y, z, currentPosition.yaw, currentPosition.pitch, world, server));
|
||||
} catch (NumberFormatException ignored) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static Position fromJson(String json) {
|
||||
return new GsonBuilder().setPrettyPrinting()
|
||||
.registerTypeAdapter(World.class, new TypeAdapter<World>() {
|
||||
@Override
|
||||
public void write(JsonWriter jsonWriter, World world) throws IOException {
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("name");
|
||||
jsonWriter.value(world.name);
|
||||
jsonWriter.name("uuid");
|
||||
jsonWriter.value(world.uuid.toString());
|
||||
jsonWriter.endObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public World read(JsonReader jsonReader) throws IOException {
|
||||
final World readWorld = new World();
|
||||
jsonReader.beginObject();
|
||||
String fieldName = null;
|
||||
while (jsonReader.hasNext()) {
|
||||
JsonToken token = jsonReader.peek();
|
||||
if (token.equals(JsonToken.NAME)) {
|
||||
fieldName = jsonReader.nextName();
|
||||
}
|
||||
if (fieldName != null) {
|
||||
if (fieldName.equals("name")) {
|
||||
jsonReader.peek();
|
||||
readWorld.name = jsonReader.nextString();
|
||||
}
|
||||
if (fieldName.equals("uuid")) {
|
||||
jsonReader.peek();
|
||||
readWorld.uuid = UUID.fromString(jsonReader.nextString());
|
||||
}
|
||||
}
|
||||
}
|
||||
jsonReader.endObject();
|
||||
return readWorld;
|
||||
}
|
||||
})
|
||||
.registerTypeAdapter(Server.class, new TypeAdapter<Server>() {
|
||||
@Override
|
||||
public void write(JsonWriter jsonWriter, Server world) throws IOException {
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("name");
|
||||
jsonWriter.value(world.name);
|
||||
jsonWriter.endObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server read(JsonReader jsonReader) throws IOException {
|
||||
final Server readServer = new Server();
|
||||
jsonReader.beginObject();
|
||||
String fieldName = null;
|
||||
while (jsonReader.hasNext()) {
|
||||
JsonToken token = jsonReader.peek();
|
||||
if (token.equals(JsonToken.NAME)) {
|
||||
fieldName = jsonReader.nextName();
|
||||
}
|
||||
if (fieldName != null) {
|
||||
if (fieldName.equals("name")) {
|
||||
jsonReader.peek();
|
||||
readServer.name = jsonReader.nextString();
|
||||
}
|
||||
}
|
||||
}
|
||||
jsonReader.endObject();
|
||||
return readServer;
|
||||
}
|
||||
})
|
||||
.create().fromJson(json, Position.class);
|
||||
/**
|
||||
* 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 {
|
||||
if (value.startsWith("~")) {
|
||||
return currentValue + Double.parseDouble(value.substring(1));
|
||||
} else {
|
||||
return Double.parseDouble(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user