Drag Craft
Last updated on
A simple crafting system using ox_inventory drag-and-drop hooks. Players craft items by dragging one item onto another in their inventory.
Configuration
Recipes are defined in dragcraft/config.lua. Each recipe key is a pair of item names separated by a space — these are the two items the player drags onto each other.
Recipe Properties
| Property | Type | Required | Description |
|---|---|---|---|
duration | number | Yes | Duration in milliseconds for the craft to complete |
client | table | No | Client-side callbacks (before and after) |
server | table | No | Server-side callbacks (before, after, and optional metadata) |
costs | table | Yes | Items required and whether they are consumed |
result | table | Yes | Array of resulting items from the craft |
Cost Properties
Each entry in the costs table is keyed by item name:
| Property | Type | Description |
|---|---|---|
need | number | Quantity required in the dragged slot. If between 0.0 and 1.0, removes that percentage of durability instead. |
remove | boolean | Whether the item should be removed/consumed upon completion |
A need value of 0.1 with remove = true removes 10% durability per craft, allowing up to 10 uses before the item breaks.
Ingredient counts are checked against the dragged slots, not the player's total item count. The items are also removed from those slots on success.
Result Properties
Each entry in the result array:
| Property | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Item name to give the player |
amount | number | No | Fixed quantity to give (use this or min/max) |
min | number | No | Minimum random quantity |
max | number | No | Maximum random quantity |
Callback Functions
The client table supports before and after. The server table supports before, after, and metadata:
| Callback | Context | Description |
|---|---|---|
before(recipeData) | Client / Server | Runs before crafting starts. Return false to cancel the craft. Returning true or nil continues normally. |
after(recipeData) | Client / Server | Runs after crafting completes successfully. |
metadata(resultName, fromSlot, toSlot) | Server | Optional. Return a metadata table applied to that result item. |
The recipeData parameter passed to before / after contains all the properties defined for that specific recipe.
Complete Example
RECIPES = {
['garbage scrapmetal'] = {
duration = 2000,
client = {
before = function(recipeData)
-- Client logic before crafting
-- Return false to cancel the craft
end,
after = function(recipeData)
-- Client logic after successfully crafting
end,
},
server = {
before = function(recipeData)
-- Server logic before crafting
-- Return false to cancel the craft
end,
after = function(recipeData)
-- Server logic after successfully crafting
end,
metadata = function(resultName, fromSlot, toSlot)
-- Optional metadata for the resulting item
-- return { quality = 100 }
end,
},
costs = {
['garbage'] = { need = 1, remove = true },
['scrapmetal'] = { need = 0.1, remove = true },
},
result = {
{ name = 'lockpick', amount = 1 },
},
},
}Exports
addRecipe
Register a recipe externally from another resource.
Craftable recipes (costs, result, duration, and server callbacks) must be registered on the server. The client export only stores client callbacks and cannot create a working craft.
-- server
local success = exports.sleepless_inventory_addons:addRecipe(key, data, target?)
-- client (callbacks only)
exports.sleepless_inventory_addons:addRecipe(key, data)Server Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The recipe key — two item names separated by a space (e.g., 'garbage scrapmetal') |
data | table | The recipe data. Must include costs and result |
target | number? | Optional player id to sync the recipe UI to. Omit (or pass nil) to sync all players |
Server Returns
| Type | Description |
|---|---|
boolean | true if the recipe was registered |
Client Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The same recipe key used on the server |
data | table | Client-side data. Only client callbacks are used from this call |
A client-only addRecipe call does not register the recipe on the server. Players will not be able to craft it unless the same key is also registered from a server resource.
Callbacks only work in the context you register them. A client-side call can only register client callbacks, and a server-side call can only register server callbacks. Functions cannot be sent over the network.
local success = exports.sleepless_inventory_addons:addRecipe('garbage scrapmetal', {
duration = 2000,
server = {
before = function(recipeData)
-- Server logic before crafting
-- Return false to cancel
end,
after = function(recipeData)
-- Server logic after crafting
end,
metadata = function(resultName, fromSlot, toSlot)
-- Optional metadata for the resulting item
end,
},
costs = {
['garbage'] = { need = 1, remove = true },
['scrapmetal'] = { need = 0.1, remove = true },
},
result = {
{ name = 'lockpick', amount = 1 },
},
})-- Only registers client callbacks. The craftable recipe must already
-- exist in config or be registered from the server export.
exports.sleepless_inventory_addons:addRecipe('garbage scrapmetal', {
client = {
before = function(recipeData)
-- Client logic before crafting
-- Return false to cancel
end,
after = function(recipeData)
-- Client logic after crafting
end,
},
})