Skip to content

Commit

Permalink
Fix missing EffPlaySound version checks (#6910)
Browse files Browse the repository at this point in the history
  • Loading branch information
sovdeeth authored Aug 1, 2024
1 parent c53a57e commit a93ace5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/main/java/ch/njol/skript/effects/EffPlaySound.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import java.util.Locale;
import java.util.OptionalLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Name("Play Sound")
Expand All @@ -53,7 +54,7 @@
"<a href=\"https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Sound.html\">Spigot sound names</a> " +
"are supported. Playing resource pack sounds are supported too. The sound category is 'master' by default. ",
"",
"Playing a sound from an entity directly will result in the sound coming from said entity, even while moving.",
"When running 1.18+, playing a sound from an entity directly will result in the sound coming from said entity, even while moving.",
"If the sound is custom, a location emitter will follow the entity. Do note that pitch and volume ",
"are reflected based on the entity, and Minecraft may not use the values from this syntax.",
"",
Expand All @@ -68,22 +69,27 @@
"play sound \"custom.music.1\" in jukebox category at {speakerBlock}",
"play sound \"BLOCK_AMETHYST_BLOCK_RESONATE\" with seed 1 on target entity for the player #1.20.1+"
})
@RequiredPlugins("Paper 1.19.4+ or Adventure API 4.12.0+ (sound seed)")
@RequiredPlugins("Minecraft 1.18.1+ (entity emitters), Paper 1.19.4+ or Adventure API 4.12.0+ (sound seed)")
@Since("2.2-dev28, 2.4 (sound categories), 2.9.0 (sound seed & entity emitter)")
public class EffPlaySound extends Effect {

private static final boolean ADVENTURE_API = Skript.classExists("net.kyori.adventure.sound.Sound$Builder");
public static final Pattern KEY_PATTERN = Pattern.compile("([a-z0-9._-]+:)?[a-z0-9/._-]+");
private static final boolean PLAYER_ENTITY_EMITTER = Skript.methodExists(Player.class, "playSound", Entity.class, Sound.class, SoundCategory.class, float.class, float.class);
private static final boolean WORLD_ENTITY_EMITTER = Skript.methodExists(World.class, "playSound", Entity.class, String.class, SoundCategory.class, float.class, float.class);
public static final Pattern KEY_PATTERN = Pattern.compile("([a-z0-9._-]+:)?([a-z0-9/._-]+)");

static {
String additional = "";
if (ADVENTURE_API)
additional = "[[with] seed %-number%] ";
String emitterTypes = "locations";
if (PLAYER_ENTITY_EMITTER)
emitterTypes += "/entities";
Skript.registerEffect(EffPlaySound.class,
"play sound[s] %strings% " + additional + "[(in|from) %-soundcategory%] " +
"[(at|with) volume %-number%] [(and|at|with) pitch %-number%] (at|on|from) %locations/entities% [(to|for) %-players%]",
"[(at|with) volume %-number%] [(and|at|with) pitch %-number%] (at|on|from) %" + emitterTypes + "% [(to|for) %-players%]",
"play sound[s] %strings% " + additional + "[(in|from) %-soundcategory%] " +
"[(at|with) volume %-number%] [(and|at|with) pitch %-number%] [(to|for) %players%] [(at|on|from) %-locations/entities%]"
"[(at|with) volume %-number%] [(and|at|with) pitch %-number%] [(to|for) %players%] [(at|on|from) %-" + emitterTypes + "%]"
);
}

Expand Down Expand Up @@ -148,33 +154,33 @@ protected void execute(Event event) {
if (players != null) {
if (emitters == null) {
for (Player player : players.getArray(event)) {
play(Player::playSound, Player::playSound, ADVENTURE_API ? Player::playSound : null, ADVENTURE_API ? Player::playSound : null,
play(PLAYER_ENTITY_EMITTER ? Player::playSound : null, Player::playSound, ADVENTURE_API ? Player::playSound : null, ADVENTURE_API ? Player::playSound : null,
player, player.getLocation(), sounds.getArray(event), category, volume, pitch, seed);
}
} else {
for (Player player : players.getArray(event)) {
for (Object emitter : emitters.getArray(event)) {
if (emitter instanceof Entity) {
if (emitter instanceof Entity && PLAYER_ENTITY_EMITTER) {
Entity entity = (Entity) emitter;
play(Player::playSound, Player::playSound, ADVENTURE_API ? Player::playSound : null, ADVENTURE_API ? Player::playSound : null,
player, entity, sounds.getArray(event), category, volume, pitch, seed);
} else if (emitter instanceof Location) {
Location location = (Location) emitter;
play(Player::playSound, Player::playSound, ADVENTURE_API ? Player::playSound : null, ADVENTURE_API ? Player::playSound : null,
play(PLAYER_ENTITY_EMITTER ? Player::playSound : null, Player::playSound, ADVENTURE_API ? Player::playSound : null, ADVENTURE_API ? Player::playSound : null,
player, location, sounds.getArray(event), category, volume, pitch, seed);
}
}
}
}
} else if (emitters != null) {
for (Object emitter : emitters.getArray(event)) {
if (emitter instanceof Entity) {
if (emitter instanceof Entity && WORLD_ENTITY_EMITTER) {
Entity entity = (Entity) emitter;
play(World::playSound, World::playSound, ADVENTURE_API ? World::playSound : null, ADVENTURE_API ? World::playSound : null,
entity.getWorld(), entity, sounds.getArray(event), category, volume, pitch, seed);
} else if (emitter instanceof Location) {
Location location = (Location) emitter;
play(World::playSound, World::playSound, ADVENTURE_API ? World::playSound : null, ADVENTURE_API ? World::playSound : null,
play(WORLD_ENTITY_EMITTER ? World::playSound : null, World::playSound, ADVENTURE_API ? World::playSound : null, ADVENTURE_API ? World::playSound : null,
location.getWorld(), location, sounds.getArray(event), category, volume, pitch, seed);
}
}
Expand Down Expand Up @@ -203,7 +209,7 @@ public String toString(@Nullable Event event, boolean debug) {
return builder.toString();
}

private <T, E> void play(@NotNull SoundReceiver<T, Entity> entityReceiver,
private <T, E> void play(@Nullable SoundReceiver<T, Entity> entityReceiver,
@NotNull SoundReceiver<T, Location> locationReceiver,
@Nullable AdventureEmitterSoundReceiver<T> adventureLocationReceiver,
@Nullable AdventureEntitySoundReceiver<T> adventureEmitterReceiver,
Expand All @@ -224,7 +230,7 @@ void play(
);

static <T, E> void play(
@NotNull SoundReceiver<T, Entity> entityReceiver,
@Nullable SoundReceiver<T, Entity> entityReceiver,
@NotNull SoundReceiver<T, Location> locationReceiver,
@NotNull T receiver, @NotNull E emitter, @NotNull String[] sounds,
@NotNull SoundCategory category, float volume, float pitch, OptionalLong seed
Expand All @@ -236,10 +242,18 @@ static <T, E> void play(
key = enumSound.getKey();
} catch (IllegalArgumentException alternative) {
sound = sound.toLowerCase(Locale.ENGLISH);
if (!KEY_PATTERN.matcher(sound).matches())
Matcher keyMatcher = KEY_PATTERN.matcher(sound);
if (!keyMatcher.matches())
continue;
try {
key = NamespacedKey.fromString(sound);
String namespace = keyMatcher.group(1);
String keyValue = keyMatcher.group(2);
if (namespace == null) {
key = NamespacedKey.minecraft(keyValue);
} else {
namespace = namespace.substring(0, namespace.length() - 1);
key = new NamespacedKey(namespace, keyValue);
}
} catch (IllegalArgumentException argument) {
// The user input invalid characters
}
Expand All @@ -249,7 +263,7 @@ static <T, E> void play(
continue;
if (emitter instanceof Location) {
locationReceiver.play(receiver, (Location) emitter, key.getKey(), category, volume, pitch);
} else if (emitter instanceof Entity) {
} else if (emitter instanceof Entity && entityReceiver != null) {
entityReceiver.play(receiver, (Entity) emitter, key.getKey(), category, volume, pitch);
}
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test "1.17 playsound exception":
# would throw exception on <1.17
play sound "entity.experience_orb.pickup" with volume 1 at spawn of world "world"

0 comments on commit a93ace5

Please sign in to comment.