World - Loot Tables Module
anvil.api.world.loot_tables
LootTable
Bases: AddonObject
A Minecraft Bedrock loot table for defining random item/block drops and rewards.
Loot tables are used throughout Minecraft to define what items drop when: - Entities die - Blocks are broken - Chests generate - Fishing occurs - Trading with villagers
Each loot table contains one or more pools, and each pool contains weighted entries that can have functions applied to modify the resulting items.
Documentation reference
identifier
property
Returns the identifier of the addon object in the format 'namespace:name'.
Returns:
| Name | Type | Description |
|---|---|---|
str |
Identifier
|
The identifier of the addon object. |
name
property
Returns the name of the addon object.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The name of the addon object. |
table_path
property
Get the relative path of this loot table for referencing.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Relative path from behavior pack root. |
__export__()
Exports the addon object after potentially shortening its content and replacing backslashes. Logs the event and writes the object to a file.
__init__(name)
Initialize a LootTable instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the loot table (used for filename and referencing). |
required |
content(content)
Sets the content of the addon object and returns the object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
any
|
The content to be set for the addon object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
'AddonObject'
|
The instance of the current AddonObject. |
pool(rolls=1)
Create a new loot pool in this loot table.
Pools are rolled independently, so multiple pools allow for multiple categories of loot with different roll counts and mechanics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rolls
|
int | list[int, int]
|
Number of times to roll this pool. Can be exact number or [min, max] range. Defaults to 1. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
_LootPool |
_LootPool
|
The created pool for adding entries and configuration. |
queue()
Queue this loot table for generation in the behavior pack.
Exports all pools and their entries to JSON format and adds the loot table file to the generation queue.
Returns:
| Name | Type | Description |
|---|---|---|
LootTable |
LootTable
|
Self for method chaining. |
set_identifier_data(data)
Sets the data of the addon object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
any
|
The data to be set for the addon object. |
required |
Usage Example
from anvil.api.world.loot_tables import LootTable
from anvil.api.vanilla.items import MinecraftItemTypes
table = LootTable("my_loot")
pool = table.pool(rolls=1)
# Add simple items
pool.entry(MinecraftItemTypes.Apple(), weight=5)
pool.entry(MinecraftItemTypes.GoldenApple(), weight=1)
# Add complex items with functions
pool.entry(MinecraftItemTypes.DiamondSword(), weight=1).enchant_with_levels(30)
table.queue()