-
Notifications
You must be signed in to change notification settings - Fork 4
/
fuel_client.lua
516 lines (423 loc) · 13.8 KB
/
fuel_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
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
local QBCore = exports['qb-core']:GetCoreObject()
local isNearPump = false
local isFueling = false
local currentFuel = 0.0
local currentCost = 0.0
local currentCash = 1000
local fuelSynced = false
local inBlacklisted = false
function ManageFuelUsage(vehicle)
if not DecorExistOn(vehicle, Config.FuelDecor) then
SetFuel(vehicle, math.random(200, 800) / 10)
elseif not fuelSynced then
SetFuel(vehicle, GetFuel(vehicle))
fuelSynced = true
end
if IsVehicleEngineOn(vehicle) then
SetFuel(vehicle, GetVehicleFuelLevel(vehicle) - Config.FuelUsage[Round(GetVehicleCurrentRpm(vehicle), 1)] * (Config.Classes[GetVehicleClass(vehicle)] or 1.0) / 10)
end
end
Citizen.CreateThread(function()
DecorRegister(Config.FuelDecor, 1)
for i = 1, #Config.Blacklist do
if type(Config.Blacklist[i]) == 'string' then
Config.Blacklist[GetHashKey(Config.Blacklist[i])] = true
else
Config.Blacklist[Config.Blacklist[i]] = true
end
end
for i = #Config.Blacklist, 1, -1 do
table.remove(Config.Blacklist, i)
end
while true do
Citizen.Wait(1000)
local ped = PlayerPedId()
if IsPedInAnyVehicle(ped) then
local vehicle = GetVehiclePedIsIn(ped)
if Config.Blacklist[GetEntityModel(vehicle)] then
inBlacklisted = true
else
inBlacklisted = false
end
if not inBlacklisted and GetPedInVehicleSeat(vehicle, -1) == ped then
ManageFuelUsage(vehicle)
end
else
if fuelSynced then
fuelSynced = false
end
if inBlacklisted then
inBlacklisted = false
end
end
end
end)
function FindNearestFuelPump()
local coords = GetEntityCoords(PlayerPedId())
local fuelPumps = {}
local handle, object = FindFirstObject()
local success
repeat
if Config.PumpModels[GetEntityModel(object)] then
table.insert(fuelPumps, object)
end
success, object = FindNextObject(handle, object)
until not success
EndFindObject(handle)
local pumpObject = 0
local pumpDistance = 1000
for k,v in pairs(fuelPumps) do
local dstcheck = GetDistanceBetweenCoords(coords, GetEntityCoords(v))
if dstcheck < pumpDistance then
pumpDistance = dstcheck
pumpObject = v
end
end
return pumpObject, pumpDistance
end
Citizen.CreateThread(function()
while true do
Citizen.Wait(250)
local pumpObject, pumpDistance = FindNearestFuelPump()
if pumpDistance < 2.5 then
isNearPump = pumpObject
if Config.UseESX then
currentCash = ESX.GetPlayerData().money
end
else
isNearPump = false
Citizen.Wait(math.ceil(pumpDistance * 20))
end
end
end)
function DrawText3Ds(x, y, z, text)
local onScreen,_x,_y=World3dToScreen2d(x,y,z)
local px,py,pz=table.unpack(GetGameplayCamCoords())
SetTextScale(0.35, 0.35)
SetTextFont(4)
SetTextProportional(1)
SetTextColour(255, 255, 255, 215)
SetTextEntry("STRING")
SetTextCentre(1)
AddTextComponentString(text)
DrawText(_x,_y)
end
function LoadAnimDict(dict)
if not HasAnimDictLoaded(dict) then
RequestAnimDict(dict)
while not HasAnimDictLoaded(dict) do
Citizen.Wait(1)
end
end
end
AddEventHandler('fuel:startFuelUpTick', function(pumpObject, ped, vehicle)
currentFuel = GetVehicleFuelLevel(vehicle)
nozle = CreateObject(GetHashKey("prop_cs_fuel_nozle"), 0, 0, 0, true, true, true) -- creates object
AttachEntityToEntity(nozle, ped, GetPedBoneIndex(ped, 0x49D9), 0.1, 0.02, 0.02, 90.0, 40.0, 170.0, true, true, false, true, 1, true) -- object is attached to left hand
TriggerServerEvent("InteractSound_SV:PlayOnSource", "pickupnozzle", 0.4)----------------pick up sound
Wait(250)
while isFueling do
Wait(500)
TriggerServerEvent("InteractSound_SV:PlayOnSource", "refuel", 0.3) ----------------------filling sound
Wait(250)
local oldFuel = DecorGetFloat(vehicle, Config.FuelDecor)
local fuelToAdd = math.random(10, 20) / 10.0
local extraCost = fuelToAdd / 1.5
if not pumpObject then
if GetAmmoInPedWeapon(ped, 883325847) - fuelToAdd * 100 >= 0 then
currentFuel = oldFuel + fuelToAdd
SetPedAmmo(ped, 883325847, math.floor(GetAmmoInPedWeapon(ped, 883325847) - fuelToAdd * 100))
else
isFueling = false
end
else
currentFuel = oldFuel + fuelToAdd
end
if currentFuel > 100.0 then
currentFuel = 100.0
isFueling = false
end
currentCost = currentCost + extraCost
if currentCash >= currentCost then
SetFuel(vehicle, currentFuel)
else
isFueling = false
end
end
if pumpObject then
TriggerServerEvent('fuel:pay', currentCost)
lib.notify({
title = 'Fuel',
description = 'Fueling is complete!',
type = 'success'
})
DeleteEntity(nozle) -------------------------------------------------------------------------------------------deletes nozle
TriggerServerEvent("InteractSound_SV:PlayOnSource", "putbacknozzle", 0.4)-----------------------------------------putback sound
Wait(250)
end
currentCost = 0.0
end)
function Round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
AddEventHandler('fuel:refuelFromPump', function(pumpObject, ped, vehicle)
TaskTurnPedToFaceEntity(ped, vehicle, 1000)
Citizen.Wait(1000)
SetCurrentPedWeapon(ped, -1569615261, true)
LoadAnimDict("timetable@gardener@filling_can")
TaskPlayAnim(ped, "timetable@gardener@filling_can", "gar_ig_5_filling_can", 2.0, 8.0, -1, 50, 0, 0, 0, 0)
TriggerEvent('fuel:startFuelUpTick', pumpObject, ped, vehicle)
while isFueling do
Citizen.Wait(1)
for k,v in pairs(Config.DisableKeys) do
DisableControlAction(0, v)
end
local vehicleCoords = GetEntityCoords(vehicle)
if pumpObject then
local stringCoords = GetEntityCoords(pumpObject)
local extraString = ""
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.CancelFuelingPump .. extraString)
lib.showTextUI('[E] - Cancel Fueling', {
style = {
backgroundColor = 'red',
color = 'white'
}
})
DrawText3Ds(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z + 0.5, Round(currentFuel, 1) .. "%")
else
DrawText3Ds(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z + 0.5, Config.Strings.CancelFuelingJerryCan .. "\nGas can: ~g~" .. Round(GetAmmoInPedWeapon(ped, 883325847) / 4500 * 100, 1) .. "% | Vehicle: " .. Round(currentFuel, 1) .. "%")
end
if not IsEntityPlayingAnim(ped, "timetable@gardener@filling_can", "gar_ig_5_filling_can", 3) then
TaskPlayAnim(ped, "timetable@gardener@filling_can", "gar_ig_5_filling_can", 2.0, 8.0, -1, 50, 0, 0, 0, 0)
end
if IsControlJustReleased(0, 38) or DoesEntityExist(GetPedInVehicleSeat(vehicle, -1)) or (isNearPump and GetEntityHealth(pumpObject) <= 0) then
isFueling = false
end
end
ClearPedTasks(ped)
RemoveAnimDict("timetable@gardener@filling_can")
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(1)
local ped = PlayerPedId()
if not isFueling and ((isNearPump and GetEntityHealth(isNearPump) > 0) or (GetSelectedPedWeapon(ped) == 883325847 and not isNearPump)) then
if IsPedInAnyVehicle(ped) and GetPedInVehicleSeat(GetVehiclePedIsIn(ped), -1) == ped then
local pumpCoords = GetEntityCoords(isNearPump)
--DrawText3Ds(pumpCoords.x, pumpCoords.y, pumpCoords.z + 1.2, Config.Strings.ExitVehicle)
lib.showTextUI('Exit the vehicle to refuel', {
style = {
backgroundColor = 'green',
color = 'white'
}
})
else
local vehicle = GetPlayersLastVehicle()
local vehicleCoords = GetEntityCoords(vehicle)
if DoesEntityExist(vehicle) and GetDistanceBetweenCoords(GetEntityCoords(ped), vehicleCoords) < 2.5 then
if not DoesEntityExist(GetPedInVehicleSeat(vehicle, -1)) then
local stringCoords = GetEntityCoords(isNearPump)
local canFuel = true
if GetSelectedPedWeapon(ped) == 883325847 then
stringCoords = vehicleCoords
if GetAmmoInPedWeapon(ped, 883325847) < 100 then
canFuel = false
end
end
if GetVehicleFuelLevel(vehicle) < 95 and canFuel then
if currentCash > 0 then
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.EToRefuel)
lib.showTextUI('[E] - Refuel Vehicle', {
style = {
backgroundColor = 'green',
color = 'white'
}
})
if IsControlJustReleased(0, 38) then
isFueling = true
TriggerEvent('fuel:refuelFromPump', isNearPump, ped, vehicle)
LoadAnimDict("timetable@gardener@filling_can")
end
else
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.NotEnoughCash)
lib.hideTextUI()
end
elseif not canFuel then
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.JerryCanEmpty)
lib.showTextUI('[E] - Jerry Can Empty', {
style = {
backgroundColor = 'red',
color = 'white'
}
})
else
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.FullTank)
lib.showTextUI('Fuel Tank Full', {
style = {
backgroundColor = 'red',
color = 'white'
}
})
end
end
elseif isNearPump then
local stringCoords = GetEntityCoords(isNearPump)
if currentCash >= Config.JerryCanCost then
if not HasPedGotWeapon(ped, 883325847, false) then
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.PurchaseJerryCan)
lib.showTextUI('[E] - Buy Jerry Can', {
style = {
backgroundColor = 'green',
color = 'white'
}
})
if IsControlJustReleased(0, 38) then
TriggerServerEvent('fuel:addPetrolCan')
TriggerEvent("inventory:client:ItemBox", QBCore.Shared.Items["weapon_petrolcan"], "add")
TriggerServerEvent('fuel:pay', Config.JerryCanCost)
end
else
local refillCost = Round(Config.RefillCost * (1 - GetAmmoInPedWeapon(ped, 883325847) / 4500))
if refillCost > 0 then
if currentCash >= refillCost then
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.RefillJerryCan .. refillCost)
lib.showTextUI('[E] - Refill Jerry Can', {
style = {
backgroundColor = 'green',
color = 'white'
}
})
if IsControlJustReleased(0, 38) then
TriggerServerEvent('fuel:pay', refillCost)
SetPedAmmo(ped, 883325847, 4500)
end
else
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.NotEnoughCashJerryCan)
lib.hideTextUI()
end
else
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.JerryCanFull)
lib.showTextUI('Jerry Can Full', {
style = {
backgroundColor = 'red',
color = 'white'
}
})
end
end
else
--DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.NotEnoughCash)
lib.hideTextUI()
end
else
Citizen.Wait(250)
end
end
else
lib.hideTextUI()
Citizen.Wait(250)
end
end
end)
function CreateBlip(coords)
local blip = AddBlipForCoord(coords)
SetBlipSprite(blip, 361)
SetBlipScale(blip, 0.9)
SetBlipColour(blip, 4)
SetBlipDisplay(blip, 4)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("Gas Station")
EndTextCommandSetBlipName(blip)
return blip
end
if Config.ShowNearestGasStationOnly then
Citizen.CreateThread(function()
local currentGasBlip = 0
while true do
Citizen.Wait(10000)
local coords = GetEntityCoords(PlayerPedId())
local closest = 1000
local closestCoords
for k,v in pairs(Config.GasStations) do
local dstcheck = GetDistanceBetweenCoords(coords, v)
if dstcheck < closest then
closest = dstcheck
closestCoords = v
end
end
if DoesBlipExist(currentGasBlip) then
RemoveBlip(currentGasBlip)
end
currentGasBlip = CreateBlip(closestCoords)
end
end)
elseif Config.ShowAllGasStations then
Citizen.CreateThread(function()
for k,v in pairs(Config.GasStations) do
CreateBlip(v)
end
end)
end
function GetFuel(vehicle)
return DecorGetFloat(vehicle, Config.FuelDecor)
end
function SetFuel(vehicle, fuel)
if type(fuel) == 'number' and fuel >= 0 and fuel <= 100 then
SetVehicleFuelLevel(vehicle, fuel + 0.0)
DecorSetFloat(vehicle, Config.FuelDecor, GetVehicleFuelLevel(vehicle))
end
end
if Config.EnableHUD then
local function DrawAdvancedText(x,y ,w,h,sc, text, r,g,b,a,font,jus)
SetTextFont(font)
SetTextProportional(0)
SetTextScale(sc, sc)
N_0x4e096588b13ffeca(jus)
SetTextColour(r, g, b, a)
SetTextDropShadow(0, 0, 0, 0,255)
SetTextEdge(1, 0, 0, 0, 255)
SetTextDropShadow()
SetTextOutline()
SetTextEntry("STRING")
AddTextComponentString(text)
DrawText(x - 0.1+w, y - 0.02+h)
end
local mph = 0
local kmh = 0
local fuel = 0
local displayHud = false
local x = 0.01135
local y = 0.002
Citizen.CreateThread(function()
while true do
Citizen.Wait(50)
local ped = PlayerPedId()
if IsPedInAnyVehicle(ped) and not (Config.RemoveHUDForBlacklistedVehicle and inBlacklisted) then
local vehicle = GetVehiclePedIsIn(ped)
local speed = GetEntitySpeed(vehicle)
mph = tostring(math.ceil(speed * 2.236936))
kmh = tostring(math.ceil(speed * 3.6))
fuel = tostring(math.ceil(GetVehicleFuelLevel(vehicle)))
displayHud = true
else
displayHud = false
Citizen.Wait(500)
end
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(1)
if displayHud then
DrawAdvancedText(0.130 - x, 0.77 - y, 0.005, 0.0028, 0.6, mph, 255, 255, 255, 255, 6, 1)
DrawAdvancedText(0.174 - x, 0.77 - y, 0.005, 0.0028, 0.6, kmh, 255, 255, 255, 255, 6, 1)
DrawAdvancedText(0.2195 - x, 0.77 - y, 0.005, 0.0028, 0.6, fuel, 255, 255, 255, 255, 6, 1)
DrawAdvancedText(0.148 - x, 0.7765 - y, 0.005, 0.0028, 0.4, "mp/h km/h Fuel", 255, 255, 255, 255, 6, 1)
else
Citizen.Wait(750)
end
end
end)
end