Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replacing net events with callbacks #39

Merged
merged 2 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 7 additions & 37 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,7 @@ exports('getRespawnHoldTimeDeprecated', function()
return RespawnHoldTime
end)

RegisterNetEvent('hospital:client:adminHeal', function()
if GetInvokingResource() then return end
SetEntityHealth(cache.ped, 200)
TriggerServerEvent("hospital:server:resetHungerThirst")
end)

RegisterNetEvent('hospital:client:KillPlayer', function()
if GetInvokingResource() then return end
lib.callback('qbx_medical:client:killPlayer', function()
SetEntityHealth(cache.ped, 0)
end)

Expand Down Expand Up @@ -179,7 +172,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,18 +198,18 @@ 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()
doLimbAlert()
TriggerServerEvent("hospital:server:resetHungerThirst")
lib.callback('qbx_medical:server:resetHungerAndThirst')
end

exports('resetAllInjuries', ResetAllInjuries)
Expand Down Expand Up @@ -263,38 +256,18 @@ function ApplyBleed(level)
SendBleedAlert()
end

---Creates random injuries on the player
RegisterNetEvent('hospital:client:SetPain', function()
if GetInvokingResource() then return end
ApplyBleed(math.random(1, 4))

local bone = Config.Bones[24816]
CreateInjury(BodyParts[bone], bone, 4)

bone = Config.Bones[40269]
CreateInjury(BodyParts[bone], bone, 4)

TriggerServerEvent('hospital:server:SyncInjuries', {
limbs = BodyParts,
isBleeding = BleedLevel
})
end)

exports('getBleedStateLabelDeprecated', function(level)
return Config.BleedingStates[level]
end)

---heals player wounds.
---@param type? "full"|any heals all wounds if full otherwise heals only major wounds.
RegisterNetEvent('hospital:client:HealInjuries', function(type)
if GetInvokingResource() then return end
lib.callback.register('qbx_medical:client:heal', function(type)
if type == "full" then
ResetAllInjuries()
else
ResetMinorInjuries()
end
TriggerServerEvent("hospital:server:RestoreWeaponDamage")

exports.qbx_core:Notify(Lang:t('success.wounds_healed'), 'success')
end)

Expand All @@ -320,7 +293,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 +304,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)
133 changes: 57 additions & 76 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)
TriggerClientEvent("hospital:client:HealInjuries", eventData.id, "full")
revivePlayer(eventData.id)
lib.callback('qbx_medical:client:heal', eventData.id, false, "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,15 +90,6 @@ lib.callback.register('hospital:GetPlayerStatus', function(_, playerId)
return damage
end)

---@param isDead boolean
RegisterNetEvent('hospital:server:SetDeathStatus', function(isDead)
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)
end)

RegisterNetEvent('qbx_medical:server:playerDied', function()
if GetInvokingResource() then return end
local src = source
Expand All @@ -105,74 +98,50 @@ RegisterNetEvent('qbx_medical:server:playerDied', function()
player.Functions.SetMetaData("isdead", true)
end)

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

---@param amount number
RegisterNetEvent('hospital:server:SetArmor', function(amount)
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("armor", amount)
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData("inlaststand", false)
end)

RegisterNetEvent('hospital:server:resetHungerThirst', function()
if GetInvokingResource() then return end
---@param amount number
lib.callback.register('qbx_medical:server:setArmor', function(source, amount)
local player = exports.qbx_core:GetPlayer(source)

if not player then return end

player.Functions.SetMetaData('hunger', 100)
player.Functions.SetMetaData('thirst', 100)

TriggerClientEvent('hud:client:UpdateNeeds', source, 100, 100)
player.Functions.SetMetaData("armor", amount)
end)

---Triggers the event on the player or src, if no target is specified
---@param src number playerId of the one triggering the event
---@param event string event name
---@param targetPlayerId? string playerId of the target of the event
local function triggerEventOnPlayer(src, event, targetPlayerId)
if not targetPlayerId then
TriggerClientEvent(event, src)
return
end

local player = exports.qbx_core:GetPlayer(tonumber(targetPlayerId))

if not player then
TriggerClientEvent('ox_lib:notify', src, { description = Lang:t('error.not_online'), type = 'error' })
return
local function resetHungerAndThirst(player)
if type(player == 'number') then
player = exports.qbx_core:GetPlayer(player)
end

TriggerClientEvent(event, player.PlayerData.source)
player.Functions.SetMetaData('hunger', 100)
player.Functions.SetMetaData('thirst', 100)
TriggerClientEvent('hud:client:UpdateNeeds', player.PlayerData.source, 100, 100)
end

lib.callback.register('qbx_medical:server:resetHungerAndThirst', resetHungerAndThirst)

lib.addCommand('revive', {
help = Lang:t('info.revive_player_a'),
restricted = "admin",
params = {
{ name = 'id', help = Lang:t('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
triggerEventOnPlayer(source, 'hospital:client:Revive', args.id)
end)

lib.addCommand('setpain', {
help = Lang:t('info.pain_level'),
restricted = "admin",
params = {
{ name = 'id', help = Lang:t('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
triggerEventOnPlayer(source, 'hospital:client:SetPain', 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('kill', {
Expand All @@ -182,7 +151,13 @@ lib.addCommand('kill', {
{ name = 'id', help = Lang:t('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
triggerEventOnPlayer(source, 'hospital:client:KillPlayer', 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
lib.callback('qbx_medical:client:killPlayer', args.id)
end)

lib.addCommand('aheal', {
Expand All @@ -192,7 +167,13 @@ lib.addCommand('aheal', {
{ name = 'id', help = Lang:t('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
triggerEventOnPlayer(source, 'hospital:client:adminHeal', 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
lib.callback('qbx_medical:client:heal', args.id, false, "full")
end)

lib.callback.register('qbx_medical:server:respawn', function(source)
Expand Down
Loading