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");
When using the Sound
object, the CommandAPI will return null
if the specified Sound
could not be found. For this reason, it's recommended to use the NamespacedKey
object for optimal compatibility with client-side resourcepacks.
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.get("sound"), 100.0f, 1.0f);
})
.register();
new CommandAPICommand("sound")
.withArguments(new SoundArgument.NamespacedKey("sound"))
.executesPlayer((player, args) -> {
player.getWorld().playSound(player.getLocation(), ((NamespacedKey) args.get("sound")).asString(), 100.0f, 1.0f);
})
.register();
CommandAPICommand("sound")
.withArguments(SoundArgument("sound"))
.executesPlayer(PlayerCommandExecutor { player, args ->
player.world.playSound(player.location, args["sound"] as Sound, 100.0f, 1.0f)
})
.register()
CommandAPICommand("sound")
.withArguments(SoundArgument.NamespacedKey("sound"))
.executesPlayer(PlayerCommandExecutor { player, args ->
player.world.playSound(player.location, (args["sound"] as NamespacedKey).asString(), 100.0f, 1.0f)
})
.register()
commandAPICommand("sound") {
soundArgument("sound")
playerExecutor { player, args ->
player.world.playSound(player.location, args["sound"] as Sound, 100.0f, 1.0f)
}
}
commandAPICommand("sound") {
soundArgument("sound", true)
playerExecutor { player, args ->
player.world.playSound(player.location, (args["sound"] as NamespacedKey).asString(), 100.0f, 1.0f)
}
}