aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/ymc/acron/c2s/ReqSetConfig.java
blob: dc2c87854f79adbc0b2460e204094530f49a1cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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<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 = 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<Vec3d> {
            @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<Vec2f> {
            @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);
            }
        }
    }
}