-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.lua
182 lines (140 loc) · 6.69 KB
/
server.lua
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local printer = {}
local printerCoords = {}
local printedObject, currentItem, rewardID, randomCoords = nil, nil, nil, nil
local cooldowns = {}
local cooldownTime = D2D.Printer.cooldown * 1000
local cooldownTick = 0
local ox_inventory = exports.ox_inventory
Citizen.CreateThread(
function()
local coords = D2D.Printer.coords
if #coords > 0 then
local randomIndex = math.random(1, #coords)
randomCoords = coords[randomIndex]
printer = {coords = randomCoords, battery = 0, isPrinting = false, canStart = true, canPickup = false, timeLeft = 0}
end
end
)
local function SpawnPropOnPrinter(printerCoords, item, data)
local modelhash = GetHashKey(data.prop)
printedObject = CreateObjectNoOffset(modelhash, printerCoords.x, printerCoords.y, printerCoords.z+0.18, true)
while not DoesEntityExist(printedObject) do
Wait(1)
end
SetEntityRotation(printedObject, -150.0, 0.0, -100.0)
SetEntityDistanceCullingRadius(printedObject, 25000.0) -- Depreciated native (use at own risk), if you know of a replacement please let me know!
end
local function CheckItemAmount(playerId, itemName, requiredAmount)
local slotIds = ox_inventory:GetSlotIdsWithItem(playerId, itemName)
if not slotIds or type(slotIds) ~= "table" then
print("Error: Invalid slotIds for item " .. itemName)
return false
end
local totalCount = 0
for _, slotId in ipairs(slotIds) do
local slotData = ox_inventory:GetSlot(playerId, slotId)
totalCount = totalCount + slotData.count
end
if totalCount >= requiredAmount then
Debug("Player has enough " .. itemName .. ": " .. totalCount)
return true
else
TriggerClientEvent('D2D-3DPrinter:Notifications', playerId, D2D.Translation["insufficientMaterials"])
return false
end
end
RegisterServerEvent("D2D-3DPrinter:requestPrinter")
AddEventHandler("D2D-3DPrinter:requestPrinter", function()
local playerId = source
TriggerClientEvent("D2D-3DPrinter:sendPrinter", playerId, printer)
end)
RegisterServerEvent("D2D-3DPrinter:startCrafting")
AddEventHandler(
"D2D-3DPrinter:startCrafting",
function(item, data)
currentItem = item
local pID = source
local craftTime = data.craftTime
local enoughMaterials = CheckItemAmount(pID, D2D.PrinterMaterialItem, data.materialNeeded)
if not enoughMaterials then
return
end
if printer.battery < 10000 then
TriggerClientEvent('D2D-3DPrinter:Notifications', source, D2D.Translation["lowBattery"])
return
end
printer.canStart = false
printer.isPrinting = true
ox_inventory:RemoveItem(source, D2D.PrinterMaterialItem, data.materialNeeded)
TriggerClientEvent('ToggleParticleEffect', source)
local totalTicks = craftTime * 10
local batteryStep = 10000 / totalTicks
Citizen.CreateThread(function()
for tick = 1, totalTicks do
local remainingTicks = totalTicks - tick
printer.timeLeft = remainingTicks/10
local remainingBattery = math.max(0, 10000 - tick * batteryStep)
printer.battery = remainingBattery
Citizen.Wait(100)
end
SpawnPropOnPrinter(printer.coords, item, data)
TriggerClientEvent('ToggleParticleEffect', pID)
TriggerClientEvent('D2D-3DPrinter:Notifications', pID, string.format(D2D.Translation["done"], data.label))
printer.canPickup = true
printer.isPrinting = false
end)
end)
RegisterServerEvent("D2D-3DPrinter:refuel")
AddEventHandler("D2D-3DPrinter:refuel", function(slotId)
local slotData = ox_inventory:GetSlot(source, slotId)
local newBattery = nil
Debug("Received slot ID: " .. slotId)
if slotData then
-- Check if the item is a petrol can
if slotData.name == "WEAPON_PETROLCAN" then
-- Check if the itemMetadata is valid and contains ammo and durability
if slotData.metadata and slotData.metadata.ammo and slotData.metadata.durability then
local oldBattery = nil -- Placeholder for the old battery level
-- Iterate through printer objects to find the old battery level
oldBattery = printer.battery / 100 -- Scale back to percentage
local ammo = slotData.metadata.ammo
local batteryIncrease = ammo * 100 -- Scale the ammo to match the battery range
-- Debug statement to display the ammo and battery increase
Debug("Battery Refueling to: " .. batteryIncrease/100)
-- Update the printer battery with the calculated increase
printer.battery = math.min(10000, printer.battery + batteryIncrease)
newBattery = printer.battery / 100 -- Scale back to percentage
-- Calculate the fuel used for refueling
local fuelUsed = newBattery - oldBattery -- Calculate the difference
-- Deduct the fuel used from the petrol can's metadata
slotData.metadata.ammo = slotData.metadata.ammo - fuelUsed
slotData.metadata.durability = slotData.metadata.durability - fuelUsed
-- Set the updated metadata back to the petrol can item
ox_inventory:SetMetadata(source, slotId, slotData.metadata)
-- Debug statement to confirm metadata update
Debug("Updated ammo metadata: " .. slotData.metadata.ammo)
Debug("Updated durability metadata: " .. slotData.metadata.durability)
-- Send notification to the client with the updated battery level
TriggerClientEvent('D2D-3DPrinter:Notifications', source, string.format(D2D.Translation["fueled"], newBattery.. "%"))
end
end
end
end)
local rewardGiven = false
lib.callback.register('D2D-3DPrinter:Reward', function(source, item)
if not rewardGiven then
if ox_inventory:CanCarryItem(source, item, 1) then
ox_inventory:AddItem(source, item, 1)
rewardGiven = true
DeleteEntity(printedObject)
printer.canPickup = false
printer.isPrinting = false
printer.canStart = true
return true
else
TriggerClientEvent('D2D-3DPrinter:Notifications', source, D2D.Translation["cantcarry"])
end
else
DropPlayer(source, "Stop trying to duplicate items :) ")
end
end)