forked from CsokiHUN/fl_spectate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.lua
186 lines (154 loc) · 4.06 KB
/
client.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
183
184
185
186
Panel = {
visible = false,
selectedPlayer = false,
lastCoords = false,
init = function(self)
RegisterNUICallback("spectate", function(...)
self:spectate(...)
end)
RegisterNUICallback("kick", function(...)
self:kick(...)
end)
RegisterNUICallback("close", function(...)
self:close(...)
end)
RegisterNUICallback("update", function()
self:update(false)
end)
RegisterNUICallback("spectateoff", function()
self:spectateoff()
end)
end,
open = function(self, players)
self.visible = true
self:update(players)
SetNuiFocus(true, true)
SendNUIMessage({
visible = true,
})
end,
close = function(self)
self.visible = false
SetNuiFocus(false, false)
SendNUIMessage({
visible = false,
})
end,
update = function(self, players)
if not players or type(players) == "boolean" then
ESX.TriggerServerCallback("requestServerPlayers", function(players)
if not players then
return self:close()
end
SendNUIMessage({
players = players,
})
end)
return
end
SendNUIMessage({
players = players,
})
end,
spectate = function(self, data, cb)
if self.selectedPlayer then
return
end
local localPed = PlayerPedId()
self.lastCoords = GetEntityCoords(localPed)
local serverId = tonumber(data.player.serverId)
if serverId == GetPlayerServerId(PlayerId()) then
return TriggerEvent("chat:addMessage", {
args = { "Server", "You can't spectate yourself." },
color = { 0, 255, 0 },
})
end
self.selectedPlayer = serverId
ESX.TriggerServerCallback("requestPlayerCoords", function(coords)
if not coords then
self.lastCoords = false
self.selectedPlayer = false
return
end
CreateThread(updatePlayerInfo)
RequestCollisionAtCoord(coords)
SetEntityVisible(localPed, false)
SetEntityCoords(localPed, coords + vector3(0, 0, 10))
FreezeEntityPosition(localPed, true)
Wait(1500)
SetEntityCoords(localPed, coords - vector3(0, 0, 10))
local targetPed = GetPlayerPed(GetPlayerFromServerId(serverId))
NetworkSetInSpectatorMode(true, targetPed)
end, serverId)
self:close()
end,
kick = function(self, data, cb)
if (not data.player or not data.player.serverId) or not data.reason then
return cb('ok')
end
ESX.TriggerServerCallback("kickPlayerSpectate", function(players)
if not players then
return self:close()
end
self:update(players)
cb('ok')
end, tonumber(data.player.serverId), data.reason)
end,
spectateoff = function(self)
self.selectedPlayer = false
if self.lastCoords then
local localPed = PlayerPedId()
RequestCollisionAtCoord(self.lastCoords)
NetworkSetInSpectatorMode(false, localPed)
FreezeEntityPosition(localPed, false)
SetEntityCoords(localPed, self.lastCoords)
SetEntityVisible(localPed, true)
end
SendNUIMessage({
playerInfo = false,
})
end,
}
Panel.__index = Panel
Panel:init()
RegisterNetEvent("openSpectateMenu", function(players)
if Panel.visible then
return
end
Panel:open(players)
end)
function updatePlayerInfo()
while Panel.selectedPlayer do
local playerId = GetPlayerFromServerId(Panel.selectedPlayer)
local targetPed = GetPlayerPed(playerId)
SendNUIMessage({
playerInfo = {
"GodMode: "
.. (GetPlayerInvincible(Panel.selectedPlayer) and RED .. "Enabled" or GREEN .. "Disabled")
.. "</span>",
"AntiRagdoll: "
.. (not CanPedRagdoll(targetPed) and RED .. "Enabled" or GREEN .. "Disabled")
.. "</span>",
"Health: " .. GetEntityHealth(targetPed) .. "/" .. GetEntityMaxHealth(targetPed),
"Armor: " .. GetPedArmour(targetPed) .. "/" .. GetPlayerMaxArmour(playerId),
},
})
Wait(1000)
end
end
RegisterCommand("spectateoff", function()
if Panel.selectedPlayer then
TriggerEvent("chat:addMessage", {
args = { "Server", "Spectate turned off" },
color = { 0, 255, 0 },
})
Panel:spectateoff()
end
end)
RegisterKeyMapping("spectateoff", "Spectate Kikapcsolas", "keyboard", "e")
exports('spectatePlayer', function(serverId)
Panel:spectate({player = { serverId = serverId }})
end)
exports("isActive", function()
return Panel.selectedPlayer
end)