From a50e97454a3dc0b0308186c9dae8f84188b15fd0 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 4 Aug 2024 11:50:16 +0200 Subject: [PATCH] Adds a command for testing different NPC sounds --- README.md | 1 + .../blacksmithvisuals/BlacksmithVisuals.java | 4 +- .../command/PlayTestSoundCommand.java | 100 ++++++++++++++++++ .../listener/BlacksmithListener.java | 34 ++---- .../blacksmithvisuals/util/SoundHelper.java | 42 ++++++++ src/main/resources/plugin.yml | 8 ++ 6 files changed, 160 insertions(+), 29 deletions(-) create mode 100644 src/main/java/net/knarcraft/blacksmithvisuals/command/PlayTestSoundCommand.java create mode 100644 src/main/java/net/knarcraft/blacksmithvisuals/util/SoundHelper.java diff --git a/README.md b/README.md index 1481012..fb5ec55 100644 --- a/README.md +++ b/README.md @@ -29,5 +29,6 @@ teleported to end up in the correct spot. | Permission | Description | |-------------------------------|---------------------------------------------| +| blacksmithvisuals.* | Gives all permissions | | blacksmithvisuals.reload | Gives access to the reload command | | blacksmithvisuals.setposition | Gives access to the /setNPCPosition command | \ No newline at end of file diff --git a/src/main/java/net/knarcraft/blacksmithvisuals/BlacksmithVisuals.java b/src/main/java/net/knarcraft/blacksmithvisuals/BlacksmithVisuals.java index 931494a..b17e1da 100644 --- a/src/main/java/net/knarcraft/blacksmithvisuals/BlacksmithVisuals.java +++ b/src/main/java/net/knarcraft/blacksmithvisuals/BlacksmithVisuals.java @@ -1,5 +1,6 @@ package net.knarcraft.blacksmithvisuals; +import net.knarcraft.blacksmithvisuals.command.PlayTestSoundCommand; import net.knarcraft.blacksmithvisuals.command.ReloadCommand; import net.knarcraft.blacksmithvisuals.command.SetNPCPositionCommand; import net.knarcraft.blacksmithvisuals.listener.BlacksmithListener; @@ -33,7 +34,7 @@ public final class BlacksmithVisuals extends JavaPlugin { this.getConfig().options().copyDefaults(true); this.reloadConfig(); this.saveConfig(); - + try { this.configurationManager = new ConfigurationManager(this.getConfig()); } catch (InvalidConfigurationException exception) { @@ -53,6 +54,7 @@ public final class BlacksmithVisuals extends JavaPlugin { getServer().getPluginManager().registerEvents(new BlacksmithListener(this.configurationManager), this); registerCommand("reload", new ReloadCommand()); registerCommand("setNPCPosition", new SetNPCPositionCommand()); + registerCommand("playTestSound", new PlayTestSoundCommand()); } @Override diff --git a/src/main/java/net/knarcraft/blacksmithvisuals/command/PlayTestSoundCommand.java b/src/main/java/net/knarcraft/blacksmithvisuals/command/PlayTestSoundCommand.java new file mode 100644 index 0000000..caba5c4 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmithvisuals/command/PlayTestSoundCommand.java @@ -0,0 +1,100 @@ +package net.knarcraft.blacksmithvisuals.command; + +import net.citizensnpcs.api.CitizensAPI; +import net.citizensnpcs.api.npc.NPC; +import net.knarcraft.blacksmith.trait.BlacksmithTrait; +import net.knarcraft.blacksmith.trait.ScrapperTrait; +import net.knarcraft.blacksmithvisuals.container.SoundData; +import net.knarcraft.blacksmithvisuals.util.SoundHelper; +import org.bukkit.Sound; +import org.bukkit.SoundCategory; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * A command for testing different sounds for blacksmiths and scrappers + */ +public class PlayTestSoundCommand implements TabExecutor { + + private List soundCategories = null; + private List sounds = null; + + @Override + public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, + @NotNull String[] arguments) { + NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender); + if (npc == null || (!npc.hasTrait(BlacksmithTrait.class) && !npc.hasTrait(ScrapperTrait.class))) { + commandSender.sendMessage("You must select a scrapper or blacksmith NPC before executing this command"); + return true; + } + + if (arguments.length < 2) { + return false; + } + + SoundCategory soundCategory; + Sound sound; + + try { + soundCategory = SoundCategory.valueOf(arguments[0].toUpperCase()); + sound = Sound.valueOf(arguments[1].toUpperCase()); + } catch (IllegalArgumentException exception) { + return false; + } + + float volume = 1; + float pitch = 1; + + try { + if (arguments.length > 2) { + volume = Float.parseFloat(arguments[2]); + } + if (arguments.length > 3) { + pitch = Float.parseFloat(arguments[3]); + } + } catch (NumberFormatException exception) { + return false; + } + + SoundData soundData = new SoundData(soundCategory, sound, volume, pitch, 0, true); + SoundHelper.playSound(npc.getEntity(), soundData); + return true; + } + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, + @NotNull String[] arguments) { + if (arguments.length == 1) { + if (soundCategories == null) { + soundCategories = new ArrayList<>(); + for (SoundCategory soundCategory : SoundCategory.values()) { + soundCategories.add(soundCategory.name()); + } + } + List copy = new ArrayList<>(soundCategories); + copy.removeIf((item) -> !item.contains(arguments[0])); + return copy; + } else if (arguments.length == 2) { + if (sounds == null) { + sounds = new ArrayList<>(); + for (Sound sound : Sound.values()) { + sounds.add(sound.name()); + } + } + List copy = new ArrayList<>(sounds); + copy.removeIf((item) -> !item.contains(arguments[1])); + return copy; + } else if (arguments.length == 3 || arguments.length == 4) { + return List.of("0.5", "1", "1.5"); + } + return List.of(); + } + +} diff --git a/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java b/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java index 1706e8a..f301ca8 100644 --- a/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java +++ b/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java @@ -21,9 +21,9 @@ import net.knarcraft.blacksmithvisuals.container.SoundData; import net.knarcraft.blacksmithvisuals.manager.ConfigurationManager; import net.knarcraft.blacksmithvisuals.property.NPCPosition; import net.knarcraft.blacksmithvisuals.property.SoundIdentifier; +import net.knarcraft.blacksmithvisuals.util.SoundHelper; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -76,22 +76,22 @@ public class BlacksmithListener implements Listener { @EventHandler public void onReforgeSuccess(@NotNull BlacksmithReforgeSucceedEvent event) { - playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.REFORGING_SUCCESS)); + SoundHelper.playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.REFORGING_SUCCESS)); } @EventHandler public void onSalvageSuccess(@NotNull ScrapperSalvageSucceedEvent event) { - playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.SALVAGING_SUCCESS)); + SoundHelper.playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.SALVAGING_SUCCESS)); } @EventHandler public void onReforgeFail(@NotNull BlacksmithReforgeFailEvent event) { - playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.REFORGING_FAILURE)); + SoundHelper.playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.REFORGING_FAILURE)); } @EventHandler public void onSalvageFail(@NotNull ScrapperSalvageFailEvent event) { - playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.SALVAGING_FAILURE)); + SoundHelper.playSound(event.getNpc().getEntity(), this.configurationManager.getSoundData(SoundIdentifier.SALVAGING_FAILURE)); } /** @@ -243,7 +243,7 @@ public class BlacksmithListener implements Listener { return; } - playSound(entity, soundData); + SoundHelper.playSound(entity, soundData); // Don't play disabled animations if (!animationData.animateOffHand()) { @@ -276,27 +276,5 @@ public class BlacksmithListener implements Listener { } } - /** - * Plays a sound according to the given sound data - * - * @param entity

The entity to play the sound from

- * @param soundData

The data describing the sound to play

- */ - private void playSound(@NotNull Entity entity, @NotNull SoundData soundData) { - // Don't play disabled sounds - if (!soundData.enabled()) { - return; - } - - World world = entity.getLocation().getWorld(); - if (world == null) { - return; - } - - int delay = Math.max(soundData.offsetTicks(), 0); - Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () -> - world.playSound(entity, soundData.sound(), soundData.soundCategory(), - soundData.volume(), soundData.pitch()), delay); - } } diff --git a/src/main/java/net/knarcraft/blacksmithvisuals/util/SoundHelper.java b/src/main/java/net/knarcraft/blacksmithvisuals/util/SoundHelper.java new file mode 100644 index 0000000..6be58a0 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmithvisuals/util/SoundHelper.java @@ -0,0 +1,42 @@ +package net.knarcraft.blacksmithvisuals.util; + +import net.knarcraft.blacksmithvisuals.BlacksmithVisuals; +import net.knarcraft.blacksmithvisuals.container.SoundData; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.jetbrains.annotations.NotNull; + +/** + * A helper class for working with sounds + */ +public final class SoundHelper { + + private SoundHelper() { + + } + + /** + * Plays a sound according to the given sound data + * + * @param entity

The entity to play the sound from

+ * @param soundData

The data describing the sound to play

+ */ + public static void playSound(@NotNull Entity entity, @NotNull SoundData soundData) { + // Don't play disabled sounds + if (!soundData.enabled()) { + return; + } + + World world = entity.getLocation().getWorld(); + if (world == null) { + return; + } + + int delay = Math.max(soundData.offsetTicks(), 0); + Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () -> + world.playSound(entity, soundData.sound(), soundData.soundCategory(), + soundData.volume(), soundData.pitch()), delay); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a99a4da..16a4a54 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -20,6 +20,10 @@ commands: / reforging-workstation / crafting-workstation description: Used to set a blacksmith or scrapper NPC's positions + playTestSound: + permission: blacksmithvisuals.playtestsound + usage: / [volume] [pitch] + description: Plays the specified sound at the selected NPC's location permissions: blacksmithvisuals.*: description: Gives all permissions @@ -27,9 +31,13 @@ permissions: children: - blacksmithvisuals.reload - blacksmithvisuals.setposition + - blacksmithvisuals.playtestsound blacksmithvisuals.reload: description: Allows reloading the plugin default: false blacksmithvisuals.setposition: description: Allows use of the /setNPCPosition command + default: false + blacksmithvisuals.playtestsound: + description: Allows use of the /playTestSound command default: false \ No newline at end of file