Search in sources :

Example 1 with ItemTransform

use of net.minecraft.client.renderer.block.model.ItemTransform in project MinecraftForge by MinecraftForge.

the class ModelBuilder method toJson.

@VisibleForTesting
public JsonObject toJson() {
    JsonObject root = new JsonObject();
    if (this.parent != null) {
        root.addProperty("parent", this.parent.getLocation().toString());
    }
    if (!this.ambientOcclusion) {
        root.addProperty("ambientocclusion", this.ambientOcclusion);
    }
    if (this.guiLight != null) {
        root.addProperty("gui_light", this.guiLight.getSerializedName());
    }
    Map<Perspective, ItemTransform> transforms = this.transforms.build();
    if (!transforms.isEmpty()) {
        JsonObject display = new JsonObject();
        for (Entry<Perspective, ItemTransform> e : transforms.entrySet()) {
            JsonObject transform = new JsonObject();
            ItemTransform vec = e.getValue();
            if (vec.equals(ItemTransform.NO_TRANSFORM))
                continue;
            if (!vec.rotation.equals(ItemTransform.Deserializer.DEFAULT_ROTATION)) {
                transform.add("rotation", serializeVector3f(vec.rotation));
            }
            if (!vec.translation.equals(ItemTransform.Deserializer.DEFAULT_TRANSLATION)) {
                transform.add("translation", serializeVector3f(e.getValue().translation));
            }
            if (!vec.scale.equals(ItemTransform.Deserializer.DEFAULT_SCALE)) {
                transform.add("scale", serializeVector3f(e.getValue().scale));
            }
            display.add(e.getKey().name, transform);
        }
        root.add("display", display);
    }
    if (!this.textures.isEmpty()) {
        JsonObject textures = new JsonObject();
        for (Entry<String, String> e : this.textures.entrySet()) {
            textures.addProperty(e.getKey(), serializeLocOrKey(e.getValue()));
        }
        root.add("textures", textures);
    }
    if (!this.elements.isEmpty()) {
        JsonArray elements = new JsonArray();
        this.elements.stream().map(ElementBuilder::build).forEach(part -> {
            JsonObject partObj = new JsonObject();
            partObj.add("from", serializeVector3f(part.from));
            partObj.add("to", serializeVector3f(part.to));
            if (part.rotation != null) {
                JsonObject rotation = new JsonObject();
                rotation.add("origin", serializeVector3f(part.rotation.origin));
                rotation.addProperty("axis", part.rotation.axis.getSerializedName());
                rotation.addProperty("angle", part.rotation.angle);
                if (part.rotation.rescale) {
                    rotation.addProperty("rescale", part.rotation.rescale);
                }
                partObj.add("rotation", rotation);
            }
            if (!part.shade) {
                partObj.addProperty("shade", part.shade);
            }
            JsonObject faces = new JsonObject();
            for (Direction dir : Direction.values()) {
                BlockElementFace face = part.faces.get(dir);
                if (face == null)
                    continue;
                JsonObject faceObj = new JsonObject();
                faceObj.addProperty("texture", serializeLocOrKey(face.texture));
                if (!Arrays.equals(face.uv.uvs, part.uvsByFace(dir))) {
                    faceObj.add("uv", new Gson().toJsonTree(face.uv.uvs));
                }
                if (face.cullForDirection != null) {
                    faceObj.addProperty("cullface", face.cullForDirection.getSerializedName());
                }
                if (face.uv.rotation != 0) {
                    faceObj.addProperty("rotation", face.uv.rotation);
                }
                if (face.tintIndex != -1) {
                    faceObj.addProperty("tintindex", face.tintIndex);
                }
                faces.add(dir.getSerializedName(), faceObj);
            }
            if (!part.faces.isEmpty()) {
                partObj.add("faces", faces);
            }
            elements.add(partObj);
        });
        root.add("elements", elements);
    }
    if (customLoader != null)
        return customLoader.toJson(root);
    return root;
}
Also used : JsonArray(com.google.gson.JsonArray) ItemTransform(net.minecraft.client.renderer.block.model.ItemTransform) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) BlockElementFace(net.minecraft.client.renderer.block.model.BlockElementFace) Direction(net.minecraft.core.Direction) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 BlockElementFace (net.minecraft.client.renderer.block.model.BlockElementFace)1 ItemTransform (net.minecraft.client.renderer.block.model.ItemTransform)1 Direction (net.minecraft.core.Direction)1