Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: check DoesVehicleUseFuel in canInteract #749

Merged
merged 2 commits into from
Jul 18, 2024
Merged
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
500 changes: 0 additions & 500 deletions server-data/resources/[ox]/ox_fuel/client.lua

This file was deleted.

142 changes: 142 additions & 0 deletions server-data/resources/[ox]/ox_fuel/client/fuel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
local config = require("config")
local state = require("client.state")
local utils = require("client.utils")
local fuel = {}

---@param vehState StateBag
---@param vehicle integer
---@param amount number
---@param replicate? boolean
function fuel.setFuel(vehState, vehicle, amount, replicate)
if DoesEntityExist(vehicle) then
amount = math.clamp(amount, 0, 100)

SetVehicleFuelLevel(vehicle, amount)
vehState:set("fuel", amount, replicate)
end
end

function fuel.getPetrolCan(coords, refuel)
TaskTurnPedToFaceCoord(cache.ped, coords.x, coords.y, coords.z, 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 exports.ox_inventory:GetItemCount("WEAPON_PETROLCAN") then
return TriggerServerEvent("ox_fuel:fuelCan", true, config.petrolCan.refillPrice)
end

TriggerServerEvent("ox_fuel:fuelCan", false, config.petrolCan.price)
end

ClearPedTasks(cache.ped)
end

function fuel.startFueling(vehicle, isPump)
local vehState = Entity(vehicle).state
local fuelAmount = vehState.fuel or GetVehicleFuelLevel(vehicle)
local duration = math.ceil((100 - fuelAmount) / config.refillValue) * config.refillTick
local price, moneyAmount
local durability = 0

if 100 - fuelAmount < config.refillValue then
return lib.notify({ type = "error", description = locale("tank_full") })
end

if isPump then
price = 0
moneyAmount = utils.getMoney()

if config.priceTick > moneyAmount then
return lib.notify({
type = "error",
description = locale("not_enough_money", config.priceTick),
})
end
elseif not state.petrolCan then
return lib.notify({ type = "error", description = locale("petrolcan_not_equipped") })
elseif state.petrolCan.metadata.ammo <= config.durabilityTick then
return lib.notify({
type = "error",
description = locale("petrolcan_not_enough_fuel"),
})
end

state.isFueling = true

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",
},
})

state.isFueling = false
end)

while state.isFueling do
if isPump then
price += config.priceTick

if price + config.priceTick >= moneyAmount then
lib.cancelProgress()
end
elseif state.petrolCan then
durability += config.durabilityTick

if durability >= state.petrolCan.metadata.ammo then
lib.cancelProgress()
durability = state.petrolCan.metadata.ammo
break
end
else
break
end

fuelAmount += config.refillValue

if fuelAmount >= 100 then
state.isFueling = false
fuelAmount = 100.0
end

Wait(config.refillTick)
end

ClearPedTasks(cache.ped)

if isPump then
TriggerServerEvent("ox_fuel:pay", price, fuelAmount, NetworkGetNetworkIdFromEntity(vehicle))
else
TriggerServerEvent("ox_fuel:updateFuelCan", durability, NetworkGetNetworkIdFromEntity(vehicle), fuelAmount)
end
end

return fuel
140 changes: 140 additions & 0 deletions server-data/resources/[ox]/ox_fuel/client/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
local config = require("config")

if not config then
return
end

SetFuelConsumptionState(true)
SetFuelConsumptionRateMultiplier(config.globalFuelConsumptionRate)

AddTextEntry("fuelHelpText", locale("fuel_help"))
AddTextEntry("petrolcanHelpText", locale("petrolcan_help"))
AddTextEntry("fuelLeaveVehicleText", locale("leave_vehicle"))
AddTextEntry("ox_fuel_station", locale("fuel_station_blip"))

local utils = require("client.utils")
local state = require("client.state")
local fuel = require("client.fuel")

require("client.stations")

local function startDrivingVehicle()
local vehicle = cache.vehicle

if not DoesVehicleUseFuel(vehicle) then
return
end

local vehState = Entity(vehicle).state

if not vehState.fuel then
vehState:set("fuel", GetVehicleFuelLevel(vehicle), true)
while not vehState.fuel do
Wait(0)
end
end

SetVehicleFuelLevel(vehicle, vehState.fuel)

local fuelTick = 0

while cache.seat == -1 do
if not DoesEntityExist(vehicle) then
return
end

local fuelAmount = tonumber(vehState.fuel)
local newFuel = GetVehicleFuelLevel(vehicle)

if fuelAmount > 0 then
if GetVehiclePetrolTankHealth(vehicle) < 700 then
newFuel -= math.random(10, 20) * 0.01
end

