<commit_before><commit_msg>Added a chat callback library to starfall.<commit_after>---------------------------------------------------------------------
-- 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
{'fraction_non_alphanumeric': 0.3207885304659498, 'fraction_numerical': 0.0, 'mean_word_length': 1.5037369207772795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083033', 'n_tokens_mistral': 426, 'n_tokens_neox': 395, 'n_words': 128}