package net.knarcraft.minstrel.command; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; import net.knarcraft.minstrel.trait.MinstrelTrait; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * The main command, delegating sub-commands */ public class MinstrelCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender); if (npc == null || !npc.hasTrait(MinstrelTrait.class)) { sender.sendMessage("You must select a minstrel NPC before running this command"); return true; } MinstrelTrait minstrelTrait = npc.getTraitNullable(MinstrelTrait.class); if (args.length < 1) { return false; } return switch (args[0].toLowerCase()) { case "addsong" -> new AddSongCommand(minstrelTrait).onCommand(sender, command, label, args); case "removesong" -> new RemoveSongCommand(minstrelTrait).onCommand(sender, command, label, args); case "listsongs" -> new ListSongsCommand(minstrelTrait).onCommand(sender, command, label, args); case "pitch" -> updatePitch(minstrelTrait, args.length > 1 ? args[1] : null, args.length > 2 && Boolean.parseBoolean(args[2]), sender); case "volume" -> updateVolume(minstrelTrait, args.length > 1 ? args[1] : null, args.length > 2 && Boolean.parseBoolean(args[2]), sender); default -> false; /* Sub-commands: AddSong category identifier duration (remember to run play again) RemoveSong index ListSongs Global sub-commands StopAll - Stops all minstrels from playing PlayALl - Starts playing for all minstrels //TODO: Perhaps split this into two plugins instead? Or make this a generic plugin which happens to add a Minstrel trait? CreatePlaylist name AddSong playlist category identifier duration Play playlist - Plays the specified playlist at the executor's location */ }; } /** * Updates the pitch for a minstrel if possible * * @param minstrelTrait
The minstrel to update the pitch for
* @param newPitchThe new pitch for the minstrel, or null to display the current pitch
* @param senderThe sender to send error/success messages to
* @returnTrue if the pitch was successfully updated
*/ private boolean updatePitch(@NotNull MinstrelTrait minstrelTrait, @Nullable String newPitch, boolean forceRefresh, @NotNull CommandSender sender) { if (newPitch == null) { sender.sendMessage("Current pitch: " + minstrelTrait.getPitch()); return true; } try { float pitch = Float.parseFloat(newPitch); if (pitch < 0) { sender.sendMessage("The pitch cannot be negative"); } else { minstrelTrait.setPitch(pitch); if (forceRefresh && sender instanceof Player player) { minstrelTrait.getPlaylist().stop(player); minstrelTrait.getPlaylist().play(minstrelTrait, player); } } } catch (NumberFormatException exception) { sender.sendMessage("The given pitch is not a number!"); return false; } sender.sendMessage("Pitch set to " + newPitch); return true; } /** * Updates the volume for a minstrel if possible * * @param minstrelTraitThe minstrel to update the pitch for
* @param newVolumeThe new volume for the minstrel
* @param senderThe sender to send error/success messages to
* @returnTrue if the volume was successfully updated
*/ private boolean updateVolume(MinstrelTrait minstrelTrait, String newVolume, boolean forceRefresh, CommandSender sender) { if (newVolume == null) { sender.sendMessage("Current volume: " + minstrelTrait.getVolume()); return true; } try { float volume = Float.parseFloat(newVolume); if (volume <= 0) { sender.sendMessage("The volume must be greater than 0"); } else { minstrelTrait.setVolume(volume); if (forceRefresh && sender instanceof Player player) { minstrelTrait.getPlaylist().stop(player); minstrelTrait.getPlaylist().play(minstrelTrait, player); } } } catch (NumberFormatException exception) { sender.sendMessage("The given volume is not a number!"); return false; } sender.sendMessage("Volume set to " + newVolume); return true; } }