/*
	nCom
	Date: 22.07.2011
	Author: NeoPhoenix
	Latest Update:
		- 04.12.2011
		 Adding CMDFloodprotection for PNWL
*/
#if defined included_ncom
	#endinput
#else
	#define included_ncom
#endif
#include <a_samp>
forward nOnPlayerCommandText(playerid,cmdtext[]);
public OnPlayerCommandText(playerid, cmdtext[])
{
	/*if(PlayerInfo[playerid][IsInCRoom] != 0 || !PlayerEvent[playerid][LOGGED_IN] || GetPVarInt(playerid,"onplayercmd_muted") == 1) 
	{//Hat die CMDFloodgrenze überschritten, ist nicht eingeloggt oder befindet sich im CRaum
		return 1;
	}*/
	
	if(strlen(cmdtext) > 1)
	{
		new command[144],params[144];
		new endpos = strfind(cmdtext," ");
		if(endpos != -1)
		{
		    if(strlen(cmdtext) > endpos) strmid(params,cmdtext,endpos+1,strlen(cmdtext));
			strmid(command,cmdtext,1,endpos);
		}
		else strmid(command,cmdtext,1,strlen(cmdtext));
		format(command,144,"ncom_%s",command);
		CallRemoteFunction(command,"ds",playerid,params[0]?params:"\n");
		if(funcidx(command) == -1) SendClientMessage(playerid,ROT,"Command nicht vorhanden");//OnPlayerText(playerid,cmdtext);
		else
		{
			//Commandfloodprotection
			new cmds = GetPVarInt(playerid,"onplayercmd_ms");
			SetPVarInt(playerid,"onplayercmd_ms",(cmds+1));
			if((cmds+1) >= MAX_CMDS_PER_10_SECONDS)
			{
				SetPVarInt(playerid,"onplayercmd_muted",MAX_MUTE_TIME);
				format(query,sizeof(query),"CMDFlood: Du hast in 5 Sekunden mehr als %d Commands eingegeben. Bitte warte ein wenig...",MAX_CMDS_PER_10_SECONDS);
				SendClientMessage(playerid,0x77AE35FF,query);
			}
		}
		return 1;
	}
	CallLocalFunction("OnPlayerText","ds",playerid,cmdtext);
	return 1;
}

#define ncom:%0(%1) forward ncom_%0(%1);\
public ncom_%0(%1)
#define has_params(%0) ((%0[0] && %0[0] != '\n'))
#define OnPlayerCommandText nOnPlayerCommandText

stock sscanf(ss_string[], format[], {Float,_}:...)
{
	#if defined isnull
		if (isnull(ss_string))
	#else
		if (ss_string[0] == 0 || (ss_string[0] == 1 && ss_string[1] == 0))
	#endif
		{
			return format[0];
		}
	#pragma tabsize 4
	new
		formatPos = 0,
		ss_stringPos = 0,
		paramPos = 2,
		paramCount = numargs(),
		delim = ' ';
	while (ss_string[ss_stringPos] && ss_string[ss_stringPos] <= ' ')
	{
		ss_stringPos++;
	}
	while (paramPos < paramCount && ss_string[ss_stringPos])
	{
		switch (format[formatPos++])
		{
			case '\0':
			{
				return 0;
			}
			case 'i', 'd':
			{
				new
					neg = 1,
					num = 0,
					ch = ss_string[ss_stringPos];
				if (ch == '-')
				{
					neg = -1;
					ch = ss_string[++ss_stringPos];
				}
				do
				{
					ss_stringPos++;
					if ('0' <= ch <= '9')
					{
						num = (num * 10) + (ch - '0');
					}
					else
					{
						return -1;
					}
				}
				while ((ch = ss_string[ss_stringPos]) > ' ' && ch != delim);
				setarg(paramPos, 0, num * neg);
			}
			case 'h', 'x':
			{
				new
					num = 0,
					ch = ss_string[ss_stringPos];
				do
				{
					ss_stringPos++;
					switch (ch)
					{
						case 'x', 'X':
						{
							num = 0;
							continue;
						}
						case '0' .. '9':
						{
							num = (num << 4) | (ch - '0');
						}
						case 'a' .. 'f':
						{
							num = (num << 4) | (ch - ('a' - 10));
						}
						case 'A' .. 'F':
						{
							num = (num << 4) | (ch - ('A' - 10));
						}
						default:
						{
							return -1;
						}
					}
				}
				while ((ch = ss_string[ss_stringPos]) > ' ' && ch != delim);
				setarg(paramPos, 0, num);
			}
			case 'c':
			{
				setarg(paramPos, 0, ss_string[ss_stringPos++]);
			}
			case 'f':
			{

				new changestr[16], changepos = 0, strpos = ss_stringPos;
				while(changepos < 16 && ss_string[strpos] && ss_string[strpos] != delim)
				{
					changestr[changepos++] = ss_string[strpos++];
    				}
				changestr[changepos] = '\0';
				setarg(paramPos,0,_:floatstr(changestr));
			}
			case 'p':
			{
				delim = format[formatPos++];
				continue;
			}
			case '\'':
			{
				new
					end = formatPos - 1,
					ch;
				while ((ch = format[++end]) && ch != '\'') {}
				if (!ch)
				{
					return -1;
				}
				format[end] = '\0';
				if ((ch = strfind(ss_string, format[formatPos], false, ss_stringPos)) == -1)
				{
					if (format[end + 1])
					{
						return -1;
					}
					return 0;
				}
				format[end] = '\'';
				ss_stringPos = ch + (end - formatPos);
				formatPos = end + 1;
			}
			case 'u':
			{
				new
					end = ss_stringPos - 1,
					id = 0,
					bool:num = true,
					ch;
				while ((ch = ss_string[++end]) && ch != delim)
				{
					if (num)
					{
						if ('0' <= ch <= '9')
						{
							id = (id * 10) + (ch - '0');
						}
						else
						{
							num = false;
						}
					}
				}
				if (num && IsPlayerConnected(id))
				{
					setarg(paramPos, 0, id);
				}
				else
				{
					#if !defined foreach
						#define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
						#define __SSCANF_FOREACH__
					#endif
					ss_string[end] = '\0';
					num = false;
					new
						name[MAX_PLAYER_NAME];
					id = end - ss_stringPos;
					foreach (Player, playerid)
					{
						GetPlayerName(playerid, name, sizeof (name));
						if (!strcmp(name, ss_string[ss_stringPos], true, id))
						{
							setarg(paramPos, 0, playerid);
							num = true;
							break;
						}
					}
					if (!num)
					{
						setarg(paramPos, 0, INVALID_PLAYER_ID);
					}
					ss_string[end] = ch;
					#if defined __SSCANF_FOREACH__
						#undef foreach
						#undef __SSCANF_FOREACH__
					#endif
				}
				ss_stringPos = end;
			}
			case 's', 'z':
			{
				new
					i = 0,
					ch;
				if (format[formatPos])
				{
					while ((ch = ss_string[ss_stringPos++]) && ch != delim)
					{
						setarg(paramPos, i++, ch);
					}
					if (!i)
					{
						return -1;
					}
				}
				else
				{
					while ((ch = ss_string[ss_stringPos++]))
					{
						setarg(paramPos, i++, ch);
					}
				}
				ss_stringPos--;
				setarg(paramPos, i, '\0');
			}
			default:
			{
				continue;
			}
		}
		while (ss_string[ss_stringPos] && ss_string[ss_stringPos] != delim && ss_string[ss_stringPos] > ' ')
		{
			ss_stringPos++;
		}
		while (ss_string[ss_stringPos] && (ss_string[ss_stringPos] == delim || ss_string[ss_stringPos] <= ' '))
		{
			ss_stringPos++;
		}
		paramPos++;
	}
	do
	{
		if ((delim = format[formatPos++]) > ' ')
		{
			if (delim == '\'')
			{
				while ((delim = format[formatPos++]) && delim != '\'') {}
			}
			else if (delim != 'z')
			{
				return delim;
			}
		}
	}
	while (delim > ' ');
	return 0;
}
