Sound arguments

A sound argument command with a list of Minecraft sounds as suggestions

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 must be parameterized over Sound or NamespacedKey to indicate whether it will return a Sound object or a NamespacedKey object. A SoundType can be provided to specify whether the SoundArgument will return a Sound or NamespacedKey. If no SoundType is provided, the SoundArgument will default to returning a Sound object:

// Makes a SoundArgument that returns a Sound
new SoundArgument<Sound>("sound");
new SoundArgument<Sound>("sound", SoundType.SOUND);

// Makes a SoundArgument that returns a NamespacedKey
new SoundArgument<NamespacedKey>("sound", SoundType.NAMESPACED_KEY);

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>("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", SoundType.NAMESPACED_KEY))
    .executesPlayer((player, args) -> {
        player.getWorld().playSound(player.getLocation(), ((NamespacedKey) args[0]).asString(), 100.0f, 1.0f);
    })
    .register();
CommandAPICommand("sound")
    .withArguments(SoundArgument<Sound>("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", SoundType.NAMESPACED_KEY))
    .executesPlayer(PlayerCommandExecutor { player, args ->
        player.world.playSound(player.location, (args[0] as NamespacedKey).asString(), 100.0f, 1.0f)
    })
    .register()