Skip to content

Commit

Permalink
SoundUtils - handle Sound class for enum/interface (#7199)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ShaneBeee authored Nov 8, 2024
1 parent 1fb7e67 commit 00650de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 54 deletions.
31 changes: 26 additions & 5 deletions src/main/java/ch/njol/skript/effects/EffPlaySound.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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/._-]+)");

Expand Down Expand Up @@ -149,11 +151,8 @@ protected void execute(Event event) {
// validate strings
List<NamespacedKey> 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())
Expand Down Expand Up @@ -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;
}

}
49 changes: 0 additions & 49 deletions src/main/java/ch/njol/skript/util/SoundUtils.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffPlaySound.sk
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 00650de

Please sign in to comment.