Skip to content

Exports & hooks ​

What other resources may rely on. np_drugs depends on none of them (only on oxmysql, np_npchandler and np_prophandler) and contains no gang code; np_gangs uses this API through its own adapter. Everything below is server-side unless marked client, and checked against the code by tests/test_seams.lua: an export, hook or event in the code and not documented (or the other way round) fails the suite.

Rules that hold for all of it:

  • Exports never throw. A call that cannot be answered returns nil, false or an empty list.
  • Tables you get are copies. Changing them changes nothing in np_drugs.
  • Row shapes only grow: keys are added, never renamed or removed.
  • Wait for np_drugs:server:ready (or check GetResourceState('np_drugs') == 'started' and retry) before the first call. np_drugs fires it again after every restart; hooks have to be registered again then.

Server exports ​

server

Export Returns
Sell(src, { slot, count, price, channel, buyer, coords }) ok, result. Runs a sale through the engine: beforeSale, item removal, payment, reputation, sold event. result is the sold payload or an error code. A veto returns false, 'vetoed', reason
GetPrice(what, coords, src) The price of one item before any hook, with its factors (below). nil when what is not a drug
GetItemInfo(item, opts) For an inventory item table: { substance, units, grade, purity?, stamp, batch, packed, tier, container? } or nil. purity only when the batch was tested. Packed items return nil unless opts.reveal is passed by a resource in Config.TrustedResources
GetSellable(src) { slot, count, info } for the sellable drug items in a player's pockets
GetSubstances() The substance registry, in display order (below)
GetBatch(batchId) { id, substance, purity, grade, stamp, createdBy, createdAt, cut, parent, origin, units, tested, bad }
GetStamp(stampId) { id, owner, label, glyph, color, design, createdAt } or nil
GetStampReputation(stampId) { score = 0..100, label, tier, uses, badBatches, priceMult }, nil while nobody pressed it yet
GetDemand(coords, substance) Base demand multiplier of the configured area
GetProductionLines() The production lines (below)
GetLabs(filter) Placed labs (below). filter = { coords, radius } or nil
Dispatch(opts) A witness report to the police (below). The report id, or false, code
OrderRehab(target, class, opts) ok, reason. Court-ordered rehab (below)
GetRehab(target) { { class, day, total, paused, court, missed } }: the programs of a loaded character
GetBodyState(src) { load, line, tolerance = {…}, dependence = {…}, stages = {…}, active = {…}, bac }
IsHigh(src) boolean

useItem is also exported: the callback np_inventory and ox_inventory call when one of np_drugs' items is used (server = { export = 'np_drugs.useItem' }). Other resources have no reason to call it.

Client exports ​

client

Export Returns
IsHigh() boolean
GetActiveSubstances() A list of substance ids

Hooks ​

lua
local id = exports.np_drugs:registerHook(name, fn, opts)   -- opts = { resource = GetCurrentResourceName() }
exports.np_drugs: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.

Name Payload Return
stampSource { source, identifier } A list of stamps the player may press at the batch table: { { id = 'gang:3', label = 'SSK', glyph = 'crown', color = 'aqua' } }. Lists of all hooks are joined with the player's personal stamps
beforeProduce { source, identifier, line, substance, site = { id, coords }, units } false, reason to veto. Or { yieldShare = { { to = <payee>, percent = 15 } }, yieldMult = 1.0 }. Or nothing
beforeSale { source, identifier, channel, item = { name, substance, units, grade, purity, stamp, batch }, count, coords, price, basePrice, buyer = { type = 'npc' | 'player' }, preview } false, reason to veto. Or { price = n, priceMult = n, payees = { { to = <payee>, percent = 70 } } }. Or nothing
describe { source, identifier, kind, coords, description = { sex, features, vehicle, direction, street } } Rows for a witness report about that player: { { label = 'Bandana', value = 'green', icon = 'flag' } }. Joined from all hooks (at most Config.Dispatch.maxHookRows, 6)

beforeSale ​

price is what the seller asks per item, basePrice what np_drugs would pay (GetPrice). item.units is what one item holds: for an opened portion, what is left in it. item.purity is only there when the batch was tested.

A payee is a character identifier (paid as dirty cash, to pockets when online) or stash:<inventory id> (dirty cash added to that inventory). Whatever is not assigned goes to the seller. With several hooks, vetoes win, priceMult values multiply, the last explicit price wins and payee lists are joined. A price above Config.Sale.priceCeiling × the base price is capped. Shares above 100 % in total are scaled down, a share for an unknown stash stays with the seller, and an offline character is paid at the next login.

channel is street for np_drugs' own basic sale. Other resources pass their own name (np_gangs uses corner).

preview = true is a quote

The street sale asks the hooks what a price would become before the player offers it, so the card can show a tax honestly. Nothing is sold, no item moves, no sold event follows, and the same question may come several times a second while a slider moves. Answer a preview exactly as you would answer the sale and do nothing else: no alerts, no counters, no cooldowns, no money, no remembered state. On a real sale preview is nil.

beforeProduce ​

units is the yield the run is planned for (sets × the line's yield). site.id is the id of the place a lab stands at (GetLabs rows carry it as site), site.coords the lab's position. yieldShare percentages are delivered as product items of the finished batch (to a character's pockets, or into stash:<inventory id>); what cannot be delivered waits for the character's next login.

GetPrice ​

lua
local q = exports.np_drugs:GetPrice(item, coords, src)
local q = exports.np_drugs:GetPrice({ substance = 'cocaine', units = 1, grade = 'B', stamp = 'gang:3' }, coords)
  • what: the inventory item table of a slot ({ name, metadata }), or { substance, units, grade?, stamp?, opened? } for a product in nobody's hands. Without a grade the grade of unbatched product applies.
  • coords: where it would be sold. Without them demand is Config.Demand.default.
  • src: the seller, optional. Demand modifiers that belong to one player (buyers thinning out where that seller has stood too long) only apply with it.
lua
{ price = 83,            -- per item: unitPrice × units × every factor, rounded. This is `basePrice` in beforeSale
  basePrice = 83,
  unitPrice = 75,        -- Config.Substances[substance].price
  unit = 'g', units = 1,
  substance = 'cocaine', grade = 'B', stamp = 'gang:3', opened = false,
  factors = { grade = 0.8, stamp = 1.05, demand = 1.32, opened = 1 },
  ceiling = 664 }        -- the most a hook can make of it, nil without a ceiling

A concealed item answers nil unless the caller is a trusted resource; for one that is, the quote is for the product inside.

GetSubstances ​

lua
{ { id = 'cocaine', label = 'Cocaine', tint = '', glyph = 'snowflake', class = 'stimulant', legal = false,
    order = 4, unit = 'g', dose = 0.25, price = 75, sellable = true,
    portions = { { id = 'street', item = 'cocaine_bag_s', units = 0.5, tier = 'street', bulk = false, price = 38 },
                 { id = 'standard', item = 'cocaine_bag', units = 1, tier = 'standard', bulk = false, price = 75 },
                 { id = 'regulars', item = 'cocaine_bag_l', units = 3.5, tier = 'regulars', bulk = false, price = 263 },
                 { id = 'wholesale', item = 'cocaine_brick', units = 250, tier = 'wholesale', bulk = true, price = 18750 } } },
  … }

tint is a Nimbus tile tint ('' is the neutral grey). unit is what one unit is (g, pill, drink, cig), dose the units of one normal use, price the base price of one unit at grade A, demand 1.0, no stamp.

GetProductionLines ​

One row per enabled line, in display order:

lua
{ id = 'cook', label = 'Meth cook', substance = 'meth', unit = 'g',
  equipment = { 'lab_glassware', 'gas_burner' },
  ingredients = {
    { id = 'precursor', item = 'pseudoephedrine', items = { 'pseudoephedrine', 'cold_medicine' }, perSet = 4 },
    { id = 'solvent', item = 'solvent', items = { 'solvent' }, perSet = 1 } },
  items = { 'cold_medicine', 'pseudoephedrine', 'solvent' },
  stages = { { id = 'mix', seconds = 30, attended = true }, { id = 'heat', seconds = 420, attended = true },
             { id = 'crystallise', seconds = 480, attended = false }, { id = 'dry', seconds = 240, attended = false } },
  seconds = 1170, sets = 3, yield = 115, power = 2.4, smell = 34, respirator = true }

GetLabs ​

One row per placed lab, sorted by id. A lab exists from its first piece of equipment until the last piece, the product and the waste are gone:

lua
{ id = 'L48213', line = 'cook', substance = 'meth',
  coords = { x = 1005.2, y = -3200.1, z = -38.5 }, bucket = 0,
  owner = 'char1:abc', running = true, stage = 'heat', cook = 'char1:abc',
  power = 2.4, site = 'house:12', siteKind = 'property' }   -- siteKind: 'fixed' | 'property' | 'garage' | 'open'

Dispatch ​

lua
local id = exports.np_drugs:Dispatch({
  kind = 'corner', code = '10-31', coords = vector3(90.0, -1900.0, 21.0),
  note = 'Three of them, one keeps watch.',
  describe = { { label = 'Colour', value = 'green', icon = 'flag' } },
})

The same path np_drugs' own street sale and lab fires take: a report id is returned at once, the call reaches the police after the time a witness needs, through the configured adapter, and np_drugs:server:witness fires with it.

Field
kind A kind of Config.Dispatch.kinds (sale, lab, robbery, other), or your own id. An unknown kind keeps its name and uses the code, icon and priority of other
coords Where. Needed unless source is given
source The suspect's server id, optional. Its client describes itself, the describe hooks run for it, and the per-suspect cooldown applies
code, title Text shown instead of the kind's own (16 / 64 characters)
note A sentence added to the report (160 characters)
describe Rows you already know, joined with the hooks' rows. false: no description at all
delay Seconds until the call goes out (default: a roll within Config.Dispatch.delay)

Returns false, 'not_available' when the dispatch module is off, false, 'cooldown' for a suspect who was just reported, false, 'invalid_params' without a position. Resources that would rather not call an export fire the server-local event np_drugs:server:witnessCall with the same table.

OrderRehab ​

lua
local ok, reason = exports.np_drugs:OrderRehab(target, 'opioid', { by = 'Judge Hall', clinic = 'pillbox' })

target is the server id of a loaded character, or a character identifier (applied when they next load). class is a substance class (alcohol, nicotine, cannabis, stimulant, empathogen, opioid). reason is enrolled, updated (a running program is now court-ordered), queued, or an error code (unknown_class, not_found). A court-ordered program cannot be left; a missed day or a dirty test fires rehab with event = 'violation'.

Stamps ​

A stamp id is dealer:<character identifier> for a personal stamp or <namespace>:<id> for one supplied through stampSource (np_gangs uses gang:<id>). np_drugs stores the reputation per stamp id; whoever supplied the stamp owns its label, glyph and colour.

Events and state bags are on Events, state bags & commands.