From df7ec1cb1af196123f8e2e228987d60f5da05bc7 Mon Sep 17 00:00:00 2001 From: Krakenied Date: Mon, 30 Dec 2024 12:42:50 +0100 Subject: Fix Sounds enum on older versions Finally closes https://github.com/LMBishop/Quests/issues/756 --- .../quests/bukkit/util/SoundUtils.java | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'bukkit/src') 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 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; } -- cgit v1.2.3-70-g09d2