2023-03-02 15:06:30 +00:00
|
|
|
local Consumables = Config.Consumables
|
|
|
|
|
local Emotes = Config.Emotes
|
2025-09-13 02:54:36 +01:00
|
|
|
local isConsuming = {}
|
|
|
|
|
|
|
|
|
|
local function massMakeUseable()
|
|
|
|
|
for k, v in pairs(Config.Consumables) do
|
|
|
|
|
createUseableItem(k, function(source, item)
|
|
|
|
|
local src = source
|
|
|
|
|
if not isConsuming[src] and hasItem(item.name, 1, src) then
|
|
|
|
|
|
|
|
|
|
registerConsume(src, item.name)
|
|
|
|
|
TriggerClientEvent(getScript()..':Consume', src, item.name)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function registerConsume(src, item)
|
|
|
|
|
-- Start building server sided item return table
|
|
|
|
|
local consumable = Consumables[item]
|
|
|
|
|
isConsuming[src] = {
|
|
|
|
|
item = item,
|
|
|
|
|
rewardItem = checkReward(consumable) or nil,
|
|
|
|
|
returnItem = checkReturnItem(consumable) or nil,
|
|
|
|
|
packItem = checkPackItem(consumable) or nil
|
|
|
|
|
}
|
|
|
|
|
debugPrint("^5Debug^7: ^2Player ^3"..src.." ^2started consuming ^7'^2"..item.."^7'")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
RegisterNetEvent(getScript()..":RegisterConsuming", function(item)
|
|
|
|
|
local src = source
|
|
|
|
|
registerConsume(src, item)
|
|
|
|
|
end)
|
2022-04-13 20:01:09 +01:00
|
|
|
|
2025-05-12 22:52:03 +01:00
|
|
|
onResourceStart(function()
|
|
|
|
|
for k, v in pairs(Config.Consumables) do
|
2025-09-13 02:54:36 +01:00
|
|
|
if not Items[k] then
|
|
|
|
|
print("^1Debug^7: ^2Item check ^7- '^1"..k.."^7' ^2not found in the shared lua^7")
|
|
|
|
|
end
|
|
|
|
|
if not Config.Emotes[v.emote] then
|
|
|
|
|
print("^1Debug^7: ^2Emote check ^7- '^1"..k.."^7' ^2requested emote ^7'^6"..v.emote.."^7' - ^2not found in config^7.^2lua^7")
|
|
|
|
|
end
|
2025-05-12 22:52:03 +01:00
|
|
|
end
|
|
|
|
|
|
2025-09-13 02:54:36 +01:00
|
|
|
massMakeUseable()
|
|
|
|
|
|
2025-05-12 22:52:03 +01:00
|
|
|
--Export Import System--
|
2025-05-22 14:10:30 +01:00
|
|
|
GlobalState.jimConsumableItems = Consumables
|
|
|
|
|
GlobalState.jimConsumableEmotes = Emotes
|
2025-05-12 22:52:03 +01:00
|
|
|
end, true)
|
2022-08-24 09:23:43 +01:00
|
|
|
|
2025-05-22 14:10:30 +01:00
|
|
|
onResourceStop(function()
|
|
|
|
|
GlobalState.jimConsumableItems = Consumables
|
|
|
|
|
GlobalState.jimConsumableEmotes = Emotes
|
|
|
|
|
end)
|
|
|
|
|
|
2025-04-01 19:34:44 +01:00
|
|
|
local syncScheduled = false
|
|
|
|
|
function syncConsumables()
|
2025-05-22 14:10:30 +01:00
|
|
|
debugPrint("^5Statebag^7: ^2Sending ^6"..countTable(Consumables).." ^3Consumables ^2to all clients^7")
|
2025-09-13 02:54:36 +01:00
|
|
|
massMakeUseable()
|
2025-05-22 14:10:30 +01:00
|
|
|
GlobalState.jimConsumableItems = Consumables
|
2025-04-01 19:34:44 +01:00
|
|
|
syncScheduled = false
|
|
|
|
|
end
|
2025-05-22 14:10:30 +01:00
|
|
|
|
2025-04-01 19:34:44 +01:00
|
|
|
local emoteSyncScheduled = false
|
|
|
|
|
function syncEmotes()
|
2025-05-22 14:10:30 +01:00
|
|
|
debugPrint("^5Statebag^7: ^2Sending ^6"..countTable(Emotes).." ^3Emotes to all clients^7")
|
|
|
|
|
GlobalState.jimConsumableEmotes = Emotes
|
2025-04-01 19:34:44 +01:00
|
|
|
emoteSyncScheduled = false
|
2023-02-14 14:52:57 +00:00
|
|
|
end
|
|
|
|
|
|
2025-09-13 02:54:36 +01:00
|
|
|
-- Return item
|
|
|
|
|
function checkPackItem(consumable)
|
|
|
|
|
local result = nil
|
|
|
|
|
if consumable.pack then
|
|
|
|
|
result = { item = consumable.pack.item, amount = consumable.pack.amount }
|
|
|
|
|
end
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Return item
|
|
|
|
|
function checkReturnItem(consumable)
|
|
|
|
|
local result = nil
|
|
|
|
|
if consumable.returnItem then
|
|
|
|
|
result = { item = consumable.returnItem.item, amount = consumable.returnItem.amount }
|
|
|
|
|
end
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Reward items
|
|
|
|
|
function checkReward(consumable)
|
|
|
|
|
local result = nil
|
|
|
|
|
local rewardItem = consumable.rewards or nil
|
|
|
|
|
if rewardItem then
|
|
|
|
|
debugPrint("Reward Item detected")
|
|
|
|
|
for i = 1, consumable.amounttogive or 1 do
|
|
|
|
|
local rarity = math.random(1, 4) -- rarity calculation
|
|
|
|
|
while true do
|
|
|
|
|
local item = math.random(1, countTable(rewardItem)) -- random item in the list to pick
|
|
|
|
|
if rewardItem[item].rarity >= rarity then
|
|
|
|
|
result = result or {}
|
|
|
|
|
result[#result+1] = { item = rewardItem[item].item, amount = math.random(1, rewardItem[item].max or 1) }
|
|
|
|
|
debugPrint("^5Debug^7: ^2Cachced reward prize^7: '^6"..rewardItem[item].item.."^7'")
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
Wait(10)
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
RegisterNetEvent(getScript()..":server:finishConsume", function(needTypes)
|
|
|
|
|
local src = source
|
|
|
|
|
|
|
|
|
|
if isConsuming[src] ~= nil then
|
|
|
|
|
|
|
|
|
|
removeItem(isConsuming[src].item, 1, src)
|
|
|
|
|
if isConsuming[src].rewardItem then
|
|
|
|
|
debugPrint("^5Debug^7: ^2Giving player ^3"..src.." ^2reward items^7")
|
|
|
|
|
for i = 1, #isConsuming[src].rewardItem do
|
|
|
|
|
local item = isConsuming[src].rewardItem[i]
|
|
|
|
|
addItem(item.item, item.amount, nil, src)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if isConsuming[src].returnItem then
|
|
|
|
|
debugPrint("^5Debug^7: ^2Giving player ^3"..src.." ^2return item^7")
|
|
|
|
|
for i = 1, #isConsuming[src].returnItem do
|
|
|
|
|
local item = isConsuming[src].returnItem[i]
|
|
|
|
|
addItem(item.item, item.amount, nil, src)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if isConsuming[src].packItem then
|
|
|
|
|
for i = 1, #isConsuming[src].packItem do
|
|
|
|
|
debugPrint("^5Debug^7: ^2Giving player ^3"..src.." ^2Pack items^7")
|
|
|
|
|
local item = isConsuming[src].packItem[i]
|
|
|
|
|
addItem(item.item, item.amount, nil, src)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Needs
|
|
|
|
|
if needTypes.thirst then
|
2025-10-01 01:20:33 +01:00
|
|
|
debugPrint("^5Debug^7: ^2Thrist^7: [^6"..(needTypes.thirst or 0).."^7]")
|
2025-09-13 02:54:36 +01:00
|
|
|
setThirst(src, needTypes.thirst)
|
|
|
|
|
end
|
|
|
|
|
if needTypes.hunger then
|
2025-10-01 01:20:33 +01:00
|
|
|
debugPrint("^5Debug^7: ^2Hunger^7: [^6"..(needTypes.hunger or 0).."^7]")
|
2025-09-13 02:54:36 +01:00
|
|
|
setHunger(src, needTypes.hunger)
|
|
|
|
|
end
|
2025-10-01 01:20:33 +01:00
|
|
|
if needTypes.stress then
|
|
|
|
|
debugPrint("^5Debug^7: ^2Stress^7: [^6"..(needTypes.stress or 0).."^7]")
|
|
|
|
|
setStress(src, needTypes.stress)
|
|
|
|
|
end
|
2025-09-13 02:54:36 +01:00
|
|
|
isConsuming[src] = nil
|
|
|
|
|
else
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterNetEvent(getScript()..":server:stopConsume", function()
|
|
|
|
|
local src = source
|
|
|
|
|
debugPrint("^5Player stopped consuming early^7", src)
|
|
|
|
|
isConsuming[src] = nil
|
|
|
|
|
end)
|
|
|
|
|
|
2025-04-01 19:34:44 +01:00
|
|
|
RegisterNetEvent(getScript()..':server:syncAddItem', function(itemName, data)
|
|
|
|
|
if not Consumables[itemName] then
|
|
|
|
|
Consumables[itemName] = data
|
2025-05-22 14:23:12 +01:00
|
|
|
--createUseableItem(itemName, function(source, item) TriggerClientEvent(getScript()..':Consume', source, itemName) end)
|
2025-05-12 22:52:03 +01:00
|
|
|
debugPrint("^5Debug^7: "..GetInvokingResource().." ^2is sending new ^3Item^7: '"..itemName.."'")
|
2025-04-01 19:34:44 +01:00
|
|
|
if not syncScheduled then
|
|
|
|
|
syncScheduled = true
|
2025-05-22 14:10:30 +01:00
|
|
|
SetTimeout(5000, syncConsumables)
|
2025-04-01 19:34:44 +01:00
|
|
|
end
|
|
|
|
|
else
|
2025-05-12 22:52:03 +01:00
|
|
|
debugPrint("^1Debug^7: "..GetInvokingResource().." ^2is sending ^1duplicate ^3Item^7: '"..itemName.."'")
|
2025-04-01 19:34:44 +01:00
|
|
|
end
|
|
|
|
|
end)
|
2023-02-14 14:40:09 +00:00
|
|
|
|
2025-04-01 19:34:44 +01:00
|
|
|
RegisterNetEvent(getScript()..':server:syncAddEmote', function(emoteName, data)
|
|
|
|
|
if not Emotes[emoteName] then
|
|
|
|
|
Emotes[emoteName] = data
|
2025-05-12 22:52:03 +01:00
|
|
|
debugPrint("^5Debug^7: "..GetInvokingResource().." ^2is sending new ^3Emote^7: '"..emoteName.."'")
|
2025-04-01 19:34:44 +01:00
|
|
|
if not emoteSyncScheduled then
|
|
|
|
|
emoteSyncScheduled = true
|
2025-05-22 14:10:30 +01:00
|
|
|
SetTimeout(5000, syncEmotes)
|
2025-04-01 19:34:44 +01:00
|
|
|
end
|
|
|
|
|
else
|
2025-05-12 22:52:03 +01:00
|
|
|
debugPrint("^1Debug^7: "..GetInvokingResource().." ^2is sending ^1duplicate ^3Emote^7: '"..emoteName.."'")
|
2025-04-01 19:34:44 +01:00
|
|
|
end
|
2023-03-02 15:06:30 +00:00
|
|
|
end)
|
2023-02-14 14:40:09 +00:00
|
|
|
|
2025-05-12 22:52:03 +01:00
|
|
|
--[[
|
2025-04-01 19:34:44 +01:00
|
|
|
Core.Commands.Add('consumableCreator', "Create consumables (admin only)", {}, false, function(source)
|
|
|
|
|
if source > 0 then return TriggerClientEvent(getScript()..":client:consumableCreator", source) end
|
|
|
|
|
end, 'admin')
|
2023-03-02 15:06:30 +00:00
|
|
|
|
2025-04-01 19:34:44 +01:00
|
|
|
Core.Commands.Add('testConsumableEmotes', "Test consumables Emotes (admin only)", {}, false, function(source)
|
|
|
|
|
if source > 0 then return TriggerClientEvent(getScript()..":client:testEmotes", source) end
|
2025-05-12 22:52:03 +01:00
|
|
|
end, 'admin')
|
|
|
|
|
]]
|