forked from overextended/ox_fuel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
408 lines (346 loc) · 10.6 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)
if currentWeapon and currentWeapon.name == 'WEAPON_PETROLCAN' then
fuelingCan = currentWeapon
else
fuelingCan = nil
end
end)
local function isVehicleCloseEnough(playerCoords, vehicle)
return #(GetEntityCoords(vehicle) - playerCoords) <= 3
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
local entityType
if entityHit then entityType = GetEntityType(entityHit) end
if entityHit and entityType ~= 0 then
return entityHit, entityType
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
lib.onCache('seat', function(seat)
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
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 inStation = false
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
inStation = true
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 vehicle = GetPlayersLastVehicle()
if not isVehicleCloseEnough(playerCoords, vehicle) and Config.petrolCan.enabled then
DisplayHelpTextThisFrame('petrolcanHelpText', false)
else
DisplayHelpTextThisFrame('fuelHelpText', false)
end
end
pumpDistance = #(GetEntityCoords(cache.ped) - pump)
Wait(0)
end
nearestPump = nil
end
end
Wait(100)
until pumpDistance > 15
break
end
Wait(100)
inStation = false
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()
TriggerEvent('ox_inventory:disarm')
TriggerServerEvent('ox_fuel:UpdateCanDurability', fuelingCan, 0)
end
end
fuel += Config.refillValue
if fuel >= 100 then
isFueling = false
fuel = 100.0
end
Wait(Config.refillTick)
end
setFuel(Vehicle, vehicle, fuel, true)
if isPump then
TriggerServerEvent('ox_fuel:pay', price, fuel)
else
TriggerServerEvent('ox_fuel:UpdateCanDurability', fuelingCan, fuelingCan.metadata.ammo - durability)
end
end
local function GetPetrolCan(pumpCoord)
LocalPlayer.state.invBusy = true
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
local petrolCan = ox_inventory:Search('count', 'WEAPON_PETROLCAN')
if petrolCan > 0 then
TriggerServerEvent('ox_fuel:fuelCan', true, Config.petrolCan.refillPrice)
else
TriggerServerEvent('ox_fuel:fuelCan', false, Config.petrolCan.price)
end
end
LocalPlayer.state.invBusy = false
end
if not Config.qtarget then
local bones = {'wheel_rr', 'wheel_lr'}
RegisterCommand('startfueling', function()
if lib.progressActive() then return end
local vehicle = GetPlayersLastVehicle()
local petrolCan = GetSelectedPedWeapon(cache.ped) == `WEAPON_PETROLCAN`
local playerCoords = GetEntityCoords(cache.ped)
local moneyAmount = ox_inventory:Search(2, 'money')
if not petrolCan then
if not inStation or isFueling or cache.vehicle then return end
if not isVehicleCloseEnough(playerCoords, vehicle) and Config.petrolCan.enabled then
if moneyAmount >= Config.petrolCan.price then
GetPetrolCan(nearestPump)
else
lib.notify({type = 'error', description = locale('petrolcan_cannot_afford')})
end
elseif isVehicleCloseEnough(playerCoords, vehicle) then
if moneyAmount >= Config.priceTick then
startFueling(vehicle, true)
else
lib.notify({type = 'error', description = locale('refuel_cannot_afford')})
end
else
return lib.notify({type = 'error', description = locale('vehicle_far')})
end
else
if not Config.petrolCan.enabled or isFueling or cache.vehicle then return end
if nearestPump then return lib.notify({type = 'error', description = locale('pump_fuel_with_can')}) end
local entityHit, _ = Raycast()
if not entityHit then return end
for i = 1, #bones do
local fuelcap = GetEntityBoneIndexByName(entityHit, bones[i])
local fuelcapPosition = GetWorldPositionOfEntityBone(entityHit, fuelcap)
local distance = #(GetEntityCoords(cache.ped) - fuelcapPosition)
local closeToVehicle = distance < 1.3 and true or false
if closeToVehicle then
return startFueling(entityHit, false)
else
if i == #bones then return lib.notify({type = 'error', description = locale('vehicle_far')}) end
end
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(GetPlayersLastVehicle(), 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 isVehicleCloseEnough(GetEntityCoords(cache.ped), GetPlayersLastVehicle())
end
},
{
action = function (entity)
if ox_inventory:Search(2, 'money') >= Config.petrolCan.price then
GetPetrolCan(GetEntityCoords(entity))
else
lib.notify({type = 'error', description = locale('petrolcan_cannot_afford')})
end
end,
icon = "fas fa-faucet",
label = locale('petrolcan_buy_or_refill'),
},
},
distance = 2
})
exports.qtarget:Vehicle({
options = {
{
action = function (entity)
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 GetSelectedPedWeapon(cache.ped) == `WEAPON_PETROLCAN` and Config.petrolCan.enabled
end
}
},
distance = 2
})
end
AddTextEntry('fuelHelpText', locale('fuel_help'))
AddTextEntry('petrolcanHelpText', locale('petrolcan_help'))
AddTextEntry('fuelLeaveVehicleText', locale('leave_vehicle'))