Skip to content

Commit

Permalink
Handle sounds that are JsonObjects. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Plancke committed Sep 16, 2018
1 parent 242db6b commit 9646d4b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/net/hypixel/resourcepack/impl/SoundsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -40,8 +41,16 @@ public void convert(Pack pack) throws IOException {
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(jsonElement.getAsString() + ".ogg");
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 + "'");
Expand All @@ -52,7 +61,16 @@ public void convert(Pack pack) throws IOException {

// windows fix
sound = sound.replaceAll("\\\\", "/");
newSoundsArray.add(sound);

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);
}
Expand Down

0 comments on commit 9646d4b

Please sign in to comment.