diff --git a/client/main.lua b/client/main.lua index 2516c28..ce9feb4 100644 --- a/client/main.lua +++ b/client/main.lua @@ -1,157 +1,90 @@ local IsAlreadyDrunk = false -local DrunkLevel = -1 +local DrunkLevel = -1 -function Drunk(level, start) - - CreateThread(function() - - local playerPed = PlayerPedId() - - if start then - DoScreenFadeOut(800) - Wait(1000) - end - - if level == 0 then - - RequestAnimSet("move_m@drunk@slightlydrunk") - - while not HasAnimSetLoaded("move_m@drunk@slightlydrunk") do - Wait(0) - end - - SetPedMovementClipset(playerPed, "move_m@drunk@slightlydrunk", true) - - elseif level == 1 then - - RequestAnimSet("move_m@drunk@moderatedrunk") - - while not HasAnimSetLoaded("move_m@drunk@moderatedrunk") do - Wait(0) - end - - SetPedMovementClipset(playerPed, "move_m@drunk@moderatedrunk", true) - - elseif level == 2 then - - RequestAnimSet("move_m@drunk@verydrunk") - - while not HasAnimSetLoaded("move_m@drunk@verydrunk") do - Wait(0) - end - - SetPedMovementClipset(playerPed, "move_m@drunk@verydrunk", true) - - end +local function SetDrunkEffect(level, start) + local playerPed = PlayerPedId() + local animSet = { + [0] = "move_m@drunk@slightlydrunk", + [1] = "move_m@drunk@moderatedrunk", + [2] = "move_m@drunk@verydrunk" + } - SetTimecycleModifier("spectator5") - SetPedMotionBlur(playerPed, true) - SetPedIsDrunk(playerPed, true) + if start then + DoScreenFadeOut(800) + Wait(1000) + end - if start then - DoScreenFadeIn(800) + if animSet[level] then + RequestAnimSet(animSet[level]) + while not HasAnimSetLoaded(animSet[level]) do + Wait(0) end + SetPedMovementClipset(playerPed, animSet[level], true) + end - end) + SetTimecycleModifier("spectator5") + SetPedMotionBlur(playerPed, true) + SetPedIsDrunk(playerPed, true) + if start then + DoScreenFadeIn(800) + end end -function Reality() - - CreateThread(function() - - local playerPed = PlayerPedId() +local function RemoveDrunkEffect() + local playerPed = PlayerPedId() - DoScreenFadeOut(800) - Wait(1000) + DoScreenFadeOut(800) + Wait(1000) - ClearTimecycleModifier() - ResetScenarioTypesEnabled() - ResetPedMovementClipset(playerPed, 0) - SetPedIsDrunk(playerPed, false) - SetPedMotionBlur(playerPed, false) + ClearTimecycleModifier() + ResetScenarioTypesEnabled() + ResetPedMovementClipset(playerPed, 0) + SetPedIsDrunk(playerPed, false) + SetPedMotionBlur(playerPed, false) - DoScreenFadeIn(800) + DoScreenFadeIn(800) +end - end) +local function HandleDrunkStatus(status) + local start = not IsAlreadyDrunk + local level = (status.val > 500000 and 2) or (status.val > 250000 and 1) or 0 + if status.val > 0 then + if level ~= DrunkLevel then + SetDrunkEffect(level, start) + end + IsAlreadyDrunk = true + DrunkLevel = level + elseif IsAlreadyDrunk then + RemoveDrunkEffect() + IsAlreadyDrunk = false + DrunkLevel = -1 + end end AddEventHandler('esx_status:loaded', function(status) - - TriggerEvent('esx_status:registerStatus', 'drunk', 0, '#8F15A5', + TriggerEvent('esx_status:registerStatus', 'drunk', 0, '#8F15A5', function(status) - if status.val > 0 then - return true - else - return false - end + return status.val > 0 end, function(status) status.remove(1500) end ) - CreateThread(function() - - while true do - - Wait(1000) - - TriggerEvent('esx_status:getStatus', 'drunk', function(status) - - if status.val > 0 then - - local start = true - - if IsAlreadyDrunk then - start = false - end - - local level = 0 - - if status.val <= 250000 then - level = 0 - elseif status.val <= 500000 then - level = 1 - else - level = 2 - end - - if level ~= DrunkLevel then - Drunk(level, start) - end - - IsAlreadyDrunk = true - DrunkLevel = level - end - - if status.val == 0 then - - if IsAlreadyDrunk then - Reality() - end - - IsAlreadyDrunk = false - DrunkLevel = -1 - - end - - end) - - end - - end) - + CreateThread(function() + while true do + Wait(Config.GetDrunkStatusTick) + TriggerEvent('esx_status:getStatus', 'drunk', HandleDrunkStatus) + end + end) end) RegisterNetEvent('esx_optionalneeds:onDrink') AddEventHandler('esx_optionalneeds:onDrink', function() - local playerPed = PlayerPedId() - - TaskStartScenarioInPlace(playerPed, "WORLD_HUMAN_DRINKING", 0, 1) + TaskStartScenarioInPlace(playerPed, "WORLD_HUMAN_DRINKING", 0, true) Wait(1000) ClearPedTasksImmediately(playerPed) - -end) \ No newline at end of file +end) diff --git a/config.lua b/config.lua index 0dea84d..3f32fa2 100644 --- a/config.lua +++ b/config.lua @@ -1,5 +1,5 @@ Config = {} -Config.TickTime = 100 -Config.UpdateClientTime = 5000 Config.Locale = GetConvar('esx:locale', 'en') + +Config.GetDrunkStatusTick = 5000 diff --git a/server/main.lua b/server/main.lua index e71416d..e704aa1 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,11 +1,7 @@ ESX.RegisterUsableItem('beer', function(source) - local xPlayer = ESX.GetPlayerFromId(source) - xPlayer.removeInventoryItem('beer', 1) - TriggerClientEvent('esx_status:add', source, 'drunk', 250000) TriggerClientEvent('esx_optionalneeds:onDrink', source) TriggerClientEvent('esx:showNotification', source, TranslateCap('used_beer')) - end)