Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### About
Started off as my first script, and for whatever reason, I decided to release it. As it was pretty badly created, I felt like I should rewrite it and make a better version, so ended up spending a few hours doing so.
Updated this useful fuel script for ESX. (Updated FXManifest and removed-replaced deprecated code)

### Installation
1) Download the latest version in the "code" tab on GitHub.
Expand All @@ -11,8 +11,8 @@ Started off as my first script, and for whatever reason, I decided to release it
There are currently two (client-sided) exports available, which should help you control the fuel level for vehicles whenever needed.

```
SetFuel(vehicle --[[ Vehicle ]], value --[[ Number: (0-100) ]])
GetFuel(vehicle --[[ Vehicle ]]) -- Returns the vehicle's fuel level.
SetFuel(--[[Vehicle--]] vehicle, --[[Float/Int: (0-100)--]] value)
GetFuel(--[[Vehicle--]] vehicle) -- Returns the vehicle's fuel level.
```

**Example usage:**
Expand All @@ -27,6 +27,6 @@ function StoreVehicleInGarage(vehicle)
local plate = GetVehicleNumberPlateText(vehicle)
local fuelLevel = exports["LegacyFuel"]:GetFuel(vehicle)

TriggerServerEvent('vehiclesStored', plate, fuelLevel)
TriggerServerEvent('vehiclesStored', plate, fuel)
end
```
6 changes: 1 addition & 5 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ Config.EnableHUD = true
Config.ShowNearestGasStationOnly = true
Config.ShowAllGasStations = false

-- Modify the fuel-cost here, using a multiplier value. Setting the value to 2.0 would cause a doubled increase.
Config.CostMultiplier = 1.0

-- Configure the strings as you wish here.
Config.Strings = {
ExitVehicle = "Exit the vehicle to refuel",
Expand All @@ -35,8 +32,7 @@ Config.Strings = {
NotEnoughCash = "Not enough cash",
RefillJerryCan = "Press ~g~E ~w~ to refill the jerry can for ",
NotEnoughCashJerryCan = "Not enough cash to refill jerry can",
JerryCanFull = "Jerry can is full",
TotalCost = "Cost",
JerryCanFull = "Jerry can is full"
}

if not Config.UseESX then
Expand Down
17 changes: 7 additions & 10 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
fx_version 'bodacious'
fx_version 'cerulean'

game 'gta5'

author 'InZidiuZ'
description 'Legacy Fuel'
version '1.3'
shared_script '@es_extended/imports.lua'

-- What to run
client_scripts {
server_scripts {
'config.lua',
'functions/functions_client.lua',
'source/fuel_client.lua'
'source/fuel_server.lua'
}

server_scripts {
client_scripts {
'config.lua',
'source/fuel_server.lua'
'source/fuel_client.lua'
}

exports {
Expand Down
156 changes: 117 additions & 39 deletions source/fuel_client.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
if Config.UseESX then
Citizen.CreateThread(function()
while not ESX do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

ESX = exports["es_extended"]:getSharedObject()

Citizen.Wait(500)
end
end)
end

local isNearPump = false
local isFueling = false
local currentFuel = 0.0
Expand All @@ -33,16 +29,16 @@ end
Citizen.CreateThread(function()
DecorRegister(Config.FuelDecor, 1)

for index = 1, #Config.Blacklist do
if type(Config.Blacklist[index]) == 'string' then
Config.Blacklist[GetHashKey(Config.Blacklist[index])] = true
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[index]] = true
Config.Blacklist[Config.Blacklist[i]] = true
end
end

for index = #Config.Blacklist, 1, -1 do
table.remove(Config.Blacklist, index)
for i = #Config.Blacklist, 1, -1 do
table.remove(Config.Blacklist, i)
end

while true do
Expand Down Expand Up @@ -74,6 +70,37 @@ Citizen.CreateThread(function()
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)
Expand All @@ -84,13 +111,7 @@ Citizen.CreateThread(function()
isNearPump = pumpObject

if Config.UseESX then
local playerData = ESX.GetPlayerData()
for i=1, #playerData.accounts, 1 do
if playerData.accounts[i].name == 'money' then
currentCash = playerData.accounts[i].money
break
end
end
currentCash = ESX.GetPlayerData().money
end
else
isNearPump = false
Expand All @@ -100,6 +121,30 @@ Citizen.CreateThread(function()
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)

Expand All @@ -108,7 +153,7 @@ AddEventHandler('fuel:startFuelUpTick', function(pumpObject, ped, vehicle)

local oldFuel = DecorGetFloat(vehicle, Config.FuelDecor)
local fuelToAdd = math.random(10, 20) / 10.0
local extraCost = fuelToAdd / 1.5 * Config.CostMultiplier
local extraCost = fuelToAdd / 1.5

if not pumpObject then
if GetAmmoInPedWeapon(ped, 883325847) - fuelToAdd * 100 >= 0 then
Expand Down Expand Up @@ -143,6 +188,12 @@ AddEventHandler('fuel:startFuelUpTick', function(pumpObject, ped, vehicle)
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)
Expand All @@ -153,8 +204,10 @@ AddEventHandler('fuel:refuelFromPump', function(pumpObject, ped, vehicle)
TriggerEvent('fuel:startFuelUpTick', pumpObject, ped, vehicle)

while isFueling do
for _, controlIndex in pairs(Config.DisableKeys) do
DisableControlAction(0, controlIndex)
Citizen.Wait(1)

for k,v in pairs(Config.DisableKeys) do
DisableControlAction(0, v)
end

local vehicleCoords = GetEntityCoords(vehicle)
Expand All @@ -164,7 +217,7 @@ AddEventHandler('fuel:refuelFromPump', function(pumpObject, ped, vehicle)
local extraString = ""

if Config.UseESX then
extraString = "\n" .. Config.Strings.TotalCost .. ": ~g~$" .. Round(currentCost, 1)
extraString = "\nCost: ~g~$" .. Round(currentCost, 1)
end

DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.CancelFuelingPump .. extraString)
Expand All @@ -180,8 +233,6 @@ AddEventHandler('fuel:refuelFromPump', function(pumpObject, ped, vehicle)
if IsControlJustReleased(0, 38) or DoesEntityExist(GetPedInVehicleSeat(vehicle, -1)) or (isNearPump and GetEntityHealth(pumpObject) <= 0) then
isFueling = false
end

Citizen.Wait(0)
end

ClearPedTasks(ped)
Expand All @@ -190,6 +241,8 @@ 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
Expand Down Expand Up @@ -284,26 +337,42 @@ Citizen.CreateThread(function()
else
Citizen.Wait(250)
end

Citizen.Wait(0)
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 _, gasStationCoords in pairs(Config.GasStations) do
local dstcheck = GetDistanceBetweenCoords(coords, gasStationCoords)
for k,v in pairs(Config.GasStations) do
local dstcheck = GetDistanceBetweenCoords(coords, v)

if dstcheck < closest then
closest = dstcheck
closestCoords = gasStationCoords
closestCoords = v
end
end

Expand All @@ -312,18 +381,27 @@ if Config.ShowNearestGasStationOnly then
end

currentGasBlip = CreateBlip(closestCoords)

Citizen.Wait(10000)
end
end)
elseif Config.ShowAllGasStations then
Citizen.CreateThread(function()
for _, gasStationCoords in pairs(Config.GasStations) do
CreateBlip(gasStationCoords)
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)
Expand All @@ -350,6 +428,8 @@ if Config.EnableHUD then

Citizen.CreateThread(function()
while true do
Citizen.Wait(50)

local ped = PlayerPedId()

if IsPedInAnyVehicle(ped) and not (Config.RemoveHUDForBlacklistedVehicle and inBlacklisted) then
Expand All @@ -366,13 +446,13 @@ if Config.EnableHUD then

Citizen.Wait(500)
end

Citizen.Wait(50)
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)
Expand All @@ -381,8 +461,6 @@ if Config.EnableHUD then
else
Citizen.Wait(750)
end

Citizen.Wait(0)
end
end)
end
5 changes: 2 additions & 3 deletions source/fuel_server.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
ESX = nil

if Config.UseESX then
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

ESX = exports["es_extended"]:getSharedObject()

RegisterServerEvent('fuel:pay')
AddEventHandler('fuel:pay', function(price)
Expand Down