Exports, events & hooks ​
What other resources may rely on: restaurants and shops that use farm goods, jobs that pay farmers, gangs that tax a
field, a weather resource that feeds the seasons. np_farming depends on none of them. 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,falseor an empty list. - Tables you get are copies. Changing them changes nothing in np_farming.
- Row shapes only grow: keys are added, never renamed or removed.
- Wait for
np_farming:server:ready(or checkGetResourceState('np_farming') == 'started'and retry) before the first call. np_farming fires it again after every restart; hooks have to be registered again then.
Hooks ​
server
local id = exports.np_farming:registerHook(name, fn, opts) -- opts = { resource = GetCurrentResourceName() }
exports.np_farming: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 |
|---|---|---|
beforePlant |
{ source, identifier, plot, slot, crop, coords } |
false, reason to veto (a zone where nothing may grow). Or nothing |
beforeHarvest |
{ source, identifier, plant, plot, crop, count, stars, tool = { source, name } } |
false, reason to veto. Or { yieldMult = 1.0 } (several hooks multiply). Or nothing |
beforeProcess |
{ source, identifier, station, stationType, recipe, batches } |
false, reason to veto. Or nothing |
beforeSale |
{ source, identifier, buyer, item, stars, count, price, basePrice, coords, preview } |
false, reason to veto. Or { price = n, priceMult = n, payees = { { to = <payee>, percent = 30 } } }. Or nothing |
np_farming uses its own hooks too: the co-op payout split (beforeSale), pollination and the np_minigames bonus
(beforeHarvest), keeping crops off a hive's slot (beforePlant).
beforeSale ​
price is what one item sells for after quality, demand and reputation, basePrice the same before any hook (see
GetPrice). A payee is a character identifier (paid as clean money; an offline character at the next login) or
stash:<inventory id>. Whatever is not assigned to payees goes to the seller. With several hooks, vetoes win,
priceMult values multiply, the last explicit price wins and payee lists are joined; shares above 100 % in total are
scaled down.
preview = true is a quote
The market screen asks what a stack would bring. Nothing is sold, and a hook must answer a preview exactly as it would answer the sale and do nothing else: no counters, no money, no remembered state.
-- a market fee of 5 % at every buyer, paid into a city stash
exports.np_farming:registerHook('beforeSale', function(p)
return { payees = { { to = 'stash:city_treasury', percent = 5 } } }
end, { resource = GetCurrentResourceName() })
-- no chili on the Paleto Bay plot
exports.np_farming:registerHook('beforePlant', function(p)
if p.plot == 'paleto_1' and p.crop == 'chili' then return false, 'not here' end
end, { resource = GetCurrentResourceName() })
Server exports ​
server
| Export | Returns |
|---|---|
GetCrops() |
The crop registry in display order (below) |
GetItemInfo(item) |
For an inventory item table: { name, base, kind, crop, label, stars, fresh, freshUntil, durability }, or nil when it is no np_farming item |
GiveItem(src, name, count, stars) |
ok, slot | reason. A farming item with its quality and freshness (metadata, or the _q2 / _q3 item on an inventory without metadata) |
FindTool(src, toolType) |
found | nil, options. The server's tool check (below) |
GetTools() |
The tool types (below) |
GetFarmerLevel(src) |
{ level, xp, current, next, progress, max } of a loaded character, or nil |
AddXp(src, amount, reason) |
{ xp, level, gained, leveledUp }: farm work done elsewhere counts for the farmer level |
GetWorld() |
{ season, seasonDay, seasonDays, weather, raining, snowing, temperature, frost, hour, minute, night, source } |
GetPlots(filter) |
Plots (plots module): { { id, label, kind, coords, rows, cols, soil, owner, rentedUntil, used, slots } }. filter = { owner } or { coords, radius } |
GetPlants(filter) |
Plants (crops module): { { id, plot, slot, crop, stage, state, water, nutrients, health, ripeAt, rotAt, coords } }. filter = { plot } or { owner } |
GetStations(filter) |
Placed stations (processing module): { { id, type, label, coords, queue, tray } } |
GetPrice(item, stars, buyer) |
The price of one item before hooks (market module): { price, basePrice, stars, buyer, demand, bonus }, or nil |
RegisterCrop(id, def) |
true | false, code. A trusted resource adds a crop (below) |
PostContract(spec) |
A business posts an order (contracts module); the contract id, or false, code (below) |
registerHook(name, fn, opts) / removeHook(id) |
Hooks |
Without its module, a list export answers {}, GetPrice answers nil and PostContract answers
false, 'not_available'.
useItem is also exported: the callback np_inventory and ox_inventory call when one of np_farming's items is used
(server = { export = 'np_farming.useItem' }). Other resources have no reason to call it.
Client exports ​
client
| Export | Returns |
|---|---|
GetFarmerLevel() |
The same shape as the server's, from the client's mirror |
GetWorld() |
The same shape as the server's, from the client's mirror |
GetCrops ​
{ { id = 'tomato', label = 'Tomato', glyph = 'apple-whole', tint = 'red', kind = 'crop', level = 1, growMinutes = 45,
stages = { 'seed', 'sprout', 'grow', 'flower', 'ripe' }, seasons = { spring = 1.0, summer = 1.2, autumn = 0.8, winter = 0 },
soils = { 'field', 'garden', 'greenhouse' }, tool = 'cutting', rotMinutes = 30, yield = { 5, 7 }, rotation = { 'wheat', 'corn' },
seed = 'tomato_seed', produce = 'tomato' } }
FindTool ​
local found, options = exports.np_farming:FindTool(src, 'cutting')
-- found = { ok = true, tool = 'cutting', source = 'hand', name = 'WEAPON_MACHETE', label = 'Machete', weapon = 'WEAPON_MACHETE',
-- item = nil, slot = nil, durability = nil, speedMult = 1.2 }
-- nothing fits: found = nil, options = { { kind = 'item', name = 'sickle', label = 'Sickle' }, { kind = 'weapon', name = 'WEAPON_KNIFE', label = 'Knife' }, … }
A tool type is satisfied by a GTA weapon of its list in the hand (source = 'hand', with the in-hand speed bonus), a
weapon the player carries ('loadout': the ESX loadout, or a weapon item of np_inventory / ox_inventory / qb-inventory),
or an item of its list with durability left ('item'). The weapon in the hand is GetSelectedPedWeapon on the server
ped; a weapon held but not owned does not count where ownership can be read. On a server without a framework the
loadout is the client's report (the weakest path). See Tools.
GetTools ​
{ { id = 'cutting', label = 'Cutting', icon = 'scissors', utility = false, inHandBonus = 0.2,
items = { { name = 'sickle', label = 'Sickle' } }, weapons = { { name = 'WEAPON_KNIFE', label = 'Knife' } } } }
RegisterCrop ​
exports.np_farming:RegisterCrop(id, def) adds a crop (a Config.Crops entry, keys on
Configuration → Crops): its seed and produce items are generated and registered, and every
client gets it. Only resources listed in Config.Compat.crops.resources may call it (not_allowed otherwise). Calling it
again with the same id tunes the crop; its item names never change. It is never exposed as a client RPC.
PostContract ​
local id, err = exports.np_farming:PostContract({
business = 'pizza_this', label = 'Pizza This', -- who orders (an np_shops shop id puts the goods in its stock)
item = 'tomato', count = 40, minStars = 2, days = 3,
reward = 780, -- the whole order, paid per delivered share
stash = 'pizza_this_stock', -- where deliveries go
-- coords = vector3(x, y, z), -- optional: deliver here instead of at a board
})
The calling resource funds the reward itself. Deliveries go into stash (or the np_shops stock stash when business is
an np_shops shop id); without either, invalid_params.
Events ​
server
Server-local (AddEventHandler), never sent to clients.
| Event | Payload |
|---|---|
np_farming:server:ready |
none; np_farming (re)started and answers exports |
np_farming:server:planted |
{ source, identifier, plant, plot, slot, crop, coords } |
np_farming:server:harvested |
{ source, identifier, plant, plot, crop, item, count, stars, tool, coords } |
np_farming:server:rotted |
{ plant, plot, crop, owner } |
np_farming:server:processed |
{ source, identifier, station, recipe, item, count, stars } (source nil when the owner is offline) |
np_farming:server:sold |
{ source, identifier, buyer, item, stars, count, price, total, payees, coords } |
np_farming:server:contractCompleted |
{ contract, buyer, identifier, item, count, reward, late } |
np_farming:server:plotRented |
{ source, identifier, plot, days, paid, paidUntil } |
np_farming:server:theft |
{ source, identifier, plot, plant, owner, coords } |
np_farming:server:levelUp |
{ source, identifier, level, xp } |
tool in harvested is { source = 'hand' | 'loadout' | 'item', name } or nil for a crop picked by hand. payees
in sold is { { to, percent, amount, paid = 'paid' | 'queued' | 'failed' } }.
AddEventHandler('np_farming:server:theft', function(p)
print(('theft on %s by %s'):format(p.plot, p.identifier))
end)
Weather and seasons ​
np_farming reads the weather of Renewed-Weathersync, qb-weathersync, cd_easytime or vSync when one runs, then np_admin's
(GlobalState.npAdminWeather) or np_menu's (GlobalState.npMenuWeather) world tool, else runs its own seasonal cycle.
Another weather resource feeds it from a small file in np_farming's integrations/server/ that adds itself to
NpFarming.WorldSources; there is no export for it.
Price page ​
GET https://<server>/np_farming/prices is a read-only HTML page of today's best produce price per crop (★, the market
module's GetPrice), reloading itself every minute. np_farming puts it on an np_screens surface when
Config.Compat.screens.board names one; any other screen or browser source may show it too.