Files
jim_bridge/crafting.lua

450 lines
20 KiB
Lua
Raw Permalink Normal View History

2024-01-17 13:39:23 +00:00
if IsDuplicityVersion() then
if GetResourceState(OXLibExport):find("start") then
createCallback(GetCurrentResourceName()..':server:GetStashItems', function(source, stashName) local stash = getStash(stashName) return stash end)
else
createCallback(GetCurrentResourceName()..':server:GetStashItems', function(source, cb, stashName) local stash = getStash(stashName) cb(stash) end)
end
2024-01-11 17:06:33 +00:00
end
local timeout, timing, stashItems = 0, false, {}
2024-01-11 17:06:33 +00:00
function GetStashTimeout(stashName, stop)
if stop then stashItems, timing, timeout = {}, false, 0 return end
if #stashItems > 0 then return true end
if timeout <= 0 then
stashItems = triggerCallback(GetCurrentResourceName()..':server:GetStashItems', stashName)
timeout = 10000
if not timing then
CreateThread(function()
timing = true
while timeout > 0 do timeout -= 1000 Wait(1000) end
timing, stashItems, timeout = false, {}, 0
end)
end
end
return false
2024-01-11 17:06:33 +00:00
end
local CraftLock = false
function craftingMenu(data)
2024-01-11 17:06:33 +00:00
if CraftLock then return end
if data.stashName and not GetStashTimeout(data.stashName) then
--triggerNotify(nil, "Chacking", "success")
end
if (data.job or data.gang) and not jobCheck(data.job or data.gang) then return end
local Menu, hasjob = {}, false
2024-01-11 17:06:33 +00:00
local Recipes = data.craftable.Recipes
for i = 1, #Recipes do
if not Recipes[i]["amount"] then Recipes[i]["amount"] = 1 end
for k, v in pairs(Recipes[i]) do
2024-01-11 17:06:33 +00:00
if k ~= "amount" and k ~= "job" and k ~= "gang" then
if Recipes[i].job then
2024-01-17 21:05:38 +00:00
for l, b in pairs(Recipes[i].job) do
hasjob = hasJob(l, nil, b)
if hasjob == true then break end
2024-01-11 17:06:33 +00:00
end
else hasjob = true end
local setheader, settext, disable = "", "", false
if hasjob then
local itemTable = {}
2024-01-11 17:06:33 +00:00
for l, b in pairs(Recipes[i][tostring(k)]) do
settext = settext..(settext ~= "" and br or "")..(Items[l] and Items[l].label or "error - "..l)..(b > 1 and " x"..b or "")
itemTable[l] = b
2024-01-11 17:06:33 +00:00
Wait(0)
end
if Config.System.Debug then print("^6Bridge^7: ^2Checking"..(data.stashName and " ^7'^6"..data.stashName.."^7'" or "").." ^2ingredients^7 - ^6"..k.."^7") end
if data.stashName then disable = not stashhasItem(stashItems, itemTable)
else disable = not hasItem(itemTable) end
2024-01-24 17:10:44 +00:00
setheader = (Items[tostring(k)] and Items[tostring(k)].label or "error - "..tostring(k))..(Recipes[i]["amount"] > 1 and " x"..Recipes[i]["amount"] or "")..(not disable and " ✔️" or "")
2024-01-11 17:06:33 +00:00
Menu[#Menu + 1] = {
isMenuHeader = disable,
2024-01-11 17:06:33 +00:00
icon = invImg(tostring(k)),
header = setheader, txt = settext,
onSelect = function()
local transdata = { item = k, craft = data.craftable.Recipes[i], craftable = data.craftable, coords = data.coords, stashName = data.stashName, onBack = data.onBack }
2024-01-23 22:39:46 +00:00
if Config.Crafting.MultiCraft then multiCraft(transdata) else makeItem(transdata) end
2024-01-11 17:06:33 +00:00
end,
}
end
end
2024-01-23 22:39:46 +00:00
Wait(0)
2024-01-11 17:06:33 +00:00
end
end
openMenu(Menu, { header = data.craftable.Header, onBack = data.onBack or nil, canClose = true, onExit = function() end, })
lookEnt(data.coords)
end
function multiCraft(data) local Menu = {}
local success = Config.Crafting.MultiCraftAmounts
if data.stashName and not GetStashTimeout(data.stashName) then
--triggerNotify(nil, "Refreshing stashinfo", "success")
end
Menu[#Menu+1] = {
isMenuHeader = true,
icon = invImg(data.item),
header = Items[data.item].label,
}
for k in pairsByKeys(success) do
local settext = ""
local itemTable = {}
2024-01-11 17:06:33 +00:00
for l, b in pairs(data.craft[data.item]) do
itemTable[l] = (b * k)
2024-01-11 17:06:33 +00:00
settext = settext..(settext ~= "" and br or "")..Items[l].label..(b*k > 1 and "- x"..b*k or "")
Wait(0)
end
local disable = false
if Config.System.Debug then print("^6Bridge^7: ^2Checking "..(data.stashName and "^7'^6"..data.stashName.."^7'" or "inventory").."^7x^5"..k.." ^2ingredients^7 - ^6"..data.item.."^7") end
if data.stashName then disable = not stashhasItem(stashItems, itemTable)
else disable = not hasItem(itemTable) end
2024-01-11 17:06:33 +00:00
Menu[#Menu + 1] = {
isMenuHeader = disable,
arrow = not disable,
2024-01-11 17:06:33 +00:00
header = "Craft - x"..k * data.craft.amount,
txt = settext,
onSelect = function ()
makeItem({item = data.item, craft = data.craft, craftable = data.craftable, amount = k, coords = data.coords, stashName = data.stashName, onBack = data.onBack })
end,
}
end
openMenu(Menu, { header = data.craftable.Header, onBack = function() craftingMenu(data) end, })
end
function makeItem(data)
2024-01-23 22:39:46 +00:00
if CraftLock then return end
CraftLock = true
2024-01-11 17:06:33 +00:00
2024-01-23 22:39:46 +00:00
local bartime = data.craftable.progressBar and data.craftable.progressBar.time or 5000
local bartext = data.craftable.progressBar and data.craftable.progressBar.label or Loc[Config.Lan].progressbar["progress_make"]
local animDict = data.craftable.Anims.animDict or "amb@prop_human_parking_meter@male@idle_a"
local anim = data.craftable.Anims.anim or "idle_a"
local amount = data.amount and (data.amount ~= 1) and data.amount or 1
2024-01-11 17:06:33 +00:00
2024-01-23 22:39:46 +00:00
local crafted, crafting = true, true
local cam = createTempCam(PlayerPedId(), data.coords)
startTempCam(cam)
2024-01-11 17:06:33 +00:00
for i = 1, amount do
2024-01-22 22:39:16 +00:00
for k, v in pairs(data.craft) do
if k ~= "amount" and k ~= "job" then
if type(v) == "table" then
for l, b in pairs(v) do
if crafting and progressBar({
label = "Using "..b.." "..Items[l].label,
time = 1000,
cancel = true,
dict = 'pickup_object',
anim = "putdown_low",
flag = 48,
icon = l,
}) then
--TriggerEvent('inventory:client:ItemBox', Items[l], "use", b) -- Show item box for each item
else
2024-01-23 22:39:46 +00:00
crafted, crafting = false, false
break
2024-01-22 22:39:16 +00:00
end
Wait(200)
2024-01-11 17:06:33 +00:00
end
2024-01-22 22:39:16 +00:00
if crafted then
if crafting and progressBar({
label = bartext..Items[data.item].label,
time = bartime,
cancel = true,
dict = animDict,
anim = anim,
flag = 8,
icon = data.item,
}) then
TriggerServerEvent(GetCurrentResourceName()..":Crafting:GetItem", data.item, data.craft, data.stashName)
else
crafting = false
break
end
2024-01-11 17:06:33 +00:00
end
end
end
end
Wait(500)
end
stopTempCam()
CraftLock = false
lockInv(false)
craftingMenu(data)
ClearPedTasks(PlayerPedId())
end
RegisterNetEvent(GetCurrentResourceName()..":Crafting:GetItem", function(ItemMake, craftable, stashName)
local src, amount, stashItems = source, craftable and craftable.amount or 1, stashName and getStash(stashName)
2024-01-11 17:06:33 +00:00
if stashName then
local itemRemove = {}
for k, v in pairs(craftable[ItemMake] or {}) do
for _, b in pairs(stashItems or {}) do
if k == b.name then itemRemove[k] = v end
2024-01-11 17:06:33 +00:00
end
end
stashRemoveItem(stashItems, stashName, itemRemove)
else
if craftable then
for k, v in pairs(craftable[ItemMake] or {}) do
2024-01-11 17:06:33 +00:00
TriggerEvent(GetCurrentResourceName()..":server:toggleItem", false, tostring(k), v, src)
end
end
end
TriggerEvent(GetCurrentResourceName()..":server:toggleItem", true, ItemMake, amount, src)
2024-01-23 22:39:46 +00:00
if GetResourceState("core_skills"):find("start") then exports["core_skills"]:AddExperience(src, 2) end
2024-01-11 17:06:33 +00:00
end)
--[[SHOPS]]--
function openShop(data)
if (data.job or data.gang) and not jobCheck(data.job or data.gang) then return end
if GetResourceState(OXInv):find("start") then
exports[OXInv]:openInventory('shop', { type = data.shop })
else
2024-01-17 13:39:23 +00:00
TriggerServerEvent(Config.General.JimShops and "jim-shops:ShopOpen" or "inventory:server:OpenInventory", "shop", data.items.label, data.items)
2024-01-11 17:06:33 +00:00
end
lookEnt(data.coords)
end
-- Client & Server side
function hasItem(items, amount, src) local amount = amount and amount or 1
local grabInv = nil
local foundInv = ""
if type(items) ~= "table" then items = { [items] = amount and amount or 1, } end
if GetResourceState(OXInv):find("start") then
foundInv = OXInv
grabInv = src and exports.ox_inventory:GetInventoryItems(src) or exports[OXInv]:GetPlayerItems()
2024-01-11 17:06:33 +00:00
elseif GetResourceState(QSInv):find("start") then
foundInv = QSInv
grabInv = src and exports[QSInv]:GetInventory(src) or exports[QSInv]:getUserInventory()
2024-01-11 17:06:33 +00:00
2024-01-22 17:14:56 +00:00
elseif GetResourceState(OrigenInv):find("start") then
foundInv = OrigenInv
grabInv = src and exports[OrigenInv]:getPlayerInventory(src) or exports[OrigenInv]:getPlayerInventory()
2024-01-22 17:14:56 +00:00
elseif GetResourceState(CoreInv):find("start") then
foundInv = CoreInv
2024-01-22 15:31:04 +00:00
if src then
if GetResourceState(QBExport):find("start") or GetResourceState(QBXExport):find("start") then
2024-01-22 16:55:58 +00:00
grabInv = Core.Functions.GetPlayer(src).PlayerData.items
2024-01-22 15:31:04 +00:00
elseif GetResourceState(ESXExport):find("start") then
local Player = ESX.GetPlayerFromId(src)
grabInv = Player.getInventory(false)
end
else
local p = promise.new()
Core.Functions.TriggerCallback('core_inventory:server:getInventory', function(cb) p:resolve(cb) end)
local result = Citizen.Await(p)
2024-01-22 16:55:58 +00:00
if type(result) == "string" then result = json.decode(result) end
2024-01-22 15:31:04 +00:00
grabInv = result
end
2024-01-11 17:06:33 +00:00
elseif GetResourceState(CodeMInv):find("start") then
foundInv = CodeMInv
grabInv = src and exports[CodeMInv]:GetUserInventory(src) or exports[CodeMInv]:GetClientPlayerInventory()
2024-01-11 17:06:33 +00:00
elseif GetResourceState(QBInv):find("start") then
foundInv = QBInv
grabInv = src and Core.Functions.GetPlayer(src).PlayerData.items or Core.Functions.GetPlayerData().items
2024-01-11 17:06:33 +00:00
else
print("^4ERROR^7: ^2No Inventory detected ^7- ^2Check ^3exports^1.^2lua^7")
end
2024-01-11 17:06:33 +00:00
if grabInv then
local hasTable = {}
for item, amount in pairs(items) do
if not Items[item] then print("^4ERROR^7: ^2Script can't find ingredient item in Shared Items - ^1"..item.."^7") end
local count = 0
for _, itemData in pairs(grabInv) do
if itemData and (itemData.name == item) then count += (itemData.count or itemData.amount or 1) end
2024-01-11 17:06:33 +00:00
end
foundInv = foundInv:gsub("%-", "^7-^6"):gsub("%_", "^7_^6")
local foundMessage = "^6Bridge^7: ^3hasItem^7[^6"..foundInv.."^7]: "..tostring(item).." ^3"..count.."^7/^3"..amount
if count >= amount then foundMessage = foundMessage.." ^5FOUND^7" else foundMessage = foundMessage .." ^1NOT FOUND^7" end
if Config.System.Debug then print(foundMessage) end
hasTable[item] = { hasItem = count >= amount, count = count }
2024-01-11 17:06:33 +00:00
end
for k, v in pairs(hasTable) do if not v.hasItem then return false, hasTable end end
return true, hasTable
2024-01-11 17:06:33 +00:00
end
end
-- Stash Items
function openStash(data)
if (data.job or data.gang) and not jobCheck(data.job or data.gang) then return end
if GetResourceState(OXInv):find("start") then
exports[OXInv]:openInventory('stash', data.stash)
elseif GetResourceState(CodeMInv):find("start") then
exports[CodeMInv]:OpenStash(data.stash, 400000, 100)
else
TriggerEvent("inventory:client:SetCurrentStash", data.stash)
TriggerServerEvent("inventory:server:OpenInventory", "stash", data.stash, data.stashOptions)
2024-01-11 17:06:33 +00:00
end
lookEnt(data.coords)
end
2024-01-23 22:03:27 +00:00
function getStash(stashName) local stashResource = ""
2024-01-11 17:06:33 +00:00
local stashItems, items = {}, {}
2024-01-23 22:03:27 +00:00
if GetResourceState(OXInv):find("start") then stashResource = OXInv
2024-01-11 17:06:33 +00:00
stashItems = exports[OXInv]:Inventory(stashName).items
2024-01-22 16:55:58 +00:00
2024-01-23 22:03:27 +00:00
elseif GetResourceState(QSInv):find("start") then stashResource = QSInv
2024-01-11 17:06:33 +00:00
stashItems = exports[QSInv]:GetStashItems(stashName)
2024-01-22 16:55:58 +00:00
2024-01-23 22:03:27 +00:00
elseif GetResourceState(CoreInv):find("start") then stashResource = CoreInv
2024-01-22 16:55:58 +00:00
stashItems = exports[CoreInv]:getInventory(stashName)
2024-01-23 22:03:27 +00:00
elseif GetResourceState(CodeMInv):find("start") then stashResource = CodeMInv
2024-01-11 17:06:33 +00:00
stashItems = exports[CodeMInv]:GetInventoryItems('Stash', stashName)
2024-01-22 16:55:58 +00:00
2024-01-23 22:03:27 +00:00
elseif GetResourceState(OrigenInv):find("start") then stashResource = OrigenInv
2024-01-24 17:10:44 +00:00
stashItems = exports[OrigenInv]:GetStashItems(stashName)
2024-01-22 17:14:56 +00:00
2024-01-23 22:03:27 +00:00
elseif GetResourceState(QBInv):find("start") then stashResource = QBInv
2024-01-11 17:06:33 +00:00
local result = MySQL.scalar.await('SELECT items FROM stashitems WHERE stash = ?', { stashName })
if result then stashItems = json.decode(result) end
end
2024-01-25 17:32:40 +00:00
2024-01-23 22:03:27 +00:00
if Config.System.Debug then print("^6Bridge^7: ^2Retrieving ^3Stash^2 with ^7"..stashResource) end
2024-01-11 17:06:33 +00:00
if stashItems then
for _, item in pairs(stashItems) do
local itemInfo = Items[item.name:lower()]
if itemInfo then
local indexNum = #items+1 -- Added to help recreate missing slot numbers
items[(item.slot and item.slot) or indexNum] = {
2024-01-23 22:03:27 +00:00
name = itemInfo.name or nil,
2024-01-11 17:06:33 +00:00
amount = tonumber(item.amount) or tonumber(item.count),
2024-01-23 22:03:27 +00:00
info = item.info or "",
label = itemInfo.label or nil,
description = itemInfo.description or "",
weight = itemInfo.weight or nil,
type = itemInfo.type or nil,
unique = itemInfo.unique or nil,
useable = itemInfo.useable or nil,
image = itemInfo.image or nil,
2024-01-25 17:32:40 +00:00
slot = (item.slot and item.slot) or indexNum,
2024-01-11 17:06:33 +00:00
}
end
end
if Config.System.Debug then print("^6Bridge^7: ^3GetStashItems^7: ^2Stash information for ^7'^6"..stashName.."^7' ^2retrieved^7") end
end
return items
end
function stashRemoveItem(stashItems, stashName, items) local amount = amount and amount or 1
if GetResourceState(OXInv):find("start") then
for k, v in pairs(items) do
exports[OXInv]:RemoveItem(stashName, k, v)
if Config.System.Debug then print("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..OXInv, k, v) end
end
2024-01-25 17:32:40 +00:00
2024-01-11 17:06:33 +00:00
elseif GetResourceState(QSInv):find("start") then
2024-01-22 16:55:58 +00:00
for k, v in pairs(items) do
for l in pairs(stashItems) do
if stashItems[l].name == k then
if (stashItems[l].amount - v) <= 0 then
if Config.System.Debug then
print("^6Bridge^7: ^2None of this item left in stash ^3Stash^7", k, v)
end
stashItems[l] = nil
else
if Config.System.Debug then
print("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..QBInv, k, v)
end
exports[QSInv]:RemoveItemIntoStash(stashName, k, v, l)
2024-01-21 17:24:10 +00:00
end
end
end
end
2024-01-22 16:55:58 +00:00
elseif GetResourceState(CoreInv):find("start") then
for k, v in pairs(items) do
exports[CoreInv]:removeItemExact(stashName, k, v)
if Config.System.Debug then print("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..CoreInv, k, v) end
2024-01-11 17:06:33 +00:00
end
2024-01-22 16:55:58 +00:00
2024-01-11 17:06:33 +00:00
elseif GetResourceState(CodeMInv):find("start") then
for k, v in pairs(items) do
for l in pairs(stashItems) do
if stashItems[l].name == k then
if (stashItems[l].amount - v) <= 0 then
if Config.System.Debug then
print("^6Bridge^7: ^2None of this item left in stash ^3Stash^7", k, v)
end
stashItems[l] = nil
else
if Config.System.Debug then
print("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..CodeMInv, k, v)
end
stashItems[l].amount -= v
end
end
end
end
if Config.System.Debug then
print("^6Bridge^7: ^3saveStash^7: ^2Saving ^3QB^2 stash ^7'^6"..stashName.."^7'")
end
2024-01-22 17:14:56 +00:00
elseif GetResourceState(OrigenInv):find("start") then
for k, v in pairs(items) do
exports[OrigenInv]:RemoveItem(stashName, k, v)
if Config.System.Debug then print("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..OrigenInv, k, v) end
end
elseif GetResourceState(QBInv):find("start") then
2024-01-11 17:06:33 +00:00
for k, v in pairs(items) do
for l in pairs(stashItems) do
if stashItems[l].name == k then
if (stashItems[l].amount - v) <= 0 then
if Config.System.Debug then
print("^6Bridge^7: ^2None of this item left in stash ^3Stash^7", k, v)
end
stashItems[l] = nil
else
if Config.System.Debug then
print("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..QBInv, k, v)
end
stashItems[l].amount -= v
end
end
end
end
if Config.System.Debug then
print("^6Bridge^7: ^3saveStash^7: ^2Saving ^3QB^2 stash ^7'^6"..stashName.."^7'")
end
MySQL.Async.insert('INSERT INTO stashitems (stash, items) VALUES (:stash, :items) ON DUPLICATE KEY UPDATE items = :items', { ['stash'] = stashName, ['items'] = json.encode(stashItems) })
else
print("^4ERROR^7: ^2No Inventory detected ^7- ^2Check ^3exports^1.^2lua^7")
2024-01-11 17:06:33 +00:00
end
end
RegisterNetEvent(GetCurrentResourceName()..":server:stashRemoveItem", stashRemoveItem)
function stashhasItem(stashItems, items, amount)
local invs = {OXInv, QSInv, CoreInv, CodeMInv, OrigenInv, QBInv}
local foundInv = ""
for _, inv in ipairs(invs) do
if GetResourceState(inv):find("start") then
foundInv = inv:gsub("%-", "^7-^6"):gsub("%_", "^7_^6")
break
end
end
if type(items) ~= "table" then items = { [items] = amount and amount or 1, } end
local hasTable = {}
for item, amount in pairs(items) do
local count = 0
for _, itemData in pairs(stashItems) do
2024-01-19 18:10:05 +00:00
if itemData and (itemData.name == item) then
count += (itemData.amount or 1)
end
end
local debugMsg = string.format("^6Bridge^7: ^3stashHasItem^7[^6%s^7]: %s '%s' ^3%d^7/^3%d^7", foundInv, (count >= amount and "^5FOUND^7" or "^1NOT FOUND^7"), item, count, amount)
if Config.System.Debug then print(debugMsg) end
hasTable[item] = { hasItem = (count >= amount), count = count }
2024-01-11 17:06:33 +00:00
end
for k, v in pairs(hasTable) do if v.hasItem == false then return false, hasTable end end
return true, hasTable
2024-01-23 18:59:02 +00:00
end