LootTable argument
The LootTableArgument
class can be used to get a Bukkit LootTable
object.
Example – Filling a chest with loot table contents
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:
mccmd
/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:
java
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();