78 lines
2.6 KiB
Java
78 lines
2.6 KiB
Java
package net.minecraft.client.renderer.block.model;
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
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.mojang.math.Transformation;
|
|
import java.lang.reflect.Type;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.resources.model.BlockModelRotation;
|
|
import net.minecraft.client.resources.model.ModelState;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.util.GsonHelper;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public record Variant(ResourceLocation modelLocation, Transformation rotation, boolean uvLock, int weight) implements ModelState {
|
|
@Override
|
|
public Transformation getRotation() {
|
|
return this.rotation;
|
|
}
|
|
|
|
@Override
|
|
public boolean isUvLocked() {
|
|
return this.uvLock;
|
|
}
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public static class Deserializer implements JsonDeserializer<Variant> {
|
|
@VisibleForTesting
|
|
static final boolean DEFAULT_UVLOCK = false;
|
|
@VisibleForTesting
|
|
static final int DEFAULT_WEIGHT = 1;
|
|
@VisibleForTesting
|
|
static final int DEFAULT_X_ROTATION = 0;
|
|
@VisibleForTesting
|
|
static final int DEFAULT_Y_ROTATION = 0;
|
|
|
|
public Variant deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
|
|
JsonObject jsonObject = json.getAsJsonObject();
|
|
ResourceLocation resourceLocation = this.getModel(jsonObject);
|
|
BlockModelRotation blockModelRotation = this.getBlockRotation(jsonObject);
|
|
boolean bl = this.getUvLock(jsonObject);
|
|
int i = this.getWeight(jsonObject);
|
|
return new Variant(resourceLocation, blockModelRotation.getRotation(), bl, i);
|
|
}
|
|
|
|
private boolean getUvLock(JsonObject json) {
|
|
return GsonHelper.getAsBoolean(json, "uvlock", false);
|
|
}
|
|
|
|
protected BlockModelRotation getBlockRotation(JsonObject json) {
|
|
int i = GsonHelper.getAsInt(json, "x", 0);
|
|
int j = GsonHelper.getAsInt(json, "y", 0);
|
|
BlockModelRotation blockModelRotation = BlockModelRotation.by(i, j);
|
|
if (blockModelRotation == null) {
|
|
throw new JsonParseException("Invalid BlockModelRotation x: " + i + ", y: " + j);
|
|
} else {
|
|
return blockModelRotation;
|
|
}
|
|
}
|
|
|
|
protected ResourceLocation getModel(JsonObject json) {
|
|
return ResourceLocation.parse(GsonHelper.getAsString(json, "model"));
|
|
}
|
|
|
|
protected int getWeight(JsonObject json) {
|
|
int i = GsonHelper.getAsInt(json, "weight", 1);
|
|
if (i < 1) {
|
|
throw new JsonParseException("Invalid weight " + i + " found, expected integer >= 1");
|
|
} else {
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
}
|