Skip to main content

Hooks & Events

nd_crafting provides hooks and events so you can integrate with other systems like XP, logging, or custom logic.

Server Hooks

Configure hooks in config/hooks.lua:

Config.Hooks = {
-- Called when a player starts crafting
onCraftStart = function(source, data)
-- data.stationId, data.recipeId, data.craftId, data.quantity
print(('Player %s started crafting %s'):format(source, data.recipeId))
end,

-- Called when crafting completes successfully
onCraftComplete = function(source, data)
-- data.stationId, data.recipeId, data.craftId, data.outputs
-- Great for XP systems!
exports['your_xp_system']:AddXP(source, 'crafting', 10)
end,

-- Called when crafting fails (bad luck)
onCraftFail = function(source, data)
-- data.stationId, data.recipeId, data.craftId
print(('Player %s craft failed'):format(source))
end,

-- Called when player collects completed craft
onCraftCollect = function(source, data)
-- data.stationId, data.recipeId, data.craftId, data.outputs
end,

-- Called when player cancels a craft
onCraftCancel = function(source, data)
-- data.stationId, data.recipeId, data.craftId, data.refundedItems
end,
}

Hook Data Structure

All hooks receive a data table with:

FieldTypeDescription
stationIdstringStation where craft occurred
recipeIdstringRecipe that was crafted
craftIdnumberUnique database ID
quantitynumberHow many were crafted
outputstableItems produced (on complete/collect)
refundedItemstableItems returned (on cancel)

Server Exports

-- Get all stations
local stations = exports.nd_crafting:GetAllStations()

-- Get station by ID
local station = exports.nd_crafting:GetStation('workbench_1')

-- Get player's active crafts
local crafts = exports.nd_crafting:GetPlayerCrafts(source)

-- Check if player is near a station
local isNear = exports.nd_crafting:IsPlayerNearStation(source, 'workbench_1')

Client Events

-- Station opened
RegisterNetEvent('nd_crafting:client:stationOpened', function(stationId)
print('Opened station:', stationId)
end)

-- Station closed
RegisterNetEvent('nd_crafting:client:stationClosed', function()
print('Closed crafting menu')
end)

-- Craft started
RegisterNetEvent('nd_crafting:client:craftStarted', function(craftId, recipeId)
print('Started crafting:', recipeId)
end)

-- Craft completed
RegisterNetEvent('nd_crafting:client:craftCompleted', function(craftId, recipeId)
print('Craft ready to collect:', recipeId)
end)

Integration Examples

XP System Integration

Config.Hooks = {
onCraftComplete = function(source, data)
local recipe = Config.Recipes[data.recipeId]
local xpAmount = recipe.duration / 60 -- 1 XP per minute of crafting

exports['esx_xp']:AddXP(source, 'crafting', xpAmount)
end,
}

Discord Logging

Config.Hooks = {
onCraftComplete = function(source, data)
local identifier = GetPlayerIdentifierByType(source, 'discord')
local recipe = Config.Recipes[data.recipeId]

-- Send to Discord webhook
PerformHttpRequest('YOUR_WEBHOOK_URL', function() end, 'POST',
json.encode({
embeds = {{
title = 'Craft Completed',
description = ('**%s** crafted **%s**'):format(
GetPlayerName(source),
recipe.label
),
color = 3066993,
}}
}),
{ ['Content-Type'] = 'application/json' }
)
end,
}

Economy Integration

Config.Hooks = {
onCraftStart = function(source, data)
-- Charge crafting fee
local fee = 100
exports['qb-core']:GetPlayer(source).Functions.RemoveMoney('cash', fee)
end,
}