script_author('foxpro')
script_name('ChatID')
local sampev = require 'lib.samp.events'
local players = {}
local enable = true
local myId = -1
function main()
if not isSampfuncsLoaded() or not isSampLoaded() then
return
end
while not isSampAvailable() do
wait(1)
end
while sampGetGamestate() < 3 do
wait(1)
end
players = getAllSampPlayers()
sampRegisterChatCommand(
'chatid',
function()
enable = not enable
sampAddChatMessage(enable and 'ID ников включены' or 'ID ников выключены', -1)
end
)
wait(-1)
end
function sampev.onInitGame(playerId)
myId = playerId
end
function sampev.onServerMessage(color, text)
if enable then
local toChange = false
for id, name in pairs(players) do
if string.find(text, name) and not string.find(text, string.format('%s%%[%u%%]', name, id)) then
text = string.gsub(text, name, string.format('%s[%d]', name, id))
toChange = true
end
end
if toChange then
return {color, text}
end
end
end
function onReceiveRpc(id, bs)
if id == 137 then
local id = raknetBitStreamReadInt16(bs)
raknetBitStreamIgnoreBits(bs, 40)
local nickLen = raknetBitStreamReadInt8(bs)
local name = raknetBitStreamReadString(bs, nickLen)
players[id] = name
end
if id == 138 then
local id = raknetBitStreamReadInt16(bs)
players[id] = nil
end
end
function getAllSampPlayers()
players = {}
for i = 0, sampGetMaxPlayerId() do
if sampIsPlayerConnected(i) or i == myId then
players[i] = sampGetPlayerNickname(i)
end
end
return players
end