Skip to content

Exports & hooks ​

All exports are called as exports.np_shops:<name>(...). np_shops depends on none of the resources that call it, and every caller should treat it as optional.

Rules that hold for all of them:

  • Exports never throw. A call that can't be answered returns nil, false (plus an error code) or an empty list.
  • Tables you get are copies. Changing them changes nothing in np_shops.
  • Row shapes only grow: keys are added, never renamed or removed.
  • Wait for np_shops:server:ready (or check GetResourceState('np_shops') == 'started' and retry) before the first call. np_shops fires it again after every restart. Shops you registered with RegisterShop and your hooks live in memory, so register them again then. They are removed when your resource stops.

The export list is checked against the code by the test suite, so this page and the code can't drift apart.

Server ​

server

Export Returns Description
RegisterShop(id, def) true or false, code Add or replace a shop owned by your resource. See below. Codes: invalid_shop:<field>, exists (a config or editor shop has that id)
RemoveShop(id) true or false, 'not_found' Only shops your resource registered
GetShop(id) ShopPublic or nil One shop. See ShopPublic
GetShops(filter) ShopPublic[] filter = { type?, brand?, coords?, radius? } or nil. Hidden shops are left out
GetShopCoords(id) list With an id: the { x, y, z } counters of that shop. Without: { { id, label, coords = { x, y, z } } } of every visible shop
GetBrand(id) brand or nil { id, label, group, type, color, strength, plate, logo, icon, tagline, sells }
GetBrands() table Every brand, keyed by id
OpenShop(src, id, location) true or false, code Opens the storefront for a player standing at the shop. Codes: too_far, closed, not_found
IsShopOpen(id) open, reason reason = 'hours', 'robbed', 'owner' or 'admin' when closed
AddStock(id, name, count) new count or false, code Codes: not_found, unlimited
GetStock(id, name) count, nil or false nil = unlimited, false = the shop doesn't sell it
registerHook(name, fn, opts) hook id or nil See Hooks
removeHook(id) — Remove one of your hooks
lua
-- shops near a point, for a map or a vending resource
local nearby = exports.np_shops:GetShops({ type = 'retail', coords = vec3(25.7, -1347.3, 29.5), radius = 500.0 })

-- is the 24/7 open, and why not?
local open, reason = exports.np_shops:IsShopOpen('247_innocence')

-- a delivery job topped up the stock
exports.np_shops:AddStock('247_innocence', 'ecola', 24)

Fuel stations with a shop (np_fuel) ​

np_fuel calls these for a shop linked with one of its stations. np_fuel is the authority for a linked pair: it sells it, holds the shared account and mirrors the staff. Other resources can read GetShopOwner and GetShopStaff too.

Export Returns Description
GetShopOwner(shopId) { identifier, name } or nil The owning character; nil when not owned or unknown
CanOwnShop(src, shopId) true or nil, code May that player own the shop? Codes: not_available (ownership off), not_found, not_for_sale (not ownable), no_player, no_business_license
SetLinkedOwner(shopId, identifier, name, reason) true or nil, code np_fuel's handover of a linked pair; identifier = nil makes the shop unowned. No money moves: open wholesale orders are cancelled, staff, duty, price overrides and the ledger are cleared, the stock stash goes to the previous owner's pickup stash, and np_shops:server:ownerChanged fires. Codes: not_available, not_found, invalid_params, busy (a sale of that shop runs right now: try again), failed
SetLinkStaff(shopId, identifier, name, role) true or nil, code Mirrors a staff member of the linked station: role = 'manager' or 'employee', nil removes them. The same role again does nothing. Codes: not_available, not_found (shop not owned), invalid_params (unknown role, or the owner)
GetShopStaff(shopId) list { { identifier, name, role } } managers and employees of an owned shop (never the owner); {} when unowned
DrainShopBalance(shopId) dollars What the shop's own account held (signed, may be negative); it is 0 afterwards. 0 when unowned
CloseShopOrders(shopId) dollars Cancels every open wholesale order and refunds it into the account; returns the refund. 0 when unowned or nothing was open

For a merged pair, np_shops books every sale, wage, commission, wholesale order, deposit and withdrawal through np_fuel's MoveLinkBalance, and the storefront of a linked shop lists the player's open fuel bills from np_fuel's GetFuelBills (paid with PayFuelBill, see Checkout → Fuel bills).

Client ​

client

Export Returns Description
OpenShop(id) boolean Opens the nearest counter of that shop when the player is in range
IsOpen() boolean An np_shops screen has focus
Close() — Close the open screen

RegisterShop ​

lua
exports.np_shops:RegisterShop('npd_hardware', {
  label = 'Hardware Supply',           -- required
  brand = 'bean-hardware',             -- optional: registry id (logo, color). Without: icon / tint
  icon = 'flask', tint = 'olive',      -- optional, brandless look
  type = 'retail',                     -- optional, default 'retail'
  locations = { vec3(2747.3, 3472.8, 55.7) },   -- required: vector3 or { coords = vector3, heading, radius }
  groups = { police = 0 },             -- optional: job lock ({ [job] = minGrade })
  currency = 'money',                  -- optional: default item currency ('black_money' for dirty shops)
  blip = false,                        -- optional
  items = {                            -- required, 1..200 lines
    { name = 'acetone', price = 12, stock = 20 },   -- stock nil = unlimited
  },
})

The definition takes every key of a config shop (Shops & brands → Adding a shop). Prices are whole numbers of 0 or more. The shop is removed when your resource stops.

ShopPublic ​

lua
{ id = '247_innocence', brand = '247', type = 'retail', label = '24/7', tagline = nil,
  coords = { x = 25.7, y = -1347.3, z = 29.5 },                              -- first counter
  locations = { { x = 25.7, y = -1347.3, z = 29.5, heading = 270.0, radius = 2.0 } },
  blip = { sprite = 52, color = 2, scale = 0.7 }, hours = nil, clerk = nil, pumps = nil,
  robbable = true, ownable = true, ownablePrice = 180000, hidden = false, icon = nil, tint = nil }

ownablePrice is the shop's own sale price in whole dollars (ownable.price), missing when the shop is not ownable. np_fuel adds it to a brand station's price for the bundle.

Hooks ​

lua
local id = exports.np_shops:registerHook(name, fn, opts)   -- opts = { resource = GetCurrentResourceName(), shops = { 'id', ... }? }
exports.np_shops:removeHook(id)

Hooks run synchronously, in registration order, each with its own copy of the payload. A hook that errors is logged and skipped. Hooks of a stopped resource are removed. An unknown name returns nil. opts.shops limits the hook to those shop ids.

Name Payload Return
beforeBuy { source, identifier, shopId, items = { { name, count, price } }, total, method } false, reason vetoes the whole purchase. The player sees reason when it is a locale key, else a generic refusal. Runs before the payment
afterBuy beforeBuy plus { brand, ref, receipt }. items are the lines that were handed over Ignored. Runs after the payment and the hand-over (financing too)
beforeRob { source, shopId, target, cops } false, reason vetoes the robbery
afterRob { source, shopId, target, amount, coords } Ignored
beforeSell { source, shopId, name, count, price, stolen } false, reason vetoes a pawn sale. A number replaces the price per item
priceModifier { source, shopId, name, count, unit } A number replaces the unit price (the last hook that answers wins). Also called for quotes, so do nothing else in it

beforeBuy and afterBuy ​

beforeBuy is a veto. It runs after every other check (stock, license, age, limit) but before the payment: the player may still cancel the np_payments sheet or not afford it, and a line may still fail to be handed over (and is refunded). Don't record or reward anything in beforeBuy; only say no. items are the lines the player is about to pay for, price is the unit price.

afterBuy runs once the money is taken and the goods are handed over. Its payload is the beforeBuy payload plus brand, ref (the payment reference: the np_payments ref, invoice:<id> for pay later, the first instalment invoice for financing) and receipt (the receipt id). items are the lines that were actually handed over, in the same { name, count, price } shape. A paper trail belongs here. np_drugs logs precursor buyers this way:

lua
exports.np_shops:registerHook('beforeBuy', function(p)
  -- p = { source = 3, identifier = 'char1:abc', shopId = 'npd_hardware',
  --       items = { { name = 'acetone', count = 2, price = 12 } }, total = 24, method = 'cash' }
  if isBlacklisted(p.identifier) then return false, 'paper_trail' end   -- veto only
end, { resource = GetCurrentResourceName(), shops = { 'npd_hardware' } })

exports.np_shops:registerHook('afterBuy', function(p)
  -- p = { ..., brand = 'bean-hardware', items = { { name = 'acetone', count = 2, price = 12 } },
  --       total = 24, method = 'cash', ref = 'nps…', receipt = 'R…' }
  for _, it in ipairs(p.items) do logPrecursor(p.identifier, p.shopId, it.name, it.count, p.receipt) end
end, { resource = GetCurrentResourceName(), shops = { 'npd_hardware' } })

np_shops:server:purchased carries the same fields as afterBuy, for listeners that don't need a hook.

lua
-- 20 % off everything at Bean Machine for EMS (the job check is yours)
exports.np_shops:registerHook('priceModifier', function(p)
  if isEms(p.source) then return math.floor(p.unit * 0.8) end
end, { resource = GetCurrentResourceName(), shops = { 'beanmachine_alta' } })

Fuel grades go through priceModifier too, with the name fuel:<grade>.