From e4e5c6438e6c5cf2ce4f5edc37d2998c96ccd8cf Mon Sep 17 00:00:00 2001 From: Manason Date: Wed, 22 Nov 2023 08:07:07 -0800 Subject: [PATCH 1/2] refactor: using death state as a statebag --- client/dead.lua | 2 +- client/laststand.lua | 2 +- client/main.lua | 18 +++++++++++++----- server/main.lua | 30 ++++++++---------------------- shared/main.lua | 3 ++- 5 files changed, 25 insertions(+), 30 deletions(-) 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..6795f97 100644 --- a/server/main.lua +++ b/server/main.lua @@ -16,12 +16,20 @@ local playerState AddEventHandler('QBCore:Server:OnPlayerLoaded', function() playerState = Player(source).state + playerState:set(DEATH_STATE_STATE_BAG, Config.DeathState.ALIVE, 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 = string.sub(bagName, 8) + 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 +40,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 +88,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 From 2a09d12318cb995bf4cae204f9e918870ce6627d Mon Sep 17 00:00:00 2001 From: Manason Date: Wed, 22 Nov 2023 08:57:02 -0800 Subject: [PATCH 2/2] loading deathstate from metadata and using utility method --- server/main.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/server/main.lua b/server/main.lua index 6795f97..39ace51 100644 --- a/server/main.lua +++ b/server/main.lua @@ -14,9 +14,16 @@ 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, Config.DeathState.ALIVE, true) + 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) @@ -24,7 +31,7 @@ AddEventHandler('QBCore:Server:OnPlayerLoaded', function() end) AddStateBagChangeHandler(DEATH_STATE_STATE_BAG, nil, function(bagName, _, value) - local playerId = string.sub(bagName, 8) + 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)