Skip to content

World - Loot Tables Module

anvil.api.world.loot_tables

LootTable

Bases: JsonFile[_LootTableFile]

A loot table: what drops from entities, blocks, chests, fishing and other rewards.

Each pool is rolled on its own, and each roll picks one of its entries by weight. Entries take functions (entry.functions.SetCount(...)) and conditions (entry.conditions.random_chance(...)).

table = LootTable("ruins_chest")
pool = table.pool((1, 3))
pool.entry("minecraft:emerald", weight=5).functions.SetCount((1, 4))
pool.entry(None, weight=10)
table.queue()
Documentation reference

identifier property

The file's identifier.

name property

The identifier's name, without its namespace.

table_path property

The loot table's path from the behavior pack root, as other files reference it.

__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

__schema__() classmethod

The JSON Schema (draft 2020-12) of this file: the schema of its _document_type, with the file's type as title and where it is written under x-anvil-file.

Raises:

Type Description
TypeError

When the class is an abstract file base, with no document type.

pool(rolls=1)

Adds a pool to the loot table.

Parameters:

Name Type Description Default
rolls int | tuple[int, int]

How many times the pool is rolled, or the minimum and maximum. Defaults to 1.

1

Returns:

Name Type Description
_LootPool _LootPool

The pool, to add entries, conditions and tiers to.

queue()

Queues the loot table for export.

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).functions.EnchantWithLevels((30, 30))

table.queue()