From 1a0d75bbb14227ce4bc8da6babe33c8768f623f8 Mon Sep 17 00:00:00 2001 From: ah-OOG-ah <75745146+ah-OOG-ah@users.noreply.github.com> Date: Wed, 20 Aug 2025 03:27:58 -0400 Subject: [PATCH] Split model deserializer into a different file --- .../gtnhlib/client/model/ModelLoader.java | 3 +- .../gtnhlib/client/model/json/JsonModel.java | 213 +----------------- .../client/model/json/ModelDeserializer.java | 198 ++++++++++++++++ 3 files changed, 211 insertions(+), 203 deletions(-) create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/ModelDeserializer.java diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelLoader.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelLoader.java index 5bebf12e..b9d24617 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelLoader.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelLoader.java @@ -1,5 +1,6 @@ package com.gtnewhorizon.gtnhlib.client.model; +import com.gtnewhorizon.gtnhlib.client.model.json.ModelDeserializer; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -37,7 +38,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; public class ModelLoader { private static final Gson GSON = new GsonBuilder() - .registerTypeAdapter(JsonModel.class, new JsonModel.Deserializer()).create(); + .registerTypeAdapter(JsonModel.class, new ModelDeserializer()).create(); private static final List unloadedModels = new ObjectArrayList<>(); private static final Map loadedModels = new Object2ObjectOpenHashMap<>(); private static final Map modelsToBake = new Object2ObjectOpenHashMap<>(); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/JsonModel.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/JsonModel.java index 11b363a2..5d32c08b 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/JsonModel.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/JsonModel.java @@ -4,45 +4,31 @@ import static com.gtnewhorizon.gtnhlib.client.renderer.util.DirectionUtil.ALL_DI import static java.lang.Math.max; import static java.lang.Math.min; -import java.lang.reflect.Type; +import com.gtnewhorizon.gtnhlib.client.model.NdQuadBuilder; +import com.gtnewhorizon.gtnhlib.client.model.Variant; +import com.gtnewhorizon.gtnhlib.client.renderer.quad.BakedModel; +import com.gtnewhorizon.gtnhlib.client.renderer.quad.Quad; +import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadBuilder; +import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadView; +import it.unimi.dsi.fastutil.Function; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectImmutableList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Random; import java.util.function.Supplier; - +import lombok.Getter; import net.minecraft.block.Block; import net.minecraft.util.ResourceLocation; import net.minecraft.world.IBlockAccess; import net.minecraftforge.common.util.ForgeDirection; - import org.jetbrains.annotations.Nullable; import org.joml.Matrix4f; import org.joml.Vector3f; import org.joml.Vector4f; -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.gtnewhorizon.gtnhlib.client.model.NdQuadBuilder; -import com.gtnewhorizon.gtnhlib.client.model.Variant; -import com.gtnewhorizon.gtnhlib.client.renderer.quad.Axis; -import com.gtnewhorizon.gtnhlib.client.renderer.quad.Quad; -import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadBuilder; -import com.gtnewhorizon.gtnhlib.client.renderer.quad.BakedModel; -import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadView; -import com.gtnewhorizon.gtnhlib.client.renderer.util.DirectionUtil; -import com.gtnewhorizon.gtnhlib.util.JsonUtil; - -import it.unimi.dsi.fastutil.Function; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import it.unimi.dsi.fastutil.objects.ObjectImmutableList; -import lombok.Getter; - public class JsonModel implements BakedModel { @Nullable @@ -223,181 +209,4 @@ public class JsonModel implements BakedModel { } } - public static class Deserializer implements JsonDeserializer { - - private Vector3f loadVec3(JsonObject in, String name) { - - final JsonArray arr = in.getAsJsonArray(name); - final Vector3f ret = new Vector3f(); - - for (int i = 0; i < 3; ++i) { - ret.setComponent(i, arr.get(i).getAsFloat()); - } - - return ret; - } - - private Vector3f loadVec3(JsonObject in, String name, Vector3f defaultv) { - if (!in.isJsonArray()) return defaultv; - return loadVec3(in, name); - } - - private Vector4f loadVec4(JsonObject in, String name) { - - final JsonArray arr = in.getAsJsonArray(name); - final Vector4f ret = new Vector4f(); - - for (int i = 0; i < 4; ++i) { - ret.setComponent(i, arr.get(i).getAsFloat()); - } - - return ret; - } - - private ModelDisplay loadADisplay(JsonObject in) { - - final Vector3f rotation = loadVec3(in, "rotation", new Vector3f(0, 0, 0)); - final Vector3f translation = loadVec3(in, "translation", new Vector3f(0, 0, 0)); - final Vector3f scale = loadVec3(in, "scale", new Vector3f(1, 1, 1)); - - return new ModelDisplay(rotation, translation, scale); - } - - private Map loadDisplay(JsonObject in) { - - // wow such long - final Map ret = new Object2ObjectOpenHashMap<>( - ModelDisplay.Position.values().length); - - if (in.has("display")) { - - final JsonObject display = in.getAsJsonObject("display"); - - for (Map.Entry j : display.entrySet()) { - - final String name = j.getKey(); - final ModelDisplay.Position pos = ModelDisplay.Position.getByName(name); - ret.put(pos, loadADisplay(j.getValue().getAsJsonObject())); - } - } - - for (ModelDisplay.Position p : ModelDisplay.Position.values()) { - ret.putIfAbsent(p, ModelDisplay.DEFAULT); - } - - return ret; - } - - private Map loadTextures(JsonObject in) { - - final Map textures = new Object2ObjectOpenHashMap<>(); - - if (in.has("textures")) { - for (Map.Entry e : in.getAsJsonObject("textures").entrySet()) { - - // Trim leading octothorpes. They indicate a texture variable, but I don't actually care. - String s = e.getValue().getAsString(); - if (s.startsWith("#")) { - s = s.substring(1); - } else if (s.startsWith("minecraft:")) { - - // Because of how we fetch textures from the atlas, minecraft textures need to have their domain - // stripped - s = s.substring(10); - } - - textures.put(e.getKey(), s); - } - } - - return textures; - } - - private ModelElement.Rotation loadRotation(JsonObject in) { - - if (in.has("rotation")) { - final JsonObject json = in.getAsJsonObject("rotation"); - - final Vector3f origin = loadVec3(json, "origin").div(16); - final Axis axis = Axis.fromName(JsonUtil.loadStr(json, "axis")); - final float angle = JsonUtil.loadFloat(json, "angle"); - final boolean rescale = JsonUtil.loadBool(json, "rescale", false); - - return new ModelElement.Rotation(origin, axis, angle, rescale); - } else { - - return null; - } - } - - private List loadFaces(JsonObject in) { - - final List ret = new ObjectArrayList<>(); - final JsonObject json = in.getAsJsonObject("faces"); - - for (Map.Entry e : json.entrySet()) { - - final ForgeDirection side = DirectionUtil.fromName(e.getKey()); - final JsonObject face = e.getValue().getAsJsonObject(); - - final Vector4f uv = (face.has("uv")) ? loadVec4(face, "uv") : null; - String texture = JsonUtil.loadStr(face, "texture"); - if (texture.startsWith("#")) texture = texture.substring(1); - final ForgeDirection cullFace = DirectionUtil.fromName(JsonUtil.loadStr(face, "cullface", "unknown")); - final int rotation = JsonUtil.loadInt(face, "rotation", 0); - final int tintIndex = JsonUtil.loadInt(face, "tintindex", -1); - - ret.add(new ModelElement.Face(side, uv, texture, cullFace, rotation, tintIndex)); - } - - return ret; - } - - private List loadElements(JsonObject in) { - - final List ret = new ObjectArrayList<>(); - - if (in.has("elements")) { - - final JsonArray arr = in.getAsJsonArray("elements"); - for (JsonElement e : arr) { - - final JsonObject json = e.getAsJsonObject(); - final Vector3f from = loadVec3(json, "from").div(16); - final Vector3f to = loadVec3(json, "to").div(16); - final ModelElement.Rotation rotation = loadRotation(json); - final boolean shade = JsonUtil.loadBool(json, "shade", true); - final List faces = loadFaces(json); - - ret.add(new ModelElement(from, to, rotation, shade, faces)); - } - } - - return ret; - } - - @Override - public JsonModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - - final JsonObject in = json.getAsJsonObject(); - - final String parent = JsonUtil.loadStr(in, "parent", ""); - ResourceLocation parentId = null; - if (!parent.isEmpty()) { - if (parent.contains(":")) { - parentId = new ModelLocation(parent.split(":")[0], parent.split(":")[1]); - } else { - parentId = new ModelLocation(parent); - } - } - - final boolean useAO = JsonUtil.loadBool(in, "ambientocclusion", true); - final Map display = loadDisplay(in); - final Map textures = loadTextures(in); - final List elements = loadElements(in); - - return new JsonModel(parentId, useAO, display, textures, elements); - } - } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/ModelDeserializer.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/ModelDeserializer.java new file mode 100644 index 00000000..548257d1 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/json/ModelDeserializer.java @@ -0,0 +1,198 @@ +package com.gtnewhorizon.gtnhlib.client.model.json; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.gtnewhorizon.gtnhlib.client.renderer.quad.Axis; +import com.gtnewhorizon.gtnhlib.client.renderer.util.DirectionUtil; +import com.gtnewhorizon.gtnhlib.util.JsonUtil; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.util.ForgeDirection; +import org.joml.Vector3f; +import org.joml.Vector4f; + +public class ModelDeserializer implements JsonDeserializer { + + private Vector3f loadVec3(JsonObject in, String name) { + + final JsonArray arr = in.getAsJsonArray(name); + final Vector3f ret = new Vector3f(); + + for (int i = 0; i < 3; ++i) { + ret.setComponent(i, arr.get(i).getAsFloat()); + } + + return ret; + } + + private Vector3f loadVec3(JsonObject in, String name, Vector3f defaultv) { + if (!in.isJsonArray()) return defaultv; + return loadVec3(in, name); + } + + private Vector4f loadVec4(JsonObject in, String name) { + + final JsonArray arr = in.getAsJsonArray(name); + final Vector4f ret = new Vector4f(); + + for (int i = 0; i < 4; ++i) { + ret.setComponent(i, arr.get(i).getAsFloat()); + } + + return ret; + } + + private ModelDisplay loadADisplay(JsonObject in) { + + final Vector3f rotation = loadVec3(in, "rotation", new Vector3f(0, 0, 0)); + final Vector3f translation = loadVec3(in, "translation", new Vector3f(0, 0, 0)); + final Vector3f scale = loadVec3(in, "scale", new Vector3f(1, 1, 1)); + + return new ModelDisplay(rotation, translation, scale); + } + + private Map loadDisplay(JsonObject in) { + + // wow such long + final Map ret = new Object2ObjectOpenHashMap<>( + ModelDisplay.Position.values().length); + + if (in.has("display")) { + + final JsonObject display = in.getAsJsonObject("display"); + + for (Map.Entry j : display.entrySet()) { + + final String name = j.getKey(); + final ModelDisplay.Position pos = ModelDisplay.Position.getByName(name); + ret.put(pos, loadADisplay(j.getValue().getAsJsonObject())); + } + } + + for (ModelDisplay.Position p : ModelDisplay.Position.values()) { + ret.putIfAbsent(p, ModelDisplay.DEFAULT); + } + + return ret; + } + + private Map loadTextures(JsonObject in) { + + final Map textures = new Object2ObjectOpenHashMap<>(); + + if (in.has("textures")) { + for (Map.Entry e : in.getAsJsonObject("textures").entrySet()) { + + // Trim leading octothorpes. They indicate a texture variable, but I don't actually care. + String s = e.getValue().getAsString(); + if (s.startsWith("#")) { + s = s.substring(1); + } else if (s.startsWith("minecraft:")) { + + // Because of how we fetch textures from the atlas, minecraft textures need to have their domain + // stripped + s = s.substring(10); + } + + textures.put(e.getKey(), s); + } + } + + return textures; + } + + private ModelElement.Rotation loadRotation(JsonObject in) { + + if (in.has("rotation")) { + final JsonObject json = in.getAsJsonObject("rotation"); + + final Vector3f origin = loadVec3(json, "origin").div(16); + final Axis axis = Axis.fromName(JsonUtil.loadStr(json, "axis")); + final float angle = JsonUtil.loadFloat(json, "angle"); + final boolean rescale = JsonUtil.loadBool(json, "rescale", false); + + return new ModelElement.Rotation(origin, axis, angle, rescale); + } else { + + return null; + } + } + + private List loadFaces(JsonObject in) { + + final List ret = new ObjectArrayList<>(); + final JsonObject json = in.getAsJsonObject("faces"); + + for (Map.Entry e : json.entrySet()) { + + final ForgeDirection side = DirectionUtil.fromName(e.getKey()); + final JsonObject face = e.getValue().getAsJsonObject(); + + final Vector4f uv = (face.has("uv")) ? loadVec4(face, "uv") : null; + String texture = JsonUtil.loadStr(face, "texture"); + if (texture.startsWith("#")) texture = texture.substring(1); + final ForgeDirection cullFace = DirectionUtil.fromName(JsonUtil.loadStr(face, "cullface", "unknown")); + final int rotation = JsonUtil.loadInt(face, "rotation", 0); + final int tintIndex = JsonUtil.loadInt(face, "tintindex", -1); + + ret.add(new ModelElement.Face(side, uv, texture, cullFace, rotation, tintIndex)); + } + + return ret; + } + + private List loadElements(JsonObject in) { + + final List ret = new ObjectArrayList<>(); + + if (in.has("elements")) { + + final JsonArray arr = in.getAsJsonArray("elements"); + for (JsonElement e : arr) { + + final JsonObject json = e.getAsJsonObject(); + final Vector3f from = loadVec3(json, "from").div(16); + final Vector3f to = loadVec3(json, "to").div(16); + final ModelElement.Rotation rotation = loadRotation(json); + final boolean shade = JsonUtil.loadBool(json, "shade", true); + final List faces = loadFaces(json); + + ret.add(new ModelElement(from, to, rotation, shade, faces)); + } + } + + return ret; + } + + @Override + public JsonModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + + final JsonObject in = json.getAsJsonObject(); + + final String parent = JsonUtil.loadStr(in, "parent", ""); + ResourceLocation parentId = null; + if (!parent.isEmpty()) { + if (parent.contains(":")) { + parentId = new ModelLocation(parent.split(":")[0], parent.split(":")[1]); + } else { + parentId = new ModelLocation(parent); + } + } + + final boolean useAO = JsonUtil.loadBool(in, "ambientocclusion", true); + final Map display = loadDisplay(in); + final Map textures = loadTextures(in); + final List elements = loadElements(in); + + return new JsonModel(parentId, useAO, display, textures, elements); + } +}