From 00650def022da2392ca037634a4d6fbc3f289093 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Fri, 8 Nov 2024 12:39:10 -0800 Subject: [PATCH] SoundUtils - handle Sound class for enum/interface (#7199) * SoundUtils - handle Sound class for enum/interface * SoundUtils - fix an error with MC 1.15 * EffPlaySound.sk - add simple test to make sure syntax doesn't throw error * EffPlaySound - remove reflection and remove to effect class * EffPlaySound - some changes * EffPlaySound.sk - fix tests failing --- .../ch/njol/skript/effects/EffPlaySound.java | 31 ++++++++++-- .../java/ch/njol/skript/util/SoundUtils.java | 49 ------------------- .../tests/syntaxes/effects/EffPlaySound.sk | 3 ++ 3 files changed, 29 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/ch/njol/skript/util/SoundUtils.java create mode 100644 src/test/skript/tests/syntaxes/effects/EffPlaySound.sk diff --git a/src/main/java/ch/njol/skript/effects/EffPlaySound.java b/src/main/java/ch/njol/skript/effects/EffPlaySound.java index 2846d616f06..b0a79a39033 100644 --- a/src/main/java/ch/njol/skript/effects/EffPlaySound.java +++ b/src/main/java/ch/njol/skript/effects/EffPlaySound.java @@ -11,6 +11,7 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; +import org.bukkit.Keyed; import org.bukkit.Location; import org.bukkit.NamespacedKey; import org.bukkit.Sound; @@ -72,6 +73,7 @@ public class EffPlaySound extends Effect { private static final boolean ENTITY_EMITTER_SOUND = Skript.methodExists(Player.class, "playSound", Entity.class, Sound.class, SoundCategory.class, float.class, float.class); private static final boolean ENTITY_EMITTER_STRING = Skript.methodExists(Player.class, "playSound", Entity.class, String.class, SoundCategory.class, float.class, float.class); private static final boolean ENTITY_EMITTER = ENTITY_EMITTER_SOUND || ENTITY_EMITTER_STRING; + private static final boolean SOUND_IS_INTERFACE = Sound.class.isInterface(); public static final Pattern KEY_PATTERN = Pattern.compile("([a-z0-9._-]+:)?([a-z0-9/._-]+)"); @@ -149,11 +151,8 @@ protected void execute(Event event) { // validate strings List validSounds = new ArrayList<>(); for (String sound : sounds.getArray(event)) { - NamespacedKey key = null; - try { - Sound enumSound = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH)); - key = enumSound.getKey(); - } catch (IllegalArgumentException alternative) { + NamespacedKey key = getSoundKeyFromEnum(sound); + if (key == null) { sound = sound.toLowerCase(Locale.ENGLISH); Matcher keyMatcher = KEY_PATTERN.matcher(sound); if (!keyMatcher.matches()) @@ -240,4 +239,26 @@ public String toString(@Nullable Event event, boolean debug) { return builder.toString(); } + @SuppressWarnings({"deprecation", "unchecked", "rawtypes"}) + private static @Nullable NamespacedKey getSoundKeyFromEnum(String soundString) { + soundString = soundString.toUpperCase(Locale.ENGLISH); + // Sound.class is an Interface (rather than an enum) as of MC 1.21.3 + if (SOUND_IS_INTERFACE) { + try { + Sound sound = Sound.valueOf(soundString); + return sound.getKey(); + } catch (IllegalArgumentException ignore) { + } + } else { + try { + Enum soundEnum = Enum.valueOf((Class) Sound.class, soundString); + if (soundEnum instanceof Keyed) { + return ((Keyed) soundEnum).getKey(); + } + } catch (IllegalArgumentException ignore) { + } + } + return null; + } + } diff --git a/src/main/java/ch/njol/skript/util/SoundUtils.java b/src/main/java/ch/njol/skript/util/SoundUtils.java deleted file mode 100644 index bbf18e2ea1c..00000000000 --- a/src/main/java/ch/njol/skript/util/SoundUtils.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ -package ch.njol.skript.util; - -import org.bukkit.Sound; -import org.jetbrains.annotations.Nullable; - -/** - * @author Peter Güttinger - */ -public abstract class SoundUtils { - private SoundUtils() {} - - static { - assert false; - } - - private final static EnumUtils util = new EnumUtils<>(Sound.class, "sounds"); - - @Nullable - public static Sound parse(final String s) { - return util.parse(s); - } - - public static String toString(final Sound s, final int flags) { - return util.toString(s, flags); - } - - public static String getAllNames() { - return util.getAllNames(); - } - -} diff --git a/src/test/skript/tests/syntaxes/effects/EffPlaySound.sk b/src/test/skript/tests/syntaxes/effects/EffPlaySound.sk new file mode 100644 index 00000000000..5cd27437d00 --- /dev/null +++ b/src/test/skript/tests/syntaxes/effects/EffPlaySound.sk @@ -0,0 +1,3 @@ +test "play sound effect": + play sound "block.stone.break" with volume 1 at spawn of world "world" + play sound "BLOCK_STONE_BREAK" with volume 1 at spawn of world "world"