From c484925b1f28a3fb4193307d04c9792f93f0a225 Mon Sep 17 00:00:00 2001 From: Michael Biddle <556637+mbiddle@users.noreply.github.com> Date: Mon, 23 Dec 2024 11:14:52 -0800 Subject: [PATCH] Rework qb-crafting to work with existing workbenches around the city instead of adding and removing crafting tables. --- client.lua | 84 ++++++------ config.lua | 342 +++++++++++++++++++++++++------------------------ fxmanifest.lua | 1 - locales/cs.lua | 5 +- locales/de.lua | 3 - locales/en.lua | 3 - locales/es.lua | 3 - locales/nl.lua | 3 - server.lua | 25 ---- 9 files changed, 213 insertions(+), 256 deletions(-) diff --git a/client.lua b/client.lua index 242b8df..b3008d0 100644 --- a/client.lua +++ b/client.lua @@ -4,15 +4,14 @@ local QBCore = exports['qb-core']:GetCoreObject() local function CraftItem(craftedItem, requiredItems, amountToCraft, xpEarned, xpType) QBCore.Functions.TriggerCallback('crafting:getPlayerInventory', function(inventory) + local inventoryDict = {} + for _, invItem in pairs(inventory) do + inventoryDict[invItem.name] = invItem.amount + end + local hasAllMaterials = true for _, reqItem in pairs(requiredItems) do - local itemAmount = 0 - for _, invItem in pairs(inventory) do - if invItem.name == reqItem.item then - itemAmount = invItem.amount - break - end - end + local itemAmount = inventoryDict[reqItem.item] or 0 if itemAmount < reqItem.amount then hasAllMaterials = false QBCore.Functions.Notify(string.format(Lang:t('notifications.notenoughMaterials')) .. amountToCraft .. 'x ' .. QBCore.Shared.Items[craftedItem].label, 'error') @@ -97,9 +96,9 @@ end local function OpenCraftingMenu(benchType) local PlayerData = QBCore.Functions.GetPlayerData() - local xpType = benchType == 'item_bench' and Config.item_bench.xpType or Config.attachment_bench.xpType - local recipes = benchType == 'item_bench' and Config.item_bench.recipes or Config.attachment_bench.recipes - local currentXP = PlayerData.metadata[xpType] + local xpType = benchType == 'item_bench' and Config.Benches.item_bench.xpType or Config.Benches.attachment_bench.xpType + local recipes = benchType == 'item_bench' and Config.Benches.item_bench.recipes or Config.Benches.attachment_bench.recipes + local currentXP = PlayerData.metadata.rep[xpType] and PlayerData.metadata.rep[xpType] or 0 QBCore.Functions.TriggerCallback('crafting:getPlayerInventory', function(inventory) local craftableItems = {} @@ -160,47 +159,44 @@ local function OpenCraftingMenu(benchType) end) end -local function PickupBench(benchType) - local playerPed = PlayerPedId() - local propHash = Config[benchType].object - local entity = GetClosestObjectOfType(GetEntityCoords(playerPed), 3.0, propHash, false, false, false) - if DoesEntityExist(entity) then - DeleteEntity(entity) - TriggerServerEvent('qb-crafting:server:addCraftingTable', benchType) - QBCore.Functions.Notify(string.format(Lang:t('notifications.pickupBench')), 'success') - end -end - -- Events -RegisterNetEvent('qb-crafting:client:useCraftingTable', function(benchType) - local playerPed = PlayerPedId() - local coordsP = GetOffsetFromEntityInWorldCoords(playerPed, 0.0, 1.0, 1.0) - local playerHeading = GetEntityHeading(PlayerPedId()) - local itemHeading = playerHeading - 90 - local workbench = CreateObject(Config[benchType].object, coordsP, true, true, true) - if itemHeading < 0 then itemHeading = 360 + itemHeading end - SetEntityHeading(workbench, itemHeading) - PlaceObjectOnGroundProperly(workbench) - TriggerServerEvent('qb-crafting:server:removeCraftingTable', benchType) - exports['qb-target']:AddTargetEntity(workbench, { +-- Function to add a target to a tool chest +local function addTargetToToolChest(entity, benchType) + exports['qb-target']:AddTargetEntity(entity, { options = { { - icon = 'fas fa-tools', + event = "qb-crafting:client:useToolChest", + icon = "fas fa-tools", label = string.format(Lang:t('menus.header')), - action = function() - OpenCraftingMenu(benchType) - end + parameters = { benchType = benchType } }, - { - event = 'crafting:pickupWorkbench', - icon = 'fas fa-hand-rock', - label = string.format(Lang:t('menus.pickupworkBench')), - action = function() - PickupBench(benchType) - end, - } }, distance = 2.5 }) +end + +-- Find all tool chests and add targets when the resource starts +CreateThread(function() + local benchHashes = {} + for benchType, benchData in pairs(Config.Benches) do + benchHashes[benchType] = GetHashKey(benchData.object) + end + + while true do + Wait(10000) + + for benchType, benchHash in pairs(benchHashes) do + for _, entity in ipairs(GetGamePool('CObject')) do + if GetEntityModel(entity) == benchHash then + addTargetToToolChest(entity, benchType) + end + end + end + end +end) + +-- Register the event to handle the interaction +RegisterNetEvent("qb-crafting:client:useToolChest", function(data) + OpenCraftingMenu(data.parameters.benchType) end) diff --git a/config.lua b/config.lua index 1e1a57c..79d432c 100644 --- a/config.lua +++ b/config.lua @@ -1,176 +1,178 @@ Config = { EnableSkillCheck = true, ImageBasePath = "nui://qb-inventory/html/images/", - item_bench = { - object = `prop_tool_bench02`, - xpType = 'craftingrep', - recipes = { - { - item = 'lockpick', - xpRequired = 0, - xpGain = 1, - requiredItems = { - { item = 'metalscrap', amount = 22 }, - { item = 'plastic', amount = 32 } - } - }, - { - item = 'screwdriverset', - xpRequired = 0, - xpGain = 2, - requiredItems = { - { item = 'metalscrap', amount = 30 }, - { item = 'plastic', amount = 42 } - } - }, - { - item = 'electronickit', - xpRequired = 0, - xpGain = 3, - requiredItems = { - { item = 'metalscrap', amount = 30 }, - { item = 'plastic', amount = 45 }, - { item = 'aluminum', amount = 28 } - } - }, - { - item = 'radioscanner', - xpRequired = 0, - xpGain = 4, - requiredItems = { - { item = 'electronickit', amount = 2 }, - { item = 'plastic', amount = 52 }, - { item = 'steel', amount = 40 } - } - }, - { - item = 'gatecrack', - xpRequired = 110, - xpGain = 5, - requiredItems = { - { item = 'metalscrap', amount = 10 }, - { item = 'plastic', amount = 50 }, - { item = 'aluminum', amount = 30 }, - { item = 'iron', amount = 17 }, - { item = 'electronickit', amount = 2 } - } - }, - { - item = 'handcuffs', - xpRequired = 160, - xpGain = 6, - requiredItems = { - { item = 'metalscrap', amount = 36 }, - { item = 'steel', amount = 24 }, - { item = 'aluminum', amount = 28 } - } - }, - { - item = 'repairkit', - xpRequired = 200, - xpGain = 7, - requiredItems = { - { item = 'metalscrap', amount = 32 }, - { item = 'steel', amount = 43 }, - { item = 'plastic', amount = 61 } - } - }, - { - item = 'pistol_ammo', - xpRequired = 250, - xpGain = 8, - requiredItems = { - { item = 'metalscrap', amount = 50 }, - { item = 'steel', amount = 37 }, - { item = 'copper', amount = 26 } - } - }, - { - item = 'ironoxide', - xpRequired = 300, - xpGain = 9, - requiredItems = { - { item = 'iron', amount = 60 }, - { item = 'glass', amount = 30 } - } - }, - { - item = 'aluminumoxide', - xpRequired = 300, - xpGain = 10, - requiredItems = { - { item = 'aluminum', amount = 60 }, - { item = 'glass', amount = 30 } - } - }, - { - item = 'armor', - xpRequired = 350, - xpGain = 11, - requiredItems = { - { item = 'iron', amount = 33 }, - { item = 'steel', amount = 44 }, - { item = 'plastic', amount = 55 }, - { item = 'aluminum', amount = 22 } - } - }, - { - item = 'drill', - xpRequired = 1750, - xpGain = 12, - requiredItems = { - { item = 'iron', amount = 50 }, - { item = 'steel', amount = 50 }, - { item = 'screwdriverset', amount = 3 }, - { item = 'advancedlockpick', amount = 2 } - } - }, - } - }, - attachment_bench = { - object = `prop_tool_bench02_ld`, - xpType = 'attachmentcraftingrep', - recipes = { - { - item = 'clip_attachment', - xpRequired = 0, - xpGain = 10, - requiredItems = { - { item = 'metalscrap', amount = 140 }, - { item = 'steel', amount = 250 }, - { item = 'rubber', amount = 60 } - } - }, - { - item = 'suppressor_attachment', - xpRequired = 0, - xpGain = 10, - requiredItems = { - { item = 'metalscrap', amount = 165 }, - { item = 'steel', amount = 285 }, - { item = 'rubber', amount = 75 } - } - }, - { - item = 'drum_attachment', - xpRequired = 0, - xpGain = 10, - requiredItems = { - { item = 'metalscrap', amount = 230 }, - { item = 'steel', amount = 365 }, - { item = 'rubber', amount = 130 } - } - }, - { - item = 'smallscope_attachment', - xpRequired = 0, - xpGain = 10, - requiredItems = { - { item = 'metalscrap', amount = 255 }, - { item = 'steel', amount = 390 }, - { item = 'rubber', amount = 145 } - } - }, + Benches = { + item_bench = { + object = 'prop_tool_bench02', + xpType = 'craftingrep', + recipes = { + { + item = 'lockpick', + xpRequired = 0, + xpGain = 1, + requiredItems = { + { item = 'metalscrap', amount = 22 }, + { item = 'plastic', amount = 32 } + } + }, + { + item = 'screwdriverset', + xpRequired = 0, + xpGain = 2, + requiredItems = { + { item = 'metalscrap', amount = 30 }, + { item = 'plastic', amount = 42 } + } + }, + { + item = 'electronickit', + xpRequired = 0, + xpGain = 3, + requiredItems = { + { item = 'metalscrap', amount = 30 }, + { item = 'plastic', amount = 45 }, + { item = 'aluminum', amount = 28 } + } + }, + { + item = 'radioscanner', + xpRequired = 0, + xpGain = 4, + requiredItems = { + { item = 'electronickit', amount = 2 }, + { item = 'plastic', amount = 52 }, + { item = 'steel', amount = 40 } + } + }, + { + item = 'gatecrack', + xpRequired = 110, + xpGain = 5, + requiredItems = { + { item = 'metalscrap', amount = 10 }, + { item = 'plastic', amount = 50 }, + { item = 'aluminum', amount = 30 }, + { item = 'iron', amount = 17 }, + { item = 'electronickit', amount = 2 } + } + }, + { + item = 'handcuffs', + xpRequired = 160, + xpGain = 6, + requiredItems = { + { item = 'metalscrap', amount = 36 }, + { item = 'steel', amount = 24 }, + { item = 'aluminum', amount = 28 } + } + }, + { + item = 'repairkit', + xpRequired = 200, + xpGain = 7, + requiredItems = { + { item = 'metalscrap', amount = 32 }, + { item = 'steel', amount = 43 }, + { item = 'plastic', amount = 61 } + } + }, + { + item = 'pistol_ammo', + xpRequired = 250, + xpGain = 8, + requiredItems = { + { item = 'metalscrap', amount = 50 }, + { item = 'steel', amount = 37 }, + { item = 'copper', amount = 26 } + } + }, + { + item = 'ironoxide', + xpRequired = 300, + xpGain = 9, + requiredItems = { + { item = 'iron', amount = 60 }, + { item = 'glass', amount = 30 } + } + }, + { + item = 'aluminumoxide', + xpRequired = 300, + xpGain = 10, + requiredItems = { + { item = 'aluminum', amount = 60 }, + { item = 'glass', amount = 30 } + } + }, + { + item = 'armor', + xpRequired = 350, + xpGain = 11, + requiredItems = { + { item = 'iron', amount = 33 }, + { item = 'steel', amount = 44 }, + { item = 'plastic', amount = 55 }, + { item = 'aluminum', amount = 22 } + } + }, + { + item = 'drill', + xpRequired = 1750, + xpGain = 12, + requiredItems = { + { item = 'iron', amount = 50 }, + { item = 'steel', amount = 50 }, + { item = 'screwdriverset', amount = 3 }, + { item = 'advancedlockpick', amount = 2 } + } + }, + } + }, + attachment_bench = { + object = 'prop_tool_bench02_ld', + xpType = 'attachmentcraftingrep', + recipes = { + { + item = 'clip_attachment', + xpRequired = 0, + xpGain = 10, + requiredItems = { + { item = 'metalscrap', amount = 140 }, + { item = 'steel', amount = 250 }, + { item = 'rubber', amount = 60 } + } + }, + { + item = 'suppressor_attachment', + xpRequired = 0, + xpGain = 10, + requiredItems = { + { item = 'metalscrap', amount = 165 }, + { item = 'steel', amount = 285 }, + { item = 'rubber', amount = 75 } + } + }, + { + item = 'drum_attachment', + xpRequired = 0, + xpGain = 10, + requiredItems = { + { item = 'metalscrap', amount = 230 }, + { item = 'steel', amount = 365 }, + { item = 'rubber', amount = 130 } + } + }, + { + item = 'smallscope_attachment', + xpRequired = 0, + xpGain = 10, + requiredItems = { + { item = 'metalscrap', amount = 255 }, + { item = 'steel', amount = 390 }, + { item = 'rubber', amount = 145 } + } + }, + } } } } diff --git a/fxmanifest.lua b/fxmanifest.lua index 0cad633..dacdd1c 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -7,7 +7,6 @@ version '1.0.0' shared_scripts { '@qb-core/shared/locale.lua', - 'locales/en.lua', 'locales/*.lua', 'config.lua' } diff --git a/locales/cs.lua b/locales/cs.lua index 201fa17..7d909eb 100644 --- a/locales/cs.lua +++ b/locales/cs.lua @@ -1,7 +1,6 @@ local Translations = { menus = { header = 'Crafting Menu', - pickupworkBench = 'Zvednout Workbench', entercraftAmount = 'Zadejte počet:', }, notifications = { @@ -9,8 +8,6 @@ local Translations = { invalidAmount = 'Špatný počet zadán', invalidInput = 'Zadán neplatný vstup', notenoughMaterials = "Nemáš dostatek materiálů!", - craftingCancelled = 'Přerušil si crafting', - tablePlace = 'Tvůj Workbench byl položen', craftMessage = 'Vycraftil si %s', xpGain = 'Získal si %d XP z %s', } @@ -22,4 +19,4 @@ if GetConvar('qb_locale', 'en') == 'cs' then warnOnMissing = true, fallbackLang = Lang, }) -end \ No newline at end of file +end diff --git a/locales/de.lua b/locales/de.lua index fffa062..2056fd9 100644 --- a/locales/de.lua +++ b/locales/de.lua @@ -1,7 +1,6 @@ local Translations = { menus = { header = 'Crafting Menü', - pickupworkBench = 'Werkbank aufnehmen', entercraftAmount = 'Menge zum Craften eingeben:', }, notifications = { @@ -9,8 +8,6 @@ local Translations = { invalidAmount = 'Ungültige Menge eingegeben', invalidInput = 'Ungültige Eingabe', notenoughMaterials = 'Du hast nicht genug Materialien!', - craftingCancelled = 'Du hast das Craften abgebrochen', - tablePlace = 'Dein Crafting-Tisch wurde platziert', craftMessage = 'Du hast ein %s gefertigt', xpGain = 'Du hast %d XP in %s gewonnen', } diff --git a/locales/en.lua b/locales/en.lua index 19e4b95..c77cca4 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -1,7 +1,6 @@ local Translations = { menus = { header = 'Crafting Menu', - pickupworkBench = 'Pick up Workbench', entercraftAmount = 'Enter Craft Amount:', }, notifications = { @@ -9,8 +8,6 @@ local Translations = { invalidAmount = 'Invalid Amount Entered', invalidInput = 'Invalid Input Entered', notenoughMaterials = "You don't have enough materials!", - craftingCancelled = 'You cancelled the crafting', - tablePlace = 'Your Crafting Table was placed', craftMessage = 'You have crafted a %s', xpGain = 'You have gained %d XP in %s', } diff --git a/locales/es.lua b/locales/es.lua index 413902c..36f313f 100644 --- a/locales/es.lua +++ b/locales/es.lua @@ -1,7 +1,6 @@ local Translations = { menus = { header = 'Menú de crafteo', - pickupworkBench = 'Agarrar banco de trabajo', entercraftAmount = 'Ingrese la cantidad:', }, notifications = { @@ -9,8 +8,6 @@ local Translations = { invalidAmount = 'El monto ingresado no es valido.', invalidInput = 'La entrada no es válida.', notenoughMaterials = "¡No tienes suficientes materiales!", - craftingCancelled = 'Crafteo cancelado.', - tablePlace = 'Su mesa de trabajo fue colocada.', craftMessage = 'Ha creado un %s', xpGain = 'Has ganado %d XP en %s', } diff --git a/locales/nl.lua b/locales/nl.lua index ea49f0c..1b2be2b 100644 --- a/locales/nl.lua +++ b/locales/nl.lua @@ -1,7 +1,6 @@ local Translations = { menus = { header = 'Crafting Menu', - pickupworkBench = 'Pak werkbank op', entercraftAmount = 'Voer hoeveelheid in:', }, notifications = { @@ -9,8 +8,6 @@ local Translations = { invalidAmount = 'Ongeldige hoeveelheid', invalidInput = 'Ongeldige invoer', notenoughMaterials = "Je hebt niet genoeg materialen!", - craftingCancelled = 'Je hebt je crafting geannuleerd', - tablePlace = 'Je hebt je werkbank geplaatst', craftMessage = 'Je hebt een %s gemaakt', xpGain = 'Je hebt %d XP verkregen %s', } diff --git a/server.lua b/server.lua index ee388da..70cc9ba 100644 --- a/server.lua +++ b/server.lua @@ -33,23 +33,6 @@ RegisterServerEvent('qb-crafting:server:removeMaterials', function(itemName, amo end end) -RegisterNetEvent('qb-crafting:server:removeCraftingTable', function(benchType) - local src = source - local Player = QBCore.Functions.GetPlayer(src) - if not Player then return end - exports['qb-inventory']:RemoveItem(src, benchType, 1, false, 'qb-crafting:server:removeCraftingTable') - TriggerClientEvent('qb-inventory:client:ItemBox', src, QBCore.Shared.Items[benchType], 'remove') - TriggerClientEvent('QBCore:Notify', src, Lang:t('notifications.tablePlace'), 'success') -end) - -RegisterNetEvent('qb-crafting:server:addCraftingTable', function(benchType) - local src = source - local Player = QBCore.Functions.GetPlayer(src) - if not Player then return end - if not exports['qb-inventory']:AddItem(src, benchType, 1, false, false, 'qb-crafting:server:addCraftingTable') then return end - TriggerClientEvent('qb-inventory:client:ItemBox', src, QBCore.Shared.Items[benchType], 'add') -end) - RegisterNetEvent('qb-crafting:server:receiveItem', function(craftedItem, requiredItems, amountToCraft, xpGain, xpType) local src = source local Player = QBCore.Functions.GetPlayer(src) @@ -69,11 +52,3 @@ RegisterNetEvent('qb-crafting:server:receiveItem', function(craftedItem, require IncreasePlayerXP(src, xpGain, xpType) end end) - --- Items - -for benchType, _ in pairs(Config) do - QBCore.Functions.CreateUseableItem(benchType, function(source) - TriggerClientEvent('qb-crafting:client:useCraftingTable', source, benchType) - end) -end