Sound arguments
The SoundArgument
class allows a command sender to retrieve the Bukkit Sound
or NamespacedKey
object to represent in-game sound effects (such as mob sounds or ambient sound effects), as well as in-game music.
The SoundArgument
can return a Sound
or NamespacedKey
object. To return a Sound
object, simply use the SoundArgument
as normal. To return a NamespacedKey
object, use the SoundArgument.NamespacedKey
constructor instead:
// Makes a SoundArgument that returns a Sound
new SoundArgument("sound");
// Makes a SoundArgument that returns a NamespacedKey
new SoundArgument.NamespacedKey("sound");
Example - Playing sound to yourself
Say we want a simple command that plays a specific sound at your location. To do this, we will make the following command:
/sound <sound>
This command simply plays the provided sound to the current player:
new CommandAPICommand("sound")
.withArguments(new SoundArgument("sound"))
.executesPlayer((player, args) -> {
player.getWorld().playSound(player.getLocation(), (Sound) args[0], 100.0f, 1.0f);
})
.register();
new CommandAPICommand("sound")
.withArguments(new SoundArgument.NamespacedKey("sound"))
.executesPlayer((player, args) -> {
player.getWorld().playSound(player.getLocation(), ((NamespacedKey) args[0]).asString(), 100.0f, 1.0f);
})
.register();
CommandAPICommand("sound")
.withArguments(SoundArgument("sound"))
.executesPlayer(PlayerCommandExecutor { player, args ->
player.world.playSound(player.location, args[0] as Sound, 100.0f, 1.0f)
})
.register()
CommandAPICommand("sound")
.withArguments(SoundArgument.NamespacedKey("sound"))
.executesPlayer(PlayerCommandExecutor { player, args ->
player.world.playSound(player.location, (args[0] as NamespacedKey).asString(), 100.0f, 1.0f)
})
.register()