Sleepless

Inventory Addons

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

PropertyTypeRequiredDescription
durationnumberYesDuration in milliseconds for the craft to complete
clienttableNoClient-side callbacks (before and after)
servertableNoServer-side callbacks (before, after, and optional metadata)
coststableYesItems required and whether they are consumed
resulttableYesArray of resulting items from the craft

Cost Properties

Each entry in the costs table is keyed by item name:

PropertyTypeDescription
neednumberQuantity required in the dragged slot. If between 0.0 and 1.0, removes that percentage of durability instead.
removebooleanWhether 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:

PropertyTypeRequiredDescription
namestringYesItem name to give the player
amountnumberNoFixed quantity to give (use this or min/max)
minnumberNoMinimum random quantity
maxnumberNoMaximum random quantity

Callback Functions

The client table supports before and after. The server table supports before, after, and metadata:

CallbackContextDescription
before(recipeData)Client / ServerRuns before crafting starts. Return false to cancel the craft. Returning true or nil continues normally.
after(recipeData)Client / ServerRuns after crafting completes successfully.
metadata(resultName, fromSlot, toSlot)ServerOptional. 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

ParameterTypeDescription
keystringThe recipe key — two item names separated by a space (e.g., 'garbage scrapmetal')
datatableThe recipe data. Must include costs and result
targetnumber?Optional player id to sync the recipe UI to. Omit (or pass nil) to sync all players

Server Returns

TypeDescription
booleantrue if the recipe was registered

Client Parameters

ParameterTypeDescription
keystringThe same recipe key used on the server
datatableClient-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,
    },
})

On this page