Skip to content

Commit

Permalink
refactor: improve consumeables logic even more
Browse files Browse the repository at this point in the history
* refactor: improve consumeables logic even more

* Update consumables.lua

* new Consumables table
  • Loading branch information
TonybynMp4 authored Feb 12, 2024
1 parent d8994b6 commit 1514054
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 56 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 = Consumables.food[itemName].anim or {
clip = 'mp_player_int_eat_burger',
dict = 'mp_player_inteat@burger',
flag = 49
},
prop = {
prop = Consumables.food[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 = Consumables.drink[itemName].anim or {
clip = 'loop_bottle',
dict = 'mp_player_intdrink',
flag = 49
},
prop = {
prop = Consumables.drink[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 = Consumables.alcohol[itemName].anim or {
clip = 'loop_bottle',
dict = 'mp_player_intdrink',
flag = 49
},
prop = {
prop = Consumables.alcohol[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 += Consumables.alcohol[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
144 changes: 129 additions & 15 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,137 @@ Config.Density = {
['scenario'] = 0.8,
}

ConsumablesEat = {
['sandwich'] = math.random(35, 54),
['tosti'] = math.random(40, 50),
['twerks_candy'] = math.random(35, 54),
['snikkel_candy'] = math.random(40, 50),
}
---@meta
---@class anim
---@field clip string
---@field dict string
---@field flag number

ConsumablesDrink = {
['water_bottle'] = math.random(35, 54),
['kurkakola'] = math.random(35, 54),
['coffee'] = math.random(40, 50),
}
---@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 consumable
---@field min number
---@field max number
---@field anim anim?
---@field prop table?
---@field stressRelief table?

---@meta
---@class consumableAlcohol : consumable

ConsumablesAlcohol = {
['whiskey'] = math.random(20, 30),
['beer'] = math.random(30, 40),
['vodka'] = math.random(20, 40),
Consumables = {
---@type table<string, consumable>
food = {
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
},
},
},
---@type table<string, consumable>
drink = {
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
},
},
},
---@type table<string, consumableAlcohol>
alcohol = {
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
78 changes: 61 additions & 17 deletions server/consumables.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,75 @@
----------- / alcohol
for alcohol in pairs(ConsumablesAlcohol) do
for alcohol, params in pairs(Consumables.alcohol) do
exports.qbx_core:CreateUseableItem(alcohol, function(source, item)
TriggerClientEvent('consumables:client:DrinkAlcohol', source, item.name)
local player = exports.qbx_core:GetPlayer(source)
if not player then return end

local drank = lib.callback.await('consumables:client:DrinkAlcohol', source, item.name)
if not drank then return end
if not exports.ox_inventory:RemoveItem(source, item.name, 1, nil, 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(Consumables.drink) do
exports.qbx_core:CreateUseableItem(drink, function(source, item)
TriggerClientEvent('consumables:client:Drink', source, item.name)
local player = exports.qbx_core:GetPlayer(source)
if not player then return end

local drank = lib.callback.await('consumables:client:Drink', source, item.name)
if not drank then return end
if not exports.ox_inventory:RemoveItem(source, item.name, 1, nil, 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(Consumables.food) do
exports.qbx_core:CreateUseableItem(food, function(source, item)
TriggerClientEvent('consumables:client:Eat', source, item.name)
local player = exports.qbx_core:GetPlayer(source)
if not player then return end

local ate = lib.callback.await('consumables:client:Drink', source, item.name)
if not ate then return end
if not exports.ox_inventory:RemoveItem(source, item.name, 1, nil, 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,19 +111,9 @@ 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

return exports.ox_inventory:RemoveItem(source, item, 1)
end)
end)

0 comments on commit 1514054

Please sign in to comment.