diff --git a/config.lua b/config.lua index 8e4ec39..5d8fd80 100644 --- a/config.lua +++ b/config.lua @@ -1,8 +1,5 @@ Config = {} --- Are you using ESX? Turn this to true if you would like fuel & jerry cans to cost something. -Config.UseESX = true - -- What should the price of jerry cans be? Config.JerryCanCost = 100 Config.RefillCost = 50 -- If it is missing half of it capacity, this amount will be divided in half, and so on. @@ -13,9 +10,6 @@ Config.FuelDecor = "_FUEL_LEVEL" -- What keys are disabled while you're fueling. Config.DisableKeys = {0, 22, 23, 24, 29, 30, 31, 37, 44, 56, 82, 140, 166, 167, 168, 170, 288, 289, 311, 323} --- Want to use the HUD? Turn this to true. -Config.EnableHUD = true - -- Configure blips here. Turn both to false to disable blips all together. Config.ShowNearestGasStationOnly = true Config.ShowAllGasStations = false @@ -39,11 +33,6 @@ Config.Strings = { TotalCost = "Cost", } -if not Config.UseESX then - Config.Strings.PurchaseJerryCan = "Press ~g~E ~w~to grab a jerry can" - Config.Strings.RefillJerryCan = "Press ~g~E ~w~ to refill the jerry can" -end - Config.PumpModels = { [-2007231801] = true, [1339433404] = true, @@ -132,5 +121,6 @@ Config.GasStations = { vector3(176.631, -1562.025, 29.263), vector3(176.631, -1562.025, 29.263), vector3(-319.292, -1471.715, 30.549), + vector3(-66.48, -2532.57, 6.14), vector3(1784.324, 3330.55, 41.253) } diff --git a/functions/functions_client.lua b/functions/functions_client.lua index 37ba151..cd7fdf8 100644 --- a/functions/functions_client.lua +++ b/functions/functions_client.lua @@ -2,6 +2,8 @@ function GetFuel(vehicle) return DecorGetFloat(vehicle, Config.FuelDecor) end +exports('GetFuel', GetFuel) + function SetFuel(vehicle, fuel) if type(fuel) == 'number' and fuel >= 0 and fuel <= 100 then SetVehicleFuelLevel(vehicle, fuel + 0.0) @@ -9,13 +11,14 @@ function SetFuel(vehicle, fuel) end end +exports('SetFuel', SetFuel) + function LoadAnimDict(dict) - if not HasAnimDictLoaded(dict) then - RequestAnimDict(dict) + if HasAnimDictLoaded(dict) then return end + RequestAnimDict(dict) - while not HasAnimDictLoaded(dict) do - Citizen.Wait(1) - end + while not HasAnimDictLoaded(dict) do + Wait(10) end end @@ -41,10 +44,10 @@ function Round(num, numDecimalPlaces) end function CreateBlip(coords) - local blip = AddBlipForCoord(coords) + local blip = AddBlipForCoord(coords.x, coords.y, coords.z) SetBlipSprite(blip, 361) - SetBlipScale(blip, 0.9) + SetBlipScale(blip, 0.6) SetBlipColour(blip, 4) SetBlipDisplay(blip, 4) SetBlipAsShortRange(blip, true) @@ -76,7 +79,7 @@ function FindNearestFuelPump() local pumpDistance = 1000 for _, fuelPumpObject in pairs(fuelPumps) do - local dstcheck = GetDistanceBetweenCoords(coords, GetEntityCoords(fuelPumpObject)) + local dstcheck = #(coords - GetEntityCoords(fuelPumpObject)) if dstcheck < pumpDistance then pumpDistance = dstcheck diff --git a/fxmanifest.lua b/fxmanifest.lua index 9103586..98ca171 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -1,23 +1,19 @@ -fx_version 'bodacious' +fx_version 'cerulean' game 'gta5' author 'InZidiuZ' description 'Legacy Fuel' version '1.3' --- What to run +shared_script 'config.lua' + client_scripts { - 'config.lua', 'functions/functions_client.lua', 'source/fuel_client.lua' } server_scripts { - 'config.lua', 'source/fuel_server.lua' } -exports { - 'GetFuel', - 'SetFuel' -} +lua54 'yes' diff --git a/source/fuel_client.lua b/source/fuel_client.lua index df886e7..5ddab32 100644 --- a/source/fuel_client.lua +++ b/source/fuel_client.lua @@ -1,18 +1,10 @@ -if Config.UseESX then - Citizen.CreateThread(function() - while not ESX do - TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) - - Citizen.Wait(500) - end - end) -end - -local isNearPump = false +local QBCore = exports['qb-core']:GetCoreObject() +local isNearPump = nil local isFueling = false local currentFuel = 0.0 local currentCost = 0.0 -local currentCash = 1000 +local todaycost = 0 +local currentCash = 0 local fuelSynced = false local inBlacklisted = false @@ -30,7 +22,7 @@ function ManageFuelUsage(vehicle) end end -Citizen.CreateThread(function() +CreateThread(function() DecorRegister(Config.FuelDecor, 1) for index = 1, #Config.Blacklist do @@ -46,12 +38,12 @@ Citizen.CreateThread(function() end while true do - Citizen.Wait(1000) + Wait(1000) local ped = PlayerPedId() - if IsPedInAnyVehicle(ped) then - local vehicle = GetVehiclePedIsIn(ped) + if IsPedInAnyVehicle(ped, false) then + local vehicle = GetVehiclePedIsIn(ped, false) if Config.Blacklist[GetEntityModel(vehicle)] then inBlacklisted = true @@ -74,41 +66,34 @@ Citizen.CreateThread(function() end end) -Citizen.CreateThread(function() +CreateThread(function() while true do - Citizen.Wait(250) + Wait(250) local pumpObject, pumpDistance = FindNearestFuelPump() if pumpDistance < 2.5 then 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 - end + currentCash = QBCore.Functions.GetPlayerData().money['cash'] else - isNearPump = false + isNearPump = nil - Citizen.Wait(math.ceil(pumpDistance * 20)) + Wait(math.ceil(pumpDistance * 20)) end end end) +local extraCost = math.random(3, 6) + AddEventHandler('fuel:startFuelUpTick', function(pumpObject, ped, vehicle) currentFuel = GetVehicleFuelLevel(vehicle) while isFueling do - Citizen.Wait(500) + Wait(500) 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 * Config.CostMultiplier if not pumpObject then if GetAmmoInPedWeapon(ped, 883325847) - fuelToAdd * 100 >= 0 then @@ -127,12 +112,14 @@ AddEventHandler('fuel:startFuelUpTick', function(pumpObject, ped, vehicle) isFueling = false end - currentCost = currentCost + extraCost - - if currentCash >= currentCost then - SetFuel(vehicle, currentFuel) - else - isFueling = false + if extraCost >= 1 then + currentCost = currentCost + extraCost + todaycost = extraCost + if currentCash >= currentCost then + SetFuel(vehicle, currentFuel) + else + isFueling = false + end end end @@ -145,55 +132,52 @@ end) AddEventHandler('fuel:refuelFromPump', function(pumpObject, ped, vehicle) TaskTurnPedToFaceEntity(ped, vehicle, 1000) - Citizen.Wait(1000) + 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) + TaskPlayAnim(ped, "timetable@gardener@filling_can", "gar_ig_5_filling_can", 2.0, 8.0, -1, 50, 0, false, false, false) TriggerEvent('fuel:startFuelUpTick', pumpObject, ped, vehicle) while isFueling do for _, controlIndex in pairs(Config.DisableKeys) do - DisableControlAction(0, controlIndex) + DisableControlAction(0, controlIndex, true) end local vehicleCoords = GetEntityCoords(vehicle) if pumpObject then local stringCoords = GetEntityCoords(pumpObject) - local extraString = "" - - if Config.UseESX then - extraString = "\n" .. Config.Strings.TotalCost .. ": ~g~$" .. Round(currentCost, 1) - end + local extraString = "\n" .. Config.Strings.TotalCost .. ": ~g~$" .. Round(todaycost, 1) DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.CancelFuelingPump .. extraString) DrawText3Ds(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z + 0.5, Round(currentFuel, 1) .. "%") + DrawText3Ds(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z + 1.3, Round(currentCost, 1) .. '~b~$~w~ cost') 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) + TaskPlayAnim(ped, "timetable@gardener@filling_can", "gar_ig_5_filling_can", 2.0, 8.0, -1, 50, 0, false, false, false) end if IsControlJustReleased(0, 38) or DoesEntityExist(GetPedInVehicleSeat(vehicle, -1)) or (isNearPump and GetEntityHealth(pumpObject) <= 0) then isFueling = false end - Citizen.Wait(0) + Wait(0) end ClearPedTasks(ped) RemoveAnimDict("timetable@gardener@filling_can") end) -Citizen.CreateThread(function() +CreateThread(function() while true do 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 + if IsPedInAnyVehicle(ped, false) and GetPedInVehicleSeat(GetVehiclePedIsIn(ped, false), -1) == ped then local pumpCoords = GetEntityCoords(isNearPump) DrawText3Ds(pumpCoords.x, pumpCoords.y, pumpCoords.z + 1.2, Config.Strings.ExitVehicle) @@ -201,7 +185,7 @@ Citizen.CreateThread(function() local vehicle = GetPlayersLastVehicle() local vehicleCoords = GetEntityCoords(vehicle) - if DoesEntityExist(vehicle) and GetDistanceBetweenCoords(GetEntityCoords(ped), vehicleCoords) < 2.5 then + if DoesEntityExist(vehicle) and #(GetEntityCoords(ped) - vehicleCoords) < 2.5 then if not DoesEntityExist(GetPedInVehicleSeat(vehicle, -1)) then local stringCoords = GetEntityCoords(isNearPump) local canFuel = true @@ -237,60 +221,50 @@ Citizen.CreateThread(function() local stringCoords = GetEntityCoords(isNearPump) if currentCash >= Config.JerryCanCost then - if not HasPedGotWeapon(ped, 883325847) then + if not HasPedGotWeapon(ped, 883325847, false) then DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.PurchaseJerryCan) if IsControlJustReleased(0, 38) then - GiveWeaponToPed(ped, 883325847, 4500, false, true) - + TriggerServerEvent('fuel:addPetrolCan') + TriggerEvent("inventory:client:ItemBox", QBCore.Shared.Items["weapon_petrolcan"], "add") TriggerServerEvent('fuel:pay', Config.JerryCanCost) - - currentCash = ESX.GetPlayerData().money end else - if Config.UseESX then - local refillCost = Round(Config.RefillCost * (1 - GetAmmoInPedWeapon(ped, 883325847) / 4500)) + 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) + if refillCost > 0 then + if currentCash >= refillCost then + DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.RefillJerryCan .. refillCost) - if IsControlJustReleased(0, 38) then - TriggerServerEvent('fuel:pay', refillCost) + 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) + SetPedAmmo(ped, 883325847, 4500) end else - DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.JerryCanFull) + DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.NotEnoughCashJerryCan) end else - DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.RefillJerryCan) - - if IsControlJustReleased(0, 38) then - SetPedAmmo(ped, 883325847, 4500) - end + DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.JerryCanFull) end end else DrawText3Ds(stringCoords.x, stringCoords.y, stringCoords.z + 1.2, Config.Strings.NotEnoughCash) end else - Citizen.Wait(250) + Wait(250) end end else - Citizen.Wait(250) + Wait(250) end - Citizen.Wait(0) + Wait(0) end end) if Config.ShowNearestGasStationOnly then - Citizen.CreateThread(function() + CreateThread(function() local currentGasBlip = 0 while true do @@ -299,7 +273,7 @@ if Config.ShowNearestGasStationOnly then local closestCoords for _, gasStationCoords in pairs(Config.GasStations) do - local dstcheck = GetDistanceBetweenCoords(coords, gasStationCoords) + local dstcheck = #(coords - gasStationCoords) if dstcheck < closest then closest = dstcheck @@ -311,78 +285,15 @@ if Config.ShowNearestGasStationOnly then RemoveBlip(currentGasBlip) end - currentGasBlip = CreateBlip(closestCoords) + if closestCoords then currentGasBlip = CreateBlip(closestCoords) end - Citizen.Wait(10000) + Wait(10000) end end) elseif Config.ShowAllGasStations then - Citizen.CreateThread(function() + CreateThread(function() for _, gasStationCoords in pairs(Config.GasStations) do CreateBlip(gasStationCoords) end 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 - 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 - - Citizen.Wait(50) - end - end) - - Citizen.CreateThread(function() - while true do - 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 - - Citizen.Wait(0) - end - end) -end diff --git a/source/fuel_server.lua b/source/fuel_server.lua index 5a61c65..67e6626 100644 --- a/source/fuel_server.lua +++ b/source/fuel_server.lua @@ -1,15 +1,18 @@ -ESX = nil +local QBCore = exports['qb-core']:GetCoreObject() -if Config.UseESX then - TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) +RegisterNetEvent('fuel:pay', function(price) + local Player = QBCore.Functions.GetPlayer(source) + local amount = math.floor(price + 0.5) - RegisterServerEvent('fuel:pay') - AddEventHandler('fuel:pay', function(price) - local xPlayer = ESX.GetPlayerFromId(source) - local amount = ESX.Math.Round(price) + if not Player or price <= 0 then return end - if price > 0 then - xPlayer.removeMoney(amount) - end - end) -end + Player.Functions.RemoveMoney('cash', amount) +end) + +RegisterNetEvent('fuel:addPetrolCan', function() + local Player = QBCore.Functions.GetPlayer(source) + + if not Player then return end + + Player.Functions.AddItem('weapon_petrolcan', 1) +end)