forked from overextended/ox_fuel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
408 lines (331 loc) · 10.3 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
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
lib.locale()
local fuelingCan = nil
AddEventHandler('ox_inventory:currentWeapon', function(currentWeapon)
fuelingCan = currentWeapon?.name == 'WEAPON_PETROLCAN' and currentWeapon
end)
local function raycast(flag)
local playerCoords = GetEntityCoords(cache.ped)
local plyOffset = GetOffsetFromEntityInWorldCoords(cache.ped, 0.0, 2.2, -0.25)
local rayHandle = StartShapeTestCapsule(playerCoords.x, playerCoords.y, playerCoords.z + 0.5, plyOffset.x, plyOffset.y, plyOffset.z, 2.2, flag or 30, cache.ped)
while true do
Wait(0)
local result, _, _, _, entityHit = GetShapeTestResult(rayHandle)
if result ~= 1 then
if entityHit and GetEntityType(entityHit) == 2 then
return entityHit
end
return false
end
end
end
local function setFuel(state, vehicle, fuel, replicate)
if DoesEntityExist(vehicle) then
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
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
-- Vehicle doesn't use fuel
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
if GetIsVehicleEngineRunning(vehicle) then
local usage = Config.rpmUsage[math.floor(GetVehicleCurrentRpm(vehicle) * 10) / 10]
local fuel = state.fuel
local newFuel = fuel - usage * multiplier
if newFuel < 0 or newFuel > 100 then
newFuel = fuel
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)
local isFueling = false
local nearestPump
local function createBlip(station)
local blip = AddBlipForCoord(station.x, station.y, station.z)
SetBlipSprite(blip, 415)
SetBlipDisplay(blip, 4)
SetBlipScale(blip, 0.6)
SetBlipColour(blip, 23)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName(locale('fuel_station_blip'))
EndTextCommandSetBlipName(blip)
return blip
end
CreateThread(function()
local blip
if Config.qtarget 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.qtarget 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
local ox_inventory = exports.ox_inventory
-- fuelingMode = 1 - Pump
-- fuelingMode = 2 - Can
local function startFueling(vehicle, isPump)
isFueling = true
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
isFueling = false
return lib.notify({type = 'error', description = locale('tank_full')})
end
if isPump then
price = 0
moneyAmount = ox_inventory:Search(2, 'money')
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',
},
})
isFueling = false
end)
while isFueling do
if isPump then
price += Config.priceTick
if price >= moneyAmount then
lib.cancelProgress()
end
else
durability += Config.durabilityTick
if durability >= fuelingCan.metadata.ammo then
lib.cancelProgress()
durability = fuelingCan.metadata.ammo
break
end
end
fuel += Config.refillValue
if fuel >= 100 then
isFueling = false
fuel = 100.0
end
Wait(Config.refillTick)
end
if isPump then
TriggerServerEvent('ox_fuel:pay', price, fuel, NetworkGetNetworkIdFromEntity(vehicle))
else
TriggerServerEvent('ox_fuel:updateFuelCan', durability, NetworkGetNetworkIdFromEntity(vehicle), fuel)
end
end
local function getPetrolCan(pumpCoord, refuel)
TaskTurnPedToFaceCoord(cache.ped, pumpCoord, 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 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
end
if not Config.qtarget then
local bones = {'wheel_rr', 'wheel_lr'}
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 = ox_inventory:Search(2, 'money')
if petrolCan and moneyAmount >= Config.petrolCan.price 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')})
elseif moneyAmount >= Config.priceTick then
return startFueling(lastVehicle, true)
else
return lib.notify({type = 'error', description = locale('refuel_cannot_afford')})
end
return lib.notify({type = 'error', description = locale('vehicle_far')})
elseif petrolCan then
local vehicle = raycast()
if vehicle then
for i = 1, #bones do
local fuelcapPosition = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, bones[i]))
if #(playerCoords - fuelcapPosition) < 1.3 then
return startFueling(vehicle, false)
end
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.qtarget then
exports.qtarget:AddTargetModel(Config.pumpModels, {
options = {
{
action = function (entity)
if ox_inventory:Search(2, 'money') >= 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 then
return false
end
return lastVehicle and #(GetEntityCoords(lastVehicle) - GetEntityCoords(cache.ped)) <= 3
end
},
{
action = function (entity)
local petrolCan = Config.petrolCan.enabled and GetSelectedPedWeapon(cache.ped) == `WEAPON_PETROLCAN`
local moneyAmount = ox_inventory:Search(2, 'money')
if moneyAmount < Config.petrolCan.price then
return lib.notify({type = 'error', description = locale('petrolcan_cannot_afford')})
end
return getPetrolCan(GetEntityCoords(entity), petrolCan)
end,
icon = "fas fa-faucet",
label = locale('petrolcan_buy_or_refill'),
},
},
distance = 2
})
exports.qtarget:Vehicle({
options = {
{
action = function (entity)
if not fuelingCan then return lib.notify({type = 'error', description = locale('petrolcan_missing')}) end
if fuelingCan.metadata.ammo <= Config.durabilityTick then return end
startFueling(entity)
end,
icon = "fas fa-gas-pump",
label = locale('start_fueling'),
canInteract = function (entity)
if isFueling or cache.vehicle then
return false
end
return fuelingCan and Config.petrolCan.enabled
end
}
},
distance = 2
})
end
AddTextEntry('fuelHelpText', locale('fuel_help'))
AddTextEntry('petrolcanHelpText', locale('petrolcan_help'))
AddTextEntry('fuelLeaveVehicleText', locale('leave_vehicle'))