forked from overextended/ox_fuel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
633 lines (535 loc) · 16.7 KB
/
client.lua
File metadata and controls
633 lines (535 loc) · 16.7 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
if not lib.checkDependency('ox_lib', '3.0.0', true) then return end
lib.locale()
local playerCash = 0
local getMyFramework = function()
if GetResourceState('es_extended') == 'started' then
RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(playerData)
playerCash = playerData.money
end)
RegisterNetEvent('esx:setAccountMoney')
AddEventHandler('esx:setAccountMoney', function(account)
playerCash = account.money
end)
return 'esx', exports['es_extended']:getSharedObject()
elseif GetResourceState('qb-core') == 'started' then
return 'qb-core', exports['qb-core']:GetCoreObject()
end
end
local CoreName, Core = getMyFramework()
local currentVehicle = nil
local currentStore = nil
local storeFuel = 0
local paidForFuel = false
local fuelCount = 0
AddEventHandler('vms_stores:enterStoreZone', function(storeId)
paidForFuel = false
currentStore = storeId
storeFuel = 99999999999
end)
RegisterNetEvent('vms_stores:fuelStoreUpdated', function(storeId, fuel)
if currentStore == storeId and fuel then
storeFuel = fuel
end
end)
RegisterNetEvent('vms_stores:fuelStorePaid', function(storeId)
local Vehicle = Entity(currentVehicle).state
local fuel = Vehicle.fuel + fuelCount
TriggerServerEvent('ox_fuel:pay', fuel, NetworkGetNetworkIdFromEntity(currentVehicle))
paidForFuel = true
fuelCount = 0
currentVehicle = nil
end)
AddEventHandler('vms_stores:exitStoreZone', function()
if Config.AbilityStealFuel and fuelCount > 0 then
TriggerServerEvent('fuel:fuelHasBeenStealed', currentStore, GetVehicleNumberPlateText(currentVehicle), fuelCount)
end
paidForFuel = false
currentStore = nil
fuelCount = 0
storeFuel = 0
currentVehicle = nil
end)
local fuelingCan = nil
local isUsingOXInventory = false
Citizen.CreateThread(function()
Citizen.Wait(5000)
if GetResourceState('ox_inventory') == 'started' then
isUsingOXInventory = true
fuelingCan = exports.ox_inventory:getCurrentWeapon()
AddEventHandler('ox_inventory:currentWeapon', function(currentWeapon)
fuelingCan = currentWeapon?.name == 'WEAPON_PETROLCAN' and currentWeapon
end)
end
end)
local isFueling = false
local nearestPump
local nozzle, rope = nil, nil
local function nozzleObj(spawn)
if spawn then
local myPed = PlayerPedId()
local myCoords = GetEntityCoords(myPed)
local nearestPumpObj = GetClosestObjectOfType(myCoords.x, myCoords.y, myCoords.z, 3.0, 1933174915, false, false, false)
local nearestPumpCoords = GetEntityCoords(nearestPumpObj)
if not nearestPumpObj then
return
end
RopeLoadTextures()
rope = AddRope(nearestPumpCoords.x, nearestPumpCoords.y, nearestPumpCoords.z, 0.0, 0.0, 0.0, 1.0, 2, 1.0, 1.0, 0.0, false, false, false, 5.0, false, 1)
nozzle = CreateObject(joaat('prop_cs_fuel_nozle'), nearestPumpCoords.x, nearestPumpCoords.y, nearestPumpCoords.z, false, false, false)
AttachEntityToEntity(nozzle, myPed, GetPedBoneIndex(myPed, 18905), 0.09, 0.04, -0.01, 40.0, -90.0, -170.0, 0, false, false, false, false, true)
StartRopeWinding(rope)
RopeConvertToSimple(rope)
RopeDrawShadowEnabled(rope, true)
local myFuelPosition = GetOffsetFromEntityInWorldCoords(nozzle, 0.0, -0.0, -0.15)
AttachEntitiesToRope(rope, nearestPumpObj, nozzle, nearestPumpCoords.x, nearestPumpCoords.y, nearestPumpCoords.z, myFuelPosition.x, myFuelPosition.y, myFuelPosition.z, 3.0, false, false, nil, nil)
else
DetachRopeFromEntity(rope, nozzle)
DetachEntity(nozzle, true, false)
DeleteEntity(nozzle)
DeleteRope(rope)
nozzle = nil
rope = nil
end
end
local function getVehicleInFront()
local coords = GetEntityCoords(cache.ped)
local destination = GetOffsetFromEntityInWorldCoords(cache.ped, 0.0, 2.2, -0.25)
local handle = StartShapeTestCapsule(coords.x, coords.y, coords.z, destination.x, destination.y, destination.z, 2.2, 2, cache.ped, 4)
while true do
Wait(0)
local retval, _, _, _, entityHit = GetShapeTestResult(handle)
if retval ~= 1 then
return entityHit ~= 0 and entityHit
end
end
end
local function setFuel(state, vehicle, fuel, replicate)
if DoesEntityExist(vehicle) then
if fuel < 0 then fuel = 0 end
SetVehicleFuelLevel(vehicle, fuel)
if not state.fuel then
TriggerServerEvent('ox_fuel:createStatebag', NetworkGetNetworkIdFromEntity(vehicle), fuel)
else
state:set('fuel', fuel, replicate)
end
end
end
local lastVehicle = cache.vehicle or GetPlayersLastVehicle()
lib.onCache('seat', function(seat)
if cache.vehicle then
lastVehicle = cache.vehicle
end
if not NetworkGetEntityIsNetworked(lastVehicle) then return end
if seat == -1 then
SetTimeout(0, function()
local vehicle = cache.vehicle
local multiplier = Config.classUsage[GetVehicleClass(vehicle)] or 1.0
if multiplier == 0.0 then return end
local state = Entity(vehicle).state
if not state.fuel then
TriggerServerEvent('ox_fuel:createStatebag', NetworkGetNetworkIdFromEntity(vehicle), GetVehicleFuelLevel(vehicle))
while not state.fuel do Wait(0) end
end
SetVehicleFuelLevel(vehicle, state.fuel)
local fuelTick = 0
while cache.seat == -1 do
local fuel = state.fuel
local newFuel = fuel
if fuel > 0 then
if GetIsVehicleEngineRunning(vehicle) then
local usage = Config.rpmUsage[math.floor(GetVehicleCurrentRpm(vehicle) * 10) / 10]
newFuel -= usage * multiplier
end
if GetVehiclePetrolTankHealth(vehicle) < 700 then
newFuel -= math.random(10, 20) * 0.01
end
if fuel ~= newFuel then
if fuelTick == 15 then
fuelTick = 0
end
setFuel(state, vehicle, newFuel, fuelTick == 0)
fuelTick += 1
end
end
Wait(1000)
end
setFuel(state, vehicle, state.fuel, true)
end)
end
end)
AddTextEntry('ox_fuel_station', locale('fuel_station_blip'))
local function createBlip(station)
local blip = AddBlipForCoord(station.x, station.y, station.z)
SetBlipSprite(blip, 361)
SetBlipDisplay(blip, 4)
SetBlipScale(blip, 0.8)
SetBlipColour(blip, 6)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('ox_fuel_station')
EndTextCommandSetBlipName(blip)
return blip
end
CreateThread(function()
local blip
if Config.ox_target and Config.showBlips ~= 1 then return end
while true do
local playerCoords = GetEntityCoords(cache.ped)
for station, pumps in pairs(stations) do
local stationDistance = #(playerCoords - station)
if stationDistance < 60 then
if Config.showBlips == 1 and not blip then
blip = createBlip(station)
end
if not Config.ox_target then
repeat
if stationDistance < 15 then
local pumpDistance
repeat
playerCoords = GetEntityCoords(cache.ped)
for i = 1, #pumps do
local pump = pumps[i]
pumpDistance = #(playerCoords - pump)
if pumpDistance < 3 then
nearestPump = pump
while pumpDistance < 3 do
if cache.vehicle then
DisplayHelpTextThisFrame('fuelLeaveVehicleText', false)
elseif not isFueling then
local vehicleInRange = lastVehicle ~= 0 and #(GetEntityCoords(lastVehicle) - playerCoords) <= 3
if vehicleInRange then
DisplayHelpTextThisFrame('fuelHelpText', false)
elseif Config.petrolCan.enabled then
DisplayHelpTextThisFrame('petrolcanHelpText', false)
end
end
pumpDistance = #(GetEntityCoords(cache.ped) - pump)
Wait(0)
end
nearestPump = nil
end
end
Wait(100)
until pumpDistance > 15
break
end
Wait(100)
stationDistance = #(GetEntityCoords(cache.ped) - station)
until stationDistance > 60
end
end
end
Wait(500)
if blip then
RemoveBlip(blip)
blip = nil
end
end
end)
if Config.showBlips == 2 then
for station in pairs(stations) do createBlip(station) end
end
-- ---@return number
local function defaultMoneyCheck(isStore)
if isStore then
return 99999999999
end
if CoreName == 'esx' then
return playerCash
elseif CoreName == 'qb-core' then
return Core.Functions.GetPlayerData().money.cash
end
return 0
end
local getMoneyAmount = defaultMoneyCheck
-- fuelingMode = 1 - Pump
-- fuelingMode = 2 - Can
local function startFueling(vehicle, isPump)
if not Config.AbilityStealFuel and currentVehicle ~= nil and currentVehicle ~= vehicle then
lib.notify({type = 'error', description = locale('cant_refuel_multiple_cars')})
return
end
local Vehicle = Entity(vehicle).state
local fuel = Vehicle.fuel or GetVehicleFuelLevel(vehicle)
local duration = math.ceil((100 - fuel) / Config.refillValue) * Config.refillTick
local price, moneyAmount
local durability = 0
if 100 - fuel < Config.refillValue then
return lib.notify({type = 'error', description = locale('tank_full')})
end
if isPump then
price = 0
moneyAmount = getMoneyAmount(currentStore ~= nil)
print('money',moneyAmount)
if currentStore and storeFuel <= 0 then
return lib.notify({
type = 'error',
description = locale('not_enough_fuel')
})
end
if currentStore then
currentVehicle = vehicle
end
if Config.priceTick > moneyAmount then
return lib.notify({
type = 'error',
description = locale('not_enough_money', Config.priceTick)
})
end
elseif not fuelingCan then
return lib.notify({type = 'error', description = locale('petrolcan_not_equipped')})
elseif fuelingCan.metadata.ammo <= Config.durabilityTick then
return lib.notify({
type = 'error',
description = locale('petrolcan_not_enough_fuel')
})
end
isFueling = true
if isPump then
nozzleObj(true)
end
TaskTurnPedToFaceEntity(cache.ped, vehicle, duration)
Wait(500)
CreateThread(function()
lib.progressCircle({
duration = duration,
useWhileDead = false,
canCancel = true,
disable = {
move = true,
car = true,
combat = true,
},
anim = {
dict = isPump and 'timetable@gardener@filling_can' or 'weapon@w_sp_jerrycan',
clip = isPump and 'gar_ig_5_filling_can' or 'fire',
},
})
nozzleObj(false)
isFueling = false
end)
while isFueling do
if isPump then
if not currentStore then
price += Config.priceTick
if price + Config.priceTick >= moneyAmount then
if lib.progressActive() then
lib.cancelProgress()
end
end
else
local ranOutOfFuel = exports['vms_stores']:addFuelToCart(Config.refillValue)
if ranOutOfFuel then
if lib.progressActive() then
lib.cancelProgress()
end
break
end
fuelCount += Config.refillValue
end
else
durability += Config.durabilityTick
if durability >= fuelingCan.metadata.ammo then
if lib.progressActive() then
lib.cancelProgress()
end
durability = fuelingCan.metadata.ammo
break
end
end
fuel += Config.refillValue
if fuel >= 100 then
isFueling = false
fuel = 100.0
end
Wait(Config.refillTick)
end
ClearPedTasks(cache.ped)
if isPump then
if not currentStore then
TriggerServerEvent('ox_fuel:pay', fuel, NetworkGetNetworkIdFromEntity(vehicle), price)
else
if Config.AbilityStealFuel then
TriggerServerEvent('ox_fuel:pay', fuel, NetworkGetNetworkIdFromEntity(vehicle))
end
end
else
TriggerServerEvent('ox_fuel:updateFuelCan', durability, NetworkGetNetworkIdFromEntity(vehicle), fuel)
end
end
local function getPetrolCan(pumpCoord, refuel)
if not Config.petrolCan.enabled then
return
end
TaskTurnPedToFaceCoord(cache.ped, pumpCoord.x, pumpCoord.y, pumpCoord.z, Config.petrolCan.duration)
Wait(500)
if lib.progressCircle({
duration = Config.petrolCan.duration,
useWhileDead = false,
canCancel = true,
disable = {
move = true,
car = true,
combat = true,
},
anim = {
dict = 'timetable@gardener@filling_can',
clip = 'gar_ig_5_filling_can',
flags = 49,
}
}) then
if refuel and exports.ox_inventory:Search('count', 'WEAPON_PETROLCAN') then
return TriggerServerEvent('ox_fuel:fuelCan', true, Config.petrolCan.refillPrice)
end
TriggerServerEvent('ox_fuel:fuelCan', false, Config.petrolCan.price)
end
ClearPedTasks(cache.ped)
end
local bones = {
'petrolcap',
'petroltank',
'petroltank_l',
'hub_lr',
'engine',
}
local function getVehiclePetrolCapBoneIndex(vehicle)
for i = 1, #bones do
local boneIndex = GetEntityBoneIndexByName(vehicle, bones[i])
if boneIndex ~= -1 then
return boneIndex
end
end
end
if not Config.ox_target then
RegisterCommand('startfueling', function()
if isFueling or cache.vehicle or lib.progressActive() then return end
local petrolCan = Config.petrolCan.enabled and GetSelectedPedWeapon(cache.ped) == `WEAPON_PETROLCAN`
local playerCoords = GetEntityCoords(cache.ped)
if nearestPump then
local moneyAmount = getMoneyAmount()
if petrolCan and moneyAmount >= Config.petrolCan.refillPrice then
return getPetrolCan(nearestPump, true)
end
local vehicleInRange = lastVehicle and #(GetEntityCoords(lastVehicle) - playerCoords) <= 3
if not vehicleInRange then
if not Config.petrolCan.enabled then return end
if moneyAmount >= Config.petrolCan.price then
return getPetrolCan(nearestPump)
end
return lib.notify({type = 'error', description = locale('petrolcan_cannot_afford')})
else
return startFueling(lastVehicle, true)
end
return lib.notify({type = 'error', description = locale('vehicle_far')})
elseif petrolCan then
local vehicle = getVehicleInFront()
if vehicle then
local hasFuel = Config.classUsage[GetVehicleClass(vehicle)] or true
if hasFuel == 0.0 then return end
local boneIndex = getVehiclePetrolCapBoneIndex(vehicle)
local fuelcapPosition = boneIndex and GetWorldPositionOfEntityBone(vehicle, boneIndex)
if fuelcapPosition and #(playerCoords - fuelcapPosition) < 1.8 then
return startFueling(vehicle, false)
end
return lib.notify({type = 'error', description = locale('vehicle_far')})
end
end
end)
RegisterKeyMapping('startfueling', 'Fuel vehicle', 'keyboard', 'e')
TriggerEvent('chat:removeSuggestion', '/startfueling')
end
if Config.ox_target then
if Config.petrolCan.enabled then
exports.ox_target:addModel(Config.pumpModels, {
{
distance = 2,
onSelect = function()
print(getMoneyAmount())
if getMoneyAmount() >= Config.priceTick then
startFueling(lastVehicle, 1)
else
lib.notify({type = 'error', description = locale('refuel_cannot_afford')})
end
end,
icon = "fas fa-gas-pump",
label = locale('start_fueling'),
canInteract = function(entity)
if isFueling or cache.vehicle or lib.progressActive() then
return false
end
return lastVehicle and #(GetEntityCoords(lastVehicle) - GetEntityCoords(cache.ped)) <= 3
end
},
{
distance = 2,
onSelect = function(data)
local petrolCan = Config.petrolCan.enabled and GetSelectedPedWeapon(cache.ped) == `WEAPON_PETROLCAN`
local moneyAmount = getMoneyAmount()
if moneyAmount < Config.petrolCan.price then
return lib.notify({type = 'error', description = locale('petrolcan_cannot_afford')})
end
return getPetrolCan(data.coords, petrolCan)
end,
canInteract = function()
return isUsingOXInventory
end,
icon = "fas fa-faucet",
label = locale('petrolcan_buy_or_refill'),
},
})
else
exports.ox_target:addModel(Config.pumpModels, {
{
distance = 2,
onSelect = function()
if getMoneyAmount() >= Config.priceTick then
if GetVehicleFuelLevel(lastVehicle) >= 100 then
return lib.notify({type = 'error', description = locale('vehicle_full')})
end
startFueling(lastVehicle, 1)
else
lib.notify({type = 'error', description = locale('refuel_cannot_afford')})
end
end,
icon = "fas fa-gas-pump",
label = locale('start_fueling'),
canInteract = function(entity)
if isFueling or cache.vehicle then
return false
end
return lastVehicle and #(GetEntityCoords(lastVehicle) - GetEntityCoords(cache.ped)) <= 3
end
},
})
end
if Config.petrolCan.enabled then
exports.ox_target:addGlobalVehicle({
{
distance = 2,
onSelect = function(data)
if not fuelingCan then
return lib.notify({type = 'error', description = locale('petrolcan_not_equipped')})
end
if fuelingCan.metadata.ammo <= Config.durabilityTick then
return lib.notify({
type = 'error',
description = locale('petrolcan_not_enough_fuel')
})
end
startFueling(data.entity)
end,
icon = "fas fa-gas-pump",
label = locale('start_fueling'),
canInteract = function(entity)
if isFueling or cache.vehicle or lib.progressActive() then
return false
end
return fuelingCan and Config.petrolCan.enabled
end
}
})
end
end
AddTextEntry('fuelHelpText', locale('fuel_help'))
AddTextEntry('petrolcanHelpText', locale('petrolcan_help'))
AddTextEntry('fuelLeaveVehicleText', locale('leave_vehicle'))