-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.lua
189 lines (159 loc) · 6.03 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
local sharedConfig = require 'config.shared'
local logger = require '@qbx_core.modules.logger'
---@class Injury
---@field severity integer
---@field weaponHash number
---@class PlayerStatus
---@field injuries table<BodyPartKey, Injury>
---@field isBleeding number
---@alias Source number
local triggerEventHooks = require '@qbx_core.modules.hooks'
local function getDeathState(src)
local player = exports.qbx_core:GetPlayer(src)
return player.PlayerData.metadata.isdead and sharedConfig.deathState.DEAD
or player.PlayerData.metadata.inlaststand and sharedConfig.deathState.LAST_STAND
or sharedConfig.deathState.ALIVE
end
RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function()
local 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(sharedConfig.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 == sharedConfig.deathState.DEAD)
player.Functions.SetMetaData('inlaststand', value == sharedConfig.deathState.LAST_STAND)
Player(playerId).state:set("isDead", value == sharedConfig.deathState.DEAD or value == sharedConfig.deathState.LAST_STAND, true)
end)
---@param player table|number
local function revivePlayer(player)
TriggerClientEvent('qbx_medical:client:playerRevived', player --[[@as number]])
end
exports('Revive', revivePlayer)
---removes all ailments, sets to full health, and fills up hunger and thirst.
---@param src Source
local function heal(src)
TriggerClientEvent('qbx_medical:client:heal', src, 'full')
end
exports('Heal', heal)
---Removes any injuries with severity 2 or lower. Stops bleeding if bleed level is less than 3.
---@param src Source
local function healPartially(src)
TriggerClientEvent('qbx_medical:client:heal', src, 'partial')
end
exports('HealPartially', healPartially)
---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
---@field id number
---@param eventData EventData
AddEventHandler('txAdmin:events:healedPlayer', function(eventData)
if GetInvokingResource() ~= 'monitor' or type(eventData) ~= 'table' or type(eventData.id) ~= 'number' then
return
end
revivePlayer(eventData.id)
heal(eventData.id)
end)
local function getPlayerInjuries(state)
local injuries = {}
for bodyPartKey in pairs(sharedConfig.bodyParts) do
injuries[bodyPartKey] = state[BODY_PART_STATE_BAG_PREFIX .. bodyPartKey]
end
return injuries
end
---Get human readable info on a player's health
---@param src Source
---@return {injuries: string[], bleedLevel: integer, bleedState: string, damageCauses: table<number, true>}
local function getPlayerStatus(src)
local state = Player(src).state
local bleedLevel = state[BLEED_LEVEL_STATE_BAG]
local injuries = getPlayerInjuries(state)
local injuryStatuses = {}
local weaponsThatDamagedPlayer = {}
local i = 0
for bodyPartKey, injury in pairs(injuries) do
local bodyPart = sharedConfig.bodyParts[bodyPartKey]
i += 1
injuryStatuses[i] = bodyPart.label .. ' (' .. sharedConfig.woundLevels[injury.severity].label .. ')'
weaponsThatDamagedPlayer[injury.weaponHash] = true
end
local status = {
injuries = injuryStatuses,
bleedLevel = bleedLevel,
bleedState = sharedConfig.bleedingStates[bleedLevel],
damageCauses = weaponsThatDamagedPlayer
}
return status
end
exports('GetPlayerStatus', getPlayerStatus)
---@param amount number
lib.callback.register('qbx_medical:server:setArmor', function(source, amount)
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData('armor', amount)
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', player.PlayerData.source, 100, 100)
end
lib.callback.register('qbx_medical:server:resetHungerAndThirst', resetHungerAndThirst)
lib.addCommand('revive', {
help = locale('info.revive_player_a'),
restricted = 'group.admin',
params = {
{ name = 'id', help = locale('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
if not args.id then args.id = source end
local player = exports.qbx_core:GetPlayer(tonumber(args.id))
if not player then
exports.qbx_core:Notify(source, locale('error.not_online'), 'error')
return
end
revivePlayer(args.id)
end)
lib.addCommand('kill', {
help = locale('info.kill'),
restricted = 'group.admin',
params = {
{ name = 'id', help = locale('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
if not args.id then args.id = source end
local player = exports.qbx_core:GetPlayer(tonumber(args.id))
if not player then
exports.qbx_core:Notify(source, locale('error.not_online'), 'error')
return
end
lib.callback.await('qbx_medical:client:killPlayer', args.id)
end)
lib.addCommand('aheal', {
help = locale('info.heal_player_a'),
restricted = 'group.admin',
params = {
{ name = 'id', help = locale('info.player_id'), type = 'playerId', optional = true },
}
}, function(source, args)
if not args.id then args.id = source end
local player = exports.qbx_core:GetPlayer(tonumber(args.id))
if not player then
exports.qbx_core:Notify(source, locale('error.not_online'), 'error')
return
end
heal(args.id)
end)
lib.callback.register('qbx_medical:server:respawn', function(source)
if not triggerEventHooks('respawn', {source = source}) then return false end
TriggerEvent('qbx_medical:server:playerRespawned', source)
return true
end)
lib.callback.register('qbx_medical:server:log', function(_, event, message)
logger.log({source = 'qbx_medical', event = event, message = message})
end)