Project Zomboid

Project Zomboid

[B41 MP only] bikinitools: Server Edition
BTSE Economy Pay Pump Fuel
I made a mod to pay for gas at the gas station but it doesn't work with BTSE_Economy for cash payments. Please help to review this code or have any suggestions on how to pay for gas at the gas station using BTSE_Economy.

-- File: media/lua/client/BTSE_PayAtPump.lua

-- Check first: only run if BTSE_Economy mod is installed
if not getActivatedMods():contains("BTSE_Economy") then return end

-- Main module
BTSE_PayAtPump = {}

-- ==================================================================
-- PRICE CONFIGURATION SECTION (change the numbers below for non-coders)
BTSE_PayAtPump.PRICE_PETROL = 1.20
BTSE_PayAtPump.PRICE_DIESEL = 0.90
BTSE_PayAtPump.PRICE_LPG = 0.60
-- ==================================================================

-- 1) initPurchaseFuel: setup when starting fuel purchase
local function initPurchaseFuel(self)
local tex = self.fuelStation:getTextureName()
self.isFuelPump = tex:find("fossoil") or tex:find("gas2go")
self.prevFuel = 0
self.deltaFuel = 0
return self
end

-- 2) getPricePerLitre: retrieve price from configuration section
local function getPricePerLitre(self)
if self.fuelType == "Diesel" then return BTSE_PayAtPump.PRICE_DIESEL
elseif self.fuelType == "LPG" then return BTSE_PayAtPump.PRICE_LPG
else return BTSE_PayAtPump.PRICE_PETROL
end
end

-- 3) roundMoney: round to 2 decimal places for cents
local function roundMoney(amount)
return math.floor(amount * 100 + 0.5) / 100
end

-- 4) payForFuel: send server command to deduct BTSE balance
local function payForFuel(self)
if not self.isFuelPump then return end
local cost = roundMoney(self.deltaFuel * getPricePerLitre(self))
if cost <= 0 then return end

-- Send command to server BTSE_Economy to deduct balance
-- args: { amount = cost }
sendClientCommand("btse_economy", "deductMoney", { amount = cost })
end

-- 5) updateFuelPurchase: called repeatedly during the fueling action
local function updateFuelPurchase(self, startAmt, targetAmt)
if not self.isFuelPump then return end
local currentFuel = targetAmt
and (targetAmt - startAmt) * self:getJobDelta()
or startAmt
self.deltaFuel = currentFuel - self.prevFuel
self.prevFuel = currentFuel
payForFuel(self)
end

-- 6) handleEmergencyStop: handle abrupt stop, charge for any remaining fuel
local function handleEmergencyStop(self)
payForFuel(self)
end

-- 7) Override for vehicle refueling
require "Vehicles/TimedActions/ISRefuelFromGasPump"
local origNew = ISRefuelFromGasPump.new
local origUpd = ISRefuelFromGasPump.update
local origStop = ISRefuelFromGasPump.stop

function ISRefuelFromGasPump:new(character, part, fuelStation, time)
local action = origNew(self, character, part, fuelStation, time)
initPurchaseFuel(action)
return action
end

function ISRefuelFromGasPump:update()
origUpd(self)
updateFuelPurchase(self, self.tankStart, self.tankTarget)
end

function ISRefuelFromGasPump:stop()
handleEmergencyStop(self)
origStop(self)
end

-- 8) Override for jerry can filling
require "TimedActions/ISTakeFuel"
local origNew2 = ISTakeFuel.new
local origUpd2 = ISTakeFuel.update
local origStop2 = ISTakeFuel.stop

function ISTakeFuel:new(character, fuelStation, petrolCan, time)
local action = origNew2(self, character, fuelStation, petrolCan, time)
initPurchaseFuel(action)
return action
end

function ISTakeFuel:update()
origUpd2(self)
updateFuelPurchase(self, self.itemStart, self.itemTarget)
end

function ISTakeFuel:stop()
handleEmergencyStop(self)
origStop2(self)
end


-- File: media/lua/server/BTSE_PayAtPump_Server.lua

-- Ensure server handles deductMoney command
BTSE.Commands.Economy.deductMoney = function(playerObj, args)
local cost = args.amount or 0
-- Subtract the player's balance on the BTSE server data
local success = BTSE.Data:subtractBalance(playerObj, cost)
if not success then
-- If it fails (insufficient funds), send a failure message back to client
sendClientCommand(playerObj, "btse_economy", "transactionFailed", { reason = "Insufficient funds" })
end
end

-- Notes:
-- 1) Place client file under media/lua/client, server file under media/lua/server.
-- 2) For non-coders: to change fuel prices, update variables in the PRICE CONFIGURATION SECTION.
Last edited by Limitless; 10 May @ 2:28am