forked from BryanLTU/bryan_airdrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.lua
205 lines (165 loc) · 6.49 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
194
195
196
197
198
199
200
201
202
203
204
205
ESX = nil
local airdrops = {}
local tryCount = 0
local cooldown = math.random(Config.Airdrops.Cooldown.min, Config.Airdrops.Cooldown.max)
TriggerEvent(Config.FrameworkObj, function(obj) ESX = obj end)
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == GetCurrentResourceName() then
if Config.Debug then print(string.format('%s Started Successfully | Server Side', GetCurrentResourceName())) end
if not Config.Airdrops.OnlyCommand then
Citizen.CreateThread(function() StartAirdropLoop(); end)
end
end
end)
RegisterNetEvent('esx:playerLoaded', function(playerId, xPlayer)
TriggerClientEvent('bryan_airdrops:syncAirdrops', playerId, airdrops)
end)
RegisterNetEvent('bryan_airdrops:pickupAirdrop', function(id)
local airdrop = GetAirdrop(id)
if airdrop then
RewardPlayer(source, airdrop.items)
RemoveAirdrop(id)
TriggerClientEvent('bryan_airdrops:removeObject', -1, id)
TriggerClientEvent('bryan_airdrops:syncAirdrops', -1, airdrops)
else
if Config.Debug then
print('Could not find airdrop by it\'s ID')
end
end
end)
RegisterNetEvent('bryan_airdrops:airdropLanded', function(id)
local airdropIndex = GetAirdropIndex(id)
airdrops[airdropIndex].landed = true
end)
StartAirdropLoop = function()
while true do
while cooldown > 0 do
if Config.Debug then print(string.format('%s Minutes Till Next Airdrop Spawn', cooldown)) end
Citizen.Wait(60 * 1000)
cooldown = cooldown - 1
end
RestartCooldown()
if Config.Airdrops.SpawnOffline or (not Config.Airdrops.SpawnOffline and #ESX.GetPlayers() > 0) then
SpawnAirdrop()
end
Citizen.Wait(10)
end
end
if Config.Airdrops.Command.enabled then
ESX.RegisterCommand(Config.Airdrops.Command.name, Config.Airdrops.Command.groups, function(xPlayer, args, showError)
if Config.Debug then print(args.lootTable == nil, Config.LootTables[args.lootTable] ~= nil) end
if args.lootTable == nil or Config.LootTables[args.lootTable] ~= nil then
if Config.Airdrops.Command.CommandRestart then RestartCooldown() end
if Config.Airdrops.CoordsOfCommand and (xPlayer and xPlayer.source) then
SpawnAirdrop(args.lootTable, GetEntityCoords(GetPlayerPed(xPlayer.source)) + vector3(0.0, 0.0, 150.0))
else
SpawnAirdrop(args.lootTable)
end
if xPlayer and xPlayer.source then
Config.NotifyExecute(xPlayer.source, xPlayer)
end
else
showError(_U('command_error_loottable_not_found', args.lootTable))
end
end, true, {help = _U('command_help'), validate = false, arguments = {
{ name = 'lootTable', help = _U('command_args_lootTable'), type = 'number' },
}})
end
SpawnAirdrop = function(lootTable, customCoords)
local randomLocation = Config.Locations[math.random(1, #Config.Locations)]
local isLocationTaken = IsLocationTaken(randomLocation)
if Config.Debug then print(tryCount > 10, not isLocationTaken) end
if tryCount > 10 or not isLocationTaken or customCoords then
tryCount = 0
local airdropId = math.random(10000, 99999)
local airdropItems = SelectLoottable(lootTable)
local coords = randomLocation
if customCoords then coords = customCoords
elseif coords.z < 150.0 then coords = vector3(randomLocation.x, randomLocation.y, 200.0) end
if Config.Airdrops.DeletePrevious then
TriggerClientEvent('bryan_airdrops:removeAllObjects', -1)
airdrops = {}
end
table.insert(airdrops, {
id = airdropId,
items = airdropItems,
coords = coords
})
if Config.Debug then print('Airdrop Spawned') end
TriggerClientEvent('bryan_airdrops:syncAirdrops', -1, airdrops)
Config.NotifyPlayers(_U('notification_airdrop_spawned'))
Citizen.CreateThread(function()
local airdropIndex = GetAirdropIndex(airdropId)
while not airdrops[airdropIndex].landed do
airdrops[airdropIndex].coords = airdrops[airdropIndex].coords - vector3(0.0, 0.0, 0.01 * Config.Airdrops.FallSpeed)
Citizen.Wait(1)
end
if Config.Debug then print(string.format('Airdrop (ID: %s) Landed', airdrops[airdropIndex].id)) end
end)
elseif isLocationTaken then
tryCount = tryCount + 1
SpawnAirdrop(lootTable)
end
end
IsLocationTaken = function(location)
for k, v in pairs(airdrops) do
if v.coords == location then
return true
end
end
return false
end
SelectLoottable = function(lootTable)
if lootTable ~= nil then
return Config.LootTables[lootTable].Items
else
local randomLootTable = Config.LootTables[math.random(1, #Config.LootTables)]
local chance = math.random(1, 100)
while randomLootTable.Chance < chance do
Citizen.Wait(10)
randomLootTable = Config.LootTables[math.random(1, #Config.LootTables)]
chance = math.random(1, 100)
if Config.Debug then print('searching ' .. randomLootTable.Chance < chance) end
end
return randomLootTable.Items
end
end
RestartCooldown = function()
cooldown = math.random(Config.Airdrops.Cooldown.min, Config.Airdrops.Cooldown.max)
end
RemoveAirdrop = function(id)
for k, v in pairs(airdrops) do
if v.id == id then
airdrops[k] = nil
break
end
end
end
RewardPlayer = function(source, items)
local xPlayer = ESX.GetPlayerFromId(source)
for k, v in pairs(items) do
if v.type == 'item' then
xPlayer.addInventoryItem(v.name, v.count)
elseif v.type == 'weapon' then
xPlayer.addWeapon(v.name, v.count)
elseif v.type == 'account' then
xPlayer.addAccountMoney(v.name, v.count)
end
end
end
GetAirdrop = function(id)
for k, v in pairs(airdrops) do
if v.id == id then
return v
end
end
return nil
end
GetAirdropIndex = function(id)
for k, v in pairs(airdrops) do
if v.id == id then
return k
end
end
return nil
end