| 1 | package net.minecraft.client.renderer.block.model;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.google.gson.JsonArray;
|
| 5 | import com.google.gson.JsonDeserializationContext;
|
| 6 | import com.google.gson.JsonDeserializer;
|
| 7 | import com.google.gson.JsonElement;
|
| 8 | import com.google.gson.JsonObject;
|
| 9 | import com.google.gson.JsonParseException;
|
| 10 | import java.lang.reflect.Type;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Map.Entry;
|
| 13 | import net.minecraft.util.EnumFacing;
|
| 14 | import net.minecraft.util.JsonUtils;
|
| 15 | import net.minecraft.util.MathHelper;
|
| 16 | import org.lwjgl.util.vector.Vector3f;
|
| 17 |
|
| 18 | public class BlockPart
|
| 19 | {
|
| 20 | public final Vector3f positionFrom;
|
| 21 | public final Vector3f positionTo;
|
| 22 | public final Map<EnumFacing, BlockPartFace> mapFaces;
|
| 23 | public final BlockPartRotation partRotation;
|
| 24 | public final boolean shade;
|
| 25 |
|
| 26 | public BlockPart(Vector3f positionFromIn, Vector3f positionToIn, Map<EnumFacing, BlockPartFace> mapFacesIn, BlockPartRotation partRotationIn, boolean shadeIn)
|
| 27 | {
|
| 28 | this.positionFrom = positionFromIn;
|
| 29 | this.positionTo = positionToIn;
|
| 30 | this.mapFaces = mapFacesIn;
|
| 31 | this.partRotation = partRotationIn;
|
| 32 | this.shade = shadeIn;
|
| 33 | this.setDefaultUvs();
|
| 34 | }
|
| 35 |
|
| 36 | private void setDefaultUvs()
|
| 37 | {
|
| 38 | for (Entry<EnumFacing, BlockPartFace> entry : this.mapFaces.entrySet())
|
| 39 | {
|
| 40 | float[] afloat = this.getFaceUvs((EnumFacing)entry.getKey());
|
| 41 | ((BlockPartFace)entry.getValue()).blockFaceUV.setUvs(afloat);
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | private float[] getFaceUvs(EnumFacing p_178236_1_)
|
| 46 | {
|
| 47 | float[] afloat;
|
| 48 |
|
| 49 | switch (p_178236_1_)
|
| 50 | {
|
| 51 | case DOWN:
|
| 52 | case UP:
|
| 53 | afloat = new float[] {this.positionFrom.x, this.positionFrom.z, this.positionTo.x, this.positionTo.z};
|
| 54 | break;
|
| 55 | case NORTH:
|
| 56 | case SOUTH:
|
| 57 | afloat = new float[] {this.positionFrom.x, 16.0F - this.positionTo.y, this.positionTo.x, 16.0F - this.positionFrom.y};
|
| 58 | break;
|
| 59 | case WEST:
|
| 60 | case EAST:
|
| 61 | afloat = new float[] {this.positionFrom.z, 16.0F - this.positionTo.y, this.positionTo.z, 16.0F - this.positionFrom.y};
|
| 62 | break;
|
| 63 | default:
|
| 64 | throw new NullPointerException();
|
| 65 | }
|
| 66 |
|
| 67 | return afloat;
|
| 68 | }
|
| 69 |
|
| 70 | static class Deserializer implements JsonDeserializer<BlockPart>
|
| 71 | {
|
| 72 | public BlockPart deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
|
| 73 | {
|
| 74 | JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
|
| 75 | Vector3f vector3f = this.parsePositionFrom(jsonobject);
|
| 76 | Vector3f vector3f1 = this.parsePositionTo(jsonobject);
|
| 77 | BlockPartRotation blockpartrotation = this.parseRotation(jsonobject);
|
| 78 | Map<EnumFacing, BlockPartFace> map = this.parseFacesCheck(p_deserialize_3_, jsonobject);
|
| 79 |
|
| 80 | if (jsonobject.has("shade") && !JsonUtils.isBoolean(jsonobject, "shade"))
|
| 81 | {
|
| 82 | throw new JsonParseException("Expected shade to be a Boolean");
|
| 83 | }
|
| 84 | else
|
| 85 | {
|
| 86 | boolean flag = JsonUtils.getBoolean(jsonobject, "shade", true);
|
| 87 | return new BlockPart(vector3f, vector3f1, map, blockpartrotation, flag);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private BlockPartRotation parseRotation(JsonObject p_178256_1_)
|
| 92 | {
|
| 93 | BlockPartRotation blockpartrotation = null;
|
| 94 |
|
| 95 | if (p_178256_1_.has("rotation"))
|
| 96 | {
|
| 97 | JsonObject jsonobject = JsonUtils.getJsonObject(p_178256_1_, "rotation");
|
| 98 | Vector3f vector3f = this.parsePosition(jsonobject, "origin");
|
| 99 | vector3f.scale(0.0625F);
|
| 100 | EnumFacing.Axis enumfacing$axis = this.parseAxis(jsonobject);
|
| 101 | float f = this.parseAngle(jsonobject);
|
| 102 | boolean flag = JsonUtils.getBoolean(jsonobject, "rescale", false);
|
| 103 | blockpartrotation = new BlockPartRotation(vector3f, enumfacing$axis, f, flag);
|
| 104 | }
|
| 105 |
|
| 106 | return blockpartrotation;
|
| 107 | }
|
| 108 |
|
| 109 | private float parseAngle(JsonObject p_178255_1_)
|
| 110 | {
|
| 111 | float f = JsonUtils.getFloat(p_178255_1_, "angle");
|
| 112 |
|
| 113 | if (f != 0.0F && MathHelper.abs(f) != 22.5F && MathHelper.abs(f) != 45.0F)
|
| 114 | {
|
| 115 | throw new JsonParseException("Invalid rotation " + f + " found, only -45/-22.5/0/22.5/45 allowed");
|
| 116 | }
|
| 117 | else
|
| 118 | {
|
| 119 | return f;
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | private EnumFacing.Axis parseAxis(JsonObject p_178252_1_)
|
| 124 | {
|
| 125 | String s = JsonUtils.getString(p_178252_1_, "axis");
|
| 126 | EnumFacing.Axis enumfacing$axis = EnumFacing.Axis.byName(s.toLowerCase());
|
| 127 |
|
| 128 | if (enumfacing$axis == null)
|
| 129 | {
|
| 130 | throw new JsonParseException("Invalid rotation axis: " + s);
|
| 131 | }
|
| 132 | else
|
| 133 | {
|
| 134 | return enumfacing$axis;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | private Map<EnumFacing, BlockPartFace> parseFacesCheck(JsonDeserializationContext p_178250_1_, JsonObject p_178250_2_)
|
| 139 | {
|
| 140 | Map<EnumFacing, BlockPartFace> map = this.parseFaces(p_178250_1_, p_178250_2_);
|
| 141 |
|
| 142 | if (map.isEmpty())
|
| 143 | {
|
| 144 | throw new JsonParseException("Expected between 1 and 6 unique faces, got 0");
|
| 145 | }
|
| 146 | else
|
| 147 | {
|
| 148 | return map;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | private Map<EnumFacing, BlockPartFace> parseFaces(JsonDeserializationContext p_178253_1_, JsonObject p_178253_2_)
|
| 153 | {
|
| 154 | Map<EnumFacing, BlockPartFace> map = Maps.newEnumMap(EnumFacing.class);
|
| 155 | JsonObject jsonobject = JsonUtils.getJsonObject(p_178253_2_, "faces");
|
| 156 |
|
| 157 | for (Entry<String, JsonElement> entry : jsonobject.entrySet())
|
| 158 | {
|
| 159 | EnumFacing enumfacing = this.parseEnumFacing((String)entry.getKey());
|
| 160 | map.put(enumfacing, (BlockPartFace)p_178253_1_.deserialize((JsonElement)entry.getValue(), BlockPartFace.class));
|
| 161 | }
|
| 162 |
|
| 163 | return map;
|
| 164 | }
|
| 165 |
|
| 166 | private EnumFacing parseEnumFacing(String name)
|
| 167 | {
|
| 168 | EnumFacing enumfacing = EnumFacing.byName(name);
|
| 169 |
|
| 170 | if (enumfacing == null)
|
| 171 | {
|
| 172 | throw new JsonParseException("Unknown facing: " + name);
|
| 173 | }
|
| 174 | else
|
| 175 | {
|
| 176 | return enumfacing;
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | private Vector3f parsePositionTo(JsonObject p_178247_1_)
|
| 181 | {
|
| 182 | Vector3f vector3f = this.parsePosition(p_178247_1_, "to");
|
| 183 |
|
| 184 | if (vector3f.x >= -16.0F && vector3f.y >= -16.0F && vector3f.z >= -16.0F && vector3f.x <= 32.0F && vector3f.y <= 32.0F && vector3f.z <= 32.0F)
|
| 185 | {
|
| 186 | return vector3f;
|
| 187 | }
|
| 188 | else
|
| 189 | {
|
| 190 | throw new JsonParseException("\'to\' specifier exceeds the allowed boundaries: " + vector3f);
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | private Vector3f parsePositionFrom(JsonObject p_178249_1_)
|
| 195 | {
|
| 196 | Vector3f vector3f = this.parsePosition(p_178249_1_, "from");
|
| 197 |
|
| 198 | if (vector3f.x >= -16.0F && vector3f.y >= -16.0F && vector3f.z >= -16.0F && vector3f.x <= 32.0F && vector3f.y <= 32.0F && vector3f.z <= 32.0F)
|
| 199 | {
|
| 200 | return vector3f;
|
| 201 | }
|
| 202 | else
|
| 203 | {
|
| 204 | throw new JsonParseException("\'from\' specifier exceeds the allowed boundaries: " + vector3f);
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | private Vector3f parsePosition(JsonObject p_178251_1_, String p_178251_2_)
|
| 209 | {
|
| 210 | JsonArray jsonarray = JsonUtils.getJsonArray(p_178251_1_, p_178251_2_);
|
| 211 |
|
| 212 | if (jsonarray.size() != 3)
|
| 213 | {
|
| 214 | throw new JsonParseException("Expected 3 " + p_178251_2_ + " values, found: " + jsonarray.size());
|
| 215 | }
|
| 216 | else
|
| 217 | {
|
| 218 | float[] afloat = new float[3];
|
| 219 |
|
| 220 | for (int i = 0; i < afloat.length; ++i)
|
| 221 | {
|
| 222 | afloat[i] = JsonUtils.getFloat(jsonarray.get(i), p_178251_2_ + "[" + i + "]");
|
| 223 | }
|
| 224 |
|
| 225 | return new Vector3f(afloat[0], afloat[1], afloat[2]);
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|