LootTable argument

A loot table argument showing a list of Minecraft loot tables as suggestions

The LootTableArgument class can be used to get a Bukkit LootTable object.

Example - Filling a chest with loot table contents

Say we wanted to write a command that populates a chest with some loot table contents. For this example, we'll use the following command:

/giveloottable <loottable> <location>

We ensure that the location provided is a container (such as a chest or shulkerbox) and then update the contents of that container:

new CommandAPICommand("giveloottable")
    .withArguments(new LootTableArgument("lootTable"))
    .withArguments(new LocationArgument("location", LocationType.BLOCK_POSITION))
    .executes((sender, args) -> {
        LootTable lootTable = (LootTable) args.get("lootTable");
        Location location = (Location) args.get("location");

        BlockState state = location.getBlock().getState();

        // Check if the input block is a container (e.g. chest)
        if (state instanceof Container container && state instanceof Lootable lootable) {
            // Apply the loot table to the chest
            lootable.setLootTable(lootTable);
            container.update();
        }
    })
    .register();
CommandAPICommand("giveloottable")
    .withArguments(LootTableArgument("lootTable"))
    .withArguments(LocationArgument("location", LocationType.BLOCK_POSITION))
    .executes(CommandExecutor { _, args ->
        val lootTable = args["lootTable"] as LootTable
        val location = args["location"] as Location

        val state = location.block.state

        // Check if the input block is a container (e.g. chest)
        if (state is Container && state is Lootable) {
            // Apply the loot table to the chest
            state.lootTable = lootTable
            state.update()
        }
    })
    .register()
commandAPICommand("giveloottable") {
    lootTableArgument("loottable")
    locationArgument("location", LocationType.BLOCK_POSITION)
    anyExecutor { _, args ->
        val lootTable = args["loottable"] as LootTable
        val location = args["location"] as Location

        val state = location.block.state

        // Check if the input block is a container (e.g. chest)
        if (state is Container && state is Lootable) {
            // Apply the loot table to the chest
            state.lootTable = lootTable
            state.update()
        }
    }
}