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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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
--[[
    Project: SF.lua <https://github.com/imring/SF.lua>
    License: MIT License
    Author: imring
]]

local sampapi = require 'sampapi'
local shared = require 'sampapi.shared'
local ffi = shared.ffi

local netgame = sampapi.require('CNetGame', true)
local input = sampapi.require('CInput', true)
sampapi.require('CPlayerPool', true)

local function playerpool()
    return netgame.RefNetGame():GetPlayerPool()
end

function sampGetPlayerPoolPtr()
    return shared.get_pointer(playerpool())
end
jit.off(sampGetPlayerPoolPtr, true)

function sampIsPlayerConnected(id)
    return playerpool():IsConnected(id)
end
jit.off(sampIsPlayerConnected, true)

function sampGetPlayerNickname(id)
    if not sampIsPlayerConnected(id) then
        return ''
    end
    netgame.RefNetGame():UpdatePlayers()
    return ffi.string(playerpool():GetName(id))
end
jit.off(sampGetPlayerNickname, true)

function sampSpawnPlayer()
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:RequestSpawn()
    localplayer:Spawn()
end
jit.off(sampSpawnPlayer, true)

function sampSendChat(text)
    if text:byte(1) == 47 then -- character "/"
        input.RefInput():Send(text)
    else
        local localplayer = playerpool():GetLocalPlayer()
        playerpool():GetLocalPlayer():Chat(text)
    end
end
jit.off(sampSendChat, true)

function sampIsPlayerNpc(id)
    return sampIsPlayerConnected(id) and playerpool().m_pObject[id].m_bIsNPC == 1
end
jit.off(sampIsPlayerNpc, true)

function sampGetPlayerScore(id)
    if not sampIsPlayerConnected(id) then
        return 0
    end
    netgame.RefNetGame():UpdatePlayers()
    if id == sampGetLocalPlayerId() then
        return playerpool():GetLocalPlayerScore()
    end
    return playerpool():GetScore(id)
end
jit.off(sampGetPlayerScore, true)

function sampGetPlayerPing(id)
    if not sampIsPlayerConnected(id) then
        return 0
    end
    netgame.RefNetGame():UpdatePlayers()
    if id == sampGetLocalPlayerId() then
        return playerpool():GetLocalPlayerPing()
    end
    return playerpool():GetPing(id)
end
jit.off(sampGetPlayerPing, true)

function sampRequestClass(class)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:RequestClass(class)
end
jit.off(sampRequestClass, true)

function sampSendInteriorChange(id)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:ChangeInterior(id)
end
jit.off(sampSendInteriorChange, true)

function sampForceUnoccupiedSyncSeatId(id, seat)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:SendUnoccupiedData(id, seat)
end
jit.off(sampForceUnoccupiedSyncSeatId, true)

