diff --git a/client/main.lua b/client/main.lua index 949a475..3ebb517 100644 --- a/client/main.lua +++ b/client/main.lua @@ -1,4 +1,5 @@ local config = require('config.client') +local sharedConfig = require('config.shared') local playerState = LocalPlayer.state local alcoholLevel = playerState.alcohol or 0 local playerWalk @@ -40,10 +41,10 @@ local function drunkLoop() drunkEffect(severity) end - Wait(60000 * config.alcoholDecayTime) + Wait(60000 * sharedConfig.alcoholDecayTime) if alcoholLevel > 0 then - alcoholLevel -= config.alcoholDecayAmount + alcoholLevel -= sharedConfig.alcoholDecayAmount end if alcoholLevel >= config.ethylComaValue then diff --git a/config/client.lua b/config/client.lua index 4d47334..5bffb0f 100644 --- a/config/client.lua +++ b/config/client.lua @@ -1,8 +1,4 @@ return { - --- time in minutes before the alcohol level is updated - alcoholDecayTime = 1, - --- amount of alcohol to decay per alcoholDecayTime - alcoholDecayAmount = 0.1, --- delay in ms before the effect is first applied delayEffect = 15000, effect = { diff --git a/config/shared.lua b/config/shared.lua new file mode 100644 index 0000000..c47518b --- /dev/null +++ b/config/shared.lua @@ -0,0 +1,6 @@ +return { + --- time in minutes before the alcohol level is updated + alcoholDecayTime = 1, + --- amount of alcohol to decay per alcoholDecayTime + alcoholDecayAmount = 0.1, +} \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua index d700ffd..6344423 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -19,7 +19,10 @@ server_scripts { 'server/main.lua' } -file 'config/client.lua' +files { + 'config/client.lua', + 'config/shared.lua' +} lua54 'yes' use_experimental_fxv2_oal 'yes' diff --git a/server/main.lua b/server/main.lua index e1d7d7a..7241f92 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,5 +1,6 @@ lib.versionCheck('TonybynMp4/qbx_alcoholism') local config = require 'config.server' +local sharedConfig = require 'config.shared' for alcohol, params in pairs(config.alcoholItems) do exports.qbx_core:CreateUseableItem(alcohol, function(source, item) @@ -74,7 +75,20 @@ end) RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function() local player = exports.qbx_core:GetPlayer(source) - Player(source).state:set('alcohol', player.PlayerData.metadata.alcohol or 0, true) + local lastLoggedOut = player.PlayerData.lastLoggedOut + if not lastLoggedOut then return end + local timePassed = (os.time() - lastLoggedOut/1000) / 60 + local alcohol = player.PlayerData.metadata.alcohol + alcohol -= (timePassed / sharedConfig.alcoholDecayTime) * sharedConfig.alcoholDecayAmount + + Player(source).state:set('alcohol', alcohol, true) +end) + +RegisterNetEvent('QBCore:Client:OnPlayerUnload', function() + local player = exports.qbx_core:GetPlayer(source) + if not player then return end + player.Functions.SetMetaData('alcohol', Player(source).state.alcohol) + Player(source).state:set('alcohol', 0, true) end) AddStateBagChangeHandler('alcohol', nil, function(bagName, _, value) @@ -83,5 +97,6 @@ AddStateBagChangeHandler('alcohol', nil, function(bagName, _, value) if value == Player(source).state.alcohol then return end local player = exports.qbx_core:GetPlayer(source) if not player then return end + if value == player.PlayerData.metadata.alcohol then return end player.Functions.SetMetaData('alcohol', value) end)