Items System¶
The items module provides item management, inventory, equipment, and loot drop systems.
Item Base Classes¶
Item system for equipment, consumables, and quest items.
This module provides the base item system that can be extended for different item types.
- class barebones_rpg.items.item.ItemType(value)[source]¶
Bases:
EnumTypes of items.
- WEAPON = 1¶
- ARMOR = 2¶
- ACCESSORY = 3¶
- CONSUMABLE = 4¶
- QUEST = 5¶
- MATERIAL = 6¶
- KEY = 7¶
- MISC = 8¶
- class barebones_rpg.items.item.EquipSlot(value)[source]¶
Bases:
EnumEquipment slots.
- WEAPON = 'weapon'¶
- HEAD = 'head'¶
- BODY = 'body'¶
- LEGS = 'legs'¶
- FEET = 'feet'¶
- HANDS = 'hands'¶
- ACCESSORY_1 = 'accessory_1'¶
- ACCESSORY_2 = 'accessory_2'¶
- SHIELD = 'shield'¶
- class barebones_rpg.items.item.Item(**data)[source]¶
Bases:
BaseModelBase item class.
All items in the game inherit from this class.
Example
>>> sword = Item( ... name="Iron Sword", ... item_type=ItemType.WEAPON, ... description="A basic iron sword", ... stat_modifiers={"atk": 5}, ... range=1 ... ) >>> bow = Item( ... name="Longbow", ... item_type=ItemType.WEAPON, ... base_damage=8, ... range=5 ... ) >>> potion = Item( ... name="Health Potion", ... item_type=ItemType.CONSUMABLE, ... on_use=lambda entity: entity.heal(50) ... ) >>> legendary = Item( ... name="Legendary Sword", ... item_type=ItemType.WEAPON, ... unique=True, ... base_damage=50 ... )
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- barebones_rpg.items.item.create_weapon(name, base_damage, damage_type='physical', range=1, description='', value=0, **kwargs)[source]¶
Create a weapon item.
- Parameters:
name (
str) – Weapon namebase_damage (
int) – Base damage dealt by weapondamage_type (
str) – Type of damage (physical, magic, or custom)range (
int) – Weapon range (1=melee, higher for ranged)description (
str) – Descriptionvalue (
int) – Gold value**kwargs – Additional properties (stat_modifiers, metadata, etc.)
- Return type:
- Returns:
Weapon item
Example
>>> sword = create_weapon("Iron Sword", base_damage=10, range=1) >>> bow = create_weapon("Longbow", base_damage=8, range=5) >>> spear = create_weapon("Spear", base_damage=9, range=2)
- barebones_rpg.items.item.create_armor(name, physical_defense=0, magic_defense=0, slot=EquipSlot.BODY, description='', value=0, **kwargs)[source]¶
Create an armor item.
Inventory System¶
Inventory management system.
This module provides inventory functionality for managing collections of items.
- class barebones_rpg.items.inventory.Inventory(**data)[source]¶
Bases:
BaseModelInventory for storing items.
Supports auto-stacking, slot limits, and weight limits.
Example
>>> inv = Inventory(max_slots=20) >>> sword = create_weapon("Sword", atk=5) >>> inv.add_item(sword) >>> print(inv.count_items()) 1
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- count_items()[source]¶
Count total number of item slots used.
- Return type:
- Returns:
Number of slots used
- get_available_slots()[source]¶
Get number of available slots.
- Return type:
- Returns:
Number of free slots
- class barebones_rpg.items.inventory.Equipment(**data)[source]¶
Bases:
BaseModelEquipment manager for equipped items.
Manages which items are equipped in which slots.
Example
>>> equipment = Equipment() >>> sword = create_weapon("Sword", atk=5) >>> equipment.equip(sword) >>> print(equipment.get_total_stat_bonus("atk")) 5
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Loot System¶
Loot drop system for handling item drops from enemies and containers.
This module provides functionality for rolling on loot tables and managing dropped items.
- class barebones_rpg.items.loot.LootDrop(item, source=None, quantity=1)[source]¶
Bases:
objectRepresents an item that has been dropped.
Example
>>> from barebones_rpg.items import LootDrop, create_material >>> bone = create_material("Bone", value=5) >>> drop = LootDrop(item=bone, source=goblin, quantity=1)
- __init__(item, source=None, quantity=1)¶
- barebones_rpg.items.loot.roll_loot_table(loot_table, source=None)[source]¶
Roll on a loot table to determine which items drop.
The loot table should be a list of dictionaries with the following format:
[ {"item": "Goblin Bone", "chance": 0.3}, # 30% drop chance, lookup by name {"item": bone_item, "chance": 0.5}, # 50% drop chance, direct Item object {"item": "Rare Gem", "chance": 0.05} # 5% drop chance ]
For string item names, the LootManager is used to retrieve the item template. For Item objects, a deep copy is created. Each entry is rolled independently, so multiple items can drop from a single table.
- Parameters:
- Return type:
- Returns:
List of LootDrop objects for items that successfully dropped
Example
>>> from barebones_rpg.items import roll_loot_table, LootManager, create_material >>> >>> # Setup manager >>> LootManager().register("Bone", create_material("Bone", value=5)) >>> >>> # Define loot table >>> loot_table = [ ... {"item": "Bone", "chance": 0.5}, ... {"item": create_material("Scale", value=10), "chance": 0.1} ... ] >>> >>> # Roll for loot >>> drops = roll_loot_table(loot_table, source=goblin) >>> for drop in drops: ... print(f"Dropped: {drop.item.name}")
- barebones_rpg.items.loot.create_loot_entry(item, chance, quantity=1)[source]¶
Helper function to create a loot table entry.
- Parameters:
- Return type:
- Returns:
Dictionary formatted for use in loot tables
Example
>>> entry = create_loot_entry("Goblin Bone", 0.3) >>> entry2 = create_loot_entry(rare_item, 0.05, quantity=3) >>> loot_table = [entry, entry2]
Loot Manager¶
Global loot manager for managing item drops.
This module provides a singleton manager for mapping item names to item templates or factory functions, enabling both data-driven and code-first loot systems.
- class barebones_rpg.items.loot_manager.LootManager(*args, **kwargs)[source]¶
Bases:
objectGlobal manager for loot items (Singleton).
The LootManager allows registering item templates or factory functions that can be referenced by name in loot tables. It supports both static items (templates that get deep copied) and dynamic items (factory functions).
It also tracks unique items to ensure they only drop once per game.
Example
>>> from barebones_rpg.items import LootManager, create_material, Item >>> >>> # Register a static template >>> bone = create_material("Goblin Bone", value=5) >>> LootManager().register("Goblin Bone", bone) >>> >>> # Register a factory function >>> def random_potion(): ... import random ... healing = random.randint(30, 50) ... return create_consumable(f"Potion ({healing}hp)", ... on_use=lambda t, c: t.heal(healing)) >>> LootManager().register("Random Potion", random_potion) >>> >>> # Get items (creates new instances) >>> item1 = LootManager().get("Goblin Bone") >>> item2 = LootManager().get("Goblin Bone") >>> assert item1.id != item2.id # Different instances
- register(name, item_or_factory)[source]¶
Register an item template or factory function.
Automatically registers any callbacks (on_use) to the CallbackRegistry for serialization support.
- Parameters:
- Return type:
Example
>>> LootManager().register("Gold Coin", create_material("Gold Coin", value=1)) >>> LootManager().register("Random Weapon", lambda: create_weapon(...))
- get(name)[source]¶
Get an item by name, creating a new instance.
For item templates, creates a deep copy. For factory functions, calls the function. Tracks unique items and returns None if a unique item has already been dropped.
- Parameters:
name (
str) – Name of the item to retrieve- Return type:
- Returns:
New Item instance, or None if not found or unique already dropped
Example
>>> item = LootManager().get("Goblin Bone") >>> if item: ... player.inventory.add_item(item)
- has(name)[source]¶
Check if an item is registered.
Example
>>> if LootManager().has("Goblin Bone"): ... print("Item is registered")
- clear()[source]¶
Clear the entire manager.
This removes all registered items and resets unique item tracking. Useful for testing or resetting game state.
Example
>>> LootManager().clear() # Start fresh
- Return type:
- reset_unique_tracking()[source]¶
Reset unique item tracking without clearing the registry.
This allows unique items to drop again without re-registering them. Useful for new game+ or testing.
Example
>>> LootManager().reset_unique_tracking()
- Return type: