All pastes #91291 Raw Edit

Mine

public lua v1 · immutable
#91291 ·published 2006-07-18 13:31 UTC
rendered paste body
ARG_STRING = 0;ARG_END = 1;--quick constants/definesg_tCommands = 	{					--{name="ban",func=cc_ban, args={ARG_STRING, ARG_STRING, ARG_END}},					--{name="kick",func=cc_kick, args={ARG_END}} --quick placeholders here.				}; --TODO: ADD MACRO SUPPORT. Note: NO SPACES IN COMMANDS.g_tPrefixs = 	{					"/",										"//"				} --possible prefixes to commands. NO SPACES. BE AWARE OF CONTROL CHARACTERS:  http://www.lua.org/manual/5.1/manual.html#5.4.1 Use % to escape them								function REGISTER_COMMAND( strName, pfunc, ...)	local iNum = table.getn(g_tCommands)+1;	g_tCommands[iNum].name = string.lower(strName);	g_tCommands[iNum].func = pfunc;		g_tCommands[iNum].args = {}		local nArg = 1;	for k,v in arg do			g_tCommands[iNum].args[nArg] = v;			nArg = nArg+1;	endendREGISTER_COMMAND( "ban", cc_ban, ARG_STRING, ARG_STRING, ARG_END);REGISTER_COMMAND( "kick", cc_kick, ARG_END);function ParseArgs( userid, strText, tCommand) --can be used for concommands too, and any way of inputting text.	local argList = {} --Hold all the arg's.		for k,v in tCommand.args do 			--If only we could have switch statements in lua. :(				if(v==ARG_END) then					argList[table.getn(argList)+1] = strText;				break; --No more arg's		elseif(v==ARG_STRING) then			local subString = string.sub(strText, 1, string.find(strText, "%s")); --next word			subString = string.gsub(subString, " ", "", 1); --remove the space from the substring.			argList[table.getn(argList)+1] = tostring(subString);			strText = string.gsub(strText, subString.." ", "", 1);				else			--could add others.			--Make sure you add "something" to the table entry 'cause if you add nil it skips it and all following arg's are out by one.							--unknown arg type. Error message?					end		if( strText == "") then			break; --No more text, break out the loop.				end	end		if( table.getn(argList) > 0) then		_Msg(tCommand.name .. " was called!!!\n");		--tCommand.func(userid, argList);	-- Need to add functions before you un-comment this otherwise its an error.		for k,v in argList do					_Msg(tostring(v)..", ");					end		_Msg("END OF ARGS\n");	endend								function Client_Command( userid, strText ) --can be used for concommands too.			local bCommand = false;		strText = string.lower(strText); --Possible problems as aremoves case sensitivity for args.		for k,v in g_tCommands do --look for commands.				local cName = v.name		local tArg = string.find(strText,cName.." ")		if( tArg ) then --Found the command			if( tArg == 1) then --in correct place??							bCommand = true;				strText = string.gsub(strText, cName.." ", "", 1); --remove command and trailing space.				--v.func( userid, strText); --ARG PARSING NEEDED							ParseArgs( userid, strText, v);							break;								else				bCommand = false;			end				end				end	--for k,v in g_tCommands do					return bCommand;end-- Add error messages??function eventPlayerSay( userid, strText, bTeam ) --Is team chat important in roleplay??	local sOrig_text = strText; --we mutilate the original string, so just keep a copy of it.	strText = strText .. " "; --arg passing etc needs a trailing space.		while( string.find(strText, "%s%s", 1) ) do --double spaces probably mess up the args.				local sNum = string.find(strText, "%s%s", 1); --find double spaces, returns the character of the first space.		strText = string.sub( strText, 1, sNum-1) .. string.sub( strText, sNum+1); --Must be a better way to do this?!			end		local sPlayerName = _PlayerInfo (userid, "name")..": "; --the only engine binding used in the whole script :P.	strText = string.gsub(strText, sPlayerName, "", 1); --Take out the Player name, and the bit after it.		local bPrefix = false;		for k,v in g_tPrefixs do				local tArg = string.find(strText,v.."%a",1)	--prefix and then any letter. NOT NUMBERS.				if( tArg ) then --Found the prefix						if( tArg == 1) then --in correct place?? (starts at character 1)							bPrefix = true;				strText = string.gsub(strText, v, "", 1); --remove prefix and trailing space.							break;								else				bPrefix = false; --dont break because more prefix's to loop through							end				end				end 	-- for k,v in g_tPrefixs do			if (bPrefix) then		_Msg("Found Prefix\n");				if (Client_Command( userid, strText)) then --this way we can call a command from the console for debug instead of calling eventplayersay.			return ""; 		else			_Msg("No command found\n");		end	end	--Prefixs will only ever be in speech, though for debug stuff we may want to call the command. This allows us to do so.				--No command, carry on as usual. 	return sOrig_text;	end