Skip to content

Commit

Permalink
Add transform tye to the model item class
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Dec 22, 2021
1 parent f2ce572 commit aec521c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package slimeknights.mantle.client.model.inventory;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import lombok.Getter;
import net.minecraft.client.renderer.model.ItemCameraTransforms.TransformType;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.math.vector.Vector3f;
import slimeknights.mantle.client.model.util.ModelHelper;
Expand All @@ -25,17 +27,24 @@ public class ModelItem {
/** Y axis rotation, applied second */
@Getter
private final float y;
@Getter
private final TransformType transform;

/** Item center location in percentages, lazy loaded */
private Vector3f centerScaled;
/** Item size in percentages, lazy loaded */
private Float sizeScaled;

public ModelItem(Vector3f center, float size, float x, float y) {
this(center, size, x, y, TransformType.NONE);
}

public ModelItem(Vector3f center, float size, float x, float y, TransformType transform) {
this.center = center;
this.size = size;
this.x = x;
this.y = y;
this.transform = transform;
}

/**
Expand Down Expand Up @@ -69,6 +78,23 @@ public boolean isHidden() {
return size == 0;
}

/** Parses a transform type from a string */
private static TransformType parseTransformType(JsonObject json, String key) {
String name = JSONUtils.getString(json, key, "none");
switch (name) {
case "none": return TransformType.NONE;
case "head": return TransformType.HEAD;
case "gui": return TransformType.GUI;
case "ground": return TransformType.GROUND;
case "fixed": return TransformType.FIXED;
case "thirdperson_righthand": return TransformType.THIRD_PERSON_RIGHT_HAND;
case "thirdperson_lefthand": return TransformType.THIRD_PERSON_LEFT_HAND;
case "firstperson_righthand": return TransformType.FIRST_PERSON_RIGHT_HAND;
case "firstperson_lefthand": return TransformType.FIRST_PERSON_LEFT_HAND;
}
throw new JsonSyntaxException("Unknown transform type " + name);
}

/**
* Gets a model item from a JSON object
* @param json JSON object instance
Expand All @@ -83,7 +109,8 @@ public static ModelItem fromJson(JsonObject json) {
Vector3f center = ModelHelper.arrayToVector(json, "center");
float x = ModelHelper.getRotation(json, "x");
float y = ModelHelper.getRotation(json, "y");
return new ModelItem(center, size, x, y);
TransformType transformType = parseTransformType(json, "transform");
return new ModelItem(center, size, x, y, transformType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.model.ItemCameraTransforms.TransformType;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.item.ItemStack;
import net.minecraft.state.properties.BlockStateProperties;
Expand Down Expand Up @@ -89,7 +88,7 @@ public static void renderItem(MatrixStack matrices, IRenderTypeBuffer buffer, It
}

// render the actual item
Minecraft.getInstance().getItemRenderer().renderItem(item, TransformType.NONE, light, OverlayTexture.NO_OVERLAY, matrices, buffer);
Minecraft.getInstance().getItemRenderer().renderItem(item, modelItem.getTransform(), light, OverlayTexture.NO_OVERLAY, matrices, buffer);
matrices.pop();
}

Expand Down

0 comments on commit aec521c

Please sign in to comment.