| 1 | package net.minecraft.client.renderer.block.model;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.gson.Gson;
|
| 6 | import com.google.gson.GsonBuilder;
|
| 7 | import com.google.gson.JsonDeserializationContext;
|
| 8 | import com.google.gson.JsonDeserializer;
|
| 9 | import com.google.gson.JsonElement;
|
| 10 | import com.google.gson.JsonObject;
|
| 11 | import com.google.gson.JsonParseException;
|
| 12 | import java.io.Reader;
|
| 13 | import java.io.StringReader;
|
| 14 | import java.lang.reflect.Type;
|
| 15 | import java.util.Collections;
|
| 16 | import java.util.List;
|
| 17 | import java.util.Map;
|
| 18 | import java.util.Map.Entry;
|
| 19 | import net.minecraft.util.JsonUtils;
|
| 20 | import net.minecraft.util.ResourceLocation;
|
| 21 | import org.apache.commons.lang3.StringUtils;
|
| 22 | import org.apache.logging.log4j.LogManager;
|
| 23 | import org.apache.logging.log4j.Logger;
|
| 24 |
|
| 25 | public class ModelBlock
|
| 26 | {
|
| 27 | private static final Logger LOGGER = LogManager.getLogger();
|
| 28 | static final Gson SERIALIZER = (new GsonBuilder()).registerTypeAdapter(ModelBlock.class, new ModelBlock.Deserializer()).registerTypeAdapter(BlockPart.class, new BlockPart.Deserializer()).registerTypeAdapter(BlockPartFace.class, new BlockPartFace.Deserializer()).registerTypeAdapter(BlockFaceUV.class, new BlockFaceUV.Deserializer()).registerTypeAdapter(ItemTransformVec3f.class, new ItemTransformVec3f.Deserializer()).registerTypeAdapter(ItemCameraTransforms.class, new ItemCameraTransforms.Deserializer()).create();
|
| 29 | private final List<BlockPart> elements;
|
| 30 | private final boolean gui3d;
|
| 31 | private final boolean ambientOcclusion;
|
| 32 | private ItemCameraTransforms cameraTransforms;
|
| 33 | public String name;
|
| 34 | protected final Map<String, String> textures;
|
| 35 | protected ModelBlock parent;
|
| 36 | protected ResourceLocation parentLocation;
|
| 37 |
|
| 38 | public static ModelBlock deserialize(Reader readerIn)
|
| 39 | {
|
| 40 | return (ModelBlock)SERIALIZER.fromJson(readerIn, ModelBlock.class);
|
| 41 | }
|
| 42 |
|
| 43 | public static ModelBlock deserialize(String jsonString)
|
| 44 | {
|
| 45 | return deserialize(new StringReader(jsonString));
|
| 46 | }
|
| 47 |
|
| 48 | protected ModelBlock(List<BlockPart> elementsIn, Map<String, String> texturesIn, boolean ambientOcclusionIn, boolean gui3dIn, ItemCameraTransforms cameraTransformsIn)
|
| 49 | {
|
| 50 | this((ResourceLocation)null, elementsIn, texturesIn, ambientOcclusionIn, gui3dIn, cameraTransformsIn);
|
| 51 | }
|
| 52 |
|
| 53 | protected ModelBlock(ResourceLocation parentLocationIn, Map<String, String> texturesIn, boolean ambientOcclusionIn, boolean gui3dIn, ItemCameraTransforms cameraTransformsIn)
|
| 54 | {
|
| 55 | this(parentLocationIn, Collections.<BlockPart>emptyList(), texturesIn, ambientOcclusionIn, gui3dIn, cameraTransformsIn);
|
| 56 | }
|
| 57 |
|
| 58 | private ModelBlock(ResourceLocation parentLocationIn, List<BlockPart> elementsIn, Map<String, String> texturesIn, boolean ambientOcclusionIn, boolean gui3dIn, ItemCameraTransforms cameraTransformsIn)
|
| 59 | {
|
| 60 | this.name = "";
|
| 61 | this.elements = elementsIn;
|
| 62 | this.ambientOcclusion = ambientOcclusionIn;
|
| 63 | this.gui3d = gui3dIn;
|
| 64 | this.textures = texturesIn;
|
| 65 | this.parentLocation = parentLocationIn;
|
| 66 | this.cameraTransforms = cameraTransformsIn;
|
| 67 | }
|
| 68 |
|
| 69 | public List<BlockPart> getElements()
|
| 70 | {
|
| 71 | return this.hasParent() ? this.parent.getElements() : this.elements;
|
| 72 | }
|
| 73 |
|
| 74 | private boolean hasParent()
|
| 75 | {
|
| 76 | return this.parent != null;
|
| 77 | }
|
| 78 |
|
| 79 | public boolean isAmbientOcclusion()
|
| 80 | {
|
| 81 | return this.hasParent() ? this.parent.isAmbientOcclusion() : this.ambientOcclusion;
|
| 82 | }
|
| 83 |
|
| 84 | public boolean isGui3d()
|
| 85 | {
|
| 86 | return this.gui3d;
|
| 87 | }
|
| 88 |
|
| 89 | public boolean isResolved()
|
| 90 | {
|
| 91 | return this.parentLocation == null || this.parent != null && this.parent.isResolved();
|
| 92 | }
|
| 93 |
|
| 94 | public void getParentFromMap(Map<ResourceLocation, ModelBlock> p_178299_1_)
|
| 95 | {
|
| 96 | if (this.parentLocation != null)
|
| 97 | {
|
| 98 | this.parent = (ModelBlock)p_178299_1_.get(this.parentLocation);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | public boolean isTexturePresent(String textureName)
|
| 103 | {
|
| 104 | return !"missingno".equals(this.resolveTextureName(textureName));
|
| 105 | }
|
| 106 |
|
| 107 | public String resolveTextureName(String textureName)
|
| 108 | {
|
| 109 | if (!this.startsWithHash(textureName))
|
| 110 | {
|
| 111 | textureName = '#' + textureName;
|
| 112 | }
|
| 113 |
|
| 114 | return this.resolveTextureName(textureName, new ModelBlock.Bookkeep(this));
|
| 115 | }
|
| 116 |
|
| 117 | private String resolveTextureName(String textureName, ModelBlock.Bookkeep p_178302_2_)
|
| 118 | {
|
| 119 | if (this.startsWithHash(textureName))
|
| 120 | {
|
| 121 | if (this == p_178302_2_.modelExt)
|
| 122 | {
|
| 123 | LOGGER.warn("Unable to resolve texture due to upward reference: " + textureName + " in " + this.name);
|
| 124 | return "missingno";
|
| 125 | }
|
| 126 | else
|
| 127 | {
|
| 128 | String s = (String)this.textures.get(textureName.substring(1));
|
| 129 |
|
| 130 | if (s == null && this.hasParent())
|
| 131 | {
|
| 132 | s = this.parent.resolveTextureName(textureName, p_178302_2_);
|
| 133 | }
|
| 134 |
|
| 135 | p_178302_2_.modelExt = this;
|
| 136 |
|
| 137 | if (s != null && this.startsWithHash(s))
|
| 138 | {
|
| 139 | s = p_178302_2_.model.resolveTextureName(s, p_178302_2_);
|
| 140 | }
|
| 141 |
|
| 142 | return s != null && !this.startsWithHash(s) ? s : "missingno";
|
| 143 | }
|
| 144 | }
|
| 145 | else
|
| 146 | {
|
| 147 | return textureName;
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | private boolean startsWithHash(String hash)
|
| 152 | {
|
| 153 | return hash.charAt(0) == 35;
|
| 154 | }
|
| 155 |
|
| 156 | public ResourceLocation getParentLocation()
|
| 157 | {
|
| 158 | return this.parentLocation;
|
| 159 | }
|
| 160 |
|
| 161 | public ModelBlock getRootModel()
|
| 162 | {
|
| 163 | return this.hasParent() ? this.parent.getRootModel() : this;
|
| 164 | }
|
| 165 |
|
| 166 | public ItemCameraTransforms getAllTransforms()
|
| 167 | {
|
| 168 | ItemTransformVec3f itemtransformvec3f = this.getTransform(ItemCameraTransforms.TransformType.THIRD_PERSON);
|
| 169 | ItemTransformVec3f itemtransformvec3f1 = this.getTransform(ItemCameraTransforms.TransformType.FIRST_PERSON);
|
| 170 | ItemTransformVec3f itemtransformvec3f2 = this.getTransform(ItemCameraTransforms.TransformType.HEAD);
|
| 171 | ItemTransformVec3f itemtransformvec3f3 = this.getTransform(ItemCameraTransforms.TransformType.GUI);
|
| 172 | ItemTransformVec3f itemtransformvec3f4 = this.getTransform(ItemCameraTransforms.TransformType.GROUND);
|
| 173 | ItemTransformVec3f itemtransformvec3f5 = this.getTransform(ItemCameraTransforms.TransformType.FIXED);
|
| 174 | return new ItemCameraTransforms(itemtransformvec3f, itemtransformvec3f1, itemtransformvec3f2, itemtransformvec3f3, itemtransformvec3f4, itemtransformvec3f5);
|
| 175 | }
|
| 176 |
|
| 177 | private ItemTransformVec3f getTransform(ItemCameraTransforms.TransformType type)
|
| 178 | {
|
| 179 | return this.parent != null && !this.cameraTransforms.func_181687_c(type) ? this.parent.getTransform(type) : this.cameraTransforms.getTransform(type);
|
| 180 | }
|
| 181 |
|
| 182 | public static void checkModelHierarchy(Map<ResourceLocation, ModelBlock> p_178312_0_)
|
| 183 | {
|
| 184 | for (ModelBlock modelblock : p_178312_0_.values())
|
| 185 | {
|
| 186 | try
|
| 187 | {
|
| 188 | ModelBlock modelblock1 = modelblock.parent;
|
| 189 |
|
| 190 | for (ModelBlock modelblock2 = modelblock1.parent; modelblock1 != modelblock2; modelblock2 = modelblock2.parent.parent)
|
| 191 | {
|
| 192 | modelblock1 = modelblock1.parent;
|
| 193 | }
|
| 194 |
|
| 195 | throw new ModelBlock.LoopException();
|
| 196 | }
|
| 197 | catch (NullPointerException var5)
|
| 198 | {
|
| 199 | ;
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | static final class Bookkeep
|
| 205 | {
|
| 206 | public final ModelBlock model;
|
| 207 | public ModelBlock modelExt;
|
| 208 |
|
| 209 | private Bookkeep(ModelBlock p_i46223_1_)
|
| 210 | {
|
| 211 | this.model = p_i46223_1_;
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | public static class Deserializer implements JsonDeserializer<ModelBlock>
|
| 216 | {
|
| 217 | public ModelBlock deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
|
| 218 | {
|
| 219 | JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
|
| 220 | List<BlockPart> list = this.getModelElements(p_deserialize_3_, jsonobject);
|
| 221 | String s = this.getParent(jsonobject);
|
| 222 | boolean flag = StringUtils.isEmpty(s);
|
| 223 | boolean flag1 = list.isEmpty();
|
| 224 |
|
| 225 | if (flag1 && flag)
|
| 226 | {
|
| 227 | throw new JsonParseException("BlockModel requires either elements or parent, found neither");
|
| 228 | }
|
| 229 | else if (!flag && !flag1)
|
| 230 | {
|
| 231 | throw new JsonParseException("BlockModel requires either elements or parent, found both");
|
| 232 | }
|
| 233 | else
|
| 234 | {
|
| 235 | Map<String, String> map = this.getTextures(jsonobject);
|
| 236 | boolean flag2 = this.getAmbientOcclusionEnabled(jsonobject);
|
| 237 | ItemCameraTransforms itemcameratransforms = ItemCameraTransforms.DEFAULT;
|
| 238 |
|
| 239 | if (jsonobject.has("display"))
|
| 240 | {
|
| 241 | JsonObject jsonobject1 = JsonUtils.getJsonObject(jsonobject, "display");
|
| 242 | itemcameratransforms = (ItemCameraTransforms)p_deserialize_3_.deserialize(jsonobject1, ItemCameraTransforms.class);
|
| 243 | }
|
| 244 |
|
| 245 | return flag1 ? new ModelBlock(new ResourceLocation(s), map, flag2, true, itemcameratransforms) : new ModelBlock(list, map, flag2, true, itemcameratransforms);
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | private Map<String, String> getTextures(JsonObject p_178329_1_)
|
| 250 | {
|
| 251 | Map<String, String> map = Maps.<String, String>newHashMap();
|
| 252 |
|
| 253 | if (p_178329_1_.has("textures"))
|
| 254 | {
|
| 255 | JsonObject jsonobject = p_178329_1_.getAsJsonObject("textures");
|
| 256 |
|
| 257 | for (Entry<String, JsonElement> entry : jsonobject.entrySet())
|
| 258 | {
|
| 259 | map.put(entry.getKey(), ((JsonElement)entry.getValue()).getAsString());
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | return map;
|
| 264 | }
|
| 265 |
|
| 266 | private String getParent(JsonObject p_178326_1_)
|
| 267 | {
|
| 268 | return JsonUtils.getString(p_178326_1_, "parent", "");
|
| 269 | }
|
| 270 |
|
| 271 | protected boolean getAmbientOcclusionEnabled(JsonObject p_178328_1_)
|
| 272 | {
|
| 273 | return JsonUtils.getBoolean(p_178328_1_, "ambientocclusion", true);
|
| 274 | }
|
| 275 |
|
| 276 | protected List<BlockPart> getModelElements(JsonDeserializationContext p_178325_1_, JsonObject p_178325_2_)
|
| 277 | {
|
| 278 | List<BlockPart> list = Lists.<BlockPart>newArrayList();
|
| 279 |
|
| 280 | if (p_178325_2_.has("elements"))
|
| 281 | {
|
| 282 | for (JsonElement jsonelement : JsonUtils.getJsonArray(p_178325_2_, "elements"))
|
| 283 | {
|
| 284 | list.add((BlockPart)p_178325_1_.deserialize(jsonelement, BlockPart.class));
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | return list;
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | public static class LoopException extends RuntimeException
|
| 293 | {
|
| 294 | }
|
| 295 | }
|
| 296 |
|