Food Consumption API
The food consumption system provides OSRS-accurate eating mechanics with 3-tick eat delay and combat attack delay integration.EatDelayManager
Manages per-player eating cooldowns. Location:packages/shared/src/systems/shared/character/EatDelayManager.ts
Methods
canEat()
Check if player can eat (not on cooldown).playerId- Player to checkcurrentTick- Current game tick
true if player can eat, false if still on cooldown
Example:
recordEat()
Record that player just ate.playerId- Player who atecurrentTick- Current game tick
getRemainingCooldown()
Get remaining cooldown ticks.playerId- Player to checkcurrentTick- Current game tick
0 if ready to eat, otherwise ticks remaining
Example:
clearPlayer()
Clear player’s eat cooldown (on death, disconnect).clear()
Clear all state (for testing or server reset).getTrackedCount()
Get the number of tracked players (for debugging/monitoring).CombatSystem Extensions
TheCombatSystem provides methods for eat delay integration.
Location: packages/shared/src/systems/shared/combat/CombatSystem.ts
Methods
isPlayerOnAttackCooldown()
Check if player is on attack cooldown.playerId- Player to checkcurrentTick- Current game tick
true if player has pending attack cooldown
Example:
addAttackDelay()
Add delay ticks to player’s next attack.playerId- Player to modifydelayTicks- Ticks to add to attack cooldown
OSRS-Accurate: Only called when player is ALREADY on cooldown. If weapon is ready, eating does not add delay.
PlayerSystem Extensions
ThePlayerSystem handles food consumption.
Location: packages/shared/src/systems/shared/character/PlayerSystem.ts
Event Handlers
handleItemUsed()
Handles food consumption with OSRS-accurate timing. Triggered by:ITEM_USED event (emitted by InventorySystem)
Validation:
- Player ID validation (string type check)
- Eat delay check (3-tick cooldown)
- Heal amount bounds checking (
Math.min(..., MAX_HEAL_AMOUNT)) - Item type check (consumable/food only)
- Validate player ID
- Check eat delay (reject if on cooldown)
- Record eat action
- Consume food (emit
INVENTORY_REMOVE_ITEM) - Apply healing (emit
PLAYER_HEALTH_UPDATEDif health changed) - Show OSRS-style message
- Apply attack delay if in combat
Network Protocol
useItem Packet
Client sendsuseItem packet to consume food.
Packet: useItem
Payload:
handleUseItem() in packages/server/src/systems/ServerNetwork/handlers/inventory.ts
Validation:
- Rate limiting (3/sec via
getConsumeRateLimiter()) - Payload structure validation
- Item ID validation (
isValidItemId()) - Slot bounds checking (
isValidInventorySlot())
Constants
Combat Constants
Rate Limiting
getEquipRateLimiter() to allow OSRS-style PvP gear+eat combos.
Events
INVENTORY_USE
Emitted by server handler when player uses an item. Payload:InventorySystem
ITEM_USED
Emitted byInventorySystem after validating item exists at slot.
Payload:
PlayerSystem
PLAYER_HEALTH_UPDATED
Emitted when player health changes (healing or damage). Payload:StatusBars.tsx)
Security Features
Input Validation
All inputs validated server-side:Rate Limiting
Consume-After-Check
Food is only removed AFTER all validation passes:Testing
Unit Tests
EatDelayManager (__tests__/EatDelayManager.test.ts):
- 197 lines, 16 test cases
- Boundary conditions (tick 3 exact boundary)
- Multi-player isolation
- Cleanup edge cases
- OSRS timing accuracy
__tests__/CombatSystem.eatDelay.test.ts):
- 236 lines, integration tests
- Mid-combat eating scenario
- Weapon-ready eating scenario
- State consistency (both
nextAttackTicksandCombatDataupdated)