-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathModelConverter.java
More file actions
72 lines (62 loc) · 3.18 KB
/
ModelConverter.java
File metadata and controls
72 lines (62 loc) · 3.18 KB
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
package net.hypixel.resourcepack.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.hypixel.resourcepack.Converter;
import net.hypixel.resourcepack.PackConverter;
import net.hypixel.resourcepack.Util;
import net.hypixel.resourcepack.pack.Pack;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
public class ModelConverter extends Converter {
public ModelConverter(PackConverter packConverter) {
super(packConverter);
}
@Override
public void convert(Pack pack) throws IOException {
Path models = pack.getMinecraftPath().resolve("models");
remapModelJson(models.resolve("block"));
remapModelJson(models.resolve("item"));
}
protected void remapModelJson(Path path) throws IOException {
if (!Files.exists(path)) {
return;
}
Files.list(path)
.filter(path1 -> path1.toString().endsWith(".json"))
.forEach(model -> {
try {
JsonObject jsonObject = Util.readJson(packConverter.getGson(), model);
// minify the JSON, so we can replace spaces in paths easily
// TODO Improvement: handle this in a cleaner way?
String content = jsonObject.toString();
content = content.replaceAll("items/", "item/");
content = content.replaceAll("blocks/", "block/");
content = content.replaceAll(" ", "_");
Files.write(model, content.getBytes(StandardCharsets.UTF_8));
// handle the remapping of textures, for models that use default texture names
jsonObject = Util.readJson(packConverter.getGson(), model);
if (jsonObject.has("textures")) {
NameConverter nameConverter = packConverter.getConverter(NameConverter.class);
JsonObject textureObject = jsonObject.getAsJsonObject("textures");
for (Map.Entry<String, JsonElement> entry : textureObject.entrySet()) {
String value = entry.getValue().getAsString();
if (value.startsWith("block/")) {
textureObject.addProperty(entry.getKey(), "block/" + nameConverter.getBlockMapping().remap(value.substring("block/".length())));
} else if (value.startsWith("item/")) {
textureObject.addProperty(entry.getKey(), "item/" + nameConverter.getItemMapping().remap(value.substring("item/".length())));
}
}
}
Files.write(model, packConverter.getGson()
.toJson(jsonObject)
.getBytes(StandardCharsets.UTF_8)
);
} catch (IOException e) {
throw Util.propagate(e);
}
});
}
}