-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.lua
More file actions
148 lines (117 loc) · 4.29 KB
/
Copy pathserver.lua
File metadata and controls
148 lines (117 loc) · 4.29 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local ox_inventory = exports["ox_inventory"]
lib.versionCheck("spectrumdevofficial/sd_fakeplate")
local plateChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
local function generatePlate()
local plate = ""
for i = 1, 8 do
local rand = math.random(1, #plateChars)
plate = plate..plateChars:sub(rand, rand)
end
return plate
end
local function generateMetadata(metadata, plate)
local newMetadata = metadata or {}
local plate = newMetadata.plate or plate or generatePlate()
newMetadata.plate = plate
newMetadata.label = plate
return newMetadata
end
local function getUsableScrewdriver(source)
local items = ox_inventory:Search(source, "slots", Config.Screwdriver.ItemName)
if #items == 0 then
return false, "screwdriver_missing"
end
for _, item in pairs(items) do
if not Config.Screwdriver.Durability.Enabled then
return true, item
end
item.metadata = item.metadata or {}
item.metadata.durability = item.metadata.durability or 100
local newDurability = item.metadata.durability - Config.Screwdriver.Durability.ReduceAmount
if newDurability >= 0 then
return true, item, newDurability
end
end
return false, "screwdriver_destroyed"
end
local function changePlate(source, slotId)
local playerPed = GetPlayerPed(source)
local playerCoords = GetEntityCoords(playerPed)
local vehicle, vehicleCoords = lib.getClosestVehicle(playerCoords, 5.0)
if not vehicle then
return false, "vehicle_not_found"
end
local hasScrewdriver, screwdriverData, newDurability = getUsableScrewdriver(source)
if not hasScrewdriver then
return false, screwdriverData
end
local slotData = ox_inventory:GetSlot(source, slotId)
if not slotData then
return false, "plate_missing"
elseif slotData.name ~= Config.Plate.ItemName then
return false, "plate_missing"
elseif not slotData.metadata.plate then
slotData.metadata = generateMetadata(slotData.metadata)
end
local netId = NetworkGetNetworkIdFromEntity(vehicle)
local animResult, animMessage = lib.callback.await("sd_fakeplate:playAnim", source, netId)
if not animResult then
return false, animMessage
end
local removeResult, response = ox_inventory:RemoveItem(source, slotData.name, 1, nil, slotData.slot)
if not removeResult then
return false, "remove_failed"
end
local curPlate = GetVehicleNumberPlateText(vehicle)
local newMetadata = generateMetadata(nil, curPlate)
local addResult, response = ox_inventory:AddItem(source, slotData.name, 1, newMetadata)
if not addResult then
return false, "add_failed"
end
if newDurability then
ox_inventory:SetDurability(source, screwdriverData.slot, newDurability)
end
local newPlate = slotData.metadata.plate
SetVehicleNumberPlateText(vehicle, newPlate)
ox_inventory:UpdateVehicle(curPlate, newPlate)
local vehicleState = Entity(vehicle).state
local realPlate = vehicleState.plate
if not realPlate then
realPlate = curPlate
Entity(vehicle).state.plate = realPlate
elseif realPlate == newPlate then
Entity(vehicle).state.plate = nil
end
return true, "plate_changed"
end
local BusyPlayers = {}
AddEventHandler("ox_inventory:usedItem", function(source, itemName, slotId, metadata)
if itemName ~= Config.Plate.ItemName then return end
if BusyPlayers[source] then return end
BusyPlayers[source] = true
local result, message = changePlate(source, slotId)
if message then
local notifyType = result and "success" or "error"
lib.notify(source, {
type = notifyType,
title = locale("notify_title"),
description = locale(message),
icon = "tarp",
})
end
BusyPlayers[source] = nil
end)
ox_inventory:registerHook("createItem", function(payload)
local metadata = payload.metadata
local newMetadata = generateMetadata(metadata)
return newMetadata
end, {
itemFilter = {
[Config.Plate.ItemName] = true
}
})
local currentResource = GetCurrentResourceName()
AddEventHandler("onResourceStop", function(resource)
if resource ~= currentResource then return end
ox_inventory:removeHooks()
end)