/
SoundsConverter.java
85 lines (69 loc) · 3.77 KB
/
SoundsConverter.java
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
package net.hypixel.resourcepack.impl;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
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.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
public class SoundsConverter extends Converter {
public SoundsConverter(PackConverter packConverter) {
super(packConverter);
}
@Override
public void convert(Pack pack) throws IOException {
Path soundsJsonPath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "sounds.json");
if (!soundsJsonPath.toFile().exists()) return;
JsonObject sounds = Util.readJson(packConverter.getGson(), soundsJsonPath);
JsonObject newSoundsObject = new JsonObject();
for (Map.Entry<String, JsonElement> entry : sounds.entrySet()) {
if (entry.getValue().isJsonObject()) {
JsonObject soundObject = entry.getValue().getAsJsonObject();
if (soundObject.has("sounds") && soundObject.get("sounds").isJsonArray()) {
JsonArray soundsArray = soundObject.getAsJsonArray("sounds");
JsonArray newSoundsArray = new JsonArray();
for (JsonElement jsonElement : soundsArray) {
String sound;
if (jsonElement instanceof JsonObject) {
sound = ((JsonObject) jsonElement).get("name").getAsString();
} else if (jsonElement instanceof JsonPrimitive) {
sound = jsonElement.getAsString();
} else {
throw new IllegalArgumentException("Unknown element type: " + jsonElement.getClass().getSimpleName());
}
Path baseSoundsPath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "sounds");
Path path = baseSoundsPath.resolve(sound + ".ogg");
if (!Util.fileExistsCorrectCasing(path)) {
String rewrite = path.toFile().getCanonicalPath().substring(baseSoundsPath.toString().length() + 1, path.toFile().getCanonicalPath().length() - 4);
if (PackConverter.DEBUG) System.out.println(" Rewriting Sound: '" + jsonElement.getAsString() + "' -> '" + rewrite + "'");
sound = rewrite;
} else {
sound = jsonElement.getAsString();
}
// windows fix
sound = sound.replaceAll("\\\\", "/");
JsonElement newSound = null;
if (jsonElement instanceof JsonObject) {
((JsonObject) jsonElement).addProperty("name", sound);
newSound = jsonElement;
} else if (jsonElement instanceof JsonPrimitive) {
newSound = new JsonPrimitive(jsonElement.getAsString());
}
newSoundsArray.add(newSound);
}
soundObject.add("sounds", newSoundsArray);
}
newSoundsObject.add(entry.getKey().toLowerCase(), soundObject);
}
}
Files.write(soundsJsonPath, Collections.singleton(packConverter.getGson().toJson(newSoundsObject)), Charset.forName("UTF-8"));
}
}