if fuelAmount ~= newFuel then
if fuelTick == 15 then
fuelTick = 0
end

fuel.setFuel(vehState, vehicle, newFuel, fuelTick == 0)
fuelTick += 1
end
end

Wait(1000)
end

fuel.setFuel(vehState, vehicle, vehState.fuel, true)
end

if cache.seat == -1 then
CreateThread(startDrivingVehicle)
end

lib.onCache("seat", function(seat)
if cache.vehicle then
state.lastVehicle = cache.vehicle
end

if seat == -1 then
SetTimeout(0, startDrivingVehicle)
end
end)

if config.ox_target then
return require("client.target")
end

RegisterCommand("startfueling", function()
if state.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)
local nearestPump = state.nearestPump

if nearestPump then
local moneyAmount = utils.getMoney()

if petrolCan and moneyAmount >= config.petrolCan.refillPrice then
return fuel.getPetrolCan(nearestPump, true)
end

local vehicleInRange = state.lastVehicle and #(GetEntityCoords(state.lastVehicle) - playerCoords) <= 3

if not vehicleInRange then
if not config.petrolCan.enabled then
return
end

if moneyAmount >= config.petrolCan.price then
return fuel.getPetrolCan(nearestPump)
end

return lib.notify({ type = "error", description = locale("petrolcan_cannot_afford") })
elseif moneyAmount >= config.priceTick then
return fuel.startFueling(state.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 = utils.getVehicleInFront()

if vehicle and DoesVehicleUseFuel(vehicle) then
local boneIndex = utils.getVehiclePetrolCapBoneIndex(vehicle)
local fuelcapPosition = boneIndex and GetWorldPositionOfEntityBone(vehicle, boneIndex)

if fuelcapPosition and #(playerCoords - fuelcapPosition) < 1.8 then
return fuel.startFueling(vehicle, false)
end

return lib.notify({ type = "error", description = locale("vehicle_far") })
end
end
end)

RegisterKeyMapping("startfueling", "Fuel vehicle", "keyboard", "e")
TriggerEvent("chat:removeSuggestion", "/startfueling")
22 changes: 22 additions & 0 deletions server-data/resources/[ox]/ox_fuel/client/state.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---@class State
---@field petrolCan SlotWithItem?
---@field isFueling boolean
---@field nearestPump vector3?
---@field lastVehicle number?
local state = {
isFueling = false,
lastVehicle = cache.vehicle or GetPlayersLastVehicle()
}

if state.lastVehicle == 0 then state.lastVehicle = nil end

---@param data? SlotWithItem
local function setPetrolCan(data)
state.petrolCan = data?.name == 'WEAPON_PETROLCAN' and data or nil
end

setPetrolCan(exports.ox_inventory:getCurrentWeapon())

AddEventHandler('ox_inventory:currentWeapon', setPetrolCan)

return state
81 changes: 81 additions & 0 deletions server-data/resources/[ox]/ox_fuel/client/stations.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local config = require("config")
local state = require("client.state")
local utils = require("client.utils")
local stations = lib.load("data.stations")

if config.showBlips == 2 then
for station in pairs(stations) do
utils.createBlip(station)
end
end

if config.ox_target and config.showBlips ~= 1 then
return
end

---@param point CPoint
local function onEnterStation(point)
if config.showBlips == 1 and not point.blip then
point.blip = utils.createBlip(point.coords)
end
end

---@param point CPoint
local function nearbyStation(point)
if point.currentDistance > 15 then
return
end

local pumps = point.pumps
local pumpDistance

for i = 1, #pumps do
local pump = pumps[i]
pumpDistance = #(cache.coords - pump)

if pumpDistance <= 3 then
state.nearestPump = pump

repeat
local playerCoords = GetEntityCoords(cache.ped)
pumpDistance = #(GetEntityCoords(cache.ped) - pump)

if cache.vehicle then
DisplayHelpTextThisFrame("fuelLeaveVehicleText", false)
elseif not state.isFueling then
local vehicleInRange = state.lastVehicle ~= 0 and #(GetEntityCoords(state.lastVehicle) - playerCoords) <= 3

if vehicleInRange then
DisplayHelpTextThisFrame("fuelHelpText", false)
elseif config.petrolCan.enabled then
DisplayHelpTextThisFrame("petrolcanHelpText", false)
end
end

Wait(0)
until pumpDistance > 3

state.nearestPump = nil

return
end
end
end

---@param point CPoint
local function onExitStation(point)
if point.blip then
point.blip = RemoveBlip(point.blip)
end
end

for station, pumps in pairs(stations) do
lib.points.new({
coords = station,
distance = 60,
onEnter = onEnterStation,
onExit = onExitStation,
nearby = nearbyStation,
pumps = pumps,
})
end
Loading
Loading