-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
255 lines (210 loc) · 7.17 KB
/
Copy pathclient.lua
File metadata and controls
255 lines (210 loc) · 7.17 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
--[[
Vehicle Spawner v2.0
Author: choda
]]
local menuOpen = false
local currentView = 'categories' -- 'categories' or 'vehicles'
local selectedIndex = 1
local selectedCategory = nil
local lastVehicle = nil
local allowedCategories = {} -- categories this player is allowed to see, filtered server-side
-- ===================== Helpers =====================
local function ShowNotification(msg, notifyType)
exports['mythic_notify']:SendAlert(notifyType or 'inform', msg, Config.NotifyDuration)
end
local function DrawText2D(x, y, text, scale, r, g, b, a, centered)
SetTextFont(4)
SetTextScale(scale, scale)
SetTextColour(r, g, b, a)
SetTextOutline()
SetTextEntry('STRING')
AddTextComponentString(text)
SetTextCentre(centered or false)
DrawText(x, y)
end
local function DrawRect2D(x, y, w, h, r, g, b, a)
DrawRect(x, y, w, h, r, g, b, a)
end
-- ===================== Vehicle Spawning =====================
local function SpawnVehicle(model)
local hash = GetHashKey(model)
if not IsModelInCdimage(hash) or not IsModelAVehicle(hash) then
ShowNotification('Invalid vehicle model: ' .. model, 'error')
return
end
RequestModel(hash)
local timeout = 0
while not HasModelLoaded(hash) and timeout < 5000 do
Wait(10)
timeout = timeout + 10
end
if not HasModelLoaded(hash) then
ShowNotification('Failed to load vehicle model: ' .. model, 'error')
return
end
if Config.DeletePreviousVehicle and lastVehicle and DoesEntityExist(lastVehicle) then
DeleteEntity(lastVehicle)
lastVehicle = nil
end
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
local heading = GetEntityHeading(ped)
local fwd = GetEntityForwardVector(ped)
local spawnPos = vector3(
coords.x + fwd.x * Config.SpawnDistance,
coords.y + fwd.y * Config.SpawnDistance,
coords.z
)
local veh = CreateVehicle(hash, spawnPos.x, spawnPos.y, spawnPos.z, heading, true, false)
SetEntityAsMissionEntity(veh, true, true)
SetVehicleOnGroundProperly(veh)
SetVehicleHasBeenOwnedByPlayer(veh, true)
SetVehicleNeedsToBeHotwired(veh, false)
SetVehicleDoorsLocked(veh, 1)
if Config.SetPlate then
SetVehicleNumberPlateText(veh, Config.PlatePrefix .. tostring(math.random(1000, 9999)))
end
if Config.SpawnInVehicle then
TaskWarpPedIntoVehicle(ped, veh, -1)
end
lastVehicle = veh
SetModelAsNoLongerNeeded(hash)
ShowNotification('Vehicle spawned: ' .. model, 'success')
end
local function DeleteCurrentVehicle()
local ped = PlayerPedId()
local veh = GetVehiclePedIsIn(ped, false)
if veh ~= 0 then
DeleteEntity(veh)
ShowNotification('Vehicle deleted.', 'success')
elseif lastVehicle and DoesEntityExist(lastVehicle) then
DeleteEntity(lastVehicle)
lastVehicle = nil
ShowNotification('Vehicle deleted.', 'success')
else
ShowNotification('No vehicle to delete.', 'error')
end
end
-- ===================== Menu =====================
local function OpenMenu(categories)
allowedCategories = categories or {}
menuOpen = true
currentView = 'categories'
selectedIndex = 1
end
local function CloseMenu()
menuOpen = false
end
local function GetCategoryList()
return allowedCategories
end
local function GetVehicleList(category)
local list = {}
for _, v in ipairs(Config.Vehicles[category].vehicles) do
list[#list + 1] = v.label
end
return list
end
local function RenderMenu()
local x = 0.85
local startY = 0.30
local rowHeight = 0.032
local width = 0.20
local entries = (currentView == 'categories') and GetCategoryList() or GetVehicleList(selectedCategory)
DrawRect2D(x, startY - 0.028, width + 0.02, 0.045, 10, 10, 10, 210)
DrawText2D(x, startY - 0.043, Config.MenuTitle, 0.38, 255, 255, 255, 255, true)
for i, label in ipairs(entries) do
local y = startY + (i - 1) * rowHeight
local r, g, b = 255, 255, 255
if i == selectedIndex then
DrawRect2D(x, y, width + 0.02, rowHeight - 0.002, 255, 255, 255, 130)
r, g, b = 0, 0, 0
else
DrawRect2D(x, y, width + 0.02, rowHeight - 0.002, 20, 20, 20, 170)
end
DrawText2D(x, y - 0.011, label, 0.3, r, g, b, 255, true)
end
DrawText2D(x, startY + (#entries * rowHeight) + 0.012, '~g~ENTER~s~ Select ~r~BACKSPACE~s~ Back/Close', 0.26, 255, 255, 255, 255, true)
return entries
end
local function HandleSelect(entries)
local label = entries[selectedIndex]
if not label then return end
if currentView == 'categories' then
selectedCategory = label
currentView = 'vehicles'
selectedIndex = 1
else
local vehData = nil
for _, v in ipairs(Config.Vehicles[selectedCategory].vehicles) do
if v.label == label then
vehData = v
break
end
end
if vehData then
if vehData.model == 'delete' then
DeleteCurrentVehicle()
else
SpawnVehicle(vehData.model)
end
end
CloseMenu()
end
end
local function HandleBack()
if currentView == 'vehicles' then
currentView = 'categories'
selectedIndex = 1
else
CloseMenu()
end
end
-- ===================== Input / Open Command =====================
RegisterKeyMapping('vehiclespawner', 'Open Vehicle Spawner Menu', 'keyboard', Config.OpenKey)
RegisterCommand('vehiclespawner', function()
if menuOpen then
CloseMenu()
return
end
TriggerServerEvent('vehiclespawner:requestOpen')
end, false)
RegisterNetEvent('vehiclespawner:openMenu', function(categories)
OpenMenu(categories)
end)
RegisterNetEvent('vehiclespawner:notify', function(msg, notifyType)
ShowNotification(msg, notifyType)
end)
-- Frontend controls (up/down/accept/cancel) used to drive the menu
local CONTROL_UP = 172
local CONTROL_DOWN = 173
local CONTROL_ACCEPT = 176
local CONTROL_CANCEL = 177
CreateThread(function()
while true do
if menuOpen then
DisableControlAction(0, CONTROL_UP, true)
DisableControlAction(0, CONTROL_DOWN, true)
DisableControlAction(0, CONTROL_ACCEPT, true)
DisableControlAction(0, CONTROL_CANCEL, true)
local entries = RenderMenu()
if IsDisabledControlJustPressed(0, CONTROL_UP) then
selectedIndex = selectedIndex - 1
if selectedIndex < 1 then selectedIndex = #entries end
elseif IsDisabledControlJustPressed(0, CONTROL_DOWN) then
selectedIndex = selectedIndex + 1
if selectedIndex > #entries then selectedIndex = 1 end
elseif IsDisabledControlJustPressed(0, CONTROL_ACCEPT) then
HandleSelect(entries)
elseif IsDisabledControlJustPressed(0, CONTROL_CANCEL) then
HandleBack()
end
Wait(0)
else
Wait(200)
end
end
end)
CreateThread(function()
print('^2[vehiclespawner]^7 Vehicle Spawner v2.0 by ^3choda^7 loaded. Default key: ' .. Config.OpenKey)
end)