diff --git a/client/dead.lua b/client/dead.lua index bd99fdd..9e714ad 100644 --- a/client/dead.lua +++ b/client/dead.lua @@ -23,7 +23,7 @@ exports('playDeadAnimation', playDeadAnimation) ---put player in death animation and make invincible function OnDeath() if DeathState == Config.DeathState.DEAD then return end - DeathState = Config.DeathState.DEAD + SetDeathState(Config.DeathState.DEAD) TriggerServerEvent('qbx_medical:server:playerDied') TriggerServerEvent("InteractSound_SV:PlayOnSource", "demo", 0.1) local player = cache.ped diff --git a/client/laststand.lua b/client/laststand.lua index 0d09706..ee6678e 100644 --- a/client/laststand.lua +++ b/client/laststand.lua @@ -74,7 +74,7 @@ function StartLastStand() ResurrectPlayer() SetEntityHealth(ped, 150) PlayUnescortedLastStandAnimation() - DeathState = Config.DeathState.LAST_STAND + SetDeathState(Config.DeathState.LAST_STAND) TriggerServerEvent('qbx_medical:server:onPlayerLaststand') CreateThread(function() while DeathState == Config.DeathState.LAST_STAND do diff --git a/client/main.lua b/client/main.lua index 62a875f..71ee8a6 100644 --- a/client/main.lua +++ b/client/main.lua @@ -30,14 +30,22 @@ function SetBleedLevel(level) playerState:set(BLEED_LEVEL_STATE_BAG, level, true) end +DeathState = playerState[DEATH_STATE_STATE_BAG] or Config.DeathState.ALIVE + +AddStateBagChangeHandler(DEATH_STATE_STATE_BAG, ('player:%s'):format(cache.serverId), function(_, _, value) + DeathState = value +end) + +function SetDeathState(deathState) + playerState:set(DEATH_STATE_STATE_BAG, deathState, true) +end + BleedTickTimer, AdvanceBleedTimer = 0, 0 FadeOutTimer, BlackoutTimer = 0, 0 ---@type number Hp = nil -DeathState = Config.DeathState.ALIVE - DeathTime = 0 LaststandTime = 0 RespawnHoldTime = 5 @@ -63,7 +71,7 @@ exports('isDead', function() end) exports('setIsDeadDeprecated', function(isDead) - DeathState = isDead and Config.DeathState.DEAD or Config.DeathState.ALIVE + SetDeathState(isDead and Config.DeathState.DEAD or Config.DeathState.ALIVE) end) exports('getLaststand', function() @@ -71,7 +79,7 @@ exports('getLaststand', function() end) exports('setLaststand', function(inLaststand) - DeathState = inLaststand and Config.DeathState.LAST_STAND or Config.DeathState.ALIVE + SetDeathState(inLaststand and Config.DeathState.LAST_STAND or Config.DeathState.ALIVE) end) exports('getDeathTime', function() @@ -244,7 +252,7 @@ RegisterNetEvent('qbx_medical:client:playerRevived', function() if DeathState ~= Config.DeathState.ALIVE then local pos = GetEntityCoords(ped, true) NetworkResurrectLocalPlayer(pos.x, pos.y, pos.z, GetEntityHeading(ped), true, false) - DeathState = Config.DeathState.ALIVE + SetDeathState(Config.DeathState.ALIVE) SetEntityInvincible(ped, false) EndLastStand() end diff --git a/server/main.lua b/server/main.lua index 03b4512..39ace51 100644 --- a/server/main.lua +++ b/server/main.lua @@ -14,14 +14,29 @@ local triggerEventHooks = require 'modules.hooks.server' local playerState +local function getDeathState(src) + local player = exports.qbx_core:GetPlayer(src) + return player.PlayerData.metadata.isdead and Config.DeathState.DEAD + or player.PlayerData.metadata.inlaststand and Config.DeathState.LAST_STAND + or Config.DeathState.ALIVE +end + AddEventHandler('QBCore:Server:OnPlayerLoaded', function() playerState = Player(source).state + playerState:set(DEATH_STATE_STATE_BAG, getDeathState(source), true) playerState:set(BLEED_LEVEL_STATE_BAG, 0, true) for bodyPartKey in pairs(Config.BodyParts) do playerState:set(BODY_PART_STATE_BAG_PREFIX .. bodyPartKey, nil, true) end end) +AddStateBagChangeHandler(DEATH_STATE_STATE_BAG, nil, function(bagName, _, value) + local playerId = GetPlayerFromStateBagName(bagName) + local player = exports.qbx_core:GetPlayer(playerId) + player.Functions.SetMetaData("isdead", value == Config.DeathState.DEAD) + player.Functions.SetMetaData("inlaststand", value == Config.DeathState.LAST_STAND) +end) + RegisterNetEvent('qbx_medical:server:playerDamagedByWeapon', function(hash) if WeaponsThatDamagedPlayers[source][hash] then return end WeaponsThatDamagedPlayers[source][hash] = true @@ -32,8 +47,6 @@ local function revivePlayer(player) if type(player) == "number" then player = exports.qbx_core:GetPlayer(player) end - player.Functions.SetMetaData("isdead", false) - player.Functions.SetMetaData("inlaststand", false) WeaponsThatDamagedPlayers[player.PlayerData.source] = nil TriggerClientEvent('qbx_medical:client:playerRevived', player.PlayerData.source) end @@ -82,26 +95,6 @@ lib.callback.register('hospital:GetPlayerStatus', function(_, playerId) return damage end) -RegisterNetEvent('qbx_medical:server:playerDied', function() - if GetInvokingResource() then return end - local src = source - local player = exports.qbx_core:GetPlayer(src) - if not player then return end - player.Functions.SetMetaData("isdead", true) -end) - -RegisterNetEvent('qbx_medical:server:onPlayerLaststand', function() - if GetInvokingResource() then return end - local player = exports.qbx_core:GetPlayer(source) - player.Functions.SetMetaData("inlaststand", true) -end) - -RegisterNetEvent('qbx_medical:server:onPlayerLaststandEnd', function() - if GetInvokingResource() then return end - local player = exports.qbx_core:GetPlayer(source) - player.Functions.SetMetaData("inlaststand", false) -end) - ---@param amount number lib.callback.register('qbx_medical:server:setArmor', function(source, amount) local player = exports.qbx_core:GetPlayer(source) diff --git a/shared/main.lua b/shared/main.lua index 992a9bf..86081ce 100644 --- a/shared/main.lua +++ b/shared/main.lua @@ -1,2 +1,3 @@ BODY_PART_STATE_BAG_PREFIX = 'qbx_medical:injuries:' -BLEED_LEVEL_STATE_BAG = 'qbx_medical:bleedLevel' \ No newline at end of file +BLEED_LEVEL_STATE_BAG = 'qbx_medical:bleedLevel' +DEATH_STATE_STATE_BAG = 'qbx_medical:deathState' \ No newline at end of file