From 85045e1e4a15e0a5657d189e83dd202a2c37f2b0 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Wed, 13 Jul 2022 11:16:27 -0700 Subject: First Commit Signed-off-by: Trumeet --- src/main/java/moe/ymc/acron/c2s/ReqCmd.java | 51 +++++++++++ src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java | 100 ++++++++++++++++++++++ src/main/java/moe/ymc/acron/c2s/Request.java | 6 ++ 3 files changed, 157 insertions(+) create mode 100644 src/main/java/moe/ymc/acron/c2s/ReqCmd.java create mode 100644 src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java create mode 100644 src/main/java/moe/ymc/acron/c2s/Request.java (limited to 'src/main/java/moe/ymc/acron/c2s') diff --git a/src/main/java/moe/ymc/acron/c2s/ReqCmd.java b/src/main/java/moe/ymc/acron/c2s/ReqCmd.java new file mode 100644 index 0000000..bcd8d48 --- /dev/null +++ b/src/main/java/moe/ymc/acron/c2s/ReqCmd.java @@ -0,0 +1,51 @@ +package moe.ymc.acron.c2s; + +import com.google.gson.*; +import com.google.gson.annotations.SerializedName; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Type; + +public record ReqCmd(@SerializedName("id") int id, + @SerializedName("cmd") @NotNull String cmd, + @SerializedName("config") @Nullable ReqSetConfig config) + implements Request { + @Override + public void validate() { + if (cmd == null) { + throw new IllegalArgumentException("Property 'cmd' cannot be null."); + } + } + + @Override + public int getId() { + return id; + } + + public static class ReqCmdDeserializer implements JsonDeserializer { + @Override + public ReqCmd deserialize(JsonElement json, + Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + final JsonObject object = json.getAsJsonObject(); + final int id = object.has("id") ? + object.get("id").getAsInt() : + 0; + final String cmd = object.has("cmd") ? + object.get("cmd").getAsString() : + null; + // We cannot use context#deserialize here + // because RuntimeTypeAdapterFactory keeps kicking in + // and asking for the 'type' property + // which is obviously redundant for an inner field. + // Thus, I pass it directly to the deserializer + // to bypass the RuntimeTypeAdapterFactory. + final ReqSetConfig reqSetConfig = object.has("config") ? + new ReqSetConfig.ReqSetConfigDeserializer() + .deserialize(object.get("config"), ReqSetConfig.class, context) : + null; + return new ReqCmd(id, cmd, reqSetConfig); + } + } +} diff --git a/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java b/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java new file mode 100644 index 0000000..dc2c878 --- /dev/null +++ b/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java @@ -0,0 +1,100 @@ +package moe.ymc.acron.c2s; + +import com.google.gson.*; +import com.google.gson.annotations.SerializedName; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Type; + +public record ReqSetConfig(@SerializedName("id") int id, + @SerializedName("world") @Nullable WorldKey world, + @SerializedName("pos") @Nullable Vec3d pos, + @SerializedName("rot") @Nullable Vec2f rot, + @SerializedName("name") @Nullable String name) + implements Request { + @Override + public void validate() { + } + + @Override + public int getId() { + return 0; + } + + public static class ReqSetConfigDeserializer implements JsonDeserializer { + @Override + public ReqSetConfig deserialize(JsonElement json, + Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + final JsonObject object = json.getAsJsonObject(); + final int id = object.has("id") ? + object.get("id").getAsInt() : + -1; + final WorldKey world = object.has("world") ? + WorldKey.valueOf(object.get("world").getAsString().toUpperCase()) : + null; + final Vec3d pos = object.has("pos") ? + context.deserialize(object.get("pos"), Vec3d.class) : + null; + final Vec2f rot = object.has("rot") ? + context.deserialize(object.get("rot"), Vec2f.class) : + null; + final String name = object.has("name") ? + object.get("name").getAsString() : + null; + return new ReqSetConfig(-1, + world, + pos, + rot, + name); + } + } + + public enum WorldKey { + OVERWORLD, + NETHER, + END + } + + public record Vec3d(@SerializedName("x") double x, + @SerializedName("y") double y, + @SerializedName("z") double z) { + public static class Vec3dDeserializer implements JsonDeserializer { + @Override + public Vec3d deserialize(JsonElement json, + Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + final JsonObject object = json.getAsJsonObject(); + final double x = object.has("x") ? + object.get("x").getAsDouble() : + 0.0; + final double y = object.has("y") ? + object.get("y").getAsDouble() : + 0.0; + final double z = object.has("z") ? + object.get("z").getAsDouble() : + 0.0; + return new Vec3d(x, y, z); + } + } + } + + public record Vec2f(@SerializedName("x") float x, + @SerializedName("y") float y) { + public static class Vec2fDeserializer implements JsonDeserializer { + @Override + public Vec2f deserialize(JsonElement json, + Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + final JsonObject object = json.getAsJsonObject(); + final float x = object.has("x") ? + object.get("x").getAsFloat() : + 0.0f; + final float y = object.has("y") ? + object.get("y").getAsFloat() : + 0.0f; + return new Vec2f(x, y); + } + } + } +} diff --git a/src/main/java/moe/ymc/acron/c2s/Request.java b/src/main/java/moe/ymc/acron/c2s/Request.java new file mode 100644 index 0000000..af81705 --- /dev/null +++ b/src/main/java/moe/ymc/acron/c2s/Request.java @@ -0,0 +1,6 @@ +package moe.ymc.acron.c2s; + +public interface Request { + int getId(); + void validate() throws IllegalArgumentException; +} -- cgit v1.2.3