summaryrefslogtreecommitdiffstats
path: root/bukkit
diff options
context:
space:
mode:
Diffstat (limited to 'bukkit')
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/SoundUtils.java56
1 files changed, 40 insertions, 16 deletions
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 8eacd5d4..9059b35e 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,38 +3,62 @@ package com.leonardobishop.quests.bukkit.util;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
-import java.util.regex.Pattern;
-
public class SoundUtils {
/**
* Play a sound to a player
*
- * @param soundString the sound string as formatted by a user in the config, e.g ENTITY_PLAYER_LEVELUP:2:3
+ * @param soundString the sound string as formatted by a user in the config, e.g. ENTITY_PLAYER_LEVELUP:2:3
+ * or namespaced, e.g (minecraft:block.decorated_pot.step):2:3
*/
public static void playSoundForPlayer(Player player, String soundString) {
- if (soundString == null || soundString.isEmpty()) return;
+ if (soundString == null || soundString.isEmpty()) {
+ return;
+ }
- String[] parts = soundString.split(Pattern.quote(":"));
- float pitch = 1;
- float volume = 3;
- try {
- switch (parts.length) {
- case 3:
- volume = Float.parseFloat(parts[2]);
- case 2:
- pitch = Float.parseFloat(parts[1]);
+ String[] parts = soundString.split(":");
+ boolean namespaced = parts.length >= 2 && parts[0].startsWith("(") && parts[1].endsWith(")");
+
+ // (namespace:key):pitch:volume
+ // 0 1 2 3
+ // ENTITY_PLAYER_LEVELUP:2:3
+ // 0 1 2
+ int pitchIndex = namespaced ? 2 : 1;
+ int volumeIndex = pitchIndex + 1;
+
+ float pitch = 1.0f;
+ if (parts.length >= pitchIndex + 1) {
+ try {
+ pitch = Float.parseFloat(parts[pitchIndex]);
+ } catch (NumberFormatException ignored) {
+ }
+ }
+
+ float volume = 3.0f;
+ if (parts.length >= volumeIndex + 1) {
+ try {
+ volume = Float.parseFloat(parts[volumeIndex]);
+ } catch (NumberFormatException ignored) {
}
- } catch (NumberFormatException ignored) { }
+ }
+
+ if (namespaced) {
+ // 0123456789
+ // (space:id)
+ // (space - length 6
+ // id) - length 3
+ String sound = soundString.substring(1, parts[0].length() + parts[1].length());
+ player.playSound(player.getLocation(), sound, volume, pitch);
+ return;
+ }
Sound sound;
try {
sound = Sound.valueOf(parts[0]);
- } catch (IllegalArgumentException ex) {
+ } catch (IllegalArgumentException ignored) {
return;
}
player.playSound(player.getLocation(), sound, volume, pitch);
}
-
}