Inventory System
The inventory system manages player item storage with 28 slots (OSRS standard), stackable item support, drag-and-drop operations, OSRS-style context menus, and database persistence.Inventory code lives in
packages/shared/src/systems/shared/character/InventorySystem.ts and packages/client/src/game/systems/InventoryActionDispatcher.ts.Core Constants
Inventory Structure
Each player has aPlayerInventory containing:
Money Pouch System
Hyperscape uses an RS3-style money pouch for protected coin storage:Architecture
- Money Pouch (
characters.coins): Protected storage, doesn’t use inventory slots - Physical Coins (
inventorywithitemId='coins'): Stackable item, uses inventory slot
Coin Pouch Withdrawal
Players can withdraw coins from the money pouch to inventory:Security Features
UI Components
role="button"for screen readerstabIndex={0}for keyboard focus- Enter/Space key activation
- Descriptive
aria-label
Error Handling
Testing: The coin pouch system has 32 comprehensive unit tests covering input validation, insufficient coins, inventory full, stack overflow, new stack creation, existing stack updates, and atomicity simulation.
Key Operations
Adding Items
Dropping Items
Pickup Items
Transaction Locking
For atomic operations like bank deposits, store purchases, and trades, the inventory system supports transaction locks:Database Persistence
Inventories are persisted to the database via theDatabaseSystem:
Item Consumption
Food and Healing
Players can consume food items to restore health with OSRS-accurate mechanics:OSRS-Accurate Behavior: Food is consumed even at full health, and the eat delay applies regardless. Attack delay is only added if the player is already on attack cooldown.
Eat Delay Manager
TheEatDelayManager tracks per-player eating cooldowns:
Combat Integration
When eating during combat, attack delay is added to prevent instant attacks:Context Menus
Inventory items support OSRS-style context menus with manifest-driven actions.Manifest-Driven Actions
Items can define explicitinventoryActions in their manifest:
Action Types
Item Helpers
Theitem-helpers.ts module provides type detection utilities for OSRS-accurate inventory actions:
Testing: The item-helpers module has 510 lines of comprehensive unit tests covering all type detection functions and edge cases.
Context Menu Colors
Context menus use OSRS-accurate color coding with centralized constants:All interaction handlers (NPCInteractionHandler, MobInteractionHandler, ItemInteractionHandler, etc.) now use these centralized constants instead of hardcoded values.
Inventory Events
UI Features
Hover Tooltips
Inventory and equipment items show tooltips on hover:- Follows mouse cursor
- Shows item stats and bonuses
- Displays level requirements
- Includes examine text
- Auto-hides on mouse leave
Click-to-Unequip
Equipment slots support left-click to unequip (OSRS-style):- Left-click equipped item to unequip
- Item moves to first available inventory slot
- If inventory full, shows “Your inventory is full” message
- Right-click still shows context menu with “Remove” option
This matches OSRS behavior where left-clicking equipment unequips it directly.
UI Integration
The client displays the inventory viaInventoryPanel.tsx:
- 28-slot grid layout
- Drag-and-drop item movement
- OSRS-style right-click context menus with manifest-driven actions and colored labels
- Left-click primary actions (Eat, Wield, Use, etc.) using
getPrimaryAction() - Shift-click to drop (OSRS-style instant drop)
- Invalid target feedback: “Nothing interesting happens.” when using item on invalid target
- Stack quantity display with OSRS-style formatting
- Coin pouch separate display
- Cancel option always shown last in context menus
- Performance optimizations:
useMemoanduseCallbackfor render efficiency
Context Menus
Manifest-Driven Actions
Items define their actions initems.json:
Action Ordering
Actions are ordered by priority (first action is left-click default):InventoryActionDispatcher
TheInventoryActionDispatcher is the single source of truth for handling inventory actions. It eliminates duplication between context menu selections and left-click primary actions:
Testing: The dispatcher has 333 lines of comprehensive unit tests covering all action types, error handling, and edge cases.
Equipment System Integration
The inventory system integrates with the equipment system for atomic equip/unequip operations that prevent item duplication.Atomic Equip Operation
Atomic Unequip Operation
New InventorySystem Helper Methods
- Transaction locks prevent concurrent equip/unequip race conditions
- Order of operations prevents both duplication and item loss:
- Equip: Remove from inventory FIRST, then equip
- Unequip: Clear equipment FIRST, then add to inventory
- Rollback on failure restores state if operation fails
- Inventory validation checks space before unequipping
Related Documentation
- Equipment System (equip/unequip mechanics)
- Bank System (bank storage)
- Trading System (player-to-player trading)
- Ground Items & Loot (item drops)
- Item Data Structure (item definitions)
- Combat System (eat delay mechanics)