| 1 | package net.minecraft.client.resources.model.cuboid;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.gson.Gson;
|
| 5 | import com.google.gson.GsonBuilder;
|
| 6 | import com.google.gson.JsonDeserializationContext;
|
| 7 | import com.google.gson.JsonDeserializer;
|
| 8 | import com.google.gson.JsonElement;
|
| 9 | import com.google.gson.JsonObject;
|
| 10 | import com.google.gson.JsonParseException;
|
| 11 | import java.io.Reader;
|
| 12 | import java.lang.reflect.Type;
|
| 13 | import java.util.ArrayList;
|
| 14 | import java.util.List;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.client.resources.model.UnbakedModel;
|
| 18 | import net.minecraft.client.resources.model.geometry.UnbakedGeometry;
|
| 19 | import net.minecraft.client.resources.model.sprite.TextureSlots;
|
| 20 | import net.minecraft.resources.Identifier;
|
| 21 | import net.minecraft.util.GsonHelper;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public record CuboidModel(
|
| 26 | @Nullable UnbakedGeometry geometry,
|
| 27 | UnbakedModel.@Nullable GuiLight guiLight,
|
| 28 | @Nullable Boolean ambientOcclusion,
|
| 29 | @Nullable ItemTransforms transforms,
|
| 30 | TextureSlots.Data textureSlots,
|
| 31 | @Nullable Identifier parent
|
| 32 | ) implements UnbakedModel {
|
| 33 | @VisibleForTesting
|
| 34 | static final Gson GSON = new GsonBuilder()
|
| 35 | .registerTypeAdapter(CuboidModel.class, new CuboidModel.Deserializer())
|
| 36 | .registerTypeAdapter(CuboidModelElement.class, new CuboidModelElement.Deserializer())
|
| 37 | .registerTypeAdapter(CuboidFace.class, new CuboidFace.Deserializer())
|
| 38 | .registerTypeAdapter(ItemTransform.class, new ItemTransform.Deserializer())
|
| 39 | .registerTypeAdapter(ItemTransforms.class, new ItemTransforms.Deserializer())
|
| 40 | .create();
|
| 41 |
|
| 42 | public static CuboidModel fromStream(final Reader reader) {
|
| 43 | return GsonHelper.fromJson(GSON, reader, CuboidModel.class);
|
| 44 | }
|
| 45 |
|
| 46 | @Environment(EnvType.CLIENT)
|
| 47 | public static class Deserializer implements JsonDeserializer<CuboidModel> {
|
| 48 | public CuboidModel deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
| 49 | JsonObject object = json.getAsJsonObject();
|
| 50 | UnbakedGeometry elements = this.getElements(context, object);
|
| 51 | String parentName = this.getParentName(object);
|
| 52 | TextureSlots.Data textureMap = this.getTextureMap(object);
|
| 53 | Boolean hasAmbientOcclusion = this.getAmbientOcclusion(object);
|
| 54 | ItemTransforms transforms = null;
|
| 55 | if (object.has("display")) {
|
| 56 | JsonObject display = GsonHelper.getAsJsonObject(object, "display");
|
| 57 | transforms = context.deserialize(display, ItemTransforms.class);
|
| 58 | }
|
| 59 |
|
| 60 | UnbakedModel.GuiLight guiLight = null;
|
| 61 | if (object.has("gui_light")) {
|
| 62 | guiLight = UnbakedModel.GuiLight.getByName(GsonHelper.getAsString(object, "gui_light"));
|
| 63 | }
|
| 64 |
|
| 65 | Identifier parentLocation = parentName.isEmpty() ? null : Identifier.parse(parentName);
|
| 66 | return new CuboidModel(elements, guiLight, hasAmbientOcclusion, transforms, textureMap, parentLocation);
|
| 67 | }
|
| 68 |
|
| 69 | private TextureSlots.Data getTextureMap(final JsonObject object) {
|
| 70 | if (object.has("textures")) {
|
| 71 | JsonObject texturesObject = GsonHelper.getAsJsonObject(object, "textures");
|
| 72 | return TextureSlots.parseTextureMap(texturesObject);
|
| 73 | } else {
|
| 74 | return TextureSlots.Data.EMPTY;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | private String getParentName(final JsonObject object) {
|
| 79 | return GsonHelper.getAsString(object, "parent", "");
|
| 80 | }
|
| 81 |
|
| 82 | protected @Nullable Boolean getAmbientOcclusion(final JsonObject object) {
|
| 83 | return object.has("ambientocclusion") ? GsonHelper.getAsBoolean(object, "ambientocclusion") : null;
|
| 84 | }
|
| 85 |
|
| 86 | protected @Nullable UnbakedGeometry getElements(final JsonDeserializationContext context, final JsonObject object) {
|
| 87 | if (!object.has("elements")) {
|
| 88 | return null;
|
| 89 | }
|
| 90 |
|
| 91 | List<CuboidModelElement> elements = new ArrayList<>();
|
| 92 |
|
| 93 | for (JsonElement element : GsonHelper.getAsJsonArray(object, "elements")) {
|
| 94 | elements.add(context.deserialize(element, CuboidModelElement.class));
|
| 95 | }
|
| 96 |
|
| 97 | return new UnbakedCuboidGeometry(elements);
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|