Skip to content

Commit

Permalink
refactor: replacing net events with callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason committed Nov 19, 2023
1 parent ab65f7d commit 236c26c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 70 deletions.
32 changes: 3 additions & 29 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 @@ -216,7 +209,7 @@ function ResetAllInjuries()
SendBleedAlert()
MakePedLimp()
doLimbAlert()
TriggerServerEvent("hospital:server:resetHungerThirst")
lib.callback('qbx_medical:server:resetHungerAndThirst')
end

exports('resetAllInjuries', ResetAllInjuries)
Expand Down Expand Up @@ -263,37 +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)

lib.callback('qbx_medical:server:syncInjuries', false, false,{
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
lib.callback('qbx_medical:server:clearWeaponWounds')
exports.qbx_core:Notify(Lang:t('success.wounds_healed'), 'success')
end)

Expand Down
64 changes: 23 additions & 41 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ AddEventHandler('txAdmin:events:healedPlayer', function(eventData)
end

revivePlayer(eventData.id)
TriggerClientEvent("hospital:client:HealInjuries", eventData.id, "full")
lib.callback('qbx_medical:client:heal', eventData.id, false, "full")
end)

---@param data PlayerStatus
Expand Down Expand Up @@ -111,43 +111,23 @@ RegisterNetEvent('qbx_medical:server:onPlayerLaststandEnd', function()
end)

---@param amount number
lib.callback('qbx_medical:server:setArmor', function(source, amount)
lib.callback.register('qbx_medical:server:setArmor', function(source, amount)
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData("armor", amount)
end)

RegisterNetEvent('hospital:server:resetHungerThirst', function()
if GetInvokingResource() then return end
local player = exports.qbx_core:GetPlayer(source)

if not player then return end
local function resetHungerAndThirst(player)
if type(player == 'number') then
player = exports.qbx_core:GetPlayer(player)
end

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

TriggerClientEvent('hud:client:UpdateNeeds', source, 100, 100)
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
end

TriggerClientEvent(event, player.PlayerData.source)
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",
Expand All @@ -164,24 +144,20 @@ lib.addCommand('revive', {
revivePlayer(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)
end)

lib.addCommand('kill', {
help = Lang:t('info.kill'),
restricted = "admin",
params = {
{ 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 @@ -191,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

0 comments on commit 236c26c

Please sign in to comment.