Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;

import com.bencodez.advancedcore.api.misc.SoundResolver;
import com.bencodez.advancedcore.api.user.UserStorage;
import com.bencodez.simpleapi.debug.DebugLevel;
import com.bencodez.simpleapi.file.YMLConfig;
Expand Down Expand Up @@ -664,19 +663,9 @@ public AdvancedCoreConfigOptions() {
*
* @return The Sound object for click sound, or null if set to "none"
*/
public Sound getClickSoundSound() {

if (getClickSoundSoundStr().equalsIgnoreCase("none")) {
return null;
}
try {
return Registry.SOUNDS
.get(NamespacedKey.minecraft(getClickSoundSoundStr().replaceAll("_", ".").toLowerCase()));
} catch (Exception e) {
e.printStackTrace();
return Registry.SOUNDS.get(NamespacedKey.minecraft("ui.button.click"));
}
}
public Sound getClickSoundSound() {
return SoundResolver.resolveClickSound(getClickSoundSoundStr());
}

/**
* Loads configuration options from the YML configuration file.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.bencodez.advancedcore.api.misc;

import java.util.Locale;

import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;

/** Resolves configured sound keys without relying on registry objects' debug text. */
public final class SoundResolver {
private SoundResolver() {
}

/** Accepts canonical keys, namespaced keys and legacy Minecraft enum-style names. */
public static Sound resolve(String input) {
return resolve(input, Registry.SOUNDS);
}

static Sound resolve(String input, Registry<Sound> registry) {
if (input == null || input.isBlank()) {
return null;
}
String name = input.trim();
NamespacedKey key = parseKey(name);
if (key == null) {
return null;
}
Sound exact = registry.get(key);
if (exact != null) {
return exact;
}
// Legacy names flatten dots, but underscores inside a key remain significant.
// Never reinterpret an explicitly namespaced key as an enum constant.
if (name.indexOf(':') < 0 && name.matches("[A-Za-z0-9_]+")) {
for (Sound sound : registry) {
NamespacedKey candidate = sound.getKey();
if (NamespacedKey.MINECRAFT.equals(candidate.getNamespace())
&& candidate.getKey().replace('.', '_').equalsIgnoreCase(name)) {
return sound;
}
}
}
return null;
}

/** Parses configuration input without propagating Bukkit's validation exceptions. */
public static NamespacedKey parseKey(String input) {
if (input == null || input.isBlank()) {
return null;
}
try {
return NamespacedKey.fromString(input.trim().toLowerCase(Locale.ROOT));
} catch (IllegalArgumentException ex) {
return null;
}
}

/** Retains the GUI click option's historical mixed underscore/dot aliases. */
public static Sound resolveClickSound(String input) {
if ("none".equalsIgnoreCase(input)) {
return null;
}
Sound sound = resolve(input);
if (sound != null) {
return sound;
}
if (parseKey(input) == null) {
return resolve("ui.button.click");
}
// Explicit namespaced keys are exact, even if the registry has no match.
if (input.indexOf(':') >= 0) {
return null;
}
// This legacy transformation is click-only and bare-name-only; shared
// resolution above keeps underscores in keys such as block.note_block.chime.
NamespacedKey legacy = parseKey(input.replace('_', '.'));
return legacy == null ? resolve("ui.button.click") : Registry.SOUNDS.get(legacy);
}

/** A stable value suitable for configuration and the reward editor. */
public static String configurationKey(Sound sound) {
NamespacedKey key = sound.getKey();
return NamespacedKey.MINECRAFT.equals(key.getNamespace()) ? key.getKey() : key.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.entity.Player;

import com.bencodez.advancedcore.api.inventory.editgui.EditGUI;
import com.bencodez.advancedcore.api.misc.SoundResolver;
import com.bencodez.advancedcore.api.rewards.RewardEditData;
import com.bencodez.simpleapi.array.ArrayUtils;

Expand All @@ -21,7 +22,7 @@ public void open(Player player, RewardEditData reward) {
inv.addButton(getBooleanButton("Sound.Enabled", reward));

ArrayList<String> sounds = new ArrayList<>();
Registry.SOUNDS.forEach(sound -> sounds.add(sound.toString()));
Registry.SOUNDS.forEach(sound -> sounds.add(SoundResolver.configurationKey(sound)));

inv.addButton(getStringButton("Sound.Sound", reward, ArrayUtils.convert(sounds)));
inv.addButton(getDoubleButton("Sound.Volume", reward));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@

import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.Particle;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
Expand All @@ -41,6 +39,7 @@
import org.bukkit.potion.PotionEffectType;

import com.bencodez.advancedcore.AdvancedCorePlugin;
import com.bencodez.advancedcore.api.misc.SoundResolver;
import com.bencodez.advancedcore.api.item.ItemBuilder;
import com.bencodez.advancedcore.api.messages.PlaceholderUtils;
import com.bencodez.advancedcore.api.misc.PlayerManager;
Expand Down Expand Up @@ -593,6 +592,7 @@ public static CompletionStage<Void> persistedReplayCompletion(AdvancedCorePlugin
*/
@Getter
private AdvancedCorePlugin plugin = null;
private long lastInvalidSoundWarningAt;

/** The uuid. */
private String uuid;
Expand Down Expand Up @@ -2824,16 +2824,21 @@ public void playParticleEffect(String effectName, int data, int particles, int r
public void playSound(String soundName, float volume, float pitch) {
Player player = getPlayer();
if (player != null) {
Sound sound = null;
try {
sound = Registry.SOUNDS.get(NamespacedKey.minecraft(soundName));
} catch (Exception e) {
plugin.debug(e);
}
Sound sound = SoundResolver.resolve(soundName);
if (sound != null) {
player.playSound(player.getLocation(), sound, volume, pitch);
} else {
plugin.debug("Invalid sound: " + soundName);
// Bound both diagnostic size and repeated warnings for this user.
synchronized (this) {
long now = System.currentTimeMillis();
if (now - lastInvalidSoundWarningAt >= 30_000L) {
lastInvalidSoundWarningAt = now;
String value = String.valueOf(soundName);
value = value.substring(0, Math.min(value.length(), 128)).replaceAll("[\\p{Cntrl}]", "?");
plugin.getLogger().warning("Invalid reward sound: " + value
+ ". Use a registry key such as entity.player.levelup (warnings limited to one per 30s per user).");
}
}
}
}
}
Expand Down
Loading
Loading