mirror of
https://github.com/jimathy/jim-consumables.git
synced 2026-08-19 07:26:04 +01:00
Rework code to server side
In an attempt to tackle exploiters, I reworked a lot of the code to server side This will be tied to the new `jim_bridge` release but should function as normal now
This commit is contained in:
@@ -38,6 +38,7 @@ AddStateBagChangeHandler("jimConsumableItems", nil, function(bagName, key, value
|
|||||||
debugPrint("^5Statebag^7: ^2Synced ^6"..newItemCount.." ^2new Consumables^7")
|
debugPrint("^5Statebag^7: ^2Synced ^6"..newItemCount.." ^2new Consumables^7")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
AddStateBagChangeHandler("jimConsumableEmotes", nil, function(bagName, key, value, _unused)
|
AddStateBagChangeHandler("jimConsumableEmotes", nil, function(bagName, key, value, _unused)
|
||||||
if type(value) == "table" then
|
if type(value) == "table" then
|
||||||
local newEmoteCount = 0
|
local newEmoteCount = 0
|
||||||
@@ -128,13 +129,10 @@ RegisterNetEvent(getScript()..':Consume', function(itemName)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local returnItem = consumable.returnItem or nil
|
|
||||||
local animDict, anim = tostring(emote[1]), tostring(emote[2])
|
local animDict, anim = tostring(emote[1]), tostring(emote[2])
|
||||||
local model, model2, bone, bone2, drugeffect, stress
|
local model, model2, bone, bone2, drugeffect, stress
|
||||||
local P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12 = table.unpack({0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}) -- Default placement coord cariable
|
local P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12 = table.unpack({0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}) -- Default placement coord cariable
|
||||||
local RewardItem = consumable.rewards or nil
|
|
||||||
local type = consumable.type or ""
|
local type = consumable.type or ""
|
||||||
local pack = consumable.pack or nil
|
|
||||||
local string = "Using "
|
local string = "Using "
|
||||||
local canRun = consumable.canRun
|
local canRun = consumable.canRun
|
||||||
local stats = consumable.stats
|
local stats = consumable.stats
|
||||||
@@ -233,7 +231,7 @@ RegisterNetEvent(getScript()..':Consume', function(itemName)
|
|||||||
if model then
|
if model then
|
||||||
if IsModelValid(model) == 1 then
|
if IsModelValid(model) == 1 then
|
||||||
debugPrint("^5Debug^7: ^3PropSpawn^7: ^2Spawning consumable prop^7...")
|
debugPrint("^5Debug^7: ^3PropSpawn^7: ^2Spawning consumable prop^7...")
|
||||||
attachProp = makeProp({ prop = model, coords = vec4(pedCoords.x, pedCoords.y, pedCoords.z, 0.0)}, 1, 1)
|
attachProp = makeProp({ prop = model, coords = vec4(pedCoords.x, pedCoords.y, pedCoords.z, 0.0)}, 1, 1, true)
|
||||||
AttachEntityToEntity(attachProp, Player, bone, P1, P2, P3, P4, P5, P6, true, true, false, true, 1, true)
|
AttachEntityToEntity(attachProp, Player, bone, P1, P2, P3, P4, P5, P6, true, true, false, true, 1, true)
|
||||||
if model2 then
|
if model2 then
|
||||||
if IsModelValid(model2) == 1 then
|
if IsModelValid(model2) == 1 then
|
||||||
@@ -286,72 +284,45 @@ RegisterNetEvent(getScript()..':Consume', function(itemName)
|
|||||||
else
|
else
|
||||||
triggerNotify(nil, "Cancelled "..string, "error")
|
triggerNotify(nil, "Cancelled "..string, "error")
|
||||||
end
|
end
|
||||||
|
TriggerServerEvent(getScript()..":server:stopConsume")
|
||||||
end
|
end
|
||||||
Wait(0)
|
Wait(0)
|
||||||
time -= 12
|
time -= 12
|
||||||
end
|
end
|
||||||
StopEntityAnim(Player, anim, animDict, 1.0)
|
StopEntityAnim(Player, anim, animDict, 1.0)
|
||||||
unloadAnimDict(animDict)
|
unloadAnimDict(animDict)
|
||||||
|
|
||||||
if not cancelled then
|
if not cancelled then
|
||||||
hideText()
|
hideText()
|
||||||
removeItem(itemName, 1)
|
local needTypes = { }
|
||||||
if returnItem ~= nil then
|
|
||||||
debugPrint("returnItem detected")
|
|
||||||
currentToken = triggerCallback(AuthEvent)
|
|
||||||
addItem(returnItem.item, returnItem.amount)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Reward Item calculations
|
|
||||||
CreateThread(function()
|
|
||||||
if RewardItem then
|
|
||||||
debugPrint("Reward Item detected")
|
|
||||||
for i = 1, consumable.amounttogive 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
|
|
||||||
currentToken = triggerCallback(AuthEvent)
|
|
||||||
addItem(RewardItem[item].item, math.random(1, RewardItem[item].max or 1))
|
|
||||||
debugPrint("^5Debug^7: ^2Given reward prize^7: '^6"..RewardItem[item].item.."^7'")
|
|
||||||
break
|
|
||||||
end
|
|
||||||
Wait(100)
|
|
||||||
end
|
|
||||||
Wait(1500)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
if needStats then
|
if needStats then
|
||||||
jsonPrint(needStats)
|
|
||||||
|
--jsonPrint(needStats)
|
||||||
if needStats.hunger > 0 then
|
if needStats.hunger > 0 then
|
||||||
local hunger = 0
|
|
||||||
if Core and Core.Functions then
|
if Core and Core.Functions then
|
||||||
hunger = (Core.Functions.GetPlayerData().metadata["hunger"] or 0) + needStats.hunger
|
needTypes.hunger = (Core.Functions.GetPlayerData().metadata["hunger"] or 0) + needStats.hunger
|
||||||
hunger = hunger > 100 and 100 or hunger
|
needTypes.hunger = needTypes.hunger > 100 and 100 or needTypes.hunger
|
||||||
|
|
||||||
elseif ESX and ESX.GetPlayerData then
|
elseif ESX and ESX.GetPlayerData then
|
||||||
hunger = (needStats.hunger / 100) * 1000000
|
needTypes.hunger = (needStats.hunger / 100) * 1000000
|
||||||
hunger = hunger > 1000000 and 1000000 or hunger
|
needTypes.hunger = needTypes.hunger > 1000000 and 1000000 or needTypes.hunger
|
||||||
end
|
end
|
||||||
|
|
||||||
TriggerServerEvent(getScript()..":server:setNeed", "hunger", hunger)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if needStats.thirst > 0 then
|
if needStats.thirst > 0 then
|
||||||
local thirst = 0
|
|
||||||
if Core and Core.Functions then
|
if Core and Core.Functions then
|
||||||
thirst = (Core.Functions.GetPlayerData().metadata["thirst"] or 0) + needStats.thirst
|
needTypes.thirst = (Core.Functions.GetPlayerData().metadata["thirst"] or 0) + needStats.thirst
|
||||||
thirst = thirst > 100 and 100 or thirst
|
needTypes.thirst = needTypes.thirst > 100 and 100 or needTypes.thirst
|
||||||
|
|
||||||
elseif ESX and ESX.GetPlayerData then
|
elseif ESX and ESX.GetPlayerData then
|
||||||
thirst = (needStats.thirst / 100) * 1000000
|
needTypes.thirst = (needStats.thirst / 100) * 1000000
|
||||||
thirst = thirst > 1000000 and 1000000 or thirst
|
needTypes.thirst = needTypes.thirst > 1000000 and 1000000 or needTypes.thirst
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
TriggerServerEvent(getScript()..":server:finishConsume", needTypes)
|
||||||
|
|
||||||
TriggerServerEvent(getScript()..":server:setNeed", "thirst", thirst)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
debugPrint("^5Debug^7: ^2Hunger^7: [^6"..(needStats.hunger or 0).."^7] ^2Thrist^7: [^6"..(needStats.thirst or 0).."^7]" )
|
|
||||||
if stress and stress ~= 0 then
|
if stress and stress ~= 0 then
|
||||||
debugPrint("^5Debug^7: ^3Consume^7: ^2Relieving ^6"..stress.." ^2stress^7.")
|
debugPrint("^5Debug^7: ^3Consume^7: ^2Relieving ^6"..stress.." ^2stress^7.")
|
||||||
TriggerServerEvent('hud:server:RelieveStress', stress)
|
TriggerServerEvent('hud:server:RelieveStress', stress)
|
||||||
@@ -376,10 +347,6 @@ RegisterNetEvent(getScript()..':Consume', function(itemName)
|
|||||||
end) -- Used as overdosing/too drunk effect
|
end) -- Used as overdosing/too drunk effect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if pack then
|
|
||||||
currentToken = triggerCallback(AuthEvent)
|
|
||||||
addItem(pack.item, pack.amount)
|
|
||||||
end
|
|
||||||
if stats then
|
if stats then
|
||||||
if stats.screen then -- Screen effect activation
|
if stats.screen then -- Screen effect activation
|
||||||
debugPrint(stats.screen)
|
debugPrint(stats.screen)
|
||||||
|
|||||||
@@ -1,12 +1,49 @@
|
|||||||
local Consumables = Config.Consumables
|
local Consumables = Config.Consumables
|
||||||
local Emotes = Config.Emotes
|
local Emotes = Config.Emotes
|
||||||
|
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)
|
||||||
|
|
||||||
onResourceStart(function()
|
onResourceStart(function()
|
||||||
for k, v in pairs(Config.Consumables) do
|
for k, v in pairs(Config.Consumables) do
|
||||||
createUseableItem(k, function(source, item) TriggerClientEvent(getScript()..':Consume', source, item.name) end)
|
if not Items[k] then
|
||||||
if not Items[k] then print("^1Debug^7: ^2Item check ^7- '^1"..k.."^7' ^2not found in the shared lua^7") end
|
print("^1Debug^7: ^2Item check ^7- '^1"..k.."^7' ^2not found in the shared lua^7")
|
||||||
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
|
|
||||||
end
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
massMakeUseable()
|
||||||
|
|
||||||
--Export Import System--
|
--Export Import System--
|
||||||
GlobalState.jimConsumableItems = Consumables
|
GlobalState.jimConsumableItems = Consumables
|
||||||
@@ -21,6 +58,7 @@ end)
|
|||||||
local syncScheduled = false
|
local syncScheduled = false
|
||||||
function syncConsumables()
|
function syncConsumables()
|
||||||
debugPrint("^5Statebag^7: ^2Sending ^6"..countTable(Consumables).." ^3Consumables ^2to all clients^7")
|
debugPrint("^5Statebag^7: ^2Sending ^6"..countTable(Consumables).." ^3Consumables ^2to all clients^7")
|
||||||
|
massMakeUseable()
|
||||||
GlobalState.jimConsumableItems = Consumables
|
GlobalState.jimConsumableItems = Consumables
|
||||||
syncScheduled = false
|
syncScheduled = false
|
||||||
end
|
end
|
||||||
@@ -32,6 +70,96 @@ function syncEmotes()
|
|||||||
emoteSyncScheduled = false
|
emoteSyncScheduled = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
setThirst(src, needTypes.thirst)
|
||||||
|
end
|
||||||
|
if needTypes.hunger then
|
||||||
|
setHunger(src, needTypes.hunger)
|
||||||
|
end
|
||||||
|
debugPrint("^5Debug^7: ^2Hunger^7: [^6"..(needTypes.hunger or 0).."^7] ^2Thrist^7: [^6"..(needTypes.thirst or 0).."^7]" )
|
||||||
|
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)
|
||||||
|
|
||||||
RegisterNetEvent(getScript()..':server:syncAddItem', function(itemName, data)
|
RegisterNetEvent(getScript()..':server:syncAddItem', function(itemName, data)
|
||||||
if not Consumables[itemName] then
|
if not Consumables[itemName] then
|
||||||
Consumables[itemName] = data
|
Consumables[itemName] = data
|
||||||
|
|||||||
Reference in New Issue
Block a user