Skip to content

Commit

Permalink
refactor!: net events
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason committed Nov 18, 2023
1 parent 645a279 commit ab65f7d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 54 deletions.
6 changes: 3 additions & 3 deletions client/damage/damage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ local function findDamageCause()
end
end
if detected then
TriggerServerEvent("hospital:server:SetWeaponDamage", CurrentDamageList)
lib.callback('qbx_medical:server:SetWeaponWounds', false, false, CurrentDamageList)
end
end

Expand Down Expand Up @@ -173,7 +173,7 @@ local function checkDamage(ped, boneId, weapon, damageDone)
applyImmediateEffects(ped, bone, weapon, damageDone)
injureBodyPart(bone)

TriggerServerEvent('hospital:server:SyncInjuries', {
lib.callback('qbx_medical:server:syncInjuries', false, false,{
limbs = BodyParts,
isBleeding = BleedLevel
})
Expand All @@ -195,7 +195,7 @@ local function applyDamage(ped, damageDone, isArmorDamaged)
if damageDone >= Config.HealthDamage then
local isBodyHitOrWeakWeapon = checkBodyHitOrWeakWeapon(isArmorDamaged, bodypart, weapon)
if isBodyHitOrWeakWeapon and isArmorDamaged then
TriggerServerEvent("hospital:server:SetArmor", GetPedArmour(ped))
lib.callback('qbx_medical:server:setArmor', false, false, GetPedArmour(ped))
elseif not isBodyHitOrWeakWeapon and isDamagingEvent(damageDone, weapon) then
checkDamage(ped, bone, weapon, damageDone)
end
Expand Down
3 changes: 1 addition & 2 deletions client/laststand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function EndLastStand()
TaskPlayAnim(ped, LastStandDict, "exit", 1.0, 8.0, -1, 1, -1, false, false, false)
InLaststand = false
LaststandTime = 0
TriggerServerEvent("hospital:server:SetLaststandStatus", false)
TriggerServerEvent('qbx_medical:server:onPlayerLaststandEnd')
end

exports('endLastStandDeprecated', EndLastStand)
Expand Down Expand Up @@ -82,5 +82,4 @@ function StartLastStand()
countdownLastStand()
end
end)
TriggerServerEvent("hospital:server:SetLaststandStatus", true)
end
3 changes: 0 additions & 3 deletions client/load-unload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ local function initDeathAndLastStand(metadata)
AllowRespawn()
elseif metadata.inlaststand then
StartLastStand()
else
TriggerServerEvent("hospital:server:SetDeathStatus", false)
TriggerServerEvent("hospital:server:SetLaststandStatus", false)
end
end

Expand Down
16 changes: 6 additions & 10 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function ResetMinorInjuries()
BlackoutTimer = 0
end

TriggerServerEvent('hospital:server:SyncInjuries', {
lib.callback('qbx_medical:server:syncInjuries', false, false,{
limbs = BodyParts,
isBleeding = BleedLevel
})
Expand All @@ -205,13 +205,13 @@ function ResetAllInjuries()
FadeOutTimer = 0
BlackoutTimer = 0

TriggerServerEvent('hospital:server:SyncInjuries', {
lib.callback('qbx_medical:server:syncInjuries', false, false,{
limbs = BodyParts,
isBleeding = BleedLevel
})

CurrentDamageList = {}
TriggerServerEvent('hospital:server:SetWeaponDamage', CurrentDamageList)
lib.callback('qbx_medical:server:SetWeaponWounds', false, false, CurrentDamageList)

SendBleedAlert()
MakePedLimp()
Expand Down Expand Up @@ -274,7 +274,7 @@ RegisterNetEvent('hospital:client:SetPain', function()
bone = Config.Bones[40269]
CreateInjury(BodyParts[bone], bone, 4)

TriggerServerEvent('hospital:server:SyncInjuries', {
lib.callback('qbx_medical:server:syncInjuries', false, false,{
limbs = BodyParts,
isBleeding = BleedLevel
})
Expand All @@ -293,8 +293,7 @@ RegisterNetEvent('hospital:client:HealInjuries', function(type)
else
ResetMinorInjuries()
end
TriggerServerEvent("hospital:server:RestoreWeaponDamage")

lib.callback('qbx_medical:server:clearWeaponWounds')
exports.qbx_core:Notify(Lang:t('success.wounds_healed'), 'success')
end)

Expand All @@ -320,7 +319,7 @@ exports('getPatientStatus', getPatientStatus)

---Revives player, healing all injuries
---Intended to be called from client or server.
RegisterNetEvent('hospital:client:Revive', function()
RegisterNetEvent('qbx_medical:client:playerRevived', function()
local ped = cache.ped

if IsDead or InLaststand then
Expand All @@ -331,15 +330,12 @@ RegisterNetEvent('hospital:client:Revive', function()
EndLastStand()
end

TriggerServerEvent("hospital:server:RestoreWeaponDamage")
SetEntityMaxHealth(ped, 200)
SetEntityHealth(ped, 200)
ClearPedBloodDamage(ped)
SetPlayerSprint(cache.playerId, true)
ResetAllInjuries()
ResetPedMovementClipset(ped, 0.0)
TriggerServerEvent('hud:server:RelieveStress', 100)
TriggerServerEvent("hospital:server:SetDeathStatus", false)
TriggerServerEvent("hospital:server:SetLaststandStatus", false)
exports.qbx_core:Notify(Lang:t('info.healthy'), 'inform')
end)
71 changes: 35 additions & 36 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ local playerWeaponWounds = {}
local triggerEventHooks = require 'modules.hooks.server'

---@param data number[] weapon hashes
RegisterNetEvent('hospital:server:SetWeaponDamage', function(data)
if GetInvokingResource() then return end
local src = source
local player = exports.qbx_core:GetPlayer(src)
if not player then return end
playerWeaponWounds[player.PlayerData.source] = data
lib.callback.register('qbx_medical:server:setWeaponWounds', function(source, data)
playerWeaponWounds[source] = data
end)

RegisterNetEvent('hospital:server:RestoreWeaponDamage', function()
if GetInvokingResource() then return end
local src = source
local player = exports.qbx_core:GetPlayer(src)
playerWeaponWounds[player.PlayerData.source] = nil
lib.callback.register('qbx_medical:server:clearWeaponWounds', function(source)
playerWeaponWounds[source] = nil
end)

---@param player table|number
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)
playerWeaponWounds[source] = nil
TriggerClientEvent('qbx_medical:client:playerRevived', player.PlayerData.source)
end

---Compatibility with txAdmin Menu's heal options.
---This is an admin only server side event that will pass the target player id or -1.
---@class EventData
Expand All @@ -36,15 +40,13 @@ AddEventHandler('txAdmin:events:healedPlayer', function(eventData)
return
end

TriggerClientEvent('hospital:client:Revive', eventData.id)
revivePlayer(eventData.id)
TriggerClientEvent("hospital:client:HealInjuries", eventData.id, "full")
end)

---@param data PlayerStatus
RegisterNetEvent('hospital:server:SyncInjuries', function(data)
if GetInvokingResource() then return end
local src = source
playerStatus[src] = data
lib.callback.register('qbx_medical:server:syncInjuries', function(source, data)
playerStatus[source] = data
end)

---@param limbs BodyParts
Expand Down Expand Up @@ -88,38 +90,29 @@ lib.callback.register('hospital:GetPlayerStatus', function(_, playerId)
return damage
end)

---@param isDead boolean
RegisterNetEvent('hospital:server:SetDeathStatus', function(isDead)
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", isDead)
player.Functions.SetMetaData("isdead", true)
end)

RegisterNetEvent('qbx_medical:server:playerDied', function()
RegisterNetEvent('qbx_medical:server:onPlayerLaststand', 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)
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData("inlaststand", true)
end)

---@param bool boolean
RegisterNetEvent('hospital:server:SetLaststandStatus', function(bool)
RegisterNetEvent('qbx_medical:server:onPlayerLaststandEnd', 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("inlaststand", bool)
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData("inlaststand", false)
end)

---@param amount number
RegisterNetEvent('hospital:server:SetArmor', function(amount)
if GetInvokingResource() then return end
local src = source
local player = exports.qbx_core:GetPlayer(src)
if not player then return end
lib.callback('qbx_medical:server:setArmor', function(source, amount)
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData("armor", amount)
end)

Expand Down Expand Up @@ -162,7 +155,13 @@ lib.addCommand('revive', {
{ name = 'id', help = Lang:t('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
triggerEventOnPlayer(source, 'hospital:client:Revive', args.id)
if not args.id then args.id = source end
local player = exports.qbx_core:GetPlayer(tonumber(args.id))
if not player then
TriggerClientEvent('ox_lib:notify', source, { description = Lang:t('error.not_online'), type = 'error' })
return
end
revivePlayer(args.id)
end)

lib.addCommand('setpain', {
Expand Down

0 comments on commit ab65f7d

Please sign in to comment.