From 7be8e4e3abd24a1bf970e2a433c98be64aef5205 Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Sun, 26 Jul 2020 00:16:21 -0500 Subject: [PATCH] Clean up models Just TRSR remains as possibly needed still, have to determine if that is still needed later --- .../client/model/BakedCompositeModel.java | 77 ---------- .../mantle/client/model/BakedSimple.java | 136 ------------------ .../mantle/client/model/BakedWrapper.java | 108 -------------- .../client/model/BlockItemModelWrapper.java | 108 -------------- .../client/model/DynamicBakedWrapper.java | 35 +++++ .../client/model/FallbackModelLoader.java | 3 + .../client/{ => model}/ModelHelper.java | 112 ++++++++------- .../mantle/client/model/ModelJsonUtils.java | 86 ----------- .../mantle/client/model/TRSRBakedModel.java | 58 ++------ .../model/connected/ConnectedModel.java | 4 +- .../client/model/fluid/FluidCuboid.java | 11 +- .../client/model/fluid/FluidsModel.java | 4 +- .../model/inventory/InventoryModel.java | 4 +- .../client/model/inventory/ModelItem.java | 11 +- .../render/InventoryTileEntityRenderer.java | 2 +- .../recipe/crafting/ShapedFallbackRecipe.java | 10 +- .../slimeknights/mantle/util/JsonHelper.java | 56 ++++++++ 17 files changed, 191 insertions(+), 634 deletions(-) delete mode 100644 src/main/java/slimeknights/mantle/client/model/BakedCompositeModel.java delete mode 100644 src/main/java/slimeknights/mantle/client/model/BakedSimple.java delete mode 100644 src/main/java/slimeknights/mantle/client/model/BakedWrapper.java delete mode 100644 src/main/java/slimeknights/mantle/client/model/BlockItemModelWrapper.java create mode 100644 src/main/java/slimeknights/mantle/client/model/DynamicBakedWrapper.java rename src/main/java/slimeknights/mantle/client/{ => model}/ModelHelper.java (52%) delete mode 100644 src/main/java/slimeknights/mantle/client/model/ModelJsonUtils.java create mode 100644 src/main/java/slimeknights/mantle/util/JsonHelper.java diff --git a/src/main/java/slimeknights/mantle/client/model/BakedCompositeModel.java b/src/main/java/slimeknights/mantle/client/model/BakedCompositeModel.java deleted file mode 100644 index 88eb928..0000000 --- a/src/main/java/slimeknights/mantle/client/model/BakedCompositeModel.java +++ /dev/null @@ -1,77 +0,0 @@ -package slimeknights.mantle.client.model; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import net.minecraft.block.BlockState; -import net.minecraft.client.renderer.model.BakedQuad; -import net.minecraft.client.renderer.model.IBakedModel; -import net.minecraft.util.Direction; -import net.minecraftforge.client.model.data.IModelData; - -import javax.annotation.Nullable; -import java.util.List; -import java.util.Optional; -import java.util.Random; - -/** - * Groups multiple baked models into a single one. Does not respect state etc. when getting quads. - * Best use the Builder to create a BakedCompositeModel. - */ -public class BakedCompositeModel extends BakedWrapper { - - protected final ImmutableMap, ImmutableList> parts; - - public BakedCompositeModel(IBakedModel parent, ImmutableMap, ImmutableList> parts) { - super(parent); - - this.parts = parts; - } - - @Override - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData extraData) { - return this.parts.get(Optional.ofNullable(side)); - } - - public static class Builder { - - ImmutableList.Builder[] builders; - IBakedModel parent; - - public Builder() { - this.builders = new ImmutableList.Builder[7]; - for (int i = 0; i < 7; i++) { - this.builders[i] = ImmutableList.builder(); - } - } - - public void add(IBakedModel model, BlockState state, Random rand) { - this.add(model, state, null, rand); - for (Direction side : Direction.values()) { - this.add(model, state, side, rand); - } - } - - public void add(IBakedModel model, BlockState state, @Nullable Direction side, Random rand) { - int index; - if (side == null) { - index = 6; - } - else { - index = side.getIndex(); - } - - this.builders[index].addAll(model.getQuads(state, side, rand)); - } - - public BakedCompositeModel build(IBakedModel parent) { - ImmutableMap.Builder,ImmutableList> map = ImmutableMap.builder(); - - map.put(Optional.empty(), this.builders[6].build()); - for (Direction side : Direction.values()) { - map.put(Optional.of(side), this.builders[side.getIndex()].build()); - } - - return new BakedCompositeModel(parent, map.build()); - } - } -} diff --git a/src/main/java/slimeknights/mantle/client/model/BakedSimple.java b/src/main/java/slimeknights/mantle/client/model/BakedSimple.java deleted file mode 100644 index 94f19db..0000000 --- a/src/main/java/slimeknights/mantle/client/model/BakedSimple.java +++ /dev/null @@ -1,136 +0,0 @@ -package slimeknights.mantle.client.model; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.mojang.blaze3d.matrix.MatrixStack; -import net.minecraft.block.BlockState; -import net.minecraft.client.renderer.model.BakedQuad; -import net.minecraft.client.renderer.model.IBakedModel; -import net.minecraft.client.renderer.model.ItemCameraTransforms; -import net.minecraft.client.renderer.model.ItemOverrideList; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; -import net.minecraft.util.Direction; -import net.minecraft.util.math.vector.TransformationMatrix; -import net.minecraftforge.client.model.PerspectiveMapWrapper; -import net.minecraftforge.client.model.data.EmptyModelData; -import net.minecraftforge.client.model.data.IModelData; - -import javax.annotation.Nullable; -import java.util.List; -import java.util.Random; - -public class BakedSimple implements IBakedModel { - - private final List quads; - private final ImmutableMap transforms; - private final TextureAtlasSprite particle; - private final boolean ambientOcclusion; - private final boolean isGui3d; - private final ItemOverrideList overrides; - - public BakedSimple(ImmutableList quads, ImmutableMap transforms, IBakedModel base) { - this( - quads, - transforms, - base.getParticleTexture(), - base.isAmbientOcclusion(), - base.isGui3d(), - base.getOverrides() - ); - } - - public BakedSimple(List quads, ImmutableMap transforms, IBakedModel base) { - this( - quads, - transforms, - base.getParticleTexture(), - base.isAmbientOcclusion(), - base.isGui3d(), - base.getOverrides() - ); - } - - public BakedSimple(List quads, ImmutableMap transforms, TextureAtlasSprite particle, boolean ambientOcclusion, boolean isGui3d, ItemOverrideList overrides) { - this.quads = quads; - this.particle = particle; - this.transforms = transforms; - this.ambientOcclusion = ambientOcclusion; - this.isGui3d = isGui3d; - this.overrides = overrides; - } - - @Override - @Deprecated - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) { - return this.getQuads(state, side, rand, EmptyModelData.INSTANCE); - } - - @Override - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData extraData) { - return this.quads; - } - - @Override - public boolean isAmbientOcclusion() { - return this.ambientOcclusion; - } - - @Override - public boolean isGui3d() { - return this.isGui3d; - } - - @Override - public boolean func_230044_c_() { - return false; - } - - @Override - public boolean isBuiltInRenderer() { - return false; - } - - @Override - @Deprecated - public TextureAtlasSprite getParticleTexture() { - return this.particle; - } - - @Override - public TextureAtlasSprite getParticleTexture(IModelData data) { - return this.particle; - } - - @Override - @Deprecated - public ItemCameraTransforms getItemCameraTransforms() { - return ItemCameraTransforms.DEFAULT; - } - - @Override - public ItemOverrideList getOverrides() { - return this.overrides; - } - - @Override - public IBakedModel handlePerspective(ItemCameraTransforms.TransformType cameraTransformType, MatrixStack mat) { - return PerspectiveMapWrapper.handlePerspective(this, this.transforms, cameraTransformType, mat); - } - - public static class Wrapper extends BakedSimple { - - private final IBakedModel parent; - - @SuppressWarnings("ConstantConditions") - public Wrapper(ImmutableList quads, IBakedModel base) { - super(quads, null, base); - this.parent = base; - } - - @Override - public IBakedModel handlePerspective(ItemCameraTransforms.TransformType cameraTransformType, MatrixStack mat) { - this.parent.handlePerspective(cameraTransformType, mat); - return this; - } - } -} diff --git a/src/main/java/slimeknights/mantle/client/model/BakedWrapper.java b/src/main/java/slimeknights/mantle/client/model/BakedWrapper.java deleted file mode 100644 index 150fdb3..0000000 --- a/src/main/java/slimeknights/mantle/client/model/BakedWrapper.java +++ /dev/null @@ -1,108 +0,0 @@ -package slimeknights.mantle.client.model; - -import com.google.common.collect.ImmutableMap; -import com.mojang.blaze3d.matrix.MatrixStack; -import net.minecraft.block.BlockState; -import net.minecraft.client.renderer.model.BakedQuad; -import net.minecraft.client.renderer.model.IBakedModel; -import net.minecraft.client.renderer.model.ItemCameraTransforms; -import net.minecraft.client.renderer.model.ItemOverrideList; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; -import net.minecraft.util.Direction; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.vector.TransformationMatrix; -import net.minecraft.world.IBlockDisplayReader; -import net.minecraftforge.client.model.PerspectiveMapWrapper; -import net.minecraftforge.client.model.data.EmptyModelData; -import net.minecraftforge.client.model.data.IModelData; - -import javax.annotation.Nullable; -import java.util.List; -import java.util.Random; - -public class BakedWrapper implements IBakedModel { - - protected final IBakedModel parent; - - public BakedWrapper(IBakedModel parent) { - this.parent = parent; - } - - @Override - @Deprecated - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) { - return this.getQuads(state, side, rand, EmptyModelData.INSTANCE); - } - - @Override - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData extraData) { - return this.parent.getQuads(state, side, rand, extraData); - } - - @Override - public boolean isAmbientOcclusion() { - return this.parent.isAmbientOcclusion(); - } - - @Override - public boolean isGui3d() { - return this.parent.isGui3d(); - } - - @Override - public boolean func_230044_c_() { - return false; - } - - @Override - public boolean isBuiltInRenderer() { - return this.parent.isBuiltInRenderer(); - } - - @Override - @Deprecated - public TextureAtlasSprite getParticleTexture() { - return this.parent.getParticleTexture(); - } - - @Override - public TextureAtlasSprite getParticleTexture(IModelData data) { - return this.parent.getParticleTexture(data); - } - - @Override - @Deprecated - public ItemCameraTransforms getItemCameraTransforms() { - return this.parent.getItemCameraTransforms(); - } - - @Override - public ItemOverrideList getOverrides() { - return this.parent.getOverrides(); - } - - @Override - public IModelData getModelData(IBlockDisplayReader world, BlockPos pos, BlockState state, IModelData tileData) { - return this.parent.getModelData(world, pos, state, tileData); - } - - public static class Perspective extends BakedWrapper implements IBakedModel { - - protected final ImmutableMap transforms; - - public Perspective(IBakedModel parent, ImmutableMap transforms) { - super(parent); - this.transforms = transforms; - } - - @Override - public IBakedModel handlePerspective(ItemCameraTransforms.TransformType cameraTransformType, MatrixStack mat) { - return PerspectiveMapWrapper.handlePerspective(this, this.transforms, cameraTransformType, mat); - } - - @Override - public boolean func_230044_c_() { - return false; - } - } -} diff --git a/src/main/java/slimeknights/mantle/client/model/BlockItemModelWrapper.java b/src/main/java/slimeknights/mantle/client/model/BlockItemModelWrapper.java deleted file mode 100644 index a5cd238..0000000 --- a/src/main/java/slimeknights/mantle/client/model/BlockItemModelWrapper.java +++ /dev/null @@ -1,108 +0,0 @@ -package slimeknights.mantle.client.model; - - -import com.mojang.blaze3d.matrix.MatrixStack; -import net.minecraft.block.BlockState; -import net.minecraft.client.renderer.model.BakedQuad; -import net.minecraft.client.renderer.model.IBakedModel; -import net.minecraft.client.renderer.model.ItemCameraTransforms; -import net.minecraft.client.renderer.model.ItemOverrideList; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; -import net.minecraft.util.Direction; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.vector.TransformationMatrix; -import net.minecraft.world.IBlockDisplayReader; -import net.minecraftforge.client.model.data.EmptyModelData; -import net.minecraftforge.client.model.data.IModelData; -import slimeknights.mantle.client.ModelHelper; - -import javax.annotation.Nullable; -import java.util.List; -import java.util.Random; - -/** - * Takes a blockmodel and applies the standard-block-perspective for third_person to it - */ -public class BlockItemModelWrapper implements IBakedModel { - - private final IBakedModel parent; - - public BlockItemModelWrapper(IBakedModel parent) { - this.parent = parent; - } - - @Override - public IBakedModel handlePerspective(ItemCameraTransforms.TransformType cameraTransformType, MatrixStack mat) { - TransformationMatrix transform = null; - // fix transformation in hand - if (cameraTransformType == ItemCameraTransforms.TransformType.THIRD_PERSON_RIGHT_HAND) { - transform = ModelHelper.BLOCK_THIRD_PERSON_RIGHT; - } - else if (cameraTransformType == ItemCameraTransforms.TransformType.THIRD_PERSON_LEFT_HAND) { - transform = ModelHelper.BLOCK_THIRD_PERSON_LEFT; - } - - if (transform != null) { - mat.getLast().getMatrix().mul(transform.getMatrix()); - } - return this; - } - - @Override - @Deprecated - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) { - return this.getQuads(state, side, rand, EmptyModelData.INSTANCE); - } - - @Override - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData extraData) { - return this.parent.getQuads(state, side, rand, extraData); - } - - @Override - public boolean isAmbientOcclusion() { - return false; - } - - @Override - public boolean isGui3d() { - return this.parent.isGui3d(); - } - - @Override - public boolean func_230044_c_() { - return false; - } - - @Override - public boolean isBuiltInRenderer() { - return this.parent.isBuiltInRenderer(); - } - - @Override - @Deprecated - public TextureAtlasSprite getParticleTexture() { - return this.parent.getParticleTexture(); - } - - @Override - public TextureAtlasSprite getParticleTexture(IModelData data) { - return this.parent.getParticleTexture(data); - } - - @Override - public IModelData getModelData(IBlockDisplayReader world, BlockPos pos, BlockState state, IModelData tileData) { - return this.parent.getModelData(world, pos, state, tileData); - } - - @Override - @Deprecated - public ItemCameraTransforms getItemCameraTransforms() { - return this.parent.getItemCameraTransforms(); - } - - @Override - public ItemOverrideList getOverrides() { - return ItemOverrideList.EMPTY; - } -} diff --git a/src/main/java/slimeknights/mantle/client/model/DynamicBakedWrapper.java b/src/main/java/slimeknights/mantle/client/model/DynamicBakedWrapper.java new file mode 100644 index 0000000..92e4def --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/model/DynamicBakedWrapper.java @@ -0,0 +1,35 @@ +package slimeknights.mantle.client.model; + +import net.minecraft.block.BlockState; +import net.minecraft.client.renderer.model.BakedQuad; +import net.minecraft.client.renderer.model.IBakedModel; +import net.minecraft.util.Direction; +import net.minecraftforge.client.model.BakedModelWrapper; +import net.minecraftforge.client.model.data.EmptyModelData; +import net.minecraftforge.client.model.data.IModelData; + +import javax.annotation.Nullable; +import java.util.List; +import java.util.Random; + +/** + * Cross between {@link BakedModelWrapper} and {@link net.minecraftforge.client.model.data.IDynamicBakedModel}. + * Used to create a baked model wrapper that has a dynamic {@link #getQuads(BlockState, Direction, Random, IModelData)} without worrying about overriding the deprecated variant. + * @param Baked model parent + */ +@SuppressWarnings("WeakerAccess") +public abstract class DynamicBakedWrapper extends BakedModelWrapper { + + protected DynamicBakedWrapper(T originalModel) { + super(originalModel); + } + + @Override + @Deprecated + public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) { + return this.getQuads(state, side, rand, EmptyModelData.INSTANCE); + } + + @Override + public abstract List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData extraData); +} diff --git a/src/main/java/slimeknights/mantle/client/model/FallbackModelLoader.java b/src/main/java/slimeknights/mantle/client/model/FallbackModelLoader.java index 40364d6..38e9656 100644 --- a/src/main/java/slimeknights/mantle/client/model/FallbackModelLoader.java +++ b/src/main/java/slimeknights/mantle/client/model/FallbackModelLoader.java @@ -27,6 +27,9 @@ import java.util.Collection; import java.util.Set; import java.util.function.Function; +/** + * Loads the first model from a list of models that has a loaded mod ID, ideal for optional CTM model support + */ @RequiredArgsConstructor public class FallbackModelLoader implements IModelLoader { /** Loader instance */ diff --git a/src/main/java/slimeknights/mantle/client/ModelHelper.java b/src/main/java/slimeknights/mantle/client/model/ModelHelper.java similarity index 52% rename from src/main/java/slimeknights/mantle/client/ModelHelper.java rename to src/main/java/slimeknights/mantle/client/model/ModelHelper.java index c0f7c88..402e879 100644 --- a/src/main/java/slimeknights/mantle/client/ModelHelper.java +++ b/src/main/java/slimeknights/mantle/client/model/ModelHelper.java @@ -1,31 +1,33 @@ -package slimeknights.mantle.client; +package slimeknights.mantle.client.model; -import com.google.common.collect.ImmutableMap; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import net.minecraft.block.BlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.model.BakedQuad; +import net.minecraft.client.renderer.model.BlockModel; import net.minecraft.client.renderer.model.IBakedModel; -import net.minecraft.client.renderer.model.IModelTransform; -import net.minecraft.client.renderer.model.ItemCameraTransforms; import net.minecraft.client.renderer.model.MultipartBakedModel; import net.minecraft.client.renderer.model.WeightedBakedModel; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.vertex.VertexFormatElement; import net.minecraft.util.IItemProvider; -import net.minecraft.util.math.vector.TransformationMatrix; +import net.minecraft.util.JSONUtils; import net.minecraft.util.math.vector.Vector3f; -import net.minecraftforge.client.model.SimpleModelTransform; import net.minecraftforge.client.model.pipeline.BakedQuadBuilder; import net.minecraftforge.client.model.pipeline.VertexTransformer; -import net.minecraftforge.common.model.TransformationHelper; import javax.annotation.Nullable; +/** + * Utilities to help in custom models + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class ModelHelper { - public static final IModelTransform DEFAULT_ITEM_STATE; - public static final IModelTransform DEFAULT_TOOL_STATE; - public static final TransformationMatrix BLOCK_THIRD_PERSON_RIGHT; - public static final TransformationMatrix BLOCK_THIRD_PERSON_LEFT; + /* Baked models */ /** * Gets the model for the given block @@ -67,8 +69,53 @@ public class ModelHelper { return null; } - public static TextureAtlasSprite getTextureFromBlockstate(BlockState state) { - return Minecraft.getInstance().getBlockRendererDispatcher().getBlockModelShapes().getTexture(state); + /* JSON */ + /* Serializer without any Forge extensions */ + private static final BlockModel.Deserializer DESERIALIZER = new BlockModel.Deserializer(); + + /** + * Deserializes a BlockModel from the given context + * @param context Serialization context + * @param json JSON data + * @return BlockModel instance + */ + public static BlockModel deserialize(JsonDeserializationContext context, JsonObject json) { + // the middle parameter is not used in the BlockModel deserializer + return DESERIALIZER.deserialize(json, null, context); + } + + /** + * Converts a JSON array with 3 elements into a Vector3f + * @param json JSON object + * @param name Name of the array in the object to fetch + * @return Vector3f of data + * @throws JsonParseException If there is no array or the length is wrong + */ + public static Vector3f arrayToVector(JsonObject json, String name) { + JsonArray array = JSONUtils.getJsonArray(json, name); + if (array.size() != 3) { + throw new JsonParseException("Expected 3 " + name + " values, found: " + array.size()); + } + float[] vec = new float[3]; + for(int i = 0; i < vec.length; ++i) { + vec[i] = JSONUtils.getFloat(array.get(i), name + "[" + i + "]"); + } + + return new Vector3f(vec[0], vec[1], vec[2]); + } + + /** + * Gets a rotation from JSON + * @param json JSON parent + * @return Integer of 0, 90, 180, or 270 + */ + public static int getRotation(JsonObject json, String key) { + int i = JSONUtils.getInt(json, key, 0); + if (i >= 0 && i % 90 == 0 && i / 90 <= 3) { + return i; + } else { + throw new JsonParseException("Invalid '" + key + "' " + i + " found, only 0/90/180/270 allowed"); + } } public static BakedQuad colorQuad(int color, BakedQuad quad) { @@ -77,43 +124,6 @@ public class ModelHelper { return transformer.build(); } - private static TransformationMatrix get(float tx, float ty, float tz, float ax, float ay, float az, float s) { - return new TransformationMatrix( - new Vector3f(tx / 16, ty / 16, tz / 16), - TransformationHelper.quatFromXYZ(new Vector3f(ax, ay, az), true), - new Vector3f(s, s, s), - null).blockCenterToCorner(); - } - - static { - { - // equals forge:default-item - ImmutableMap.Builder builder = ImmutableMap.builder(); - builder.put(ItemCameraTransforms.TransformType.GROUND, get(0, 2, 0, 0, 0, 0, 0.5f)); - builder.put(ItemCameraTransforms.TransformType.HEAD, get(0, 13, 7, 0, 180, 0, 1)); - builder.put(ItemCameraTransforms.TransformType.THIRD_PERSON_RIGHT_HAND, get(0, 3, 1, 0, 0, 0, 0.55f)); - builder.put(ItemCameraTransforms.TransformType.THIRD_PERSON_LEFT_HAND, get(0, 3, 1, 0, 0, 0, 0.55f)); - builder.put(ItemCameraTransforms.TransformType.FIRST_PERSON_RIGHT_HAND, get(1.13f, 3.2f, 1.13f, 0, -90, 25, 0.68f)); - builder.put(ItemCameraTransforms.TransformType.FIRST_PERSON_LEFT_HAND, get(1.13f, 3.2f, 1.13f, 0, 90, -25, 0.68f)); - DEFAULT_ITEM_STATE = new SimpleModelTransform(builder.build()); - } - { - // equals forge:default-tool - ImmutableMap.Builder builder = ImmutableMap.builder(); - builder.put(ItemCameraTransforms.TransformType.GROUND, get(0, 2, 0, 0, 0, 0, 0.5f)); - builder.put(ItemCameraTransforms.TransformType.HEAD, get(0, 13, 7, 0, 180, 0, 1)); - builder.put(ItemCameraTransforms.TransformType.THIRD_PERSON_RIGHT_HAND, get(0, 4, 0.5f, 0, -90, 55, 0.85f)); - builder.put(ItemCameraTransforms.TransformType.THIRD_PERSON_LEFT_HAND, get(0, 4, 0.5f, 0, 90, -55, 0.85f)); - builder.put(ItemCameraTransforms.TransformType.FIRST_PERSON_RIGHT_HAND, get(1.13f, 3.2f, 1.13f, 0, -90, 25, 0.68f)); - builder.put(ItemCameraTransforms.TransformType.FIRST_PERSON_LEFT_HAND, get(1.13f, 3.2f, 1.13f, 0, 90, -25, 0.68f)); - DEFAULT_TOOL_STATE = new SimpleModelTransform(builder.build()); - - } - { - BLOCK_THIRD_PERSON_RIGHT = get(0, 2.5f, 0, 75, 45, 0, 0.375f); - BLOCK_THIRD_PERSON_LEFT = get(0, 0, 0, 0, 255, 0, 0.4f); - } - } private static class ColorTransformer extends VertexTransformer { diff --git a/src/main/java/slimeknights/mantle/client/model/ModelJsonUtils.java b/src/main/java/slimeknights/mantle/client/model/ModelJsonUtils.java deleted file mode 100644 index 4ad0d93..0000000 --- a/src/main/java/slimeknights/mantle/client/model/ModelJsonUtils.java +++ /dev/null @@ -1,86 +0,0 @@ -package slimeknights.mantle.client.model; - -import com.google.common.collect.ImmutableList; -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import net.minecraft.client.renderer.model.BlockModel; -import net.minecraft.util.JSONUtils; -import net.minecraft.util.math.vector.Vector3f; - -import java.util.List; -import java.util.function.Function; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ModelJsonUtils { - private static final BlockModel.Deserializer DESERIALIZER = new BlockModel.Deserializer(); - - /** - * Deserializes a BlockModel from the given context - * @param context Serialization context - * @param json JSON data - * @return BlockModel instance - */ - public static BlockModel deserialize(JsonDeserializationContext context, JsonObject json) { - // the middle parameter is not used in the BlockModel deserializer - return DESERIALIZER.deserialize(json, null, context); - } - - /** - * Converts a JSON array with 3 elements into a Vector3f - * @param json JSON object - * @param name Name of the array in the object to fetch - * @return Vector3f of data - * @throws JsonParseException If there is no array or the length is wrong - */ - public static Vector3f arrayToVector(JsonObject json, String name) { - JsonArray array = JSONUtils.getJsonArray(json, name); - if (array.size() != 3) { - throw new JsonParseException("Expected 3 " + name + " values, found: " + array.size()); - } - float[] vec = new float[3]; - for(int i = 0; i < vec.length; ++i) { - vec[i] = JSONUtils.getFloat(array.get(i), name + "[" + i + "]"); - } - - return new Vector3f(vec[0], vec[1], vec[2]); - } - - /** - * Gets a rotation from JSON - * @param json JSON parent - * @return Integer of 0, 90, 180, or 270 - */ - public static int getRotation(JsonObject json, String key) { - int i = JSONUtils.getInt(json, key, 0); - if (i >= 0 && i % 90 == 0 && i / 90 <= 3) { - return i; - } else { - throw new JsonParseException("Invalid '" + key + "' " + i + " found, only 0/90/180/270 allowed"); - } - } - - /** - * Parses a list from an JsonArray - * @param array Json array - * @param mapper Mapper from JsonObject to new object - * @param name Json key of the array - * @param Output type - * @return List of output objects - */ - public static List parseList(JsonArray array, Function mapper, String name) { - if (array.size() == 0) { - throw new JsonSyntaxException(name + " must have at least 1 element"); - } - // build the list - ImmutableList.Builder builder = ImmutableList.builder(); - for (int i = 0; i < array.size(); i++) { - builder.add(mapper.apply(JSONUtils.getJsonObject(array.get(i), name + "[" + i + "]"))); - } - return builder.build(); - } -} diff --git a/src/main/java/slimeknights/mantle/client/model/TRSRBakedModel.java b/src/main/java/slimeknights/mantle/client/model/TRSRBakedModel.java index 8d5b850..160e7ce 100644 --- a/src/main/java/slimeknights/mantle/client/model/TRSRBakedModel.java +++ b/src/main/java/slimeknights/mantle/client/model/TRSRBakedModel.java @@ -4,7 +4,6 @@ import com.google.common.collect.ImmutableList; import net.minecraft.block.BlockState; import net.minecraft.client.renderer.model.BakedQuad; import net.minecraft.client.renderer.model.IBakedModel; -import net.minecraft.client.renderer.model.ItemCameraTransforms; import net.minecraft.client.renderer.model.ItemOverrideList; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.vertex.VertexFormatElement; @@ -17,6 +16,7 @@ import net.minecraft.util.math.vector.Matrix4f; import net.minecraft.util.math.vector.TransformationMatrix; import net.minecraft.util.math.vector.Vector3f; import net.minecraft.util.math.vector.Vector4f; +import net.minecraftforge.client.model.data.IModelData; import net.minecraftforge.client.model.pipeline.BakedQuadBuilder; import net.minecraftforge.client.model.pipeline.VertexTransformer; import net.minecraftforge.common.model.TransformationHelper; @@ -25,11 +25,13 @@ import javax.annotation.Nullable; import java.util.List; import java.util.Random; -// for those wondering TRSR stands for Translation Rotation Scale Rotation -public class TRSRBakedModel implements IBakedModel { +/** + * Model that Translates, Rotates, Scales, then Rotates a child model + * TODO: is this still needed? + */ +public class TRSRBakedModel extends DynamicBakedWrapper { - protected final IBakedModel original; - protected final TransformationMatrix transformation; + private final TransformationMatrix transformation; private final TRSROverride override; private final int faceOffset; @@ -49,7 +51,7 @@ public class TRSRBakedModel implements IBakedModel { } public TRSRBakedModel(IBakedModel original, TransformationMatrix transform) { - this.original = original; + super(original); this.transformation = transform.blockCenterToCorner(); this.override = new TRSROverride(this); this.faceOffset = 0; @@ -57,7 +59,7 @@ public class TRSRBakedModel implements IBakedModel { /** Rotates around the Y axis and adjusts culling appropriately. South is default. */ public TRSRBakedModel(IBakedModel original, Direction facing) { - this.original = original; + super(original); this.override = new TRSROverride(this); this.faceOffset = 4 + Direction.NORTH.getHorizontalIndex() - facing.getHorizontalIndex(); @@ -67,18 +69,17 @@ public class TRSRBakedModel implements IBakedModel { } @Override - public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) { + public List getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData data) { // transform quads obtained from parent ImmutableList.Builder builder = ImmutableList.builder(); - - if (!this.original.isBuiltInRenderer()) { + if (!this.originalModel.isBuiltInRenderer()) { try { // adjust side to facing-rotation if (side != null && side.getHorizontalIndex() > -1) { side = Direction.byHorizontalIndex((side.getHorizontalIndex() + this.faceOffset) % 4); } - for (BakedQuad quad : this.original.getQuads(state, side, rand)) { + for (BakedQuad quad : this.originalModel.getQuads(state, side, rand, data)) { Transformer transformer = new Transformer(this.transformation, quad.func_187508_a()); quad.pipe(transformer); builder.add(transformer.build()); @@ -92,37 +93,6 @@ public class TRSRBakedModel implements IBakedModel { return builder.build(); } - @Override - public boolean isAmbientOcclusion() { - return false; - } - - @Override - public boolean isGui3d() { - return this.original.isGui3d(); - } - - @Override - public boolean func_230044_c_() { - return false; - } - - @Override - public boolean isBuiltInRenderer() { - return this.original.isBuiltInRenderer(); - } - - @Override - public TextureAtlasSprite getParticleTexture() { - return this.original.getParticleTexture(); - } - - @Override - @Deprecated - public ItemCameraTransforms getItemCameraTransforms() { - return this.original.getItemCameraTransforms(); - } - @Override public ItemOverrideList getOverrides() { return this.override; @@ -132,14 +102,14 @@ public class TRSRBakedModel implements IBakedModel { private final TRSRBakedModel model; - public TRSROverride(TRSRBakedModel model) { + TRSROverride(TRSRBakedModel model) { this.model = model; } @Nullable @Override public IBakedModel func_239290_a_(IBakedModel originalModel, ItemStack stack, @Nullable ClientWorld world, @Nullable LivingEntity entity) { - IBakedModel baked = this.model.original.getOverrides().func_239290_a_(originalModel, stack, world, entity); + IBakedModel baked = this.model.originalModel.getOverrides().func_239290_a_(originalModel, stack, world, entity); if (baked == null) { return null; } diff --git a/src/main/java/slimeknights/mantle/client/model/connected/ConnectedModel.java b/src/main/java/slimeknights/mantle/client/model/connected/ConnectedModel.java index 2faf2d4..4b0b070 100644 --- a/src/main/java/slimeknights/mantle/client/model/connected/ConnectedModel.java +++ b/src/main/java/slimeknights/mantle/client/model/connected/ConnectedModel.java @@ -46,7 +46,7 @@ import net.minecraftforge.client.model.data.ModelDataMap; import net.minecraftforge.client.model.data.ModelProperty; import net.minecraftforge.client.model.geometry.IModelGeometry; import slimeknights.mantle.block.IMultipartConnectedBlock; -import slimeknights.mantle.client.model.ModelJsonUtils; +import slimeknights.mantle.client.model.ModelHelper; import javax.annotation.Nullable; import java.util.Collection; @@ -443,7 +443,7 @@ public class ConnectedModel implements IModelGeometry { @Override public ConnectedModel read(JsonDeserializationContext context, JsonObject json) { - BlockModel model = ModelJsonUtils.deserialize(context, json); + BlockModel model = ModelHelper.deserialize(context, json); // root object for all model data JsonObject data = JSONUtils.getJsonObject(json, "connection"); diff --git a/src/main/java/slimeknights/mantle/client/model/fluid/FluidCuboid.java b/src/main/java/slimeknights/mantle/client/model/fluid/FluidCuboid.java index d95f85b..0b6003a 100644 --- a/src/main/java/slimeknights/mantle/client/model/fluid/FluidCuboid.java +++ b/src/main/java/slimeknights/mantle/client/model/fluid/FluidCuboid.java @@ -9,7 +9,8 @@ import lombok.Getter; import net.minecraft.util.Direction; import net.minecraft.util.JSONUtils; import net.minecraft.util.math.vector.Vector3f; -import slimeknights.mantle.client.model.ModelJsonUtils; +import slimeknights.mantle.client.model.ModelHelper; +import slimeknights.mantle.util.JsonHelper; import javax.annotation.Nullable; import java.util.Collections; @@ -91,8 +92,8 @@ public class FluidCuboid { * @return Fluid cuboid */ public static FluidCuboid fromJson(JsonObject json) { - Vector3f from = ModelJsonUtils.arrayToVector(json, "from"); - Vector3f to = ModelJsonUtils.arrayToVector(json, "to"); + Vector3f from = ModelHelper.arrayToVector(json, "from"); + Vector3f to = ModelHelper.arrayToVector(json, "to"); // if faces is defined, fill with specified faces Map faces = getFaces(json); return new FluidCuboid(from, to, faces); @@ -114,7 +115,7 @@ public class FluidCuboid { // array: multiple cubes if (json.isJsonArray()) { - return ModelJsonUtils.parseList(json.getAsJsonArray(), FluidCuboid::fromJson, key); + return JsonHelper.parseList(json.getAsJsonArray(), key, JSONUtils::getJsonObject, FluidCuboid::fromJson); } throw new JsonSyntaxException("Invalid fluid '" + key + "', must be an array or an object"); @@ -139,7 +140,7 @@ public class FluidCuboid { if (dir != null) { JsonObject face = JSONUtils.getJsonObject(entry.getValue(), name); boolean flowing = JSONUtils.getBoolean(face, "flowing", false); - int rotation = ModelJsonUtils.getRotation(face, "rotation"); + int rotation = ModelHelper.getRotation(face, "rotation"); faces.put(dir, new FluidFace(flowing, rotation)); } else { throw new JsonSyntaxException("Unknown face '" + name + "'"); diff --git a/src/main/java/slimeknights/mantle/client/model/fluid/FluidsModel.java b/src/main/java/slimeknights/mantle/client/model/fluid/FluidsModel.java index de17483..caab159 100644 --- a/src/main/java/slimeknights/mantle/client/model/fluid/FluidsModel.java +++ b/src/main/java/slimeknights/mantle/client/model/fluid/FluidsModel.java @@ -19,7 +19,7 @@ import net.minecraftforge.client.model.BakedModelWrapper; import net.minecraftforge.client.model.IModelConfiguration; import net.minecraftforge.client.model.IModelLoader; import net.minecraftforge.client.model.geometry.IModelGeometry; -import slimeknights.mantle.client.model.ModelJsonUtils; +import slimeknights.mantle.client.model.ModelHelper; import java.util.Collection; import java.util.List; @@ -68,7 +68,7 @@ public class FluidsModel implements IModelGeometry { @Override public FluidsModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents) { - BlockModel model = ModelJsonUtils.deserialize(deserializationContext, modelContents); + BlockModel model = ModelHelper.deserialize(deserializationContext, modelContents); List fluid = FluidCuboid.listFromJson(modelContents, "fluids"); return new FluidsModel(model, fluid); } diff --git a/src/main/java/slimeknights/mantle/client/model/inventory/InventoryModel.java b/src/main/java/slimeknights/mantle/client/model/inventory/InventoryModel.java index 16d7a15..fc0e8d0 100644 --- a/src/main/java/slimeknights/mantle/client/model/inventory/InventoryModel.java +++ b/src/main/java/slimeknights/mantle/client/model/inventory/InventoryModel.java @@ -19,7 +19,7 @@ import net.minecraftforge.client.model.BakedModelWrapper; import net.minecraftforge.client.model.IModelConfiguration; import net.minecraftforge.client.model.IModelLoader; import net.minecraftforge.client.model.geometry.IModelGeometry; -import slimeknights.mantle.client.model.ModelJsonUtils; +import slimeknights.mantle.client.model.ModelHelper; import java.util.Collection; import java.util.List; @@ -68,7 +68,7 @@ public class InventoryModel implements IModelGeometry { @Override public InventoryModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents) { - BlockModel model = ModelJsonUtils.deserialize(deserializationContext, modelContents); + BlockModel model = ModelHelper.deserialize(deserializationContext, modelContents); List items = ModelItem.listFromJson(modelContents, "items"); return new InventoryModel(model, items); } diff --git a/src/main/java/slimeknights/mantle/client/model/inventory/ModelItem.java b/src/main/java/slimeknights/mantle/client/model/inventory/ModelItem.java index ec30aca..2c6f041 100644 --- a/src/main/java/slimeknights/mantle/client/model/inventory/ModelItem.java +++ b/src/main/java/slimeknights/mantle/client/model/inventory/ModelItem.java @@ -4,7 +4,8 @@ import com.google.gson.JsonObject; import lombok.Getter; import net.minecraft.util.JSONUtils; import net.minecraft.util.math.vector.Vector3f; -import slimeknights.mantle.client.model.ModelJsonUtils; +import slimeknights.mantle.client.model.ModelHelper; +import slimeknights.mantle.util.JsonHelper; import java.util.List; @@ -79,9 +80,9 @@ public class ModelItem { if (size == 0) { return ModelItem.EMPTY; } - Vector3f center = ModelJsonUtils.arrayToVector(json, "center"); - float x = ModelJsonUtils.getRotation(json, "x"); - float y = ModelJsonUtils.getRotation(json, "y"); + 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); } @@ -92,6 +93,6 @@ public class ModelItem { * @return List of model items */ public static List listFromJson(JsonObject parent, String key) { - return ModelJsonUtils.parseList(JSONUtils.getJsonArray(parent, key), ModelItem::fromJson, key); + return JsonHelper.parseList(parent, key, JSONUtils::getJsonObject, ModelItem::fromJson); } } diff --git a/src/main/java/slimeknights/mantle/client/render/InventoryTileEntityRenderer.java b/src/main/java/slimeknights/mantle/client/render/InventoryTileEntityRenderer.java index 2446caa..0866f65 100644 --- a/src/main/java/slimeknights/mantle/client/render/InventoryTileEntityRenderer.java +++ b/src/main/java/slimeknights/mantle/client/render/InventoryTileEntityRenderer.java @@ -7,7 +7,7 @@ import net.minecraft.client.renderer.tileentity.TileEntityRenderer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.inventory.IInventory; import net.minecraft.tileentity.TileEntity; -import slimeknights.mantle.client.ModelHelper; +import slimeknights.mantle.client.model.ModelHelper; import slimeknights.mantle.client.model.inventory.InventoryModel; import slimeknights.mantle.client.model.inventory.ModelItem; diff --git a/src/main/java/slimeknights/mantle/recipe/crafting/ShapedFallbackRecipe.java b/src/main/java/slimeknights/mantle/recipe/crafting/ShapedFallbackRecipe.java index d2ec1bf..961a484 100644 --- a/src/main/java/slimeknights/mantle/recipe/crafting/ShapedFallbackRecipe.java +++ b/src/main/java/slimeknights/mantle/recipe/crafting/ShapedFallbackRecipe.java @@ -1,7 +1,6 @@ package slimeknights.mantle.recipe.crafting; import com.google.common.collect.ImmutableList; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; import net.minecraft.inventory.CraftingInventory; import net.minecraft.item.ItemStack; @@ -17,6 +16,7 @@ import net.minecraft.util.NonNullList; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; import slimeknights.mantle.recipe.MantleRecipeSerializers; +import slimeknights.mantle.util.JsonHelper; import java.util.List; import java.util.Optional; @@ -88,12 +88,8 @@ public class ShapedFallbackRecipe extends ShapedRecipe { @Override public ShapedFallbackRecipe read(ResourceLocation id, JsonObject json) { ShapedRecipe base = super.read(id, json); - JsonArray array = JSONUtils.getJsonArray(json, "alternatives"); - ImmutableList.Builder builder = ImmutableList.builder(); - for (int i = 0; i < array.size(); i++) { - builder.add(new ResourceLocation(JSONUtils.getString(array.get(i), "alternatives[" + i + "]"))); - } - return new ShapedFallbackRecipe(base, builder.build()); + List alternatives = JsonHelper.parseList(json, "alternatives", JSONUtils::getString, ResourceLocation::new); + return new ShapedFallbackRecipe(base, alternatives); } @Override diff --git a/src/main/java/slimeknights/mantle/util/JsonHelper.java b/src/main/java/slimeknights/mantle/util/JsonHelper.java new file mode 100644 index 0000000..eeb7520 --- /dev/null +++ b/src/main/java/slimeknights/mantle/util/JsonHelper.java @@ -0,0 +1,56 @@ +package slimeknights.mantle.util; + +import com.google.common.collect.ImmutableList; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import net.minecraft.util.JSONUtils; + +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * Utilities to help in parsing JSON + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class JsonHelper { + /** + * Parses a list from an JsonArray + * @param array Json array + * @param name Json key of the array + * @param parser Function to get a raw type from the JsonElement, typically from {@link JSONUtils} + * @param mapper Mapper from raw type to new object + * @param Element raw type + * @param Output type + * @return List of output objects + */ + public static List parseList(JsonArray array, String name, BiFunction parser, Function mapper) { + if (array.size() == 0) { + throw new JsonSyntaxException(name + " must have at least 1 element"); + } + // build the list + ImmutableList.Builder builder = ImmutableList.builder(); + for (int i = 0; i < array.size(); i++) { + builder.add(mapper.apply(parser.apply(array.get(i), name + "[" + i + "]"))); + } + return builder.build(); + } + + /** + * Parses a list from an JsonArray + * @param object Parent JSON object + * @param name Json key of the array + * @param parser Function to get a raw type from the JsonElement, typically from {@link JSONUtils} + * @param mapper Mapper from raw type to new object + * @param Element raw type + * @param Output type + * @return List of output objects + */ + public static List parseList(JsonObject object, String name, BiFunction parser, Function mapper) { + return parseList(JSONUtils.getJsonArray(object, name), name, parser, mapper); + } +}