1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/*
* Copyright (C) 2022 NotEnoughUpdates contributors
*
* This file is part of NotEnoughUpdates.
*
* NotEnoughUpdates is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* NotEnoughUpdates is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github.moulberry.notenoughupdates.miscfeatures;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.ModelHumanoidHead;
import net.minecraft.client.model.ModelSkeletonHead;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.BlockPart;
import net.minecraft.client.renderer.block.model.BlockPartFace;
import net.minecraft.client.renderer.block.model.FaceBakery;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.block.model.ModelBlock;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.IIconCreator;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.client.resources.model.IBakedModel;
import net.minecraft.client.resources.model.ModelRotation;
import net.minecraft.client.resources.model.SimpleBakedModel;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CustomSkulls implements IResourceManagerReloadListener {
private static final CustomSkulls INSTANCE = new CustomSkulls();
public static CustomSkulls getInstance() {
return INSTANCE;
}
private final ResourceLocation atlas = new ResourceLocation("notenoughupdates:custom_skull_textures_atlas");
private final ResourceLocation configuration = new ResourceLocation(
"notenoughupdates:custom_skull_textures/customskull.json");
protected final TextureMap textureMap = new TextureMap("custom_skull_textures");
public static ItemCameraTransforms.TransformType mostRecentTransformType = ItemCameraTransforms.TransformType.NONE;
protected final Map<ResourceLocation, TextureAtlasSprite> sprites = Maps.newHashMap();
private final FaceBakery faceBakery = new FaceBakery();
private final ModelSkeletonHead humanoidHead = new ModelHumanoidHead();
private final HashMap<String, CustomSkull> customSkulls = new HashMap<>();
private final Gson gson = new GsonBuilder().create();
private static class CustomSkull {
private ModelBlock model;
private IBakedModel modelBaked;
private ResourceLocation texture;
}
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
customSkulls.clear();
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(
Minecraft.getMinecraft().getResourceManager().getResource(configuration).getInputStream(),
StandardCharsets.UTF_8
))
) {
JsonObject json = gson.fromJson(reader, JsonObject.class);
if (json == null) return;
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
if (entry.getValue().isJsonObject()) {
JsonObject obj = entry.getValue().getAsJsonObject();
if (obj.has("model")) {
String location = obj.get("model").getAsString();
ResourceLocation loc = new ResourceLocation("notenoughupdates:custom_skull_textures/" + location + ".json");
CustomSkull skull = new CustomSkull();
skull.model = ModelBlock.deserialize(new InputStreamReader(Minecraft
.getMinecraft()
.getResourceManager()
.getResource(loc)
.getInputStream()));
customSkulls.put(entry.getKey(), skull);
} else if (obj.has("texture")) {
String location = obj.get("texture").getAsString();
ResourceLocation loc = new ResourceLocation("notenoughupdates:custom_skull_textures/" + location + ".png");
CustomSkull skull = new CustomSkull();
skull.texture = loc;
Minecraft.getMinecraft().getTextureManager().deleteTexture(skull.texture);
customSkulls.put(entry.getKey(), skull);
}
}
}
loadSprites();
for (CustomSkull skull : customSkulls.values()) {
if (skull.model != null) {
skull.modelBaked = bakeModel(skull.model, ModelRotation.X0_Y0, false);
}
}
Minecraft.getMinecraft().getTextureManager().loadTexture(atlas, textureMap);
} catch (Exception ignored) {
}
}
private void loadSprites() {
final Set<ResourceLocation> set = this.getAllTextureLocations();
set.remove(TextureMap.LOCATION_MISSING_TEXTURE);
IIconCreator iiconcreator = iconRegistry -> {
for (ResourceLocation resourcelocation : set) {
TextureAtlasSprite textureatlassprite = iconRegistry.registerSprite(resourcelocation);
CustomSkulls.this.sprites.put(resourcelocation, textureatlassprite);
}
};
this.textureMap.loadSprites(Minecraft.getMinecraft().getResourceManager(), iiconcreator);
this.sprites.put(new ResourceLocation("missingno"), this.textureMap.getMissingSprite());
}
protected Set<ResourceLocation> getAllTextureLocations() {
Set<ResourceLocation> set = new HashSet<>();
for (CustomSkull skull : customSkulls.values()) {
if (skull.model != null) {
set.addAll(getTextureLocations(skull.model));
}
}
return set;
}
protected Set<ResourceLocation> getTextureLocations(ModelBlock modelBlock) {
Set<ResourceLocation> set = Sets.newHashSet();
for (BlockPart blockpart : modelBlock.getElements()) {
for (BlockPartFace blockpartface : blockpart.mapFaces.values()) {
ResourceLocation resourcelocation = new ResourceLocation(
"notenoughupdates",
modelBlock.resolveTextureName(blockpartface.texture)
);
set.add(resourcelocation);
}
}
set.add(new ResourceLocation("notenoughupdates", modelBlock.resolveTextureName("particle")));
return set;
}
protected IBakedModel bakeModel(
ModelBlock modelBlockIn,
net.minecraftforge.client.model.ITransformation modelRotationIn,
boolean uvLocked
) {
TextureAtlasSprite textureatlassprite = this.sprites.get(new ResourceLocation(
"notenoughupdates",
modelBlockIn.resolveTextureName("particle")
));
SimpleBakedModel.Builder simplebakedmodel$builder = (new SimpleBakedModel.Builder(modelBlockIn)).setTexture(
textureatlassprite);
for (BlockPart blockpart : modelBlockIn.getElements()) {
for (EnumFacing enumfacing : blockpart.mapFaces.keySet()) {
BlockPartFace blockpartface = blockpart.mapFaces.get(enumfacing);
TextureAtlasSprite textureatlassprite1 = this.sprites.get(new ResourceLocation(
"notenoughupdates",
modelBlockIn.resolveTextureName(blockpartface.texture)
));
if (blockpartface.cullFace == null || !net.minecraftforge.client.model.TRSRTransformation.isInteger(
modelRotationIn.getMatrix())) {
simplebakedmodel$builder.addGeneralQuad(this.makeBakedQuad(
blockpart,
blockpartface,
textureatlassprite1,
enumfacing,
modelRotationIn,
uvLocked
));
} else {
simplebakedmodel$builder.addFaceQuad(
modelRotationIn.rotate(blockpartface.cullFace),
this.makeBakedQuad(blockpart
|