mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-18 11:19:20 +00:00
Clean up models
Just TRSR remains as possibly needed still, have to determine if that is still needed later
This commit is contained in:
1 parent
b8bcd95af3
commit
7be8e4e3ab
17 files changed
+191
-634
No files matched your search
@@ -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<Optional<Direction>, ImmutableList<BakedQuad>> parts;
|
||||
|
||||
public BakedCompositeModel(IBakedModel parent, ImmutableMap<Optional<Direction>, ImmutableList<BakedQuad>> parts) {
|
||||
super(parent);
|
||||
|
||||
this.parts = parts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData extraData) {
|
||||
return this.parts.get(Optional.ofNullable(side));
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
ImmutableList.Builder<BakedQuad>[] 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<Optional<Direction>,ImmutableList<BakedQuad>> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<BakedQuad> quads;
|
||||
private final ImmutableMap<ItemCameraTransforms.TransformType, TransformationMatrix> transforms;
|
||||
private final TextureAtlasSprite particle;
|
||||
private final boolean ambientOcclusion;
|
||||
private final boolean isGui3d;
|
||||
private final ItemOverrideList overrides;
|
||||
|
||||
public BakedSimple(ImmutableList<BakedQuad> quads, ImmutableMap<ItemCameraTransforms.TransformType, TransformationMatrix> transforms, IBakedModel base) {
|
||||
this(
|
||||
quads,
|
||||
transforms,
|
||||
base.getParticleTexture(),
|
||||
base.isAmbientOcclusion(),
|
||||
base.isGui3d(),
|
||||
base.getOverrides()
|
||||
);
|
||||
}
|
||||
|
||||
public BakedSimple(List<BakedQuad> quads, ImmutableMap<ItemCameraTransforms.TransformType, TransformationMatrix> transforms, IBakedModel base) {
|
||||
this(
|
||||
quads,
|
||||
transforms,
|
||||
base.getParticleTexture(),
|
||||
base.isAmbientOcclusion(),
|
||||
base.isGui3d(),
|
||||
base.getOverrides()
|
||||
);
|
||||
}
|
||||
|
||||
public BakedSimple(List<BakedQuad> quads, ImmutableMap<ItemCameraTransforms.TransformType, TransformationMatrix> 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<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
|
||||
return this.getQuads(state, side, rand, EmptyModelData.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BakedQuad> 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<BakedQuad> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
|
||||
return this.getQuads(state, side, rand, EmptyModelData.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BakedQuad> 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<ItemCameraTransforms.TransformType, TransformationMatrix> transforms;
|
||||
|
||||
public Perspective(IBakedModel parent, ImmutableMap<ItemCameraTransforms.TransformType, TransformationMatrix> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
|
||||
return this.getQuads(state, side, rand, EmptyModelData.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BakedQuad> 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;
|
||||
}
|
||||
}
|
||||
@@ -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 <T> Baked model parent
|
||||
*/
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public abstract class DynamicBakedWrapper<T extends IBakedModel> extends BakedModelWrapper<T> {
|
||||
|
||||
protected DynamicBakedWrapper(T originalModel) {
|
||||
super(originalModel);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
|
||||
return this.getQuads(state, side, rand, EmptyModelData.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData extraData);
|
||||
}
|
||||
@@ -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<FallbackModelLoader.BlockModelWrapper> {
|
||||
/** Loader instance */
|
||||
|
||||
+61
-51
@@ -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<ItemCameraTransforms.TransformType, TransformationMatrix> 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<ItemCameraTransforms.TransformType, TransformationMatrix> 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 {
|
||||
|
||||
@@ -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 <T> Output type
|
||||
* @return List of output objects
|
||||
*/
|
||||
public static <T> List<T> parseList(JsonArray array, Function<JsonObject, T> mapper, String name) {
|
||||
if (array.size() == 0) {
|
||||
throw new JsonSyntaxException(name + " must have at least 1 element");
|
||||
}
|
||||
// build the list
|
||||
ImmutableList.Builder<T> 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();
|
||||
}
|
||||
}
|
||||
@@ -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<IBakedModel> {
|
||||
|
||||
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<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
|
||||
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand, IModelData data) {
|
||||
// transform quads obtained from parent
|
||||
|
||||
ImmutableList.Builder<BakedQuad> 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;
|
||||
}
|
||||
|
||||
@@ -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<ConnectedModel> {
|
||||
|
||||
@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");
|
||||
|
||||
@@ -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<Direction,FluidFace> 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 + "'");
|
||||
|
||||
@@ -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<FluidsModel> {
|
||||
|
||||
@Override
|
||||
public FluidsModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents) {
|
||||
BlockModel model = ModelJsonUtils.deserialize(deserializationContext, modelContents);
|
||||
BlockModel model = ModelHelper.deserialize(deserializationContext, modelContents);
|
||||
List<FluidCuboid> fluid = FluidCuboid.listFromJson(modelContents, "fluids");
|
||||
return new FluidsModel(model, fluid);
|
||||
}
|
||||
|
||||
@@ -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<InventoryModel> {
|
||||
|
||||
@Override
|
||||
public InventoryModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents) {
|
||||
BlockModel model = ModelJsonUtils.deserialize(deserializationContext, modelContents);
|
||||
BlockModel model = ModelHelper.deserialize(deserializationContext, modelContents);
|
||||
List<ModelItem> items = ModelItem.listFromJson(modelContents, "items");
|
||||
return new InventoryModel(model, items);
|
||||
}
|
||||
|
||||
@@ -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<ModelItem> listFromJson(JsonObject parent, String key) {
|
||||
return ModelJsonUtils.parseList(JSONUtils.getJsonArray(parent, key), ModelItem::fromJson, key);
|
||||
return JsonHelper.parseList(parent, key, JSONUtils::getJsonObject, ModelItem::fromJson);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<ResourceLocation> 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<ResourceLocation> alternatives = JsonHelper.parseList(json, "alternatives", JSONUtils::getString, ResourceLocation::new);
|
||||
return new ShapedFallbackRecipe(base, alternatives);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -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 <R> Element raw type
|
||||
* @param <T> Output type
|
||||
* @return List of output objects
|
||||
*/
|
||||
public static <R,T> List<T> parseList(JsonArray array, String name, BiFunction<JsonElement,String,R> parser, Function<R, T> mapper) {
|
||||
if (array.size() == 0) {
|
||||
throw new JsonSyntaxException(name + " must have at least 1 element");
|
||||
}
|
||||
// build the list
|
||||
ImmutableList.Builder<T> 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 <R> Element raw type
|
||||
* @param <T> Output type
|
||||
* @return List of output objects
|
||||
*/
|
||||
public static <R,T> List<T> parseList(JsonObject object, String name, BiFunction<JsonElement,String,R> parser, Function<R, T> mapper) {
|
||||
return parseList(JSONUtils.getJsonArray(object, name), name, parser, mapper);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user