aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/java/moe/ymc/acron/common/Vec3d.java
blob: 593019f8acf1832ee22aec90bdbed0b594ad7c32 (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
package moe.ymc.acron.common;

import com.google.gson.*;
import com.google.gson.annotations.SerializedName;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;

public record Vec3d(@SerializedName("x") double x,
                    @SerializedName("y") double y,
                    @SerializedName("z") double z) {
    public Vec3d(@NotNull net.minecraft.util.math.Vec3d vec3d) {
        this(vec3d.x, vec3d.y, vec3d.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);
        }
    }
}