-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.lua
341 lines (295 loc) · 11.9 KB
/
client.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
ESX = nil
Citizen.CreateThread(function()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
Citizen.Wait(10)
end
end)
local spawnedItems = {}
local itemZones = {}
function CreateMapBlips(zones)
for k, v in pairs(zones) do
if (v.blipInfo ~= nil) then
local blip = AddBlipForCoord(v.coords)
SetBlipSprite (blip, v.blipInfo.blipSprite)
SetBlipDisplay(blip, 6)
SetBlipScale (blip, 0.8)
SetBlipColour (blip, v.blipInfo.blipColour)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName(v.blipInfo.label)
EndTextCommandSetBlipName(blip)
end
end
end
--[[--------------------------------------------------
Illegal Circle Zones
]]----------------------------------------------------
RegisterNetEvent('bixbi_gather:SetupTargetsClient')
AddEventHandler('bixbi_gather:SetupTargetsClient', function(zones)
itemZones = zones
CreateMapBlips(zones)
Citizen.CreateThread(function()
for l, z in pairs(itemZones) do
for k, v in ipairs(z.info) do
exports['qtarget']:AddTargetModel({v.target}, {
options = {
{
event = "bixbi_gather:TriggerCollect",
icon = "fas fa-hand-sparkles",
label = "Collect " .. v.label,
location = l,
item = z.tool,
},
},
distance = 1.5
})
end
end
end)
end)
-- RegisterNetEvent('bixbi_gather:TriggerCollect')
local collectionInProgress = false
AddEventHandler('bixbi_gather:TriggerCollect', function(data)
local source = GetPlayerServerId(PlayerId())
local coords = GetEntityCoords(PlayerPedId())
if (not collectionInProgress) then TriggerServerEvent('bixbi_gather:Collect', coords, data.location) end
end)
function CreateProp(waitTime, ConfigItem)
Citizen.CreateThread(function()
local playerPed = PlayerPedId()
local toolInfo = itemZones[ConfigItem].toolInfo
local itemModel = GetHashKey(toolInfo.toolModel)
local x,y,z = table.unpack(GetEntityCoords(playerPed))
local boneIndex = GetPedBoneIndex(playerPed, 28422)
prop = CreateObject(itemModel, x, y, z, true, true, true)
AttachEntityToEntity(prop, playerPed, boneIndex, toolInfo.xPos, toolInfo.yPos, toolInfo.zPos, toolInfo.xRot, toolInfo.yRot, toolInfo.zRot, true, true, false, true, 1, true)
Citizen.Wait(waitTime)
DeleteObject(prop)
end)
end
RegisterNetEvent('bixbi_gather:StartCollect')
AddEventHandler('bixbi_gather:StartCollect', function(pos, ConfigItem)
if (collectionInProgress) then return end
local playerPed = PlayerPedId()
local coords = GetEntityCoords(playerPed)
local nearbyObject
local nearbyID = -1
collectionInProgress = true
for i=1, #spawnedItems[ConfigItem].locations, 1 do
if (#(coords - GetEntityCoords(spawnedItems[ConfigItem].locations[i].object)) < 3.0) then
nearbyObject, nearbyID = spawnedItems[ConfigItem].locations[i], i
end
end
Citizen.Wait(100)
if (nearbyID ~= -1) then
local item = nearbyObject.item
ESX.TriggerServerCallback('bixbi_core:canHoldItem', function(canHold)
if (canHold) then
local animInfo = itemZones[ConfigItem].animInfo
if (itemZones[ConfigItem].tool ~= nil) then
local toolCount = exports['bixbi_core']:itemCount(itemZones[ConfigItem].tool)
if (toolCount > 0) then
if (animInfo.dict ~= nil) then
exports['bixbi_core']:playAnim(playerPed, animInfo.dict, animInfo.anim, -1)
else
TaskStartScenarioInPlace(playerPed, animInfo.anim, 0, false)
end
FreezeEntityPosition(playerPed, true)
if (itemZones[ConfigItem].toolInfo ~= nil) then CreateProp(6000, ConfigItem) end
exports['bixbi_core']:Loading(6000, 'Attempting to collect ' .. nearbyObject.label .. '(s)')
Citizen.Wait(6000)
ClearPedTasks(playerPed)
-- Citizen.Wait(1000)
FreezeEntityPosition(playerPed, false)
ESX.Game.DeleteObject(nearbyObject.object)
table.remove(spawnedItems[ConfigItem].locations, nearbyID)
spawnedItems[ConfigItem].count = spawnedItems[ConfigItem].count - 1
TriggerServerEvent('bixbi_gather:Success', coords, ConfigItem, item)
else
exports['bixbi_core']:Notify('error', 'You need a ' .. itemZones[ConfigItem].tool .. ' to do this.')
end
else
if (animInfo.dict ~= nil) then
exports['bixbi_core']:playAnim(playerPed, animInfo.dict, animInfo.anim, -1)
else
TaskStartScenarioInPlace(playerPed, animInfo.anim, 0, false)
end
if (itemZones[ConfigItem].toolInfo ~= nil) then CreateProp(6000, ConfigItem) end
exports['bixbi_core']:Loading(6000, 'Attempting to collect ' .. nearbyObject.label .. '(s)')
Citizen.Wait(6000)
ClearPedTasks(playerPed)
-- Citizen.Wait(1000)
ESX.Game.DeleteObject(nearbyObject.object)
table.remove(spawnedItems[ConfigItem].locations, nearbyID)
spawnedItems[ConfigItem].count = spawnedItems[ConfigItem].count - 1
TriggerServerEvent('bixbi_gather:Success', coords, ConfigItem, item, nearbyObject.itemQty)
end
else
exports['bixbi_core']:Notify('error', 'You do not have enough room to store this item.')
end
end, item, 1)
else
exports['bixbi_core']:Notify('error', 'You cannot see any resources, perhaps try again? Or maybe you\'re in the wrong area', 10000)
end
collectionInProgress = false
end)
local lastSpawnedZone = ''
function SpawnItems(EntryName)
if (lastSpawnedZone ~= EntryName) then
RemoveObjects()
lastSpawnedZone = EntryName
end
local ConfigItem = itemZones[EntryName]
local itemInfo = spawnedItems[EntryName]
if (itemInfo == nil) then
spawnedItems[EntryName] = {}
spawnedItems[EntryName].count = 0
spawnedItems[EntryName].locations = {}
spawnedItems[EntryName].zone = ConfigItem
itemInfo = spawnedItems[EntryName]
end
while (itemInfo.count < ConfigItem.maxCount) do
Citizen.Wait(1000)
for _, v in pairs(ConfigItem.info) do
if (spawnedItems[EntryName].count < ConfigItem.maxCount) then
math.randomseed(GetGameTimer())
local rarity = math.random(1, v.rarity)
if (rarity == v.rarity) then
Citizen.Wait(500)
local itemCoords = GenerateItemCoords(ConfigItem, itemInfo)
ESX.Game.SpawnLocalObject(v.model, itemCoords, function(obj)
PlaceObjectOnGroundProperly(obj)
FreezeEntityPosition(obj, true)
math.randomseed(GetGameTimer())
table.insert(spawnedItems[EntryName].locations, { object = obj, item = v.item, label = v.label })
spawnedItems[EntryName].count = itemInfo.count + 1
end)
end
else
break
end
end
end
end
function GenerateItemCoords(ConfigItem, itemInfo)
while (ESX.PlayerLoaded) do
Citizen.Wait(1)
math.randomseed(GetGameTimer())
local modX = math.random(-ConfigItem.radius, ConfigItem.radius)
Citizen.Wait(100)
math.randomseed(GetGameTimer())
local modY = math.random(-ConfigItem.radius, ConfigItem.radius)
local itemX = ConfigItem.coords.x + modX
local itemY = ConfigItem.coords.y + modY
local coordZ = GetCoordZ(itemX, itemY) or ConfigItem.coords.z
local coord = vector3(itemX, itemY, coordZ)
if ValidateItemCoords(ConfigItem, itemInfo, coord) then
coord = vector3(coord.x, coord.y, coord.z --[[- 1.2]])
return coord
end
end
end
function ValidateItemCoords(ConfigItem, itemInfo, itemCoord)
if itemInfo.count > 0 then
local validate = true
for k, v in pairs(itemInfo.locations) do
if #(itemCoord - GetEntityCoords(v.object)) < ConfigItem.spacing then
validate = false
end
end
if (#(itemCoord - ConfigItem.coords) > ConfigItem.radius) then validate = true end
return validate
else
return true
end
end
function GetCoordZ(x, y)
for height = 1, 1000 do
local foundGround, zPos = GetGroundZFor_3dCoord(x, y, height + 0.0)
if foundGround then
return zPos
end
-- Citizen.Wait(5)
end
end
function LocationLoop()
TriggerServerEvent('bixbi_gather:SetupTargets')
Citizen.CreateThread(function()
local lastSpawnedLocation = nil
while ESX.PlayerLoaded do
local locationLoopSleep = 1
local closestDistance = 1000
local areaSpawnRadius = 100
local coords = GetEntityCoords(PlayerPedId())
for k, v in pairs(itemZones) do
local distance = #(coords - v.coords)
if (distance < closestDistance) then closestDistance = distance end
if (distance < (v.radius * 1.5)) then
areaSpawnRadius = v.radius
SpawnItems(k)
lastSpawnedLocation = coords
Citizen.Wait(20000)
end
end
if (closestDistance < (areaSpawnRadius * 1.2)) then
locationLoopSleep = 1
elseif (closestDistance > 1800) then
locationLoopSleep = 45
elseif (closestDistance > 500) then
locationLoopSleep = 7
elseif (closestDistance > 200) then
locationLoopSleep = 5
end
if (lastSpawnedLocation ~= nil and #(coords - lastSpawnedLocation) > areaSpawnRadius + 50) then
RemoveObjects()
lastSpawnedLocation = nil
end
Citizen.Wait(locationLoopSleep * 1000)
end
end)
end
--[[--------------------------------------------------
Setup
--]]--------------------------------------------------
AddEventHandler('onResourceStart', function(resourceName)
if (resourceName == GetCurrentResourceName() and Config.Debug) then
while (ESX == nil) do Citizen.Wait(100) end
Citizen.Wait(5000)
ESX.PlayerLoaded = true
LocationLoop()
end
end)
RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
while (ESX == nil) do Citizen.Wait(100) end
ESX.PlayerData = xPlayer
ESX.PlayerLoaded = true
ESX.TriggerServerCallback('bixbi_core:illegalTaskBlacklist', function(result)
if (result and Config.BlacklistCertainJobs) then
return
else
LocationLoop()
end
end)
end)
RegisterNetEvent('esx:onPlayerLogout')
AddEventHandler('esx:onPlayerLogout', function()
ESX.PlayerLoaded = false
ESX.PlayerData = {}
end)
function RemoveObjects()
for _, z in pairs(spawnedItems) do
for k, v in pairs(z.locations) do
ESX.Game.DeleteObject(v.object)
end
end
spawnedItems = nil
spawnedItems = {}
lastSpawnedZone = ''
end
AddEventHandler('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
RemoveObjects()
end
end)