Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util Additions #7267

Merged
merged 8 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/main/java/ch/njol/skript/bukkitutil/SoundUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -25,19 +26,17 @@ public final class SoundUtils {
soundString = soundString.toUpperCase(Locale.ENGLISH);
if (SOUND_IS_INTERFACE) {
try {
//noinspection deprecation
//noinspection deprecation,removal
return Sound.valueOf(soundString).getKey();
} catch (IllegalArgumentException ignore) {
}
} catch (Exception ignored) {}
} else {
try {
//noinspection unchecked,rawtypes
Enum soundEnum = Enum.valueOf((Class) Sound.class, soundString);
return ((Keyed) soundEnum).getKey();
} catch (IllegalArgumentException ignore) {
}
} catch (IllegalArgumentException ignored) {}
}
return null;
return NamespacedKey.fromString(soundString.toLowerCase(Locale.ENGLISH));
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -47,11 +46,23 @@ public final class SoundUtils {
*/
public static @NotNull NamespacedKey getKey(Sound sound) {
if (SOUND_IS_INTERFACE) {
//noinspection deprecation
//noinspection deprecation,removal
return sound.getKey();
} else {
return ((Keyed) sound).getKey();
}
}

/**
* Retrieves the sound correlating to the provided {@code soundString}
* @param soundString The string to get the correlating sound
* @return The correlating {@link Sound}
*/
public static @Nullable Sound getSound(String soundString) {
NamespacedKey key = getKey(soundString);
if (key == null)
return null;
return Registry.SOUNDS.get(key);
}

}
30 changes: 1 addition & 29 deletions src/main/java/ch/njol/skript/effects/EffPlaySound.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.SoundUtils;
import ch.njol.skript.bukkitutil.sounds.SoundReceiver;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -23,10 +19,7 @@

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

@Name("Play Sound")
@Description({
Expand Down Expand Up @@ -72,8 +65,6 @@ 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;

public static final Pattern KEY_PATTERN = Pattern.compile("([a-z0-9._-]+:)?([a-z0-9/._-]+)");

static {
String seedOption = HAS_SEED ? "[[with] seed %-number%] " : "";
Expand Down Expand Up @@ -144,25 +135,6 @@ protected void execute(Event event) {
List<NamespacedKey> validSounds = new ArrayList<>();
for (String sound : sounds.getArray(event)) {
NamespacedKey key = SoundUtils.getKey(sound);
if (key == null) {
sound = sound.toLowerCase(Locale.ENGLISH);
Matcher keyMatcher = KEY_PATTERN.matcher(sound);
if (!keyMatcher.matches())
continue;
try {
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
}
}

if (key == null)
continue;
validSounds.add(key);
Expand Down
34 changes: 12 additions & 22 deletions src/main/java/ch/njol/skript/effects/EffStopSound.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.bukkitutil.SoundUtils;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -16,7 +13,6 @@
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

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

@Name("Stop Sound")
Expand Down Expand Up @@ -49,11 +45,9 @@ public class EffStopSound extends Effect {
);
}

@Nullable
private Expression<SoundCategory> category;
private @Nullable Expression<SoundCategory> category;

@Nullable
private Expression<String> sounds;
private @Nullable Expression<String> sounds;

private Expression<Player> players;
private boolean allSounds;
Expand Down Expand Up @@ -84,19 +78,15 @@ protected void execute(Event event) {
player.stopSound(category);
}
} else if (sounds != null) {
for (String sound : sounds.getArray(event)) {
try {
Sound soundEnum = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH));
for (String soundString : sounds.getArray(event)) {
Sound sound = SoundUtils.getSound(soundString);
if (sound != null) {
for (Player player : targets)
player.stopSound(soundEnum, category);

continue;
} catch (IllegalArgumentException ignored) {}
sound = sound.toLowerCase(Locale.ENGLISH);
if (!KEY_PATTERN.matcher(sound).matches())
continue;
for (Player player : targets)
player.stopSound(sound, category);
player.stopSound(sound, category);
} else if (KEY_PATTERN.matcher(soundString).matches()) {
for (Player player : targets)
player.stopSound(soundString, category);
}
}
}
}
Expand Down
Loading