function sampGetCharHandleBySampPlayerId(id)
    if id == sampGetLocalPlayerId() then
        return true, PLAYER_PED
    elseif sampIsPlayerDefined(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            local ped = remoteplayer.m_pPed.m_pGamePed
            return true, getCharPointerHandle(shared.get_pointer(ped))
        end
    end
    return false, -1
end
jit.off(sampGetCharHandleBySampPlayerId, true)

function sampGetPlayerIdByCharHandle(handle)
    if handle == PLAYER_PED then
        return true, sampGetLocalPlayerId()
    end
    for i = 0, ffi.C.MAX_PLAYERS - 1 do
        local res, pped = sampGetCharHandleBySampPlayerId(i)
        if res and pped == handle then return true, i end
    end
    return false, -1
end
jit.off(sampGetPlayerIdByCharHandle, true)

function sampGetPlayerArmor(id)
    if id == sampGetLocalPlayerId() then
        return getCharArmour(PLAYER_PED)
    elseif sampIsPlayerDefined(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            return remoteplayer.m_fReportedArmour
        end
    end
    return 0
end
jit.off(sampGetPlayerArmor, true)

function sampGetPlayerHealth(id)
    if id == sampGetLocalPlayerId() then
        return getCharHealth(PLAYER_PED)
    elseif sampIsPlayerDefined(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            return remoteplayer.m_fReportedHealth
        end
    end
    return 0
end
jit.off(sampGetPlayerHealth, true)

function sampIsPlayerPaused(id)
    if id == sampGetLocalPlayerId() then
        -- TODO: CMenuManager?
        return false
    elseif sampIsPlayerConnected(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            return remoteplayer.m_nStatus == ffi.C.PLAYER_STATE_NONE
        end
    end
end
jit.off(sampIsPlayerPaused, true)

function sampSetSpecialAction(action)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:SetSpecialAction(action)
end
jit.off(sampSetSpecialAction, true)

function sampGetPlayerCount(streamed)
    if not streamed then
        return playerpool():GetCount(true)
    end

    local players = 0
    for i = 0, ffi.C.MAX_PLAYERS - 1 do
        if i ~= sampGetLocalPlayerId() then
            local res, ped = sampGetCharHandleBySampPlayerId(i)
            local bool = res and doesCharExist(ped)
            if bool then players = players + 1 end
        end
    end
    return players
end
jit.off(sampGetPlayerCount, true)

function sampGetMaxPlayerId(streamed)
    if not streamed then
        return playerpool().m_nLargestId
    end

    local mid = sampGetLocalPlayerId()
    for i = 0, ffi.C.MAX_PLAYERS - 1 do
        if i ~= sampGetLocalPlayerId() then
            local res, ped = sampGetCharHandleBySampPlayerId(i)
            local bool = res and doesCharExist(ped)
            if bool and i > mid then mid = i end
        end
    end
    return mid
end
jit.off(sampGetMaxPlayerId, true)

function sampGetPlayerSpecialAction(id)
    if id == sampGetLocalPlayerId() then
        local localplayer = playerpool():GetLocalPlayer()
        return localplayer:GetSpecialAction()
    elseif sampIsPlayerDefined(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            return remoteplayer.m_nSpecialAction
        end
    end
    return -1
end
jit.off(sampGetPlayerSpecialAction, true)

function sampStorePlayerOnfootData(id, data)
    local player
    if id == sampGetLocalPlayerId() then
        player = playerpool():GetLocalPlayer()
    elseif sampIsPlayerDefined(id) then
        player = playerpool():GetPlayer(id)
    end
    
    if player then
        ffi.copy(ffi.cast('void*', data), player.m_onfootData, ffi.sizeof(player.m_onfootData))
    end
end
jit.off(sampStorePlayerOnfootData, true)

function sampStorePlayerIncarData(id, data)
    local player
    if id == sampGetLocalPlayerId() then
        player = playerpool():GetLocalPlayer()
    elseif sampIsPlayerDefined(id) then
        player = playerpool():GetPlayer(id)
    end
    
    if player then
        ffi.copy(ffi.cast('void*', data), player.m_incarData, ffi.sizeof(player.m_incarData))
    end
end
jit.off(sampStorePlayerIncarData, true)

function sampStorePlayerPassengerData(id, data)
    local player
    if id == sampGetLocalPlayerId() then
        player = playerpool():GetLocalPlayer()
    elseif sampIsPlayerDefined(id) then
        player = playerpool():GetPlayer(id)
    end
    
    if player then
        ffi.copy(ffi.cast('void*', data), player.m_passengerData, ffi.sizeof(player.m_passengerData))
    end
end
jit.off(sampStorePlayerPassengerData, true)

function sampStorePlayerTrailerData(id, data)
    local player
    if id == sampGetLocalPlayerId() then
        player = playerpool():GetLocalPlayer()
    elseif sampIsPlayerDefined(id) then
        player = playerpool():GetPlayer(id)
    end
    
    if player then
        ffi.copy(ffi.cast('void*', data), player.m_trailerData, ffi.sizeof(player.m_trailerData))
    end
end
jit.off(sampStorePlayerTrailerData, true)

function sampStorePlayerAimData(id, data)
    local player
    if id == sampGetLocalPlayerId() then
        player = playerpool():GetLocalPlayer()
    elseif sampIsPlayerDefined(id) then
        player = playerpool():GetPlayer(id)
    end
    
    if player then
        ffi.copy(ffi.cast('void*', data), player.m_aimData, ffi.sizeof(player.m_aimData))
    end
end
jit.off(sampStorePlayerAimData, true)

function sampSendSpawn()
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:Spawn()
end
jit.off(sampSendSpawn, true)

function sampGetPlayerAnimationId(id)
    if id == sampGetLocalPlayerId() then
        local localplayer = playerpool():GetLocalPlayer()
        return localplayer.m_animation.m_value
    elseif sampIsPlayerDefined(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            return remoteplayer.m_animation.m_value
        end
    end
    return -1
end
jit.off(sampGetPlayerAnimationId, true)

function sampSetLocalPlayerName(name)
    playerpool():SetLocalPlayerName(name)
end
jit.off(sampSetLocalPlayerName, true)

function sampGetPlayerStructPtr(id)
    if id == sampGetLocalPlayerId() then
        local localplayer = playerpool():GetLocalPlayer()
        return shared.get_pointer(localplayer)
    elseif sampIsPlayerConnected(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        return shared.get_pointer(remoteplayer)
    end
    return 0
end
jit.off(sampGetPlayerStructPtr, true)

function sampSendEnterVehicle(id, passenger)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:EnterVehicle(id, passenger)
end
jit.off(sampSendEnterVehicle, true)

function sampSendExitVehicle(id)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer:ExitVehicle(id)
end
jit.off(sampSendExitVehicle, true)

function sampIsLocalPlayerSpawned()
    -- TODO: works fine?
    local localplayer = playerpool():GetLocalPlayer()
    return localplayer.m_bClearedToSpawn == 1 and localplayer.m_bHasSpawnInfo == 1 and ( localplayer.m_bIsActive == 1 or isCharDead(PLAYER_PED) )
end
jit.off(sampIsLocalPlayerSpawned, true)

function sampGetPlayerColor(id)
    if id == sampGetLocalPlayerId() then
        local localplayer = playerpool():GetLocalPlayer()
        return localplayer:GetColorAsARGB()
    elseif sampIsPlayerConnected(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            return remoteplayer:GetColorAsARGB()
        end
    end
    return 0
end
jit.off(sampGetPlayerColor, true)

function sampForceAimSync()
    local localplayer = playerpool():GetLocalPlayer()
    localplayer.m_lastAnyUpdate = 0 -- lol
    localplayer:SendAimData()
end
jit.off(sampForceAimSync, true)

function sampForceOnfootSync()
    local localplayer = playerpool():GetLocalPlayer()
    localplayer.m_lastAnyUpdate = 0
    localplayer:SendOnfootData()
end
jit.off(sampForceOnfootSync, true)

function sampForceStatsSync()
    local localplayer = playerpool():GetLocalPlayer()
    localplayer.m_lastAnyUpdate = 0
    localplayer:SendStats()
end
jit.off(sampForceStatsSync, true)

function sampForceTrailerSync(id)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer.m_lastAnyUpdate = 0
    localplayer:SendTrailerData(id)
end
jit.off(sampForceTrailerSync, true)

function sampForceVehicleSync(id)
    local localplayer = playerpool():GetLocalPlayer()
    localplayer.m_lastAnyUpdate = 0
    localplayer:SendIncarData(id)
end
jit.off(sampForceVehicleSync, true)

-- New functions

function sampGetLocalPlayerId()
    return playerpool().m_localInfo.m_nId
end
jit.off(sampGetLocalPlayerId, true)

function sampIsPlayerDefined(id)
    if id == sampGetLocalPlayerId() then
        local localplayer = playerpool():GetLocalPlayer()
        return localplayer ~= nil
    end

    if not sampIsPlayerConnected(id) then
        return false
    end
    local remoteplayer = playerpool():GetPlayer(id)
    if remoteplayer == nil then
        return false
    end
    return remoteplayer:DoesExist() ~= 0
end
jit.off(sampIsPlayerDefined, true)

function sampGetLocalPlayerNickname()
    return sampGetPlayerNickname(sampGetLocalPlayerId())
end

function sampGetLocalPlayerColor()
    return sampGetPlayerColor(sampGetLocalPlayerId())
end

function sampSetPlayerColor(id, color)
    if id == sampGetLocalPlayerId() then
        local localplayer = playerpool():GetLocalPlayer()
        return localplayer:SetColor(color)
    elseif sampIsPlayerConnected(id) then
        local remoteplayer = playerpool():GetPlayer(id)
        if remoteplayer ~= nil then
            return remoteplayer:SetColor(color)
        end
    end
end
jit.off(sampSetPlayerColor, true)