| 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.lang.reflect.Type;
|
| 14 | import java.util.Collection;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Map;
|
| 17 | import java.util.Map.Entry;
|
| 18 | import net.minecraft.client.resources.model.ModelRotation;
|
| 19 | import net.minecraft.util.JsonUtils;
|
| 20 | import net.minecraft.util.ResourceLocation;
|
| 21 |
|
| 22 | public class ModelBlockDefinition
|
| 23 | {
|
| 24 | static final Gson GSON = (new GsonBuilder()).registerTypeAdapter(ModelBlockDefinition.class, new ModelBlockDefinition.Deserializer()).registerTypeAdapter(ModelBlockDefinition.Variant.class, new ModelBlockDefinition.Variant.Deserializer()).create();
|
| 25 | private final Map<String, ModelBlockDefinition.Variants> mapVariants = Maps.<String, ModelBlockDefinition.Variants>newHashMap();
|
| 26 |
|
| 27 | public static ModelBlockDefinition parseFromReader(Reader p_178331_0_)
|
| 28 | {
|
| 29 | return (ModelBlockDefinition)GSON.fromJson(p_178331_0_, ModelBlockDefinition.class);
|
| 30 | }
|
| 31 |
|
| 32 | public ModelBlockDefinition(Collection<ModelBlockDefinition.Variants> p_i46221_1_)
|
| 33 | {
|
| 34 | for (ModelBlockDefinition.Variants modelblockdefinition$variants : p_i46221_1_)
|
| 35 | {
|
| 36 | this.mapVariants.put(modelblockdefinition$variants.name, modelblockdefinition$variants);
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public ModelBlockDefinition(List<ModelBlockDefinition> p_i46222_1_)
|
| 41 | {
|
| 42 | for (ModelBlockDefinition modelblockdefinition : p_i46222_1_)
|
| 43 | {
|
| 44 | this.mapVariants.putAll(modelblockdefinition.mapVariants);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public ModelBlockDefinition.Variants getVariants(String p_178330_1_)
|
| 49 | {
|
| 50 | ModelBlockDefinition.Variants modelblockdefinition$variants = (ModelBlockDefinition.Variants)this.mapVariants.get(p_178330_1_);
|
| 51 |
|
| 52 | if (modelblockdefinition$variants == null)
|
| 53 | {
|
| 54 | throw new ModelBlockDefinition.MissingVariantException();
|
| 55 | }
|
| 56 | else
|
| 57 | {
|
| 58 | return modelblockdefinition$variants;
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public boolean equals(Object p_equals_1_)
|
| 63 | {
|
| 64 | if (this == p_equals_1_)
|
| 65 | {
|
| 66 | return true;
|
| 67 | }
|
| 68 | else if (p_equals_1_ instanceof ModelBlockDefinition)
|
| 69 | {
|
| 70 | ModelBlockDefinition modelblockdefinition = (ModelBlockDefinition)p_equals_1_;
|
| 71 | return this.mapVariants.equals(modelblockdefinition.mapVariants);
|
| 72 | }
|
| 73 | else
|
| 74 | {
|
| 75 | return false;
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | public int hashCode()
|
| 80 | {
|
| 81 | return this.mapVariants.hashCode();
|
| 82 | }
|
| 83 |
|
| 84 | public static class Deserializer implements JsonDeserializer<ModelBlockDefinition>
|
| 85 | {
|
| 86 | public ModelBlockDefinition deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
|
| 87 | {
|
| 88 | JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
|
| 89 | List<ModelBlockDefinition.Variants> list = this.parseVariantsList(p_deserialize_3_, jsonobject);
|
| 90 | return new ModelBlockDefinition(list);
|
| 91 | }
|
| 92 |
|
| 93 | protected List<ModelBlockDefinition.Variants> parseVariantsList(JsonDeserializationContext p_178334_1_, JsonObject p_178334_2_)
|
| 94 | {
|
| 95 | JsonObject jsonobject = JsonUtils.getJsonObject(p_178334_2_, "variants");
|
| 96 | List<ModelBlockDefinition.Variants> list = Lists.<ModelBlockDefinition.Variants>newArrayList();
|
| 97 |
|
| 98 | for (Entry<String, JsonElement> entry : jsonobject.entrySet())
|
| 99 | {
|
| 100 | list.add(this.parseVariants(p_178334_1_, entry));
|
| 101 | }
|
| 102 |
|
| 103 | return list;
|
| 104 | }
|
| 105 |
|
| 106 | protected ModelBlockDefinition.Variants parseVariants(JsonDeserializationContext p_178335_1_, Entry<String, JsonElement> p_178335_2_)
|
| 107 | {
|
| 108 | String s = (String)p_178335_2_.getKey();
|
| 109 | List<ModelBlockDefinition.Variant> list = Lists.<ModelBlockDefinition.Variant>newArrayList();
|
| 110 | JsonElement jsonelement = (JsonElement)p_178335_2_.getValue();
|
| 111 |
|
| 112 | if (jsonelement.isJsonArray())
|
| 113 | {
|
| 114 | for (JsonElement jsonelement1 : jsonelement.getAsJsonArray())
|
| 115 | {
|
| 116 | list.add((ModelBlockDefinition.Variant)p_178335_1_.deserialize(jsonelement1, ModelBlockDefinition.Variant.class));
|
| 117 | }
|
| 118 | }
|
| 119 | else
|
| 120 | {
|
| 121 | list.add((ModelBlockDefinition.Variant)p_178335_1_.deserialize(jsonelement, ModelBlockDefinition.Variant.class));
|
| 122 | }
|
| 123 |
|
| 124 | return new ModelBlockDefinition.Variants(s, list);
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | public class MissingVariantException extends RuntimeException
|
| 129 | {
|
| 130 | }
|
| 131 |
|
| 132 | public static class Variant
|
| 133 | {
|
| 134 | private final ResourceLocation modelLocation;
|
| 135 | private final ModelRotation modelRotation;
|
| 136 | private final boolean uvLock;
|
| 137 | private final int weight;
|
| 138 |
|
| 139 | public Variant(ResourceLocation modelLocationIn, ModelRotation modelRotationIn, boolean uvLockIn, int weightIn)
|
| 140 | {
|
| 141 | this.modelLocation = modelLocationIn;
|
| 142 | this.modelRotation = modelRotationIn;
|
| 143 | this.uvLock = uvLockIn;
|
| 144 | this.weight = weightIn;
|
| 145 | }
|
| 146 |
|
| 147 | public ResourceLocation getModelLocation()
|
| 148 | {
|
| 149 | return this.modelLocation;
|
| 150 | }
|
| 151 |
|
| 152 | public ModelRotation getRotation()
|
| 153 | {
|
| 154 | return this.modelRotation;
|
| 155 | }
|
| 156 |
|
| 157 | public boolean isUvLocked()
|
| 158 | {
|
| 159 | return this.uvLock;
|
| 160 | }
|
| 161 |
|
| 162 | public int getWeight()
|
| 163 | {
|
| 164 | return this.weight;
|
| 165 | }
|
| 166 |
|
| 167 | public boolean equals(Object p_equals_1_)
|
| 168 | {
|
| 169 | if (this == p_equals_1_)
|
| 170 | {
|
| 171 | return true;
|
| 172 | }
|
| 173 | else if (!(p_equals_1_ instanceof ModelBlockDefinition.Variant))
|
| 174 | {
|
| 175 | return false;
|
| 176 | }
|
| 177 | else
|
| 178 | {
|
| 179 | ModelBlockDefinition.Variant modelblockdefinition$variant = (ModelBlockDefinition.Variant)p_equals_1_;
|
| 180 | return this.modelLocation.equals(modelblockdefinition$variant.modelLocation) && this.modelRotation == modelblockdefinition$variant.modelRotation && this.uvLock == modelblockdefinition$variant.uvLock;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | public int hashCode()
|
| 185 | {
|
| 186 | int i = this.modelLocation.hashCode();
|
| 187 | i = 31 * i + (this.modelRotation != null ? this.modelRotation.hashCode() : 0);
|
| 188 | i = 31 * i + (this.uvLock ? 1 : 0);
|
| 189 | return i;
|
| 190 | }
|
| 191 |
|
| 192 | public static class Deserializer implements JsonDeserializer<ModelBlockDefinition.Variant>
|
| 193 | {
|
| 194 | public ModelBlockDefinition.Variant deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
|
| 195 | {
|
| 196 | JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
|
| 197 | String s = this.parseModel(jsonobject);
|
| 198 | ModelRotation modelrotation = this.parseRotation(jsonobject);
|
| 199 | boolean flag = this.parseUvLock(jsonobject);
|
| 200 | int i = this.parseWeight(jsonobject);
|
| 201 | return new ModelBlockDefinition.Variant(this.makeModelLocation(s), modelrotation, flag, i);
|
| 202 | }
|
| 203 |
|
| 204 | private ResourceLocation makeModelLocation(String p_178426_1_)
|
| 205 | {
|
| 206 | ResourceLocation resourcelocation = new ResourceLocation(p_178426_1_);
|
| 207 | resourcelocation = new ResourceLocation(resourcelocation.getResourceDomain(), "block/" + resourcelocation.getResourcePath());
|
| 208 | return resourcelocation;
|
| 209 | }
|
| 210 |
|
| 211 | private boolean parseUvLock(JsonObject p_178429_1_)
|
| 212 | {
|
| 213 | return JsonUtils.getBoolean(p_178429_1_, "uvlock", false);
|
| 214 | }
|
| 215 |
|
| 216 | protected ModelRotation parseRotation(JsonObject p_178428_1_)
|
| 217 | {
|
| 218 | int i = JsonUtils.getInt(p_178428_1_, "x", 0);
|
| 219 | int j = JsonUtils.getInt(p_178428_1_, "y", 0);
|
| 220 | ModelRotation modelrotation = ModelRotation.getModelRotation(i, j);
|
| 221 |
|
| 222 | if (modelrotation == null)
|
| 223 | {
|
| 224 | throw new JsonParseException("Invalid BlockModelRotation x: " + i + ", y: " + j);
|
| 225 | }
|
| 226 | else
|
| 227 | {
|
| 228 | return modelrotation;
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | protected String parseModel(JsonObject p_178424_1_)
|
| 233 | {
|
| 234 | return JsonUtils.getString(p_178424_1_, "model");
|
| 235 | }
|
| 236 |
|
| 237 | protected int parseWeight(JsonObject p_178427_1_)
|
| 238 | {
|
| 239 | return JsonUtils.getInt(p_178427_1_, "weight", 1);
|
| 240 | }
|
| 241 | }
|
| 242 | }
|
| 243 |
|
| 244 | public static class Variants
|
| 245 | {
|
| 246 | private final String name;
|
| 247 | private final List<ModelBlockDefinition.Variant> listVariants;
|
| 248 |
|
| 249 | public Variants(String nameIn, List<ModelBlockDefinition.Variant> listVariantsIn)
|
| 250 | {
|
| 251 | this.name = nameIn;
|
| 252 | this.listVariants = listVariantsIn;
|
| 253 | }
|
| 254 |
|
| 255 | public List<ModelBlockDefinition.Variant> getVariants()
|
| 256 | {
|
| 257 | return this.listVariants;
|
| 258 | }
|
| 259 |
|
| 260 | public boolean equals(Object p_equals_1_)
|
| 261 | {
|
| 262 | if (this == p_equals_1_)
|
| 263 | {
|
| 264 | return true;
|
| 265 | }
|
| 266 | else if (!(p_equals_1_ instanceof ModelBlockDefinition.Variants))
|
| 267 | {
|
| 268 | return false;
|
| 269 | }
|
| 270 | else
|
| 271 | {
|
| 272 | ModelBlockDefinition.Variants modelblockdefinition$variants = (ModelBlockDefinition.Variants)p_equals_1_;
|
| 273 | return !this.name.equals(modelblockdefinition$variants.name) ? false : this.listVariants.equals(modelblockdefinition$variants.listVariants);
|
| 274 | }
|
| 275 | }
|
| 276 |
|
| 277 | public int hashCode()
|
| 278 | {
|
| 279 | int i = this.name.hashCode();
|
| 280 | i = 31 * i + this.listVariants.hashCode();
|
| 281 | return i;
|
| 282 | }
|
| 283 | }
|
| 284 | }
|
| 285 |
|