# CRAFTING

#### FiveM Crafting Script Configuration Guide

This guide provides detailed instructions on how to configure a crafting script for your FiveM server. The following sections will cover everything from setting up locations and markers to configuring crafting items and their recipes.

**1. General Configuration**

The general configuration section includes the framework settings, marker names, and Discord token for integration.

```
Config = {}

Config.Framework = "NewESX" -- QBCore or ESX or OLDQBCore -- NewESX

Config.MarkerName = "[E] CRAFT"

Config.FormattedToken = "discord-token"  -- https://discord.com/developers/applications

Config.CraftXP = '100' -- How many XP does each Craft give? 100 XP = +1 level
```

**2. Discord Integration Functions**

Functions for making HTTP requests to the Discord API to retrieve user avatars and other data.

```
function DiscordRequest(method, endpoint, jsondata, callback)
    PerformHttpRequest("https://discord.com/api/"..endpoint, function(errorCode, resultData, resultHeaders)
        if errorCode == 200 then
            local data = json.decode(resultData)
            callback(data, nil)
        else
            callback(nil, errorCode)
        end
    end, method, #jsondata > 0 and json.encode(jsondata) or "", {["Content-Type"] = "application/json", ["Authorization"] = "Bot " .. Config.FormattedToken})
end

function GetDiscordAvatar(userID, callback)
    DiscordRequest("GET", "users/"..userID, {}, function(data, error)
        if not error and data and data.avatar then
            local avatarURL = string.format("https://cdn.discordapp.com/avatars/%s/%s.png", userID, data.avatar)
            callback(avatarURL)
        else
            callback(nil)
        end
    end)
end
```

**3. HTTP GET Function**

A function to perform HTTP GET requests.

```
function HttpGet(url, callback)
    PerformHttpRequest(url, function(err, result, headers)
        if err == 200 then
            local data = json.decode(result)
            callback(data, nil)
        else
            callback(nil, err)
        end
    end, 'GET')
end
```

**4. Crafting Locations**

Define the locations for your crafting stations, including coordinates, NPC details, and markers.

```
Config.Locations = {
    { 
        coords = vector3(235.081314, -817.780212, 29.223389),
        hash = "a_m_o_soucent_01",
        heading = 340.157471,
        marker = "Craft",
    }, 
}
```

**5. Inventory Management Functions**

Functions to add and remove items from the player's inventory.

```
-- This should add the item to the player's inventory
Config.Give = function(Player, Item, Amount)
    if Config.Framework == "QBCore" or Config.Framework == "OLDQBCore" then
        Player.Functions.AddItem(Item, tonumber(Amount))
    else
        Player.addInventoryItem(Item, tonumber(Amount))
    end
end

-- This should remove the item from the player's inventory
Config.Remove = function(Player, Item, Amount)
    if Config.Framework == "QBCore" or Config.Framework == "OLDQBCore" then
        Player.Functions.RemoveItem(Item, tonumber(Amount))
    else
        Player.removeInventoryItem(Item, tonumber(Amount))
    end
end
```

**6. Crafting Categories and Items**

Define the crafting categories and items, including the required materials, crafting time, and levels.

```
Config.Craft = {
    categories = {
        { label = 'Weapons', class = 'gun' },
        { label = 'Knives', class = 'knife' },
        { label = 'Bullet Types', class = 'ammo' },
    },
    items = {
        gun = {
            {
                label = 'Pistol 9mm',
                time = 12,
                level = 1,
                item = 'weapon_pistol',
                width = 100,
                craft = {
                    { label = 'Bandage', item = 'bandage', count = 1, width = 60},
                    { label = 'Security Blue', item = 'security_card_01', count = 2, width = 55},
                    { label = 'Security Red', item = 'security_card_02', count = 2, width = 55},
                }
            },
            {
                label = 'Rifle Mk2 5.56mm',
                time = 15,
                level = 2,
                item = 'weapon_carbinerifle_mk2',
                width = 100,
                craft = {
                    { label = 'Security Blue', item = 'security_card_01', count = 5, width = 55},
                }
            },
            {
                label = 'Assault SMG 9mm',
                time = 3850,
                level = 3,
                item = 'weapon_assaultsmg',
                width = 90,
                craft = {
                    { label = 'Security Red', item = 'security_card_02', count = 3, width = 55},
                }
            },
        },
        knife = {
            {
                label = 'Knife Model 1',
                time = 10,
                level = 1,
                item = 'weapon_knife',
                width = 70,
                craft = {
                    { label = 'Bandage', item = 'bandage', count = 1, width = 60},
                    { label = 'Security Blue', item = 'security_card_01', count = 2, width = 55},
                    { label = 'Security Red', item = 'security_card_02', count = 2, width = 55},
                }
            },
            {
                label = 'Knife Model 2',
                time = 10,
                level = 1,
                item = 'weapon_knuckle',
                width = 30,
                craft = {
                    { label = 'Bandage', item = 'bandage', count = 1, width = 60},
                    { label = 'Security Blue', item = 'security_card_01', count = 2, width = 55},
                    { label = 'Security Red', item = 'security_card_02', count = 2, width = 55},
                }
            },
            {
                label = 'Knife Model 3',
                time = 10,
                level = 1,
                item = 'weapon_machete',
                width = 65,
                craft = {
                    { label = 'Bandage', item = 'bandage', count = 1, width = 60},
                    { label = 'Security Blue', item = 'security_card_01', count = 2, width = 55},
                    { label = 'Security Red', item = 'security_card_02', count = 2, width = 55},
                }
            },
        }, 
        ammo = {            
            {
                label = '9mm Ammo',
                time = 5,
                level = 1,
                item = 'pistol_ammo',
                width = 30,
                craft = {
                    { label = 'Bandage', item = 'bandage', count = 1, width = 60},
                    { label = 'Security Blue', item = 'security_card_01', count = 2, width = 55},
                    { label = 'Security Red', item = 'security_card_02', count = 2, width = 55},
                }
            },
            {
                label = '7.62mm Ammo',
                time = 5,
                level = 1,
                item = 'rifle_ammo',
                width = 30,
                craft = {
                    { label = 'Bandage', item = 'bandage', count = 1, width = 60},
                    { label = 'Security Blue', item = 'security_card_01', count = 2, width = 55},
                    { label = 'Security Red', item = 'security_card_02', count = 2, width = 55},
                }
            },
            {
                label = 'SMG Ammo',
                time = 5,
                level = 1,
                item = 'smg_ammo',
                width = 30,
                craft = {
                    { label = 'Bandage', item = 'bandage', count = 1, width = 60},
                    { label = 'Security Blue', item = 'security_card_01', count = 2, width = 55},
                    { label = 'Security Red', item = 'security_card_02', count = 2, width = 55},
                }
            },
        },
    }
}
```
