-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
364 lines (306 loc) · 11.1 KB
/
main.lua
File metadata and controls
364 lines (306 loc) · 11.1 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
-- ============================================================
-- Entity Inspector | client/main.lua
-- Ace permission: motion_entityselector.use
-- ============================================================
local isAdmin = false
local panelOpen = false
local selectedEnt = nil
local scanning = false
local pressedAt = 0
local wasScanning = false
-- -- Key binding -----------------------------------------------
-- RegisterKeyMapping is the only correct way to detect MMB in FiveM.
-- Control 96 = INPUT_VEH_DUCK, NOT middle mouse.
RegisterCommand('+mmb_scan', function()
if panelOpen then return end
scanning = true
pressedAt = GetGameTimer()
end, false)
RegisterCommand('-mmb_scan', function()
scanning = false
end, false)
RegisterKeyMapping('+mmb_scan', 'Entity Inspector Scan', 'MOUSE_BUTTON', 'MOUSE_MIDDLE')
-- -- Resource lifecycle ----------------------------------------
AddEventHandler('onClientResourceStart', function(res)
if res ~= GetCurrentResourceName() then return end
SetNuiFocus(false, false)
SendNUIMessage({ type = 'close', data = {} })
panelOpen = false
selectedEnt = nil
scanning = false
wasScanning = false
TriggerServerEvent('motion_entityselector:checkAdmin')
end)
AddEventHandler('onClientResourceStop', function(res)
if res ~= GetCurrentResourceName() then return end
SetNuiFocus(false, false)
SendNUIMessage({ type = 'close', data = {} })
end)
RegisterNetEvent('motion_entityselector:setAdmin', function(state)
isAdmin = state
print(isAdmin
and "^2[EntitySelector] Admin access granted.^7"
or "^3[EntitySelector] No admin access.^7")
end)
-- -- Helpers --------------------------------------------------
local function getTypeName(entity)
local t = GetEntityType(entity)
if t == 1 then return "Ped"
elseif t == 2 then return "Vehicle"
elseif t == 3 then return "Object"
else return "Unknown" end
end
local function f2(n) return math.floor((n or 0) * 100) / 100 end
local function f1(n) return math.floor((n or 0) * 10) / 10 end
-- -- Raycast ---------------------------------------------------
local function raycast()
local cam = GetGameplayCamCoords()
local rot = GetGameplayCamRot(2)
local fwd = vector3(
math.sin(-math.rad(rot.z)) * math.cos(math.rad(rot.x)),
math.cos(-math.rad(rot.z)) * math.cos(math.rad(rot.x)),
math.sin( math.rad(rot.x))
)
local dest = cam + fwd * 50.0
local ray = StartShapeTestRay(
cam.x, cam.y, cam.z,
dest.x, dest.y, dest.z,
-1, PlayerPedId(), 4
)
local _, hit, _, _, entity = GetShapeTestResult(ray)
if hit == 1 and entity and entity ~= 0
and DoesEntityExist(entity)
and entity ~= PlayerPedId()
then
return entity
end
return nil
end
-- -- Bounding box ---------------------------------------------
-- Confirmed correct signature from FiveM docs:
-- GetEntityMatrix returns: forwardVector, rightVector, upVector, position
-- All four are proper vector3 values with .x .y .z
local function drawBoundingBox(entity)
if not (entity and DoesEntityExist(entity)) then return end
-- Guard the whole thing streaming objects can go invalid mid-call
local ok, err = pcall(function()
local model = GetEntityModel(entity)
if not model or model == 0 then return end
local min, max = GetModelDimensions(model)
-- FiveM confirmed order: forward, right, up, pos
local fwd, right, up, pos = GetEntityMatrix(entity)
local function corner(lx, ly, lz)
-- lx = right axis, ly = forward axis, lz = up axis
return vector3(
pos.x + right.x*lx + fwd.x*ly + up.x*lz,
pos.y + right.y*lx + fwd.y*ly + up.y*lz,
pos.z + right.z*lx + fwd.z*ly + up.z*lz
)
end
local c1 = corner(min.x, min.y, min.z)
local c2 = corner(max.x, min.y, min.z)
local c3 = corner(max.x, max.y, min.z)
local c4 = corner(min.x, max.y, min.z)
local c5 = corner(min.x, min.y, max.z)
local c6 = corner(max.x, min.y, max.z)
local c7 = corner(max.x, max.y, max.z)
local c8 = corner(min.x, max.y, max.z)
local r, g, b, a = 0, 210, 255, 255
-- Bottom face
DrawLine(c1.x,c1.y,c1.z, c2.x,c2.y,c2.z, r,g,b,a)
DrawLine(c2.x,c2.y,c2.z, c3.x,c3.y,c3.z, r,g,b,a)
DrawLine(c3.x,c3.y,c3.z, c4.x,c4.y,c4.z, r,g,b,a)
DrawLine(c4.x,c4.y,c4.z, c1.x,c1.y,c1.z, r,g,b,a)
-- Top face
DrawLine(c5.x,c5.y,c5.z, c6.x,c6.y,c6.z, r,g,b,a)
DrawLine(c6.x,c6.y,c6.z, c7.x,c7.y,c7.z, r,g,b,a)
DrawLine(c7.x,c7.y,c7.z, c8.x,c8.y,c8.z, r,g,b,a)
DrawLine(c8.x,c8.y,c8.z, c5.x,c5.y,c5.z, r,g,b,a)
-- Vertical edges
DrawLine(c1.x,c1.y,c1.z, c5.x,c5.y,c5.z, r,g,b,a)
DrawLine(c2.x,c2.y,c2.z, c6.x,c6.y,c6.z, r,g,b,a)
DrawLine(c3.x,c3.y,c3.z, c7.x,c7.y,c7.z, r,g,b,a)
DrawLine(c4.x,c4.y,c4.z, c8.x,c8.y,c8.z, r,g,b,a)
end)
if not ok then
-- Entity went invalid mid-draw, silently skip
end
end
-- -- Floating label --------------------------------------------
local function drawLabel(entity)
if not (entity and DoesEntityExist(entity)) then return end
local pos = GetEntityCoords(entity, false)
-- World3dToScreen2d returns (bool, float, float) but in some FiveM builds
-- the bool is 0/1 not true/false, so test both.
local onScreen, sx, sy = World3dToScreen2d(pos.x, pos.y, pos.z + 1.5)
if not onScreen or onScreen == 0 or not sx or not sy then return end
if sx ~= sx or sy ~= sy then return end -- NaN guard
local cam = GetGameplayCamCoords()
local dist = #(pos - cam)
if dist < 0.01 then return end
local scale = math.min(0.5, math.max(0.22, (1.0 / dist) * 2.5))
-- Resolve the name fully before touching any native text-pipeline state.
local name = "unknown"
pcall(function()
local n = GetEntityArchetypeName(entity)
if type(n) == "string" and n ~= "" then
name = n
else
local h = GetEntityModel(entity)
if h and h ~= 0 then
name = string.format("0x%08X", h)
end
end
end)
-- Wrap the entire Begin/Add/End chain so that if any native crashes it
-- doesn't leave the text command pipeline in a broken state.
pcall(function()
SetTextScale(scale, scale)
SetTextFont(4)
SetTextColour(0, 210, 255, 255)
SetTextOutline()
SetTextCentre(true)
BeginTextCommandDisplayText("STRING")
AddTextComponentSubstringPlayerName(name)
EndTextCommandDisplayText(sx, sy)
end)
end
-- -- Collect data ----------------------------------------------
local function collectData(entity)
if not DoesEntityExist(entity) then return nil end
local pos = GetEntityCoords(entity, true)
local entType = getTypeName(entity)
local model = "unknown"
pcall(function()
local n = GetEntityArchetypeName(entity)
if n and n ~= "" then model = n
else model = string.format("0x%08X", GetEntityModel(entity)) end
end)
local health, maxHp, heading = 0, 200, 0.0
pcall(function()
health = GetEntityHealth(entity)
maxHp = GetEntityMaxHealth(entity)
heading = GetEntityHeading(entity)
end)
local isNet = false
local netIdStr, ownerStr = nil, "N/A"
pcall(function()
isNet = NetworkGetEntityIsNetworked(entity)
if isNet then
local ok, nid = pcall(NetworkGetNetworkIdFromEntity, entity)
if ok and nid and nid > 0 then netIdStr = tostring(nid) end
local ok2, oid = pcall(NetworkGetEntityOwner, entity)
if ok2 and oid and oid >= 0 then
ownerStr = GetPlayerName(oid) or ("Player " .. tostring(oid))
end
end
end)
local modelHash = " "
pcall(function() modelHash = tostring(GetEntityModel(entity)) end)
local d = {
modelName = model,
entityType = entType,
handle = entity,
netId = netIdStr,
modelHash = modelHash,
health = health,
maxHealth = maxHp,
heading = f1(heading),
pos = { x = f2(pos.x), y = f2(pos.y), z = f2(pos.z) },
networked = isNet,
owner = ownerStr,
extras = {},
}
if entType == "Vehicle" then
pcall(function()
d.extras = {
body = math.floor(GetVehicleBodyHealth(entity)),
engine = math.floor(GetVehicleEngineHealth(entity)),
tank = math.floor(GetVehiclePetrolTankHealth(entity)),
}
end)
elseif entType == "Ped" then
pcall(function()
d.extras = {
armour = GetPedArmour(entity),
isPlayer = IsPedAPlayer(entity),
}
end)
end
return d
end
-- -- Panel -----------------------------------------------------
local function openPanel(ent)
panelOpen = true
selectedEnt = ent
SetNuiFocus(true, true)
local d = collectData(ent)
if d then SendNUIMessage({ type = 'open', data = d }) end
end
local function closePanel()
panelOpen = false
selectedEnt = nil
SetNuiFocus(false, false)
SendNUIMessage({ type = 'close', data = {} })
end
-- -- SCAN LOOP -------------------------------------------------
-- Wait(50) = 20 ticks/sec. Hold MMB to see box. Release to open panel.
CreateThread(function()
local hover = nil
while true do
Wait(50)
if not isAdmin or panelOpen then
hover = nil
wasScanning = false
goto continue
end
if scanning then
wasScanning = true
hover = raycast()
if hover and DoesEntityExist(hover) then
drawBoundingBox(hover)
drawLabel(hover)
else
hover = nil
end
else
if wasScanning then
wasScanning = false
if hover and DoesEntityExist(hover) then
openPanel(hover)
end
hover = nil
end
end
::continue::
end
end)
-- -- Live refresh ----------------------------------------------
CreateThread(function()
while true do
Wait(500)
if not panelOpen then goto skip end
if selectedEnt and DoesEntityExist(selectedEnt) then
local d = collectData(selectedEnt)
if d then SendNUIMessage({ type = 'update', data = d }) end
elseif selectedEnt then
closePanel()
end
::skip::
end
end)
-- -- ESC close -------------------------------------------------
CreateThread(function()
while true do
Wait(0)
if panelOpen and IsControlJustReleased(0, 200) then
closePanel()
end
end
end)
-- -- NUI Callbacks ---------------------------------------------
RegisterNUICallback('close', function(_, cb)
closePanel()
cb('ok')
end)
print("^2[EntitySelector] Loaded Hold MMB to highlight | Release to inspect | Ace: motion_entityselector.use^7")