Permalink
Browse files

Added a chat callback library to starfall.

  • Loading branch information...
daranable committed Apr 15, 2012
1 parent 149d6fc commit f1007486df0768272b5206c52759dc978463a095
Showing with 58 additions and 0 deletions.
  1. +58 −0 lua/starfall/libs_sv/chat.lua
@@ -0,0 +1,58 @@
+---------------------------------------------------------------------
+-- SF chat parsing library
+-- @author Daranable
+---------------------------------------------------------------------
+-- @server
+local chat, _ = SF.Libraries.Register("chat")
+
+local cb_instances = {}
+
+cb_instances.global = SF.Callback.new()
+
+---------------------------------------------------------------------
+-- Internal Function
+---------------------------------------------------------------------
+
+hook.Add( "PlayerSay", "starfall_chat_receive", function( ply, msg, toall )
+ local hidden
+ for _, func in pairs(cb_instances.global.listeners) do
+ local success, hide = pcall( func, msg, ply )
+ local instance = func("STARFALL_GET_INSTANCE")
+
+ if not success then MsgN(hide) end
+
+ if hide and ply == instance.player then
+ hidden = true
+ end
+ end
+
+ if hidden then
+ return ""
+ else
+ return msg
+ end
+end)
+
+SF.Libraries.AddHook( "deinitialize", function( instance )
+ for _, v in pairs( instance.data.publicfuncs ) do
+ cb_instances.global:removeListener( v )
+ end
+end
+)
+
+
+---------------------------------------------------------------------
+-- Library functions
+---------------------------------------------------------------------
+
+--- Registers a new listener function to chat
+-- @param func the function that will listen to chat
+function chat.listen( func )
+ cb_instances.global:addListener( SF.WrapFunction(func) )
+end
+
+--- Removes listener from listening to chat
+-- @param func the function getting removed
+function chat.stop( func )
+ cb_instances.global:removeListener( SF.WrapFunction(func) )
+end

0 comments on commit f100748

Please sign in to comment.