165 lines
7.3 KiB
Java
165 lines
7.3 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.google.common.collect.Maps
|
|
* com.google.gson.JsonArray
|
|
* com.google.gson.JsonDeserializationContext
|
|
* com.google.gson.JsonDeserializer
|
|
* com.google.gson.JsonElement
|
|
* com.google.gson.JsonObject
|
|
* com.google.gson.JsonParseException
|
|
* org.joml.Vector3f
|
|
* org.joml.Vector3fc
|
|
* org.jspecify.annotations.Nullable
|
|
*/
|
|
package net.minecraft.client.renderer.block.model;
|
|
|
|
import com.google.common.collect.Maps;
|
|
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 java.lang.reflect.Type;
|
|
import java.util.EnumMap;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import net.minecraft.client.renderer.block.model.BlockElementFace;
|
|
import net.minecraft.client.renderer.block.model.BlockElementRotation;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.util.GsonHelper;
|
|
import net.minecraft.util.Mth;
|
|
import org.joml.Vector3f;
|
|
import org.joml.Vector3fc;
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public record BlockElement(Vector3fc from, Vector3fc to, Map<Direction, BlockElementFace> faces, @Nullable BlockElementRotation rotation, boolean shade, int lightEmission) {
|
|
private static final boolean DEFAULT_RESCALE = false;
|
|
private static final float MIN_EXTENT = -16.0f;
|
|
private static final float MAX_EXTENT = 32.0f;
|
|
|
|
public BlockElement(Vector3fc from, Vector3fc to, Map<Direction, BlockElementFace> faces) {
|
|
this(from, to, faces, null, true, 0);
|
|
}
|
|
|
|
protected static class Deserializer
|
|
implements JsonDeserializer<BlockElement> {
|
|
private static final boolean DEFAULT_SHADE = true;
|
|
private static final int DEFAULT_LIGHT_EMISSION = 0;
|
|
|
|
protected Deserializer() {
|
|
}
|
|
|
|
public BlockElement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
JsonObject object = json.getAsJsonObject();
|
|
Vector3f from = this.getFrom(object);
|
|
Vector3f to = this.getTo(object);
|
|
BlockElementRotation rotation = this.getRotation(object);
|
|
Map<Direction, BlockElementFace> faces = this.getFaces(context, object);
|
|
if (object.has("shade") && !GsonHelper.isBooleanValue(object, "shade")) {
|
|
throw new JsonParseException("Expected shade to be a Boolean");
|
|
}
|
|
boolean shade = GsonHelper.getAsBoolean(object, "shade", true);
|
|
int lightEmission = 0;
|
|
if (object.has("light_emission")) {
|
|
boolean isNumber = GsonHelper.isNumberValue(object, "light_emission");
|
|
if (isNumber) {
|
|
lightEmission = GsonHelper.getAsInt(object, "light_emission");
|
|
}
|
|
if (!isNumber || lightEmission < 0 || lightEmission > 15) {
|
|
throw new JsonParseException("Expected light_emission to be an Integer between (inclusive) 0 and 15");
|
|
}
|
|
}
|
|
return new BlockElement((Vector3fc)from, (Vector3fc)to, faces, rotation, shade, lightEmission);
|
|
}
|
|
|
|
private @Nullable BlockElementRotation getRotation(JsonObject object) {
|
|
BlockElementRotation rotation = null;
|
|
if (object.has("rotation")) {
|
|
JsonObject rotationObject = GsonHelper.getAsJsonObject(object, "rotation");
|
|
Vector3f origin = this.getVector3f(rotationObject, "origin");
|
|
origin.mul(0.0625f);
|
|
Direction.Axis axis = this.getAxis(rotationObject);
|
|
float angle = this.getAngle(rotationObject);
|
|
boolean rescale = GsonHelper.getAsBoolean(rotationObject, "rescale", false);
|
|
rotation = new BlockElementRotation((Vector3fc)origin, axis, angle, rescale);
|
|
}
|
|
return rotation;
|
|
}
|
|
|
|
private float getAngle(JsonObject object) {
|
|
float angle = GsonHelper.getAsFloat(object, "angle");
|
|
if (Mth.abs(angle) > 45.0f) {
|
|
throw new JsonParseException("Invalid rotation " + angle + " found, only values in [-45,45] range allowed");
|
|
}
|
|
return angle;
|
|
}
|
|
|
|
private Direction.Axis getAxis(JsonObject object) {
|
|
String axisName = GsonHelper.getAsString(object, "axis");
|
|
Direction.Axis axis = Direction.Axis.byName(axisName.toLowerCase(Locale.ROOT));
|
|
if (axis == null) {
|
|
throw new JsonParseException("Invalid rotation axis: " + axisName);
|
|
}
|
|
return axis;
|
|
}
|
|
|
|
private Map<Direction, BlockElementFace> getFaces(JsonDeserializationContext context, JsonObject object) {
|
|
Map<Direction, BlockElementFace> faces = this.filterNullFromFaces(context, object);
|
|
if (faces.isEmpty()) {
|
|
throw new JsonParseException("Expected between 1 and 6 unique faces, got 0");
|
|
}
|
|
return faces;
|
|
}
|
|
|
|
private Map<Direction, BlockElementFace> filterNullFromFaces(JsonDeserializationContext context, JsonObject object) {
|
|
EnumMap result = Maps.newEnumMap(Direction.class);
|
|
JsonObject faceObjects = GsonHelper.getAsJsonObject(object, "faces");
|
|
for (Map.Entry entry : faceObjects.entrySet()) {
|
|
Direction direction = this.getFacing((String)entry.getKey());
|
|
result.put(direction, (BlockElementFace)context.deserialize((JsonElement)entry.getValue(), BlockElementFace.class));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private Direction getFacing(String name) {
|
|
Direction direction = Direction.byName(name);
|
|
if (direction == null) {
|
|
throw new JsonParseException("Unknown facing: " + name);
|
|
}
|
|
return direction;
|
|
}
|
|
|
|
private Vector3f getTo(JsonObject object) {
|
|
Vector3f to = this.getVector3f(object, "to");
|
|
if (to.x() < -16.0f || to.y() < -16.0f || to.z() < -16.0f || to.x() > 32.0f || to.y() > 32.0f || to.z() > 32.0f) {
|
|
throw new JsonParseException("'to' specifier exceeds the allowed boundaries: " + String.valueOf(to));
|
|
}
|
|
return to;
|
|
}
|
|
|
|
private Vector3f getFrom(JsonObject object) {
|
|
Vector3f from = this.getVector3f(object, "from");
|
|
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) {
|
|
throw new JsonParseException("'from' specifier exceeds the allowed boundaries: " + String.valueOf(from));
|
|
}
|
|
return from;
|
|
}
|
|
|
|
private Vector3f getVector3f(JsonObject object, String key) {
|
|
JsonArray vecArray = GsonHelper.getAsJsonArray(object, key);
|
|
if (vecArray.size() != 3) {
|
|
throw new JsonParseException("Expected 3 " + key + " values, found: " + vecArray.size());
|
|
}
|
|
float[] elements = new float[3];
|
|
for (int i = 0; i < elements.length; ++i) {
|
|
elements[i] = GsonHelper.convertToFloat(vecArray.get(i), key + "[" + i + "]");
|
|
}
|
|
return new Vector3f(elements[0], elements[1], elements[2]);
|
|
}
|
|
}
|
|
}
|
|
|