Skip to content

Instantly share code, notes, and snippets.

@Earu
Created July 25, 2021 13:33
Show Gist options
  • Save Earu/2aaeadc79bb91ced40076bbc68be48c0 to your computer and use it in GitHub Desktop.
Save Earu/2aaeadc79bb91ced40076bbc68be48c0 to your computer and use it in GitHub Desktop.
POC for detecting native PlayerSay calls and fowarding them to EasyChat's own networking (only works in sandbox derived gamemodes).
--[[
---------------------------------------------------------------------
THIS ONLY WORKS IN SANDBOX, DARKRP DOES WEIRD STUFF WITH ITS
GAMEMODE PLAYERSAY HOOK SO IT WONT WORK (SAME WITH MURDER)
---------------------------------------------------------------------
--]]
hook.Add("PostGamemodeLoaded", TAG, function()
local existing_callbacks = hook.GetTable().PlayerSay or {}
for identifier, callback in pairs(existing_callbacks) do
hook.Remove("PlayerSay", identifier)
hook.Add("ECPlayerSay", identifier, callback)
end
hook.NativeAdd = hook.NativeAdd or hook.Add
function hook.Add(event_name, ...)
if event_name == "PlayerSay" then
event_name = "ECPlayerSay"
end
hook.NativeAdd(event_name, ...)
end
hook.NativeCall = hook.NativeCall or hook.Call
function hook.Call(event_name, ...)
if event_name == "PlayerSay" then
event_name = "ECPlayerSay"
end
return hook.NativeCall(event_name, ...)
end
-- handle messages that are run by the engine (usually the say or say_team commands)
hook.NativeAdd("PlayerSay", TAG, function(ply, msg, is_team, is_local)
if not IsValid(ply) then return end -- for console just let source handle it I guess
EasyChat.ReceiveGlobalMessage(ply, msg, is_team, is_local or false)
return "" -- we handled it dont network it back the source way
end)
-- make the default behavior follow the gamemode PlayerSay one
function GAMEMODE:ECPlayerSay(ply, msg, is_team, is_local)
return self:PlayerSay(ply, msg, is_team, is_local)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment