Skip to content

Commit

Permalink
SoundUtils - fix an error with MC 1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed Nov 7, 2024
1 parent ecef9c5 commit 389de5c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/ch/njol/skript/util/SoundUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.njol.skript.util;

import ch.njol.skript.Skript;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.jetbrains.annotations.Nullable;
Expand All @@ -19,7 +20,13 @@ public class SoundUtils {
Class<?> SOUND_CLASS = Class.forName("org.bukkit.Sound");
IS_INTERFACE = SOUND_CLASS.isInterface();
VALUE_OF_METHOD = SOUND_CLASS.getDeclaredMethod("valueOf", String.class);
GET_KEY_METHOD = SOUND_CLASS.getDeclaredMethod("getKey");
if (Skript.methodExists(SOUND_CLASS, "getKey")) {
// I believe this method was added around Bukkit 1.16
// This is only added to make tests not fail when testing MC 1.15 and below
GET_KEY_METHOD = SOUND_CLASS.getDeclaredMethod("getKey");
} else {
GET_KEY_METHOD = null;
}
} catch (NoSuchMethodException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
Expand All @@ -42,7 +49,7 @@ public static NamespacedKey getSoundKeyFromEnum(String soundString) {
return sound.getKey();
} catch (IllegalArgumentException ignore) {
}
} else {
} else if (GET_KEY_METHOD != null) {
try {
Object sound = VALUE_OF_METHOD.invoke(null, soundString);
if (sound != null) {
Expand Down

0 comments on commit 389de5c

Please sign in to comment.