diff options
| author | Krakenied <Krakenied1@gmail.com> | 2024-01-06 01:33:02 +0100 |
|---|---|---|
| committer | Leonardo Bishop <13875753+LMBishop@users.noreply.github.com> | 2024-01-09 15:55:18 +0000 |
| commit | 369ded87d21ffee99649fad27ab7d930d020b6b2 (patch) | |
| tree | 95f61bc67bb457bd77dd32c4d133051dc58c501a /bukkit/src | |
| parent | 98e55ac17ce1d433ca0ba45ac2c03f370fcc7251 (diff) | |
Add namespaced sounds support
Closes https://github.com/LMBishop/Quests/issues/585
Diffstat (limited to 'bukkit/src')
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/SoundUtils.java | 56 |
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); } - } |
