diff options
author | Trumeet <yuuta@yuuta.moe> | 2022-07-26 19:06:11 -0700 |
---|---|---|
committer | Trumeet <yuuta@yuuta.moe> | 2022-07-26 19:06:11 -0700 |
commit | d8fe269327a1a51f2588a3573a4764613da16388 (patch) | |
tree | 97bd20983700bb13eba7f7d524d3c6d529303b46 /mod/src/main/java/moe/ymc/acron/c2s | |
parent | 31a5d2990ba666ce5ef4a1f79f7999a9bdfaac2a (diff) | |
download | acron-d8fe269327a1a51f2588a3573a4764613da16388.tar acron-d8fe269327a1a51f2588a3573a4764613da16388.tar.gz acron-d8fe269327a1a51f2588a3573a4764613da16388.tar.bz2 acron-d8fe269327a1a51f2588a3573a4764613da16388.zip |
Move the mod to mod/
Diffstat (limited to 'mod/src/main/java/moe/ymc/acron/c2s')
-rw-r--r-- | mod/src/main/java/moe/ymc/acron/c2s/ReqCmd.java | 51 | ||||
-rw-r--r-- | mod/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java | 63 | ||||
-rw-r--r-- | mod/src/main/java/moe/ymc/acron/c2s/Request.java | 6 |
3 files changed, 120 insertions, 0 deletions
diff --git a/mod/src/main/java/moe/ymc/acron/c2s/ReqCmd.java b/mod/src/main/java/moe/ymc/acron/c2s/ReqCmd.java new file mode 100644 index 0000000..6f34b07 --- /dev/null +++ b/mod/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<ReqCmd> { + @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() : + -1; + 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/mod/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java b/mod/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java new file mode 100644 index 0000000..fcddf35 --- /dev/null +++ b/mod/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java @@ -0,0 +1,63 @@ +package moe.ymc.acron.c2s; + +import com.google.gson.*; +import com.google.gson.annotations.SerializedName; +import moe.ymc.acron.common.Vec2f; +import moe.ymc.acron.common.Vec3d; +import moe.ymc.acron.common.WorldKey; +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 id; + } + + public static class ReqSetConfigDeserializer implements JsonDeserializer<ReqSetConfig> { + @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; + if (object.has("world")) { + world = context.deserialize(object.get("world"), WorldKey.class); + // https://stackoverflow.com/a/49574019 + if (world == null) { + throw new JsonParseException("Invalid world"); + } + } else { + world = 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(id, + world, + pos, + rot, + name); + } + } + +}
\ No newline at end of file diff --git a/mod/src/main/java/moe/ymc/acron/c2s/Request.java b/mod/src/main/java/moe/ymc/acron/c2s/Request.java new file mode 100644 index 0000000..af81705 --- /dev/null +++ b/mod/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; +} |