Inventory API
Server exports for a player's pockets, your items, the accessory strip and the player's hands. New here? Read Inventory and Items first; for one slot or another store, see Stores.
Giving and taking
Gives a player an item, pockets first, then the strip. If it fits nowhere it's dropped at their feet (why), unless Config.DropOnFullInventory = false or opts.noDrop: then nothing is given, so check before you charge.
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
| item | string | A registered item key |
| amount | number? | A whole number, 1 or more (default 1) |
| metadata | table? | Its metadata; nil to skip it and still pass opts |
| opts | string|table? | A reason for the activity log ('quest reward'), or { reason, noDrop = true, pocket = 'player'|'accessory', coords }: refuse rather than drop, only that pocket, drop there instead of at their feet |
'inventory', 'ground' (they're told), or false, reason with nothing created: 'Unknown item "drnk"', 'Amount must be a whole number of 1 or more', noDrop with no room, rarely no ped or a failed droplocal where, err = exports.whip_core:AddItem(source, 'drink', 2, false, 'quest reward')
if not where then print('nothing was given: ' .. err) end
exports.whip_core:AddItem(source, 'cash', 100, false, { pocket = 'accessory', noDrop = true }) -- the Money slot
if not where then never refunds an item that exists. whip_core:item:added doesn't fire for it.Takes an item from the pockets, then the accessory strip, in AddItem's shape. Nothing is taken unless they have the full amount.
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
| item | string | Item key |
| amount | number? | A whole number, 1 or more (default 1) |
| metadata | table? | Only the stacks whose metadata carries every one of these values ({ serial = 'AB12' }) |
| opts | table? | { reason = '...' }, for the activity log (a plain reason string works too) |
bool, string|nil: false, 'Not enough items (have 0, need 1)'director or systest in its metadata), may only move within its players' pockets and the test's own cars and containers, and is taken back when the test ends (or at next login). A live Director run marks nothing: its items are real.Whether the player has at least amount, across both pockets, and inside a wallet they carry for an item registered with wallet = true (a bank card).
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
| item | string | Item key |
| amount | number? | Minimum (default 1) |
bool, string|nilHow many of an item the player carries, across both pockets, and inside a wallet they carry for an item with wallet = true. The same count everywhere: RemoveItem takes from a wallet after the pockets, an item requirement (prompts, points, doors) and the item events count it there, and the player's own game counts it too. Cash in a wallet isn't counted here; GetSpendableCash counts it.
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
| item | string | Item key |
number, string|nilWhether an item fits in the pockets, by slots and weight.
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
| item | string | Item key |
| amount | number? | Quantity (default 1) |
| metadata | table? | This instance's metadata, so its contents are weighed too |
true, or false, reason: 'Only 3 fit', 'No room', 'Unknown item "x"', 'Amount must be a whole number of 1 or more', 'Player not loaded'Finding and reading
The first stack of an item the player carries, with its address, as a copy. Within a pocket the lowest slot comes first (FindAllItems uses the same order).
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
| item | string | Item key |
| string? | 'player' or 'accessory'; nil searches all. A pocket that doesn't exist finds nothing |
{ item, amount, metadata, addr } or nilNot in the pockets and no pocket named: an item registered with wallet = true is found inside a wallet the player carries. That stack has no addr (a place inside an item is no store address); inside = { addr, slot } is the wallet's address and the place in it.
local found = exports.whip_core:FindItem(source, 'lockpick')
if found then exports.whip_core:StoreTake(found.addr, 1, { reason = 'lockpick snapped' }) end
Every stack of an item the player carries, each as a FindItem entry.
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
| item | string | Item key |
| string? | Only this pocket; nil for all |
{ item, amount, metadata, addr }, empty if none; {}, 'Player not loaded' before they loadA deep copy of a player's whole inventory: editing it changes nothing.
| Param | Type | Description |
|---|---|---|
| source | number | Server player ID |
{ player = { [slot] = { item, amount, metadata } }, accessory = { ... } }, or {}, 'Player not loaded'Registering items
Registers an item; it joins Config.Items. Declare it in Whip.register: the same definition again changes nothing, and another running resource can't redefine your key.
| Param | Type | Description |
|---|---|---|
| key | string | Letters, digits, _ and -, 50 at most. No colon, unlike container, door and event names: prefix with an underscore ('mypd_evidence') |
| definition | table | See Item definition |
true, or false, reasonMakes an item's metadata when it's created without any: given (AddItem, StoreGive), picked up, shown on an infinite shop's shelf, or bought.
| Param | Type | Description |
|---|---|---|
| itemKey | string | Item key |
| generator | function | function(itemKey, ctx) → table. ctx.source, ctx.charId: the receiver, nil into a stash or boot. ctx.reason: 'given', 'pickup', 'shelf' or 'bought'. ctx may be nil (Director rewards) |
| opts | table? | { forBuyer = true }: bought off an infinite shop's shelf, it's made again for the buyer. Stocked shops sell real items, which keep their metadata |
true, or false, reason: another resource has a generator for that item-- A SIM card with its buyer's new number (whip_phone)
exports.whip_core:RegisterItemDefaultMetadata('sim_card', function(itemKey, ctx)
if not (ctx and ctx.charId) then return {} end -- for nobody yet: blank
return { phoneNumber = NewNumber(), owner = ctx.charId }
end, { forBuyer = true })
GetItemSetting: an item's setting as in force; on the server also 'name', 'desc' and 'weight'. OnItemSettingChanged runs fn after a change (Reset included; on a client, also when values first arrive). One per resource per setting, replaced by calling again, gone when your resource stops.
| Param | Type | Description |
|---|---|---|
| item | string | The item's key |
| key | string | The setting's key |
| fn | function | function(value, old, item, key) |
GetItemSetting: the value, or nil (no such item or key; on a client, before the values arrive). OnItemSettingChanged: true, or false, reasonUsing items and right-click options
What happens when a player uses an item (hotbar or Use). The handler gets (source, player, slotData, addr): player is a data sheet (GetPlayer's table, less metadata), slotData is { item, amount, metadata }, addr its address. Return true or nothing to use one up, false to keep it, false, 'why' to refuse (Using an item).
| Param | Type | Description |
|---|---|---|
| itemKey | string | Item key; the item needs usable = true, or players only see "Item not usable" (the console warns) |
| handler | function|table | function(source, player, slotData, addr) → boolean, or a table for a progress bar (below) |
true, or false, reason: no key or handler, a table without a positive duration, or an item another resource handles (the registration rule)-- Wears out: keep the pick, and take a point of durability off THIS one
exports.whip_core:RegisterUseHandler('pickaxe', function(source, player, slotData, addr)
local left = (slotData.metadata and slotData.metadata.durability or 100) - 1
exports.whip_core:SetItemMetadata(addr, { durability = left })
return false
end)
A use with a progress bar: pass a table#
WHIP runs canUse, the server-timed bar, checks the item is still there, takes it, then calls onComplete. false, 'why' or an error from onComplete gives it back. One timed use at a time per player.
exports.whip_core:RegisterUseHandler('field_dressing', {
label = 'Bandaging...', duration = 4000,
anim = { dict = 'missheistdockssetup1clipboard@idle_a', clip = 'idle_a' },
canUse = function(source) -- 200 is full health
if exports.whip_core:GetStat(source, 'health') >= 200 then return false, 'You are not hurt' end
return true
end,
onComplete = function(source) -- RETURN its answer: a failed heal gives the item back
return exports.whip_core:AddStat(source, 'health', 25)
end,
})
| Field | Type | Description |
|---|---|---|
| duration | number | Required. ms |
| label | string? | Default 'Using <item name>...' |
| anim, prop, disable, cancelOnDeath, cancelOnExit, notWhile | table|boolean | As the client Progress; stopped, they keep the item. cancelOnExit: leaving the seat stops it. notWhile = { 'swimming', 'falling' }: won't start or go on there |
| icon | string? | For an item with no picture ('eat', 'fuel'…) |
| canCancel | boolean? | Default true: X stops it, and they keep the item |
| consume | boolean? | Default true; false keeps it (a tool) |
| reason | string? | For the item log. Default 'used' |
| canUse | function? | function(source, player, slotData, addr) → true, or false, 'why' before the bar |
| onComplete | function? | function(source, player, item, addr), after the item is taken. A bare false also gives it back (no message, no cooldown), unlike the function form |
Examples: HUD Framework's drunk effect, whip_core/server/items/armour.lua.
Progress(source, opts).The handler for a right-click option, run when a player picks it from an item's menu.
| Param | Type | Description |
|---|---|---|
| actionKey | string | As named by an item's defaultAction or contextOptions; 32 characters at most. Prefix it with your resource's name ('my_bows:equip'): names are server-wide |
| handler | function | function(source, player, slotData, addr, how): a use handler's arguments, plus how = { from = 'ground' } right after a ground option's pick-up (skip your draw animation). Return false, 'why' to refuse |
true, or false, reason: a name another resource holds, WHIP's own included (the registration rule)Adds a right-click option to any item, yours or another resource's. It goes when your resource stops.
| Param | Type | Description |
|---|---|---|
| itemKey | string | The item; it needn't be registered yet |
| option | table | A contextOptions entry, { action, label, order?, when?, require? }, with your own action |
true, or false, reason: a malformed option, an action the item already has, or one another resource addedexports.whip_core:AddItemOption('gunny_pistol', {
action = 'my_cops:prints', label = 'Check for prints', require = { job = 'police', onDuty = true },
})
Takes back an option you added. They all go anyway when your resource stops.
true, or false, reasonItem lookups#
Every item definition.
{ [key] = definition }Pockets and the accessory strip
The pockets a character carries, in fallback order ({ 'player', 'accessory' }), as a copy. For a slot a client names, use the 'slot' SecureEvent param type instead: it checks the pocket, range and contents before your handler runs.
string[]Whip.register. Config.AccessoryStrip takes effect at whip_core's next start, so a list kept from your own start goes stale.A pocket's slot count, or the whole table.
| Param | Type | Description |
|---|---|---|
| string? | 'player', 'accessory', or omit for all |
number | tableA pocket's weight limit, or the whole table. 'player''s covers everything carried, the strip included; 'accessory''s is the strip's own, inside it.
| Param | Type | Description |
|---|---|---|
| string? | 'player', 'accessory', or omit for all |
number | tableReshapes one accessory slot: label, icon and what it accepts (server-enforced), until your resource stops. WHIP's own five slots (AccessoryStripSlots in shared/inventory_shape.lua: Phone, Money, then three Keys; whip_phone keeps the Phone slot to phones) are defaults any resource may replace; a slot another resource registered is refused, and so is everything with Config.AccessoryStrip off.
| Field | Type | Description |
|---|---|---|
| def.slot | number | Required. 1 to Config.ContainerSlots.accessory |
| def.accepts | string | string[]? | The item key it takes, or a list ({ 'cash', 'wallet' }). With neither this nor keys: anything |
| def.keys | boolean? | true: it takes any ignition key too (RegisterIgnitionKey's items) |
| def.label | string? | Default 'Accessory' |
| def.icon | string? | 'accessory' (pouch), 'plus', 'phone', 'vehicle_key', 'cash'; default: the accepted item's, or the pouch |
| def.iconSvg | string? | Inline SVG, over icon. Never from player input |
| def.tag | string? | A run tag ('run:42'): ReleaseTag(tag) restores WHIP's slot. Use a new tag each run |
bool ok, string|nil err: check it, as a refusal leaves the strip unchangedWhip.register(function()
Whip.core:RegisterAccessorySlot({ slot = 4, accepts = 'bandage', label = 'Medkit' })
end)
The accessory strip as it is now. accepts is a list of item keys; a Key slot has keys = true; a slot that takes anything has neither.
{ [slot] = { slot, accepts, keys, label, icon } }, a copyMakes an item an ignition key, for the ignition slot, the key checks and key sweeps; its metadata.vehicleId must still match the car. Declare it in Whip.register. It stays a key until the last resource that registered it stops.
| Param | Type | Description |
|---|---|---|
| itemName | string | A registered item key |
bool ok, string|nil errHands
Records the item your resource just put in the player's hand (a weapon, a tool), or nil when it's put away. Others then act on that exact item: whip_fuel uses the jerry can in hand, never another. The record is your resource's, cleared when it stops, at logout and on leaving, and an item moved or swapped out of the slot stops counting. The client sees it as GetHeldItem() and require = { held = ... }.
| Param | Type | Description |
|---|---|---|
| source | number | The player |
| addr | table|nil | One of their pocket slots, { kind = 'player', id = source, sub, slot }; nil to clear |
true, or false, reason (an empty slot, not their own pockets, another resource's record)What the player holds: nil when nothing is, or the item has left its slot.
{ addr, item, amount, metadata } or nillocal held = exports.whip_core:GetHeldItem(source)
if held and held.item == 'jerry_can' then print('fuel:', held.metadata.fuel) end
The client's BlockHands, set from the server for one player: jail, an arrest. The server keeps it too, for AreHandsBlocked(source, what). Cleared at logout or drop, or when your resource stops.
| Param | Type | Description |
|---|---|---|
| source | number | { charId } | The player, online |
| id | string | Your name for this block |
| opts | table? | As the client's: label, only = 'weapons' |
true, or false, reason (not online, a bad id or opts)exports.whip_core:BlockHands(target, 'cuffed', { label = 'You are handcuffed' })
exports.whip_core:UnblockHands(target, 'cuffed')
AreHandsBlocked: are the hands blocked by the server, for 'weapon' or (with nothing) for anything? Blocks the player's game set itself aren't seen here. FreeHands: have their game empty the hands now (a gun holstered), without blocking them.
AreHandsBlocked: true, words or false. FreeHands: true, or false, reason (not online)local blocked, why = exports.whip_core:AreHandsBlocked(source, 'weapon')
if blocked then return false, why end
Another player's pockets#
For admin tools and robberies: open a player's pockets beside the viewer's, online or offline, and drag items both ways.
Opens the target's pockets as a second panel. An online target's inventory is locked meanwhile; an offline one is loaded from the database and saved on close.
| Param | Type | Description |
|---|---|---|
| source | number | The viewer |
| target | number|table | A person: server ID, or { charId = n } (offline too) |
| opts | table? | { range? }: metres from an online target (default 5); false for none |
true, or false, reason (not loaded, no such character, already open, too far…)exports.whip_core:OpenPlayerInventory(adminSource, { charId = targetCharId }, { range = false })
Ends an inspection: unlocks an online target and saves both inventories.
| Param | Type | Description |
|---|---|---|
| source | number | The viewer |
true, or false, reason (not loaded, or none open)Whether someone else (an admin, a robber) has this character's pockets open. Not whether they have their own inventory open.
| Param | Type | Description |
|---|---|---|
| charId | number | Character ID |
boolean