Skip to main content

Items System

The items system in RPGJS allows you to manage player inventory, equipment, and item usage. You can add items to players in three different ways: using string IDs, item classes, or item objects.

Adding Items

Method 1: String ID (Pre-registered Items)

Items must be registered in the database property of provideServerModules before they can be used with string IDs.
Then you can use it:
Note: The @RpgModule decorator is available for backward compatibility with v4, but the modern approach is to use provideServerModules with the database property directly.

Method 2: Item Class (Auto-registered)

When using an item class directly, it will be automatically added to the map’s database if not already present.

Method 3: Item Object (Dynamic Items)

You can create items dynamically using objects. These are automatically added to the map’s database.
If you don’t provide an id, one will be auto-generated:

Dynamic Item Creation in Events

When creating items dynamically (especially in events), you should first add them to the map’s database, then add them to the player.

Example: Event Giving an Item

Using String ID After Dynamic Registration

If you want to use a string ID after dynamically adding an item:

Item Hooks

Items can implement lifecycle hooks that are called at specific moments:
  • onAdd(player): Called when the item is added to inventory
  • onUse(player): Called when the item is successfully used
  • onUseFailed(player): Called when item usage fails (e.g., chance roll)
  • onRemove(player): Called when the item is removed from inventory
  • onEquip(player, equip): Called when the item is equipped/unequipped

Using Classes

Using Objects

Managing Database

Adding Items to Database

Removing Items from Database

Complete Example: Shop Event