Exports, events & hooks ​
What other resources may rely on: a heist that opens a vault gate for 30 seconds, a housing script that lets a visitor in, a job script that hands out keycards, an MDT that reacts to door alarms. Everything below is server-side.
Rules that hold for all of it:
- Exports never throw on bad input. Until np_doors is ready they answer
nil/falseplus a reason code. Reason codes have a text inshared/locales/*/00_core.lua(error.<code>). - Door ids are
[a-z0-9_-], 2 to 48 characters. np_doors registers each door with np_prophandler asnpd_<id>. - Wait for
np_doors:server:ready(or checkGetResourceState('np_doors') == 'started') before the first call. - Functions you register (
onStateChange, access sources, lock types, breach methods) are owner-tracked: they are removed when your resource stops and have to be registered again.
Server exports ​
Doors and state ​
| Export | Returns | Notes |
|---|---|---|
isLocked(id) |
bool or nil |
nil = unknown door |
setLocked(id, locked, src?) |
true or false, code |
No checks: the caller decides. src is only for the log. An unlock starts the door's auto-lock |
toggle(id, src?) |
true, lockedNow or false, code |
With src the player needs access (the lock chain is skipped: a script decided) |
TimedUnlock(id, seconds, by?) |
true or false, code |
Unlocks and relocks after seconds (np_housing "let in") |
setBroken(id, broken) |
true or false, code |
A broken door is open and cannot lock until repaired |
lockdown(building, on) |
true, doorsLocked or false, code |
Building lockdown |
getDoor(id) |
door or nil |
The public view plus the ox_doorlock fields name and state (1 locked, 0 open). Also takes a label |
getDoors(filter?) |
{ door } |
filter = { building?, group?, owner = { kind, id }?, near = { coords, radius }?, text? } |
getDoorsNear(coords, radius?) |
{ { door, distance } } |
Closest first (np_housing picks MLO doors with it) |
createDoor(def) |
id or nil, code |
def is a config door; stored in npd_doors |
updateDoor(id, patch) |
true or false, code |
Any definition field; a config door becomes an override. device = { coords, heading, kind?, model? } places the wall device, device = false removes it |
removeDoor(id) |
true or false, code |
Config doors cannot be removed (config_door) |
GetLog(id, n?) |
{ { id, at, actor, name, action, method, success, detail } } |
Newest first |
Access and keys ​
| Export | Returns | Notes |
|---|---|---|
canOpen(src, id) |
bool, reason, via |
Access resolution only (no lock chain); via is the card text ("Police · grade 2+") |
addAccess(idOrGroup, rule) |
ruleId or nil, code |
Target: a door id, group:<slug> or building:<slug>. Rule shape |
removeAccess(ruleId) |
true or false, code |
Numbers are DB rules, d:<door>:<n> a config door's inline rule |
giveKey(src, id, opts?) |
keyId, true or nil, code |
opts.kind = 'item' hands out a physical door_key; otherwise a digital key: { mode = 'once' | 'timed' | 'forever' | 'window', hours?, uses?, window?, reshare?, from? } |
giveKeycard(src, opts) |
true or false, code |
A keycard item: { group, level, uses?, expires?, label? } |
shareKey(src, id, target, opts) |
keyId or nil, code |
As the player src (needs own access, or a key with reshare); target = server id, phone number or identifier |
revokeKey(keyId) |
true or false, code |
ox_doorlock shims ​
| Export | Returns | Notes |
|---|---|---|
getDoorFromName(name) |
door or nil |
An np_doors id, an imported ox_* / qb_* name or a label |
setDoorState(id, state) |
bool |
1 / true locks, 0 / false unlocks |
While ox_doorlock is not running, exports.ox_doorlock:getDoor / getDoorFromName / setDoorState are answered by
np_doors as well (compatibility).
Hooks ​
| Export | Returns | Notes |
|---|---|---|
onStateChange(fn) |
hook id | fn(id, locked, src, method, door) after every state change |
offStateChange(id) |
bool |
useItem is the item callback np_inventory and ox_inventory call (server = { export = 'np_doors.useItem' }).
Addons ​
np_doors is an np_addon host (API level 1). Register inside npAddon.attach('np_doors', fn, { api = 1 })
from a resource that declares np_addon 'np_doors', or call the exports directly. All three are owner-tracked.
| Export | Definition |
|---|---|
registerAccessSource(name, fn, via?) |
fn(src, door, rule) -> bool, via?. Rules of kind source (subject = name) ask it, and kind property asks the source named property |
registerLockType(name, def) |
def = { label, reader = 'pin' | 'keycard' | 'finger' | 'face' | nil, prepare?(src, door, session) -> data | true | nil, err, verify(src, door, value, session) -> ok, reason?, data? } |
registerBreachMethod(name, def) |
def = { label?, game?, opts?, breaks?, difficulty?, run?(src, door, def) -> bool }. With game it is an np_minigames game; with run your own flow |
-- a gym door that opens for members of a membership script
exports.np_doors:registerAccessSource('gym', function(src, door, rule)
return exports.my_gym:isMember(src, rule.param), 'Gym member'
end)
exports.np_doors:addAccess('gym_front', { kind = 'source', subject = 'gym', param = 'vespucci' })
-- an extra breach method: a crowbar, played as the skill ring
exports.np_doors:registerBreachMethod('crowbar', { label = 'Crowbar', game = 'skillcheck', difficulty = 'easy', breaks = true })
Events ​
Server-local (AddEventHandler):
| Event | Arguments |
|---|---|
np_doors:stateChanged |
(id, locked, src, method); src is 0 for the server (timer, schedule, export) |
np_doors:ring |
(id, src): somebody rang the bell (np_housing's door cam) |
np_doors:breach |
(id, src, method, success) |
np_doors:alarm |
(id, kind); kind = 'breach' | 'keypad' | 'face' | 'forced' | 'manual' |
np_doors:lockdown |
(building, on) |
np_doors:server:ready |
(): boot finished |
AddEventHandler('np_doors:alarm', function(id, kind)
local door = exports.np_doors:getDoor(id)
print(('alarm (%s) at %s, %s'):format(kind, door.label, door.building))
end)
np_doors listens to np_prophandler:doorChanged and np_prophandler:ready, and with Config.Compat.qbEvents to
qb-doorlock:server:updateState while qb-doorlock is absent. Client events (np_doors:client:*) are internal.
Client → server requests ​
The NUI and the client talk to the server over one rate-limited RPC event (np_doors:rpc, answered by
np_doors:rpcResult): door.* (peek, toggle, the lock chain steps, knock, ring, breach, repair), manager.*,
creator.save and the phone.keys.* / phone.alerts.list calls of the Keys app. They are internal:
call the exports from your own resource instead.
World devices and the 3D keypad ​
Only with Config.Devices.enabled (World devices & 3D keypad). The wall device is an np_prophandler prop
with the id np_doors:device:<door id> (data = { owner = 'np_doors', door, kind }): find its entity with
np_prophandler's getPropEntity(id) on the client or getProp(id) on the server. The door's public view (getDoor)
carries device = { prop, kind, model, coords, heading } while devices are on.
RPCs (internal, rate-limited like every other request):
| Method | Params | Returns |
|---|---|---|
device.place |
{ id, coords, heading, kind? } |
{ device }. Staff (np_doors.create or np_doors.manage); the device must lie within maxFromDoor of the door and the player within 6 m (plus Config.DistanceTolerance) of it. Errors: devices_off, unknown_door, forbidden, invalid_device, device_too_far, too_far |
device.remove |
{ id } |
true. Same permission |
keypad.press |
{ id, key } |
true. The 3D keypad's relay of one key: '0'-'9', '*' or '#', never the code. Needs observable.relay, an open lock session of the sender at the pin step of that door, a keypad device with a Config.Devices.keypad.models entry, the sender within reach plus Config.DistanceTolerance (server-side coords, bucket 0) and at most observable.rate presses per second. Errors: not_available, invalid_params, rate_limited, unknown_door, no_session, invalid_device, too_far |
Client events (internal, from the server):
| Event | Payload |
|---|---|
np_doors:client:devicePlace |
{ id, label, kind, model, center, radius, current }: starts the placement ghost for /doordevice |
np_doors:client:keypadPress |
{ door, key }: a key pressed on a keypad near you. Sent only to players within observable.range of the keypad, never to the one typing |
NUI and DUI messages of the 3D keypad:
- Lua → NUI:
keypad3d:open({ door = { id, label }, length, autoSubmit }: the transparent overlay takes the focus) andkeypad3d:close. - NUI → Lua:
keypad3d:input({ key }from the keyboard, or{ x, y }: a click as 0..1 of the screen that Lua hit-tests against the projected key centres; answers{ key, taken }, or{ key = false }for a miss) andkeypad3d:cancel. - Lua → DUI (
web/dist/keypad.html, throughSendDuiMessage):{ type = 'state', length, filled, status = 'idle' | 'busy' | 'ok' | 'error' | 'locked', line, lockoutMs? }. Only the number of digits typed, never the digits.