diff options
| author | Krakenied <krakenied1@gmail.com> | 2024-12-30 12:42:50 +0100 |
|---|---|---|
| committer | Krakenied <46192742+Krakenied@users.noreply.github.com> | 2025-05-13 20:34:15 +0200 |
| commit | df7ec1cb1af196123f8e2e228987d60f5da05bc7 (patch) | |
| tree | be6d2da2feb54300e41c0d92e6b36b43cb7ea9c2 /bukkit/src/main/java/com | |
| parent | 38a3b29c8970563d3679485823b5a567050c8da1 (diff) | |
Fix Sounds enum on older versions
Finally closes https://github.com/LMBishop/Quests/issues/756
Diffstat (limited to 'bukkit/src/main/java/com')
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/SoundUtils.java | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/SoundUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/SoundUtils.java index 9059b35e..1d8bdc71 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/SoundUtils.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/SoundUtils.java @@ -3,8 +3,24 @@ package com.leonardobishop.quests.bukkit.util; import org.bukkit.Sound; import org.bukkit.entity.Player; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + public class SoundUtils { + private static final Map<String, Sound> soundCache = new HashMap<>(); + private static final Method soundValueOfMethod; + + static { + try { + soundValueOfMethod = Sound.class.getDeclaredMethod("valueOf", String.class); + } catch (NoSuchMethodException e) { + throw new IllegalStateException("Sound#valueOf method could not be found", e); + } + } + /** * Play a sound to a player * @@ -54,7 +70,14 @@ public class SoundUtils { Sound sound; try { - sound = Sound.valueOf(parts[0]); + // TODO make per version sound getter + sound = soundCache.computeIfAbsent(parts[0], name -> { + try { + return (Sound) soundValueOfMethod.invoke(null, parts[0]); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalStateException("Sound#valueOf invocation failed", e); + } + }); } catch (IllegalArgumentException ignored) { return; } |
