-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBlockStateConverter.java
More file actions
84 lines (75 loc) · 3.68 KB
/
BlockStateConverter.java
File metadata and controls
84 lines (75 loc) · 3.68 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
73
74
75
76
77
78
79
80
81
82
83
84
package net.hypixel.resourcepack.impl;
import com.google.gson.JsonArray;
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.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
public class BlockStateConverter extends Converter {
public BlockStateConverter(PackConverter packConverter) {
super(packConverter);
}
@Override
public void convert(Pack pack) throws IOException {
Path states = pack.getMinecraftPath()
.resolve("blockstates");
if (!Files.exists(states)) {
return;
}
Files.list(states)
.filter(file -> file.toString().endsWith(".json"))
.forEach(file -> {
try {
JsonObject json = Util.readJson(packConverter.getGson(), file);
boolean anyChanges = false;
JsonObject variantsObject = json.getAsJsonObject("variants");
if (variantsObject != null) {
// change "normal" key to ""
JsonElement normal = variantsObject.get("normal");
if (normal instanceof JsonObject || normal instanceof JsonArray) {
variantsObject.add("", normal);
variantsObject.remove("normal");
anyChanges = true;
}
// update model paths to prepend block
for (Map.Entry<String, JsonElement> entry : variantsObject.entrySet()) {
if (entry.getValue() instanceof JsonObject) {
JsonObject value = (JsonObject) entry.getValue();
if (value.has("model")) {
value.addProperty("model", "block/" + value.get("model").getAsString());
anyChanges = true;
}
} else if (entry.getValue() instanceof JsonArray) { // some states have arrays
for (JsonElement jsonElement : ((JsonArray) entry.getValue())) {
if (jsonElement instanceof JsonObject) {
JsonObject value = (JsonObject) jsonElement;
if (value.has("model")) {
value.addProperty("model", "block/" + value.get("model").getAsString());
anyChanges = true;
}
}
}
}
}
}
if (anyChanges) {
Files.write(file, packConverter.getGson().toJson(json).getBytes(StandardCharsets.UTF_8));
if (PackConverter.DEBUG) {
System.out.println(" Converted " + file.getFileName());
}
}
} catch (IOException e) {
Util.propagate(e);
}
});
}
}