LootTable argument
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[0];
Location location = (Location) args[1];
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();