This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.lua
193 lines (157 loc) · 6.56 KB
/
server.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
190
191
192
193
RegisterServerEvent('msk_armor:giveNoBProofItem')
AddEventHandler('msk_armor:giveNoBProofItem', function()
local xPlayer = ESX.GetPlayerFromId(source)
local hasItem = xPlayer.getInventoryItem('nobproof')
if not hasItem or hasItem.count == 0 then
xPlayer.addInventoryItem('nobproof', 1)
logging('debug', 'Item nobproof added on Event giveNoBProofItem')
end
end)
RegisterServerEvent('msk_armor:removeNoBProofItem')
AddEventHandler('msk_armor:removeNoBProofItem', function()
local xPlayer = ESX.GetPlayerFromId(source)
local hasItem = xPlayer.getInventoryItem('nobproof')
if hasItem and hasItem.count >= 1 then
xPlayer.removeInventoryItem('nobproof', hasItem.count)
logging('debug', 'Item nobproof deleted on Event removeNoBProofItem')
end
end)
MSK.Register('msk_armor:setHotkey', function(source, itemName)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local hasItem = xPlayer.getInventoryItem(itemName)
if hasItem and hasItem.count >= 1 then
xPlayer.removeInventoryItem(itemName, 1)
return itemName, {label = Config.Armories[itemName].label, percent = Config.Armories[itemName].percent, skin = Config.Armories[itemName].skin, removeItem = Config.Armories[itemName].removeItem}
else
return itemName, false
end
end)
for k, v in pairs(Config.Armories) do
ESX.RegisterUsableItem(k, function(source)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local hasItem = xPlayer.getInventoryItem('nobproof')
if not v.jobs.enable or (v.jobs.enable and MSK.Table_Contains(v.jobs.jobs, xPlayer.job.name)) then
if hasItem and hasItem.count >= 1 then
xPlayer.removeInventoryItem('nobproof', hasItem.count)
end
xPlayer.triggerEvent('msk_armor:setArmor', k, v)
if v.removeItem then
xPlayer.removeInventoryItem(k, 1)
end
if Config.giveNoBProof then
xPlayer.addInventoryItem('nobproof', 1)
end
Config.Notification(src, _U('used_bproof'))
else
Config.Notification(src, _U('no_job'))
end
end)
end
ESX.RegisterUsableItem('nobproof', function(source)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local hasItem = xPlayer.getInventoryItem('nobproof')
if hasItem and hasItem.count >= 1 then
xPlayer.removeInventoryItem('nobproof', hasItem.count)
end
xPlayer.triggerEvent('msk_armor:setDelArmor')
Config.Notification(src, _U('used_nobproof'))
end)
MSK.Register('msk_armor:getStatusFromDB', function(source)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local vest = {health = 200, armor = 0, item = nil}
if xPlayer then
local data = MySQL.query.await('SELECT * FROM users WHERE identifier = @identifier', {
["@identifier"] = xPlayer.identifier
})
local item = MySQL.query.await('SELECT * FROM msk_armor WHERE identifier = @identifier', {
["@identifier"] = xPlayer.identifier
})
if data[1] then
if data[1].health then vest.health = data[1].health end
if data[1].armour then vest.armor = data[1].armour end
end
if item[1] then
if item[1].item then vest.item = item[1].item end
end
logging('debug', 'playerHealth: ' .. vest.health, 'playerArmor: ' .. vest.armor, 'Item: ' .. (vest.item or 'nil'))
return vest.health, vest.armor, vest.item
else
logging('error', '^1 xPlayer not found on Callback: getStatusFromDB ^0')
return 200, 0, nil
end
end)
RegisterServerEvent('msk_armor:giveBackVest')
AddEventHandler('msk_armor:giveBackVest', function(item)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
xPlayer.addInventoryItem(item, 1)
end)
RegisterServerEvent('msk_armor:refreshArmour')
AddEventHandler('msk_armor:refreshArmour', function(playerHealth, playerArmor, item)
local src = source
saveStatus({source = src}, playerHealth, playerArmor)
if item then
local xPlayer = ESX.GetPlayerFromId(src)
if item == 'remove' then
MySQL.query('DELETE FROM msk_armor WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
})
else
local data = MySQL.query.await('SELECT * FROM msk_armor WHERE identifier = @identifier', {
["@identifier"] = xPlayer.identifier
})
if data and data[1] then
MySQL.query('UPDATE msk_armor SET item = @item WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier,
['@item'] = item,
})
else
MySQL.query('INSERT INTO msk_armor (identifier, item) VALUES (@identifier, @item)', {
['@identifier'] = xPlayer.identifier,
['@item'] = item
})
end
end
end
end)
RegisterNetEvent('esx:playerLogout')
AddEventHandler('esx:playerLogout', function(source)
local playerId = source
local playerPed = GetPlayerPed(playerId)
saveStatus({source = playerId}, GetEntityHealth(playerPed), GetPedArmour(playerPed))
end)
RegisterNetEvent('esx:playerDropped')
AddEventHandler('esx:playerLogout', function(playerId, reason)
local playerId = playerId
local playerPed = GetPlayerPed(playerId)
saveStatus({source = playerId}, GetEntityHealth(playerPed), GetPedArmour(playerPed))
end)
saveStatus = function(player, playerHealth, playerArmor)
if not player then return logging('error', '^1Player not found on function ^3saveStatus^0') end
local xPlayer
if player.source then
xPlayer = ESX.GetPlayerFromId(player.source)
elseif player.identifier then
xPlayer = ESX.GetPlayerFromIdentifier(player.identifier)
elseif player.player then
xPlayer = player.player
end
if not xPlayer then return logging('error', '^1xPlayer not found on function ^3saveStatus^0') end
MySQL.query("UPDATE users SET armour = @armour, health = @health WHERE identifier = @identifier", {
['@armour'] = tonumber(playerArmor),
['@health'] = tonumber(playerHealth),
['@identifier'] = xPlayer.identifier
})
if Config.Refresh.debug then
logging('debug', '^2Update Status^0', ('Health: %s, Armor: %s'):format(playerHealth, playerArmor))
end
end
exports('saveStatus', saveStatus)
logging = function(code, ...)
if not Config.Debug then return end
MSK.Logging(code, ...)
end