Skip to content

Commit

Permalink
feat(client): add setMoneyCheck export
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Mar 31, 2023
1 parent 8cc33d9 commit ca912cc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
# ox_fuel

Basic fuel resource and alternative to LegacyFuel, meant for use with ox_inventory.

## Get vehicle fuel level
This is an incredibly complicated task for some people, and they often ask for exports to do it.

This is an incredibly complicated task for some people, and they often ask for exports to do it.
You use the native function [GetVehicleFuelLevel](https://docs.fivem.net/natives/?_0x5F739BB8), or you can use a statebag.

```lua
local fuel = GetVehicleFuelLevel(entity)
-- or Entity(entity).state.fuel
Entity(entity).state.fuel
```

## Set vehicle fuel level

```lua
Entity(entity).state.fuel = fuelAmount
```

## setPaymentMethod (server)

Replaces the standard payment method using "money" as an item.

```lua
exports.ox_fuel:setPaymentMethod(function(playerId, amount)
local xPlayer = ESX.GetPlayerFromId(playerId)
local bankAmount = xPlayer.getAccount('bank').money

if bankAmount >= amount then
xPlayer.removeAccountMoney('bank', amount)
return true
end

TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = locale('not_enough_money', amount - bankAmount)
})
end)
```

## setMoneyCheck (client)

Replaces the standard inventory search for "money".

```lua
exports.ox_fuel:setMoneyCheck(function()
local accounts = ESX.GetPlayerData().accounts

for i = 1, #accounts do
if accounts[i].name == 'bank' then
return accounts[i].money
end
end

return 0
end)
```
21 changes: 16 additions & 5 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ end

local ox_inventory = exports.ox_inventory

---@return number
local function defaultMoneyCheck()
return ox_inventory:Search('count', 'money')
end

local getMoneyAmount = defaultMoneyCheck

exports('setMoneyCheck', function(fn)
getMoneyAmount = fn or defaultMoneyCheck
end)

-- fuelingMode = 1 - Pump
-- fuelingMode = 2 - Can
local function startFueling(vehicle, isPump)
Expand All @@ -200,7 +211,7 @@ local function startFueling(vehicle, isPump)

if isPump then
price = 0
moneyAmount = ox_inventory:Search(2, 'money')
moneyAmount = getMoneyAmount()

if Config.priceTick > moneyAmount then
return lib.notify({
Expand Down Expand Up @@ -314,7 +325,7 @@ if not Config.qtarget then
local playerCoords = GetEntityCoords(cache.ped)

if nearestPump then
local moneyAmount = ox_inventory:Search(2, 'money')
local moneyAmount = getMoneyAmount()

if petrolCan and moneyAmount >= Config.petrolCan.refillPrice then
return getPetrolCan(nearestPump, true)
Expand Down Expand Up @@ -365,7 +376,7 @@ if Config.qtarget then
options = {
{
action = function (entity)
if ox_inventory:Search(2, 'money') >= Config.priceTick then
if getMoneyAmount() >= Config.priceTick then
startFueling(lastVehicle, 1)
else
lib.notify({type = 'error', description = locale('refuel_cannot_afford')})
Expand All @@ -384,7 +395,7 @@ if Config.qtarget then
{
action = function (entity)
local petrolCan = Config.petrolCan.enabled and GetSelectedPedWeapon(cache.ped) == `WEAPON_PETROLCAN`
local moneyAmount = ox_inventory:Search(2, 'money')
local moneyAmount = getMoneyAmount()

if moneyAmount < Config.petrolCan.price then
return lib.notify({type = 'error', description = locale('petrolcan_cannot_afford')})
Expand All @@ -403,7 +414,7 @@ if Config.qtarget then
options = {
{
action = function (entity)
if ox_inventory:Search(2, 'money') >= Config.priceTick then
if getMoneyAmount() >= Config.priceTick then
if GetVehicleFuelLevel(lastVehicle) >= 100 then
return lib.notify({type = 'error', description = locale('vehicle_full')})
end
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ game 'gta5'
--[[ Resource Information ]]--
name 'ox_fuel'
author 'Overextended'
version '1.2.1'
version '1.3.0'
repository 'https://github.com/overextended/ox_fuel'
description 'Fuel management system with ox_inventory support'

Expand Down

0 comments on commit ca912cc

Please sign in to comment.