CuboidModelElement .java

net.minecraft.client.resources.model.cuboid.CuboidModelElement (record): members: faces, rotation, shadeDirectionOverride, lightEmission, DEFAULT_RESCALE.

net.minecraft.client.resources.model.cuboidrecord186 linesclient7.1 KB

API Surface (2)

record CuboidModelElement(Vector3fc from, Vector3fc to, Map<Direction, CuboidFace> faces, @Nullable CuboidRotation rotation, @Nullable Direction shadeDirectionOverride, int lightEmission)
CuboidModelElement deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)

Fields & Constants (3)

DEFAULT_RESCALE
MIN_EXTENT
MAX_EXTENT

String literals (16)

"shade_direction_override"
"light_emission"
"rotation"
"origin"
"angle"
"axis"
"rescale"
"faces"
"from"
"Unknown shade direction override: "
"Invalid rotation axis: "
"Expected between 1 and 6 unique faces, got 0"
"Unknown facing: "
"' specifier exceeds the allowed boundaries: "
"Expected 3 "
" values, found: "

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

CuboidModelElement.java186 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.resources.model.cuboid;
2
3
import com.google.common.collect.Maps;
4
import com.google.gson.JsonArray;
5
import com.google.gson.JsonDeserializationContext;
6
import com.google.gson.JsonDeserializer;
7
import com.google.gson.JsonElement;
8
import com.google.gson.JsonObject;
9
import com.google.gson.JsonParseException;
10
import java.lang.reflect.Type;
11
import java.util.Locale;
12
import java.util.Map;
13
import java.util.Map.Entry;
14
import net.fabricmc.api.EnvType;
15
import net.fabricmc.api.Environment;
16
import net.minecraft.core.Direction;
17
import net.minecraft.core.Direction.Axis;
18
import net.minecraft.util.GsonHelper;
19
import org.joml.Vector3f;
20
import org.joml.Vector3fc;
21
import org.jspecify.annotations.Nullable;
22
23
@Environment(EnvType.CLIENT)
24
public record CuboidModelElement(
25
	Vector3fc from,
26
	Vector3fc to,
27
	Map<Direction, CuboidFace> faces,
28
	@Nullable CuboidRotation rotation,
29
	@Nullable Direction shadeDirectionOverride,
30
	int lightEmission
31
) {
32
	private static final boolean DEFAULT_RESCALE = false;
33
	private static final float MIN_EXTENT = -16.0F;
34
	private static final float MAX_EXTENT = 32.0F;
35
36
	public CuboidModelElement(final Vector3fc from, final Vector3fc to, final Map<Direction, CuboidFace> faces) {
37
		this(from, to, faces, null, null, 0);
38
	}
39
40
	@Environment(EnvType.CLIENT)
41
	protected static class Deserializer implements JsonDeserializer<CuboidModelElement> {
42
		private static final int DEFAULT_LIGHT_EMISSION = 0;
43
		private static final String FIELD_SHADE_DIRECTION_OVERRIDE = "shade_direction_override";
44
		private static final String FIELD_LIGHT_EMISSION = "light_emission";
45
		private static final String FIELD_ROTATION = "rotation";
46
		private static final String FIELD_ORIGIN = "origin";
47
		private static final String FIELD_ANGLE = "angle";
48
		private static final String FIELD_X = "x";
49
		private static final String FIELD_Y = "y";
50
		private static final String FIELD_Z = "z";
51
		private static final String FIELD_AXIS = "axis";
52
		private static final String FIELD_RESCALE = "rescale";
53
		private static final String FIELD_FACES = "faces";
54
		private static final String FIELD_TO = "to";
55
		private static final String FIELD_FROM = "from";
56
57
		public CuboidModelElement deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
58
			JsonObject object = json.getAsJsonObject();
59
			Vector3f from = getPosition(object, "from");
60
			Vector3f to = getPosition(object, "to");
61
			CuboidRotation rotation = this.getRotation(object);
62
			Map<Direction, CuboidFace> faces = this.getFaces(context, object);
63
			Direction shadeDirectionOverride = this.getShadeDirectionOverride(object);
64
			int lightEmission = 0;
65
			if (object.has("light_emission")) {
66
				boolean isNumber = GsonHelper.isNumberValue(object, "light_emission");
67
				if (isNumber) {
68
					lightEmission = GsonHelper.getAsInt(object, "light_emission");
69
				}
70
71
				if (!isNumber || lightEmission < 0 || lightEmission > 15) {
72
					throw new JsonParseException("Expected 'light_emission' to be an Integer between (inclusive) 0 and 15");
73
				}
74
			}
75
76
			return new CuboidModelElement(from, to, faces, rotation, shadeDirectionOverride, lightEmission);
77
		}
78
79
		private @Nullable Direction getShadeDirectionOverride(final JsonObject object) {
80
			if (!object.has("shade_direction_override")) {
81
				return null;
82
			} else {
83
				String shadeDirectionName = GsonHelper.getAsString(object, "shade_direction_override");
84
				Direction shadeDirectionOverride = Direction.byName(shadeDirectionName);
85
				if (shadeDirectionOverride == null) {
86
					throw new JsonParseException("Unknown shade direction override: " + shadeDirectionName);
87
				} else {
88
					return shadeDirectionOverride;
89
				}
90
			}
91
		}
92
93
		private @Nullable CuboidRotation getRotation(final JsonObject object) {
94
			if (!object.has("rotation")) {
95
				return null;
96
			}
97
98
			JsonObject rotationObject = GsonHelper.getAsJsonObject(object, "rotation");
99
			Vector3f origin = getVector3f(rotationObject, "origin");
100
			origin.mul(0.0625F);
101
			CuboidRotation.RotationValue rotationValue;
102
			if (!rotationObject.has("axis") && !rotationObject.has("angle")) {
103
				if (!rotationObject.has("x") && !rotationObject.has("y") && !rotationObject.has("z")) {
104
					throw new JsonParseException("Missing rotation value, expected either 'axis' and 'angle' or 'x', 'y' and 'z'");
105
				}
106
107
				float x = GsonHelper.getAsFloat(rotationObject, "x", 0.0F);
108
				float y = GsonHelper.getAsFloat(rotationObject, "y", 0.0F);
109
				float z = GsonHelper.getAsFloat(rotationObject, "z", 0.0F);
110
				rotationValue = new CuboidRotation.EulerXYZRotation(x, y, z);
111
			} else {
112
				Axis axis = this.getAxis(rotationObject);
113
				float angle = GsonHelper.getAsFloat(rotationObject, "angle");
114
				rotationValue = new CuboidRotation.SingleAxisRotation(axis, angle);
115
			}
116
117
			boolean rescale = GsonHelper.getAsBoolean(rotationObject, "rescale", false);
118
			return new CuboidRotation(origin, rotationValue, rescale);
119
		}
120
121
		private Axis getAxis(final JsonObject object) {
122
			String axisName = GsonHelper.getAsString(object, "axis");
123
			Axis axis = Axis.byName(axisName.toLowerCase(Locale.ROOT));
124
			if (axis == null) {
125
				throw new JsonParseException("Invalid rotation axis: " + axisName);
126
			} else {
127
				return axis;
128
			}
129
		}
130
131
		private Map<Direction, CuboidFace> getFaces(final JsonDeserializationContext context, final JsonObject object) {
132
			Map<Direction, CuboidFace> faces = this.filterNullFromFaces(context, object);
133
			if (faces.isEmpty()) {
134
				throw new JsonParseException("Expected between 1 and 6 unique faces, got 0");
135
			} else {
136
				return faces;
137
			}
138
		}
139
140
		private Map<Direction, CuboidFace> filterNullFromFaces(final JsonDeserializationContext context, final JsonObject object) {
141
			Map<Direction, CuboidFace> result = Maps.newEnumMap(Direction.class);
142
			JsonObject faceObjects = GsonHelper.getAsJsonObject(object, "faces");
143
144
			for (Entry<String, JsonElement> entry : faceObjects.entrySet()) {
145
				Direction direction = this.getFacing(entry.getKey());
146
				result.put(direction, context.deserialize(entry.getValue(), CuboidFace.class));
147
			}
148
149
			return result;
150
		}
151
152
		private Direction getFacing(final String name) {
153
			Direction direction = Direction.byName(name);
154
			if (direction == null) {
155
				throw new JsonParseException("Unknown facing: " + name);
156
			} else {
157
				return direction;
158
			}
159
		}
160
161
		private static Vector3f getPosition(final JsonObject object, final String key) {
162
			Vector3f from = getVector3f(object, key);
163
			if (!(from.x() < -16.0F) && !(from.y() < -16.0F) && !(from.z() < -16.0F) && !(from.x() > 32.0F) && !(from.y() > 32.0F) && !(from.z() > 32.0F)) {
164
				return from;
165
			} else {
166
				throw new JsonParseException("'" + key + "' specifier exceeds the allowed boundaries: " + from);
167
			}
168
		}
169
170
		private static Vector3f getVector3f(final JsonObject object, final String key) {
171
			JsonArray vecArray = GsonHelper.getAsJsonArray(object, key);
172
			if (vecArray.size() != 3) {
173
				throw new JsonParseException("Expected 3 " + key + " values, found: " + vecArray.size());
174
			}
175
176
			float[] elements = new float[3];
177
178
			for (int i = 0; i < elements.length; i++) {
179
				elements[i] = GsonHelper.convertToFloat(vecArray.get(i), key + "[" + i + "]");
180
			}
181
182
			return new Vector3f(elements[0], elements[1], elements[2]);
183
		}
184
	}
185
}
186

Decompiled Minecraft 26.3 source of CuboidModelElement (net/minecraft/client/resources/model/cuboid/CuboidModelElement.java) with Mojang official mappings, stored verbatim on the site.