diff --git a/README.md b/README.md index 86b926c..a54a0b4 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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:** @@ -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 ``` diff --git a/config.lua b/config.lua index 8e4ec39..eb0b8f2 100644 --- a/config.lua +++ b/config.lua @@ -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", @@ -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 diff --git a/fxmanifest.lua b/fxmanifest.lua index 9103586..88cdff1 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -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 { diff --git a/source/fuel_client.lua b/source/fuel_client.lua index df886e7..434527c 100644 --- a/source/fuel_client.lua +++ b/source/fuel_client.lua @@ -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 @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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 @@ -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) @@ -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 @@ -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) @@ -381,8 +461,6 @@ if Config.EnableHUD then else Citizen.Wait(750) end - - Citizen.Wait(0) end end) end diff --git a/source/fuel_server.lua b/source/fuel_server.lua index 5a61c65..58f6101 100644 --- a/source/fuel_server.lua +++ b/source/fuel_server.lua @@ -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)