Skip to content

Player Inventory

RocketMod exposes a surface for player inventory through UnturnedPlayer. This article documents the inventory-related types, properties, methods, and events that RocketMod provides in its own API. It does not cover the underlying SDG.Unturned PlayerInventory component, which sits below RocketMod's wrapper layer and is not part of the RocketMod API.

Prerequisites

  • Articles 1 through 7 (Fundamentals track), especially article 6 (Event Subscription and Lifecycle) for inventory event timing.
  • Article 33 (Error Handling and Logging) for defensive access patterns.

What you'll learn

  • The InventoryGroup enum and what each group represents.
  • The UnturnedPlayer.Inventory property and how RocketMod exposes inventory.
  • The UnturnedPlayer.GiveItem method for adding items.
  • The inventory lifecycle events on UnturnedPlayerEvents.

The InventoryGroup enum

RocketMod defines the InventoryGroup enum in the Rocket.Unturned.Enumerations namespace. It represents the logical inventory pages a player has:

Enum valueDescription
PRIMARYPrimary weapon slot
SECONDARYSecondary weapon slot
HANDSCurrently held item
VESTVest storage slots
SHIRTShirt storage slots
PANTSPants storage slots
BACKPACKBackpack storage slots
STORAGEExternal container storage

The enum values are ordered and can be cast to integers for array-style indexing into the underlying SDG.Unturned inventory component, though that lower layer is not part of the RocketMod API.

Accessing the inventory

RocketMod provides the Inventory property on UnturnedPlayer:

csharp
using Rocket.Unturned.Player;

UnturnedPlayer player = /* ... */;
var inventory = player.Inventory;

The return type of Inventory is determined by the underlying SDG.Unturned PlayerInventory component. RocketMod's own API surface does not expose a separate wrapper type for it -- Inventory gives you a direct reference to that component.

The Player property on UnturnedPlayer provides access to the full SDG.Unturned player object:

csharp
var sdgPlayer = player.Player;

From there, the SDG.Unturned API layer (player.Player.inventory, PlayerInventory, Items, tryAddItem, etc.) is available, but its types and methods are not part of the RocketMod API surface and are not documented here.

Giving items: UnturnedPlayer.GiveItem

RocketMod provides UnturnedPlayer.GiveItem for adding items to a player's inventory:

csharp
player.GiveItem(item);

This method is the RocketMod-level entry point for inventory delivery. The item must be assembled first -- see the next article, Item Assembly and Custom Items, for UnturnedItems.AssembleItem and how items are constructed before they can be given.

Inventory events

RocketMod exposes inventory-related events through UnturnedPlayerEvents. These fire when items are added, removed, or when inventory dimensions change.

OnInventoryAdded / OnPlayerInventoryAdded

Fires when an item enters the player's inventory:

csharp
UnturnedPlayerEvents.OnInventoryAdded += (player, group, slot, item) =>
{
    // An item was added to player's inventory.
};

OnPlayerInventoryAdded is an alias for the same event.

OnInventoryRemoved / OnPlayerInventoryRemoved

Fires when an item is removed from the player's inventory:

csharp
UnturnedPlayerEvents.OnInventoryRemoved += (player, group, slot, item) =>
{
    // An item was removed from player's inventory.
};

OnPlayerInventoryRemoved is an alias for the same event.

OnInventoryResized / OnPlayerInventoryResized

Fires when an inventory group changes size, such as when a player equips or removes clothing or a backpack:

csharp
UnturnedPlayerEvents.OnInventoryResized += (player, group, newWidth, newHeight) =>
{
    // The storage dimensions for a group changed.
};

OnPlayerInventoryResized is an alias for the same event.

OnInventoryUpdated / OnPlayerInventoryUpdated

Fires when inventory state is refreshed:

csharp
UnturnedPlayerEvents.OnInventoryUpdated += (player) =>
{
    // Inventory state was updated.
};

OnPlayerWear

Fires when a player equips or unequips wearable items (clothing, armor):

csharp
UnturnedPlayerEvents.OnPlayerWear += (player, item, slot) =>
{
    // Player changed a worn item.
};

Event name aliases

RocketMod provides two naming conventions for inventory events. The shorter names (OnInventoryAdded, OnInventoryRemoved, OnInventoryResized, OnInventoryUpdated) and the prefixed names (OnPlayerInventoryAdded, OnPlayerInventoryRemoved, etc.) point to the same events. Either set works; choose whichever fits your code style.

What RocketMod does not provide

RocketMod's own API surface does not include slot-level inventory manipulation (removing items by slot index, clearing the inventory, searching for items by ID, or counting items). Those operations require reaching through player.Player into the SDG.Unturned PlayerInventory component, which is a separate API layer. RocketMod does not wrap or re-expose those operations.

Similarly, UnturnedPlayer does not expose helper methods for checking whether a player is connected, for sending chat messages, or for dropping items to the ground. Those are separate concerns covered by other RocketMod types (UnturnedChat, events on UnturnedEvents) or the SDG.Unturned layer.

Edge cases

Null inventory reference

If the player's Unity component tree has not fully initialized, player.Player or player.Inventory may be null. Check for null before accessing either property.

Disconnected player

If the player disconnects, the underlying Unity components are destroyed. Accessing player.Player or player.Inventory after disconnection may throw a null reference exception. RocketMod's UnturnedEvents.OnPlayerDisconnected fires before the player object is cleaned up, giving your plugin a window to stop any pending inventory operations.

Full inventory

When using UnturnedPlayer.GiveItem, if the inventory has no available slot, the item is not added. The method's return value indicates success or failure.

Cross-references

What changed in this revision

  • Removed GiveItemToPlayer() helper method: not in RocketMod API. Real method is UnturnedPlayer.GiveItem(bool).
  • Removed GiveItemToAnyGroup() helper method: not in RocketMod API, and byte.MaxValue behaviour is not a documented RocketMod contract.
  • Removed GiveItemToSlot() helper method: invented; no slot-level tryAddItem(...) overload with slot index is in the RocketMod API surface.
  • Removed RemoveItemAtSlot() helper method: invented; removeItem() is SDG.Unturned, not RocketMod.
  • Removed RemoveItemsById() helper method: invented; item-ID iteration over Items is SDG.Unturned, not RocketMod.
  • Removed ClearInventory() helper method: invented; bulk clear logic relies on SDG.Unturned methods not in the RocketMod surface.
  • Removed HasItem() helper method: invented; search logic uses SDG.Unturned types.
  • Removed CountItems() helper method: invented; counting logic uses SDG.Unturned types.
  • Removed ApplyLoadout() helper method: invented; loadout system is not a RocketMod feature.
  • Removed BatchGiveItems() helper method: invented; batching logic uses SDG.Unturned types.
  • Removed GetAllItems() helper method: invented; uses ItemJar type not in RocketMod API.
  • Removed OnItemEquipped event: not found in UnturnedPlayerEvents in the real API surface.
  • Removed SDG.Unturned type references (PlayerInventory, tryAddItem, Items, getItem, removeItem, getItemCount, Item, ItemAsset, Assets.find, EAssetType, ItemJar, ItemGrant, Provider, ItemManager.dropItem): none are in the RocketMod API surface.
  • Corrected false claim that UnturnedPlayer.Inventory does not exist: it is a real property on UnturnedPlayer in the verified API surface.