Skip to content

Commit

Permalink
refactor: improve consumeables logic even more
Browse files Browse the repository at this point in the history
  • Loading branch information
TonybynMp4 authored Feb 12, 2024
1 parent d8994b6 commit e4954cd
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 50 deletions.
40 changes: 16 additions & 24 deletions client/consumables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ end

-- Events

RegisterNetEvent('consumables:client:Eat', function(itemName)
lib.callback.register('consumables:client:Eat', function(itemName)
if lib.progressBar({
duration = 5000,
label = 'Eating...',
Expand All @@ -153,12 +153,12 @@ RegisterNetEvent('consumables:client:Eat', function(itemName)
mouse = false,
combat = true
},
anim = {
anim = ConsumablesEat[itemName].anim or {
clip = 'mp_player_int_eat_burger',
dict = 'mp_player_inteat@burger',
flag = 49
},
prop = {
prop = ConsumablesEat[itemName].anim.prop or {
{
model = 'prop_cs_burger_01',
bone = 18905,
Expand All @@ -167,17 +167,14 @@ RegisterNetEvent('consumables:client:Eat', function(itemName)
}
}
}) then -- if completed
local used = lib.callback.await('consumables:server:usedItem', false, itemName)
if not used then return end

lib.callback('consumables:server:addSustenance', false, false, 'hunger', ConsumablesEat[itemName])
TriggerServerEvent('hud:server:RelieveStress', math.random(2, 4))
return true
else -- if canceled
exports.qbx_core:Notify('Canceled...', 'error')
return false
end
end)

RegisterNetEvent('consumables:client:Drink', function(itemName)
lib.callback.register('consumables:client:Drink', function(itemName)
if lib.progressBar({
duration = 5000,
label = 'Drinking...',
Expand All @@ -189,12 +186,12 @@ RegisterNetEvent('consumables:client:Drink', function(itemName)
mouse = false,
combat = true
},
anim = {
anim = ConsumablesDrink[itemName].anim or {
clip = 'loop_bottle',
dict = 'mp_player_intdrink',
flag = 49
},
prop = {
prop = ConsumablesDrink[itemName].prop or {
{
model = 'prop_ld_flow_bottle',
bone = 18905,
Expand All @@ -203,16 +200,14 @@ RegisterNetEvent('consumables:client:Drink', function(itemName)
}
}
}) then -- if completed
local used = lib.callback.await('consumables:server:usedItem', false, itemName)
if not used then return end

lib.callback('consumables:server:addSustenance', false, false, 'thirst', ConsumablesDrink[itemName])
return true
else -- if canceled
exports.qbx_core:Notify('Canceled...', 'error')
return false
end
end)

RegisterNetEvent('consumables:client:DrinkAlcohol', function(itemName)
lib.callback.register('consumables:client:DrinkAlcohol', function(itemName)
if lib.progressBar({
duration = math.random(3000, 6000),
label = 'Drinking liquor...',
Expand All @@ -224,12 +219,12 @@ RegisterNetEvent('consumables:client:DrinkAlcohol', function(itemName)
mouse = false,
combat = true
},
anim = {
anim = ConsumablesAlcohol[itemName].anim or {
clip = 'loop_bottle',
dict = 'mp_player_intdrink',
flag = 49
},
prop = {
prop = ConsumablesAlcohol[itemName].prop or {
{
model = 'prop_amb_beer_bottle',
bone = 18905,
Expand All @@ -238,19 +233,16 @@ RegisterNetEvent('consumables:client:DrinkAlcohol', function(itemName)
}
}
}) then -- if completed
local used = lib.callback.await('consumables:server:usedItem', false, itemName)
if not used then return end

TriggerServerEvent('consumables:server:addThirst', {name = itemName, amount = QBX.PlayerData.metadata.thirst + ConsumablesAlcohol[itemName]})
TriggerServerEvent('hud:server:RelieveStress', math.random(2, 4))
alcoholCount += 1
alcoholCount += ConsumablesAlcohol[itemName].alcoholLevel or 1
if alcoholCount > 1 and alcoholCount < 4 then
TriggerEvent('evidence:client:SetStatus', 'alcohol', 200)
elseif alcoholCount >= 4 then
TriggerEvent('evidence:client:SetStatus', 'heavyalcohol', 200)
end
return true
else -- if canceled
exports.qbx_core:Notify('Canceled...', 'error')
return false
end
end)

Expand Down
138 changes: 128 additions & 10 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,141 @@ Config.Density = {
['scenario'] = 0.8,
}

---@meta
---@class anim
---@field clip string
---@field dict string
---@field flag number

---@meta
---@class prop
---@field model string
---@field bone number
---@field pos vector3
---@field rot vector3

---@meta
---@class stressRelief
---@field min number
---@field max number

---@meta
---@class consumeables
---@field min number
---@field max number
---@field anim anim?
---@field prop table?
---@field stressRelief table?

---@class ConsumablesEat : consumeables

---@type ConsumablesEat
ConsumablesEat = {
['sandwich'] = math.random(35, 54),
['tosti'] = math.random(40, 50),
['twerks_candy'] = math.random(35, 54),
['snikkel_candy'] = math.random(40, 50),
['sandwich'] = {
min = 35,
max = 54,
stressRelief = {
min = 1,
max = 4
},
},
['tosti'] = {
min = 40,
max = 50,
stressRelief = {
min = 1,
max = 4
},
},
['twerks_candy'] = {
min = 35,
max = 54,
stressRelief = {
min = 1,
max = 4
},
},
['snikkel_candy'] = {
min = 40,
max = 50,
stressRelief = {
min = 1,
max = 4
},
},
}

---@class ConsumablesDrink : consumeables

---@type ConsumablesDrink
ConsumablesDrink = {
['water_bottle'] = math.random(35, 54),
['kurkakola'] = math.random(35, 54),
['coffee'] = math.random(40, 50),
['water_bottle'] = {
min = 35,
max = 54,
stressRelief = {
min = 1,
max = 4
},
},
['kurkakola'] = {
min = 35,
max = 54,
stressRelief = {
min = 1,
max = 4
},
},
['coffee'] = {
min = 40,
max = 50,
anim = {
clip = 'idle_c',
dict = 'amb@world_human_drinking@coffee@male@idle_a',
flag = 49
},
prop = {
model = 'p_amb_coffeecup_01',
bone = 28422,
pos = {x = 0.0, y = 0.0, z = 0.0},
rot = {x = 0.0, y = 0.0, z = 0.0}
},
stressRelief = {
min = -10,
max = -1
},
},
}

---@class ConsumablesAlcohol : consumeables
---@field alcoholLevel? number

---@type ConsumablesAlcohol
ConsumablesAlcohol = {
['whiskey'] = math.random(20, 30),
['beer'] = math.random(30, 40),
['vodka'] = math.random(20, 40),
['whiskey'] = {
min = 20,
max = 30,
stressRelief = {
min = 1,
max = 4
},
},
['beer'] = {
min = 30,
max = 40,
stressRelief = {
min = 1,
max = 4
},
alcoholLevel = 0.25
},
['vodka'] = {
min = 20,
max = 40,
stressRelief = {
min = 1,
max = 4
},
},
}

Config.BlacklistedScenarios = {
Expand Down
67 changes: 51 additions & 16 deletions server/consumables.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
----------- / alcohol
for alcohol in pairs(ConsumablesAlcohol) do
for alcohol, params in pairs(ConsumablesAlcohol) do
exports.qbx_core:CreateUseableItem(alcohol, function(source, item)
TriggerClientEvent('consumables:client:DrinkAlcohol', source, item.name)
local drank = lib.callback.await('consumables:client:DrinkAlcohol', src, item.name)
if not drank then return end
if not player.Functions.RemoveItem(item.name, 1, item.slot) then return end

local sustenance = player.PlayerData.metadata.thirst + math.random(params.min, params.max)
player.Functions.SetMetaData('thirst', sustenance)

if not params.stressRelief then
params.stressRelief = {
min = 1,
max = 4
}
end
TriggerEvent('hud:server:RelieveStress', source, math.random(params.stressRelief.min, params.stressRelief.max))

TriggerClientEvent('hud:client:UpdateNeeds', source, player.PlayerData.metadata.thirst, sustenance)
end)
end

----------- / Non-Alcoholic Drinks
for drink in pairs(ConsumablesDrink) do
for drink, params in pairs(ConsumablesDrink) do
exports.qbx_core:CreateUseableItem(drink, function(source, item)
TriggerClientEvent('consumables:client:Drink', source, item.name)
local drank = lib.callback.await('consumables:client:Drink', src, item.name)
if not drank then return end
if not player.Functions.RemoveItem(item.name, 1, item.slot) then return end

local sustenance = player.PlayerData.metadata.thirst + math.random(params.min, params.max)
player.Functions.SetMetaData('thirst', sustenance)

if not params.stressRelief then
params.stressRelief = {
min = 1,
max = 4
}
end
TriggerEvent('hud:server:RelieveStress', source, math.random(params.stressRelief.min, params.stressRelief.max))

TriggerClientEvent('hud:client:UpdateNeeds', source, player.PlayerData.metadata.thirst, sustenance)
end)
end

----------- / Food
for food in pairs(ConsumablesEat) do
for food, params in pairs(ConsumablesEat) do
exports.qbx_core:CreateUseableItem(food, function(source, item)
TriggerClientEvent('consumables:client:Eat', source, item.name)
local ate = lib.callback.await('consumables:client:Drink', src, item.name)
if not ate then return end
if not player.Functions.RemoveItem(item.name, 1, item.slot) then return end

local sustenance = player.PlayerData.metadata.hunger + math.random(params.min, params.max)
player.Functions.SetMetaData('hunger', sustenance)

if not params.stressRelief then
params.stressRelief = {
min = 1,
max = 4
}
end
TriggerEvent('hud:server:RelieveStress', source, math.random(params.stressRelief.min, params.stressRelief.max))

TriggerClientEvent('hud:client:UpdateNeeds', source, player.PlayerData.metadata.hunger, sustenance)
end)
end

Expand Down Expand Up @@ -57,16 +102,6 @@ exports.qbx_core:CreateUseableItem('advancedlockpick', function(source)
TriggerEvent('lockpicks:UseLockpick', source, true)
end)

lib.callback.register('consumables:server:addSustenance', function(source, needType, amount)
local player = exports.qbx_core:GetPlayer(source)
if not player then return end

local sustenance = player.PlayerData.metadata[needType] + amount
player.Functions.SetMetaData(needType, sustenance)

TriggerClientEvent('hud:client:UpdateNeeds', source, player.PlayerData.metadata[needType], sustenance)
end)

lib.callback.register('consumables:server:usedItem', function(source, item)
local player = exports.qbx_core:GetPlayer(source)
if not player then return end
Expand Down

0 comments on commit e4954cd

Please sign in to comment.