Adds a command for testing different NPC sounds
All checks were successful
KnarCraft/BlacksmithVisuals/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-08-04 11:50:16 +02:00
parent 891330af4a
commit a50e97454a
6 changed files with 160 additions and 29 deletions

View File

@ -29,5 +29,6 @@ teleported to end up in the correct spot.
| Permission | Description | | Permission | Description |
|-------------------------------|---------------------------------------------| |-------------------------------|---------------------------------------------|
| blacksmithvisuals.* | Gives all permissions |
| blacksmithvisuals.reload | Gives access to the reload command | | blacksmithvisuals.reload | Gives access to the reload command |
| blacksmithvisuals.setposition | Gives access to the /setNPCPosition command | | blacksmithvisuals.setposition | Gives access to the /setNPCPosition command |

View File

@ -1,5 +1,6 @@
package net.knarcraft.blacksmithvisuals; package net.knarcraft.blacksmithvisuals;
import net.knarcraft.blacksmithvisuals.command.PlayTestSoundCommand;
import net.knarcraft.blacksmithvisuals.command.ReloadCommand; import net.knarcraft.blacksmithvisuals.command.ReloadCommand;
import net.knarcraft.blacksmithvisuals.command.SetNPCPositionCommand; import net.knarcraft.blacksmithvisuals.command.SetNPCPositionCommand;
import net.knarcraft.blacksmithvisuals.listener.BlacksmithListener; import net.knarcraft.blacksmithvisuals.listener.BlacksmithListener;
@ -53,6 +54,7 @@ public final class BlacksmithVisuals extends JavaPlugin {
getServer().getPluginManager().registerEvents(new BlacksmithListener(this.configurationManager), this); getServer().getPluginManager().registerEvents(new BlacksmithListener(this.configurationManager), this);
registerCommand("reload", new ReloadCommand()); registerCommand("reload", new ReloadCommand());
registerCommand("setNPCPosition", new SetNPCPositionCommand()); registerCommand("setNPCPosition", new SetNPCPositionCommand());
registerCommand("playTestSound", new PlayTestSoundCommand());
} }
@Override @Override

View File

@ -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<String> soundCategories = null;
private List<String> 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<String> 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<String> 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<String> 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();
}
}

View File

@ -21,9 +21,9 @@ import net.knarcraft.blacksmithvisuals.container.SoundData;
import net.knarcraft.blacksmithvisuals.manager.ConfigurationManager; import net.knarcraft.blacksmithvisuals.manager.ConfigurationManager;
import net.knarcraft.blacksmithvisuals.property.NPCPosition; import net.knarcraft.blacksmithvisuals.property.NPCPosition;
import net.knarcraft.blacksmithvisuals.property.SoundIdentifier; import net.knarcraft.blacksmithvisuals.property.SoundIdentifier;
import net.knarcraft.blacksmithvisuals.util.SoundHelper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -76,22 +76,22 @@ public class BlacksmithListener implements Listener {
@EventHandler @EventHandler
public void onReforgeSuccess(@NotNull BlacksmithReforgeSucceedEvent event) { 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 @EventHandler
public void onSalvageSuccess(@NotNull ScrapperSalvageSucceedEvent event) { 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 @EventHandler
public void onReforgeFail(@NotNull BlacksmithReforgeFailEvent event) { 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 @EventHandler
public void onSalvageFail(@NotNull ScrapperSalvageFailEvent event) { 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; return;
} }
playSound(entity, soundData); SoundHelper.playSound(entity, soundData);
// Don't play disabled animations // Don't play disabled animations
if (!animationData.animateOffHand()) { if (!animationData.animateOffHand()) {
@ -276,27 +276,5 @@ public class BlacksmithListener implements Listener {
} }
} }
/**
* Plays a sound according to the given sound data
*
* @param entity <p>The entity to play the sound from</p>
* @param soundData <p>The data describing the sound to play</p>
*/
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);
}
} }

View File

@ -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 <p>The entity to play the sound from</p>
* @param soundData <p>The data describing the sound to play</p>
*/
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);
}
}

View File

@ -20,6 +20,10 @@ commands:
/<command> reforging-workstation /<command> reforging-workstation
/<command> crafting-workstation /<command> crafting-workstation
description: Used to set a blacksmith or scrapper NPC's positions description: Used to set a blacksmith or scrapper NPC's positions
playTestSound:
permission: blacksmithvisuals.playtestsound
usage: /<command> <sound category> <sound> [volume] [pitch]
description: Plays the specified sound at the selected NPC's location
permissions: permissions:
blacksmithvisuals.*: blacksmithvisuals.*:
description: Gives all permissions description: Gives all permissions
@ -27,9 +31,13 @@ permissions:
children: children:
- blacksmithvisuals.reload - blacksmithvisuals.reload
- blacksmithvisuals.setposition - blacksmithvisuals.setposition
- blacksmithvisuals.playtestsound
blacksmithvisuals.reload: blacksmithvisuals.reload:
description: Allows reloading the plugin description: Allows reloading the plugin
default: false default: false
blacksmithvisuals.setposition: blacksmithvisuals.setposition:
description: Allows use of the /setNPCPosition command description: Allows use of the /setNPCPosition command
default: false default: false
blacksmithvisuals.playtestsound:
description: Allows use of the /playTestSound command
default: false