Esc to clear, Enter to open the best match, click an export to open it in its page
Inventory

Panels

A panel is a slot grid that opens beside the player's inventory, with drag and drop and no NUI of your own. The server holds its items and checks every move.

OpenPanel opens a standalone panel, such as a crafting station; you save what's left from its onClose. RegisterPanelItem makes an item hold items, such as a wallet, saved in its metadata as they change.

Opening a panel

Everything goes in one config table, hooks included; every field is on the OpenPanel card.

local ok, err = exports.whip_core:OpenPanel(source, {
    id           = 'refine:input_' .. refineryId,   -- its address: { kind = 'panel', id = id }
    label        = 'Crusher — Input',
    slots        = 6,
    items        = r.input,                         -- what it held last time
    allowedItems = { 'old_radio', 'broken_phone' },

    -- The last person has walked away: keep what's left
    onClose = function(ctx)
        r.input = ctx.items   -- { [slot] = { item, amount, metadata } }
    end,
})
if not ok then print(err) end

The three hooks#

All optional, all in the OpenPanel config, each handed one table, ctx.

HookRunsDoes
onValidate(ctx)Before a player's move into, out of or within the panel, or a Split or Destroy on its slotsReturn false, 'reason' to refuse; the player sees the reason. Anything else allows it; an error refuses
onChange(ctx)After a player's move, Split or Destroy has landedReacts: pay for it, start a machine. ctx.items is what the panel holds now
onClose(ctx)Once, when the last viewer leavesSaves what was left. ctx is { panel, source, items }. Without one, players get back what they put in, and your stock goes

ctx for onValidate and onChange:

FieldWhat it is
source, panelThe player moving the item, and the panel's id
direction'in', 'out', or 'within' (panel slot to panel slot; a Split is one)
slot, otherSlotThe panel's slot, and the slot at the other end
otherThe store at the other end, as an address: { kind = 'player', id = source, sub = 'player', slot = 4 }. nil when destroying
destroyedtrue when a developer in developer mode uses Destroy: direction is 'out', no other
item, amount, metadataWhat is moving
swaponValidate only: true on the returning half of a swap, inside one panel too
replacingonValidate only: on a swap's forward half, { item, amount, metadata } of what will go back, so "one magazine at a time" can allow a swap. nil when nothing is displaced
itemsonChange only: what the panel holds now, a copy keyed by slot
hostRegisterPanelItem only: the item the panel belongs to

Act on what a player drags in from onChange: by then the move has happened, so you never pay for something that didn't arrive.

-- A pawn counter: whatever is dragged in is bought on the spot
local PRICES = { gold_ring = 150, old_watch = 80 }

exports.whip_core:OpenPanel(source, {
    id = 'pawn:' .. source, label = 'Sell to the pawn shop', slots = 1,
    allowedItems = { 'gold_ring', 'old_watch' },
    onChange = function(ctx)
        if ctx.direction ~= 'in' then return end
        local taken = exports.whip_core:StoreTake({ kind = 'panel', id = ctx.panel, slot = ctx.slot })
        if taken then   -- PayPlayer drops cash that won't fit at their feet
            exports.whip_core:PayPlayer(ctx.source, PRICES[taken.item] * taken.amount, { method = 'cash', reason = 'pawn shop' })
        end
    end,
})
The hooks belong to the panel, not to one player. Several players can share a panel id. The first OpenPanel for it that brings hooks sets them for every viewer; later openers' hooks are ignored while it's open.

Filling it from the server

A panel is a store at { kind = 'panel', id = id, slot = n }. Your own writes skip onValidate and onChange, but its slots, maxWeight, allowedItems and stack sizes apply.

exports.whip_core:StoreGive({ kind = 'panel', id = 'refine:output_1', slot = 1 }, 'steel_ingot', 4)
local panel = exports.whip_core:StoreRead({ kind = 'panel', id = 'refine:output_1' })   -- panel.items
exports.whip_core:ClosePanel(source, 'refine:output_1')

Items that hold items

The panel hangs off the item's defaultAction, or the context action named in action, which RegisterPanelItem registers for you. Contents live in metadata.slots (or storageKey), so they travel with the item.

-- The item first: a panel for an item whip_core doesn't know is refused
Whip.register(function()
    Whip.core:RegisterItem('mymech_toolbox', {
        name = 'Toolbox', weight = 2.0, stack = false,
        defaultAction  = 'mytools:open_toolbox',
        contextOptions = { { action = 'mytools:open_toolbox', label = 'Open' } },
    })
    Whip.core:RegisterPanelItem('mymech_toolbox', {
        slots = 6, label = 'Toolbox', allowedItems = { 'wrench', 'screwdriver' },
    })
end)

whip_core's wallet is the same declaration: 3 slots, allowedItems = { 'cash', 'id_card' }.

Custom storage shapes

A magazine is one record { item, ammo, serial }; a revolver's chamber is a number. Five optional RegisterPanelItem hooks handle such shapes; leave them out for a plain slot list.

host is { source, container, slot, addr, item, amount, metadata }, read fresh each time, so it's right even if the item moves while its panel is open. addr is ready for GetItemAt, SetItemMetadata and StoreTake.

Example: whip_gunny's magazine slot#

Condensed from whip_gunny/server/magpanel.lua: the magazine slot of every gun, in one declaration. The real file also covers guns with no magazine (revolvers, pump shotguns), where read shows a round count and write saves a number.

exports.whip_core:RegisterPanelItem(weaponItem, {
    action = 'whip_gunny:open_weapon',   -- a named context option, not the item's use action
    slots  = 1,
    label  = weaponCfg.label .. ' Mag',

    -- What the panel shows: the weapon's magazine, rendered as one slot. The gun keeps the
    -- magazine's WHOLE metadata (its uid and the rest), so it comes out as it went in.
    read = function(host)
        local mag = host.metadata.magazine
        if not mag then return {} end
        local item, metadata = GunnyMagazineOut(mag)   -- the record without its item name
        return { [1] = { item = item, amount = 1, metadata = metadata } }
    end,

    -- The rule allowedItems can't express: it depends on WHICH weapon, and what
    -- is already in it. The weapon is ctx.host. The reason is shown to the player.
    onValidate = function(ctx)
        local host = ctx.host
        -- Either way, not while the gun is loaded a shell at a time: the client is counting the shells.
        if GunnyLoopedReloadOpen(host.source, host.slot, host.addr.sub) then
            return false, 'Finish reloading first'
        end
        if ctx.direction ~= 'in' then return true end
        if not Config.IsMagCompatible(host.item, ctx.item) then
            return false, 'That magazine does not fit this weapon'
        end
        -- On a swap the fitted magazine is the one leaving (ctx.replacing dropping a new one onto it,
        -- ctx.swap dragging it out onto a new one), so only a SECOND one is refused.
        if host.metadata.magazine and not ctx.swap and not ctx.replacing then
            return false, 'Remove the current magazine first'
        end
        return true
    end,

    -- Fold the slot back into the weapon's own shape.
    write = function(host, items)
        local held = items[1]
        -- The magazine's whole metadata, plus its item name (server/main.lua)
        host.metadata.magazine = held and GunnyMagazineIn(held.item, held.metadata) or nil
        return host.metadata
    end,

    -- Post-commit only, never speculative: the right place to touch the ped. Swapping the
    -- visible magazine for a move that then failed would leave the model disagreeing with the inventory.
    onChange = function(ctx)
        local mag = ctx.host.metadata.magazine
        -- The rounds changed here, not by a shot: gunny numbers the change, and a count the game sent
        -- for the old magazine is then ignored (GunnyCountChanged, server/weapons.lua).
        -- Nil when this isn't the gun in the hand: then there's nothing on the ped to change.
        local version = GunnyCountChanged(ctx.host.source, ctx.host.addr, ctx.host.metadata.serial)
        if not version then return end
        if mag then
            TriggerClientEvent('whip_gunny:client:magazineInserted', ctx.host.source, ctx.host.slot, mag.ammo, version, mag.item)
        else
            TriggerClientEvent('whip_gunny:client:magazineRemoved', ctx.host.source, ctx.host.slot, version)
        end
    end,
})

Many items can share one action: every gun uses whip_gunny:open_weapon, and the handler gets whichever gun was used.

Example: a refinery

The input panel above plus a take-only output tray, each opened from a SecureEvent with near; refuse the input while it's busy. The input's onClose runs the crusher:

-- r is this refinery's state: { input = {}, output = {}, busy = false }
onClose = function(ctx)
    r.input = ctx.items
    if r.busy or next(ctx.items) == nil then return end
    r.busy = true
    SetTimeout(15000, function()
        r.output = Crush(r.input, r.output)   -- your recipes: 1 old_radio makes 3 copper_wire
        r.input, r.busy = {}, false
    end)
end,

-- The output tray
exports.whip_core:OpenPanel(source, {
    id = 'refine:output_' .. refineryId, label = 'Crusher — Output', slots = 12, items = r.output,
    onValidate = function(ctx)
        if ctx.direction == 'in' then return false, 'This tray is output only' end
    end,
    onClose = function(ctx) r.output = ctx.items end,
})

Cleanup

Panels close when a player disconnects or the resource stops, running onClose if that was the last viewer. With nothing to keep the contents (no onClose, or your resource or whip_core stopped), each character gets back what they put in and didn't take out; your stock goes, logged. A player can have several panels open: name one in ClosePanel.