forked from overextended/ox_fuel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
129 lines (104 loc) · 3.62 KB
/
server.lua
File metadata and controls
129 lines (104 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
if not lib.checkDependency('ox_lib', '3.0.0', true) then return end
lib.locale()
local getMyFramework = function()
if GetResourceState('es_extended') == 'started' then
return 'esx', exports['es_extended']:getSharedObject()
elseif GetResourceState('qb-core') == 'started' then
return 'qb-core', exports['qb-core']:GetCoreObject()
end
end
local CoreName, Core = getMyFramework()
local function setFuelState(netid, fuel)
local vehicle = NetworkGetEntityFromNetworkId(netid)
local state = vehicle and Entity(vehicle)?.state
if state then
state:set('fuel', fuel, true)
end
end
---@param playerId number
---@param price number
---@return boolean?
local function defaultPaymentMethod(playerId, price)
if CoreName == 'esx' then
local xPlayer = Core.GetPlayerFromId(playerId)
if xPlayer.getMoney() >= price then
xPlayer.removeMoney(price)
return true
else
TriggerClientEvent('ox_lib:notify', playerId, {
type = 'error',
description = locale('not_enough_money', price - xPlayer.getMoney())
})
end
elseif CoreName == 'qb-core' then
local xPlayer = Core.Functions.GetPlayer(playerId)
if xPlayer.PlayerData.money.cash >= price then
xPlayer.Functions.RemoveMoney('cash', price)
return true
else
TriggerClientEvent('ox_lib:notify', playerId, {
type = 'error',
description = locale('not_enough_money', price - xPlayer.PlayerData.money.cash)
})
end
end
end
local payMoney = defaultPaymentMethod
RegisterNetEvent('ox_fuel:pay', function(fuel, netid, price)
print(netid, fuel)
if price ~= nil then
if not payMoney(source, price) then
return
end
end
fuel = math.floor(fuel)
setFuelState(netid, fuel)
end)
RegisterNetEvent('ox_fuel:fuelCan', function(hasCan, price)
if hasCan then
local item = exports.ox_inventory:GetCurrentWeapon(source)
if not item or item.name ~= 'WEAPON_PETROLCAN' or not payMoney(source, price) then return end
item.metadata.durability = 100
item.metadata.ammo = 100
exports.ox_inventory:SetMetadata(source, item.slot, item.metadata)
TriggerClientEvent('ox_lib:notify', source, {
type = 'success',
description = locale('petrolcan_refill', price)
})
else
if not exports.ox_inventory:CanCarryItem(source, 'WEAPON_PETROLCAN', 1) then
return TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = locale('petrolcan_cannot_carry')
})
end
if not payMoney(source, price) then return end
exports.ox_inventory:AddItem(source, 'WEAPON_PETROLCAN', 1)
TriggerClientEvent('ox_lib:notify', source, {
type = 'success',
description = locale('petrolcan_buy', price)
})
end
end)
RegisterNetEvent('ox_fuel:updateFuelCan', function(durability, netid, fuel)
local source = source
local item = exports.ox_inventory:GetCurrentWeapon(source)
if item and durability > 0 then
durability = math.floor(item.metadata.durability - durability)
item.metadata.durability = durability
item.metadata.ammo = durability
exports.ox_inventory:SetMetadata(source, item.slot, item.metadata)
setFuelState(netid, fuel)
end
end)
RegisterNetEvent('ox_fuel:createStatebag', function(netid, fuel)
local vehicle = NetworkGetEntityFromNetworkId(netid)
local state = vehicle and Entity(vehicle).state
if state and not state.fuel and GetEntityType(vehicle) == 2 and NetworkGetEntityOwner(vehicle) == source then
state:set('fuel', fuel > 100 and 100 or fuel, true)
end
end)
RegisterServerEvent('fuel:fuelHasBeenStealed')
AddEventHandler('fuel:fuelHasBeenStealed', function(storeId, plate, fuelCount)
exports.vms_stores:sendAnnouncement(source, storeId, (Config.StealedFuelText):format(plate, fuelCount), 'monitoring', {fuelCount = fuelCount})
end)