Skip to content

Commit

Permalink
Merge pull request #158 from MemencioPerez/main
Browse files Browse the repository at this point in the history
Access to Sound#valueOf method through Reflection to avoid java.lang IncompatibleClassChangeError when using versions prior to 1.21.3
  • Loading branch information
Kqliber authored Jan 14, 2025
2 parents 3a8c9d2 + 63bde0d commit 9ae4882
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.extendedclip.deluxemenus.utils.AdventureUtils;
import com.extendedclip.deluxemenus.utils.DebugLevel;
import com.extendedclip.deluxemenus.utils.ExpUtils;
import com.extendedclip.deluxemenus.utils.SoundUtils;
import com.extendedclip.deluxemenus.utils.StringUtils;
import com.extendedclip.deluxemenus.utils.VersionHelper;
import net.kyori.adventure.text.minimessage.MiniMessage;
Expand Down Expand Up @@ -360,7 +361,7 @@ public void run() {

if (!executable.contains(" ")) {
try {
sound = Sound.valueOf(executable.toUpperCase());
sound = SoundUtils.getSound(executable.toUpperCase());
} catch (final IllegalArgumentException exception) {
plugin.printStacktrace(
"Sound name given for sound action: " + executable + ", is not a valid sound!",
Expand All @@ -372,7 +373,7 @@ public void run() {
String[] parts = executable.split(" ", 3);

try {
sound = Sound.valueOf(parts[0].toUpperCase());
sound = SoundUtils.getSound(parts[0].toUpperCase());
} catch (final IllegalArgumentException exception) {
plugin.printStacktrace(
"Sound name given for sound action: " + parts[0] + ", is not a valid sound!",
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/utils/SoundUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.extendedclip.deluxemenus.utils;

import org.bukkit.Sound;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SoundUtils {

public static Sound getSound(String name) {
try {
// As of Minecraft 1.21.3, the org.bukkit.Sound class type changed from Enum to Interface.
// This fixes java.lang.IncompatibleClassChangeError when trying to use versions prior to 1.21.3.
Method valueOfMethod = Class.forName("org.bukkit.Sound").getMethod("valueOf", String.class);
return (Sound) valueOfMethod.invoke(null, name);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// Use the Sound#valueOf method if Reflection fails.
return Sound.valueOf(name);
}
}
}

0 comments on commit 9ae4882

Please sign in to comment.