/* ==================================================== CONFIG START ==================================================== */

#define USE_FAKEMETA       // comment this if you are more willing to use Cstrike and Fun modules instead of FakeMeta
//#define PROCESSOR_TYPE 0 // 1 = 32bit server; 0 = 64bit server; if commented - automatic bit detection

#define SAVE_LIMIT 128 // limit of people to save (0 - dynamic [not recommended])

#define DATA_SAVE (1|2|4|8|16) // save data by:
// 1 - steam id (automatically checks is steam ID valid)
// 2 - ip
// 4 - if first two numbers of ip and nick are the same
// 8 - dynamic ip (first two numbers [123.456.*.*])
// 16 - nick
// NOTE: if save won't contain none of this numbers - next connected player will just have the longest time ago disconnected player data

/* ===================================================== CONFIG END ===================================================== */

#define PLUGIN_NAME    "Reconnect Data Saver"
#define PLUGIN_VERSION "1.4"
#define PLUGIN_AUTHOR  "Numb"

#include <amxmodx>
#if defined USE_FAKEMETA
	#include <fakemeta>
#else
	#include <cstrike>
	#include <fun>
#endif

#if defined USE_FAKEMETA
	#if !defined PROCESSOR_TYPE
		#if cellbits == 32
			#define PD_CSDEATHS        493
			#define PD_CSMONEY         115
			#define PD_CSTEAM          114
			#define PD_CSINTERNALMODEL 126
		#else
			#define PD_CSDEATHS        444
			#define PD_CSMONEY         140
			#define PD_CSTEAM          139
			#define PD_CSINTERNALMODEL 152
		#endif
	#else
		#if PROCESSOR_TYPE == 1
			#define PD_CSDEATHS        493
			#define PD_CSMONEY         115
			#define PD_CSTEAM          114
			#define PD_CSINTERNALMODEL 126
		#else
			#define PD_CSDEATHS        444
			#define PD_CSMONEY         140
			#define PD_CSTEAM          139
			#define PD_CSINTERNALMODEL 152
		#endif
	#endif

set_user_frags(iPlrId, iFragsNum, bool:bSendToAllPlugins=true)
{
	if( !native_check_plrid(iPlrId) ) // checking is player valid
		return 0;
	
	static s_iMsg_ScoreInfo;
	if( !s_iMsg_ScoreInfo )
		s_iMsg_ScoreInfo = get_user_msgid("ScoreInfo");
	
	if( bSendToAllPlugins ) // changing frags in scoreboard
	{
		emessage_begin(MSG_BROADCAST, s_iMsg_ScoreInfo);
		ewrite_byte(iPlrId);
		ewrite_short(iFragsNum);
		ewrite_short(get_pdata_int(iPlrId, PD_CSDEATHS));
		ewrite_short(0);
		ewrite_short(get_pdata_int(iPlrId, PD_CSTEAM));
		emessage_end();
	}
	else
	{
		message_begin(MSG_BROADCAST, s_iMsg_ScoreInfo);
		write_byte(iPlrId);
		write_short(iFragsNum);
		write_short(get_pdata_int(iPlrId, PD_CSDEATHS));
		write_short(0);
		write_short(get_pdata_int(iPlrId, PD_CSTEAM));
		message_end();
	}
	
	new iMsgBlockType = get_msg_block(s_iMsg_ScoreInfo);
	set_msg_block(s_iMsg_ScoreInfo, BLOCK_SET);
	set_pev(iPlrId, pev_frags, float(iFragsNum)); // setting frags silently
	set_msg_block(s_iMsg_ScoreInfo, iMsgBlockType);
	
	return 1;
}

cs_get_user_deaths(iPlrId)
{
	if( !native_check_plrid(iPlrId) ) // checking is player valid
		return 0;
	
	return get_pdata_int(iPlrId, PD_CSDEATHS);
}

cs_set_user_deaths(iPlrId, iDeathsNum, bool:bSendToAllPlugins=true)
{
	if( !native_check_plrid(iPlrId) ) // checking is player valid
		return 0;
	
	static s_iMsg_ScoreInfo;
	if( !s_iMsg_ScoreInfo )
		s_iMsg_ScoreInfo = get_user_msgid("ScoreInfo");
	
	if( bSendToAllPlugins ) // changing deaths in scoreboard
	{
		emessage_begin(MSG_BROADCAST, s_iMsg_ScoreInfo);
		ewrite_byte(iPlrId);
		ewrite_short(pev(iPlrId, pev_frags));
		ewrite_short(iDeathsNum);
		ewrite_short(0);
		ewrite_short(get_pdata_int(iPlrId, PD_CSTEAM));
		emessage_end();
	}
	else
	{
		message_begin(MSG_BROADCAST, s_iMsg_ScoreInfo);
		write_byte(iPlrId);
		write_short(pev(iPlrId, pev_frags));
		write_short(iDeathsNum);
		write_short(0);
		write_short(get_pdata_int(iPlrId, PD_CSTEAM));
		message_end();
	}
	
	new iMsgBlockType = get_msg_block(s_iMsg_ScoreInfo);
	set_msg_block(s_iMsg_ScoreInfo, BLOCK_SET);
	set_pdata_int(iPlrId, PD_CSDEATHS, iDeathsNum); // setting deaths silently
	set_msg_block(s_iMsg_ScoreInfo, iMsgBlockType);
	
	return 1;
}

cs_set_user_money(iPlrId, iNewMoney, iPlayAnimation=1, bool:bSendToAllPlugins=true)
{
	if( !native_check_plrid(iPlrId) ) // checking is player valid
		return 0;
	
	static s_iMsgId_Money;
	if( !s_iMsgId_Money )
		s_iMsgId_Money = get_user_msgid("Money");
	
	if( bSendToAllPlugins ) // sending money changes to client
	{
		emessage_begin(MSG_ONE_UNRELIABLE, s_iMsgId_Money, _, iPlrId);
		ewrite_long(iNewMoney);
		ewrite_byte(iPlayAnimation);
		emessage_end();
	}
	else
	{
		message_begin(MSG_ONE_UNRELIABLE, s_iMsgId_Money, _, iPlrId);
		write_long(iNewMoney);
		write_byte(iPlayAnimation);
		message_end();
	}
	
	new iMsgBlockType = get_msg_block(s_iMsgId_Money);
	set_msg_block(s_iMsgId_Money, BLOCK_SET);
	set_pdata_int(iPlrId, PD_CSMONEY, iNewMoney); // changing money silently
	set_msg_block(s_iMsgId_Money, iMsgBlockType);
	
	return 1;
}

enum CsTeams
{
	CS_TEAM_UNASSIGNED = 0,
	CS_TEAM_T = 1,
	CS_TEAM_CT = 2,
	CS_TEAM_SPECTATOR = 3
};

enum CsInternalModel {
	CS_DONTCHANGE = 0,
	CS_CT_URBAN = 1,
	CS_T_TERROR = 2,
	CS_T_LEET = 3,
	CS_T_ARCTIC = 4,
	CS_CT_GSG9 = 5,
	CS_CT_GIGN = 6,
	CS_CT_SAS = 7,
	CS_T_GUERILLA = 8,
	CS_CT_VIP = 9,
	CZ_T_MILITIA = 10,
	CZ_CT_SPETSNAZ = 11
};

CsTeams:cs_get_user_team(iPlrId, &{CsInternalModel,_}:iModel=CS_DONTCHANGE)
{
	if( !native_check_plrid(iPlrId) ) // checking is player valid
		return CsTeams:0;
	
	iModel = CsInternalModel:get_pdata_int(iPlrId, PD_CSINTERNALMODEL);
	
	return CsTeams:get_pdata_int(iPlrId, PD_CSTEAM);
}

cs_set_user_team(iPlrId, {CsTeams,_}:iTeam, {CsInternalModel,_}:iModel=CS_DONTCHANGE, bool:bSendToAllPlugins=true)
{
	if( !native_check_plrid(iPlrId) ) // checking is player valid
		return 0;
	
	if( (_:iTeam)<0 || (_:iTeam)>3 )
		return 0;
	
	static s_iMsg_TeamInfo;
	if( !s_iMsg_TeamInfo )
		s_iMsg_TeamInfo = get_user_msgid("TeamInfo");
	
	if( bSendToAllPlugins ) // sending team changes in scoreboard
	{
		emessage_begin(MSG_BROADCAST, s_iMsg_TeamInfo);
		ewrite_byte(iPlrId);
		switch( iTeam )
		{
			case CS_TEAM_T:         ewrite_string("TERRORIST");
			case CS_TEAM_CT:        ewrite_string("CT");
			case CS_TEAM_SPECTATOR: ewrite_string("SPECTATOR");
			default:                ewrite_string("UNASSIGNED");
		}
		emessage_end();
	}
	else
	{
		message_begin(MSG_BROADCAST, s_iMsg_TeamInfo);
		write_byte(iPlrId);
		switch( iTeam )
		{
			case CS_TEAM_T:         write_string("TERRORIST");
			case CS_TEAM_CT:        write_string("CT");
			case CS_TEAM_SPECTATOR: write_string("SPECTATOR");
			default:                write_string("UNASSIGNED");
		}
		message_end();
	}
	
	new iMsgBlockType = get_msg_block(s_iMsg_TeamInfo);
	set_msg_block(s_iMsg_TeamInfo, BLOCK_SET);
	set_pdata_int(iPlrId, PD_CSTEAM, (_:iTeam)); // changing team silently 
	if( (_:iModel)>0 && (_:iModel)<=(native_check_is_cz()?11:9) )
		set_pdata_int(iPlrId, PD_CSINTERNALMODEL, (_:iModel)); // changing model silently
	dllfunc(DLLFunc_ClientUserInfoChanged, iPlrId, engfunc(EngFunc_GetInfoKeyBuffer, iPlrId));
	set_msg_block(s_iMsg_TeamInfo, iMsgBlockType);
	
	return 1;
}

native_check_plrid(iPlrId)
{
	if( iPlrId<1 )
	{
		log_error(AMX_ERR_NATIVE, "Invalid player id: %i", iPlrId);
		return 0;
	}
	
	static s_iMaxPlayers;
	if( !s_iMaxPlayers )
		s_iMaxPlayers = get_maxplayers();
	
	if( iPlrId>s_iMaxPlayers )
	{
		log_error(AMX_ERR_NATIVE, "Invalid player id: %i", iPlrId);
		return 0;
	}
	
	if( !pev_valid(iPlrId) )
	{
		log_error(AMX_ERR_NATIVE, "Invalid player id: %i", iPlrId);
		return 0;
	}
	
	return 1;
}

native_check_is_cz()
{
	static s_iIsGameCz;
	if( !s_iIsGameCz )
	{
		static s_iModName[8];
		get_modname(s_iModName, 8);
		if( equal(s_iModName, "czero") )
		{
			s_iIsGameCz = 1;
			return 1;
		}
		else
		{
			s_iIsGameCz = -1;
			return 0;
		}
	}
	
	if( s_iIsGameCz==1 )
		return 1;
	
	return 0;
}
#endif

new bool:g_bBombPlanted;
new Float:g_fBlockSpawnAt[33];
new g_iUserTeamChanged[33];
new g_iUserTeam[33];
new g_iRoundCount;
new bool:g_bNullScore[2];

new g_iUserMoney[33];
new g_iUserFrags[33];
new g_iUserDeaths[33];
new bool:g_bIsUserConnected[33];
new bool:g_bWasCheckPerformed[33];

#if (DATA_SAVE & 1)
new Array:g_aUserSteam;
#endif

#if (DATA_SAVE & 2)
new Array:g_aUserIp;
#endif

#if (DATA_SAVE & 4)
#if !(DATA_SAVE & 2)
new Array:g_aUserIp;
#endif
#if !(DATA_SAVE & 16)
new Array:g_aUserName;
#endif
#endif
	
#if (DATA_SAVE & 8) && !(DATA_SAVE & 4) && !(DATA_SAVE & 2)
new Array:g_aUserIp;
#endif
	
#if (DATA_SAVE & 16)
new Array:g_aUserName;
#endif

new Array:g_aUserMoneyArray;
new Array:g_aUserFragArray;
new Array:g_aUserDeathArray;
new Array:g_aUserTcArray;
new Array:g_aUserTeamArray;
new g_iDataSize;

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	
	register_event("HLTV",      "Event_HLTV",       "a", "1=0", "2=0");
	register_event("TeamScore", "Event_TeamScore",  "a");
	register_event("TextMsg",   "Event_TextMsg_t",  "a", "1=1", "2=#Game_join_terrorist");
	register_event("TextMsg",   "Event_TextMsg_c",  "a", "1=1", "2=#Game_join_ct");
	register_event("ScoreInfo", "Event_ScoreInfo",  "a");
	register_event("Money",     "Event_Money",      "b");
	register_event("ResetHUD",  "Event_ResetHUD",   "be");
	
	register_logevent("LogEvent_RoundEnd",       2, "0=World triggered", "1=Round_End");
	register_logevent("LogEvent_PlantedTheBomb", 3, "2=Planted_The_Bomb");
	
#if (DATA_SAVE & 1)
	g_aUserSteam      = ArrayCreate(16);
#endif
	
#if (DATA_SAVE & 2)
	g_aUserIp         = ArrayCreate(15);
#endif
	
#if (DATA_SAVE & 4)
#if !(DATA_SAVE & 2)
	g_aUserIp         = ArrayCreate(8);
#endif
#if !(DATA_SAVE & 16)
	g_aUserName       = ArrayCreate(26);
#endif
#endif
	
#if (DATA_SAVE & 8) && !(DATA_SAVE & 4) && !(DATA_SAVE & 2)
	g_aUserIp         = ArrayCreate(8);
#endif
	
#if (DATA_SAVE & 16)
	g_aUserName       = ArrayCreate(26);
#endif
	g_aUserMoneyArray = ArrayCreate(1);
	g_aUserFragArray  = ArrayCreate(1);
	g_aUserDeathArray = ArrayCreate(1);
	g_aUserTcArray    = ArrayCreate(1);
	g_aUserTeamArray  = ArrayCreate(1);
}

public client_putinserver(iPlrId)
	g_bIsUserConnected[iPlrId] = true;

public client_disconnect(iPlrId) // sure... you can say "why I'm not getting all data here - would be much more simple"... try it yourself and be amazed by the errorlog/crash it makes if requesting cs_ info in this event
{
	if( g_bWasCheckPerformed[iPlrId] )
	{
		
		if( SAVE_LIMIT>0 && SAVE_LIMIT==g_iDataSize ) // here we delete information about longest time ago disconnected player
		{
#if (DATA_SAVE & 1)
			ArrayDeleteItem(g_aUserSteam,      0);
#endif

#if (DATA_SAVE & 2)
			ArrayDeleteItem(g_aUserIp,         0);
#endif

#if (DATA_SAVE & 4)
#if !(DATA_SAVE & 2)
			ArrayDeleteItem(g_aUserIp,         0);
#endif
#if !(DATA_SAVE & 16)
			ArrayDeleteItem(g_aUserName,       0);
#endif
#endif
	
#if (DATA_SAVE & 8) && !(DATA_SAVE & 4) && !(DATA_SAVE & 2)
			ArrayDeleteItem(g_aUserIp,         0);
#endif
	
#if (DATA_SAVE & 16)
			ArrayDeleteItem(g_aUserName,       0);
#endif
			
			ArrayDeleteItem(g_aUserFragArray,  0);
			ArrayDeleteItem(g_aUserDeathArray, 0);
			ArrayDeleteItem(g_aUserMoneyArray, 0);
			ArrayDeleteItem(g_aUserTcArray,    0);
			ArrayDeleteItem(g_aUserTeamArray,  0);
		}
		else
			g_iDataSize++;
		
		new iClientData[26];
#if DATA_SAVE & 1
		get_user_authid(iPlrId, iClientData, 26);
		
		if( contain(iClientData, ":")!=7 )
			copy(iClientData, 26, "");
		else
		{
			replace(iClientData, 26, "STEAM_", "");
			replace(iClientData, 26, "VALVE_", "");
		}
		
		ArrayPushString(g_aUserSteam, iClientData);
#endif
		
#if DATA_SAVE & 2
		get_user_ip(iPlrId, iClientData, 26, 1);
		
		ArrayPushString(g_aUserIp, iClientData);
#endif
		
#if DATA_SAVE & 4
#if !(DATA_SAVE & 2)
		if( get_user_ip(iPlrId, iClientData[1], 25, 1)>15 )
			copy(iClientData, 26, "");
		
		new iClientData[0] = contain(iClientData[1], ".");
		if( iClientData[0]<1 )
			copy(iClientData, 26, "");
		else
		{
			iClientData[0] += contain(iClientData[iClientData[0]+2], ".")+2;
			
			if( iClientData[0]>=4 && iClientData[0]<=8 && contain(iClientData[iClientData[0]+1], ".")>0 )
				copy(iClientData[iClientData[0]], (26-iClientData[0]), "");
			else
				copy(iClientData, 26, "");
		}
		
		ArrayPushString(g_aUserIp, iClientData);
#endif
		
#if !(DATA_SAVE & 16)
		get_user_name(iPlrId, iClientData, 26);
		
		ArrayPushString(g_aUserName, iClientData);
#endif
#endif

#if DATA_SAVE & 8 && !(DATA_SAVE & 4) && !(DATA_SAVE & 2)
		if( get_user_ip(iPlrId, iClientData[1], 25, 1)>15 )
			copy(iClientData, 26, "");
		
		new iClientData[0] = contain(iClientData[1], ".");
		if( iClientData[0]<1 )
			copy(iClientData, 26, "");
		else
		{
			iClientData[0] += contain(iClientData[iClientData[0]+2], ".")+2;
			
			if( iClientData[0]>=4 && iClientData[0]<=8 && contain(iClientData[iClientData[0]+1], ".")>0 )
				copy(iClientData[iClientData[0]], (26-iClientData[0]), "");
			else
				copy(iClientData, 26, "");
		}
		
		ArrayPushString(g_aUserIp, iClientData);
#endif

#if DATA_SAVE & 16 && !(DATA_SAVE & 4)
		get_user_name(iPlrId, iClientData, 26);
		
		ArrayPushString(g_aUserName, iClientData);
#endif
		ArrayPushCell(g_aUserFragArray,  g_iUserFrags[iPlrId]);
		ArrayPushCell(g_aUserDeathArray, g_iUserDeaths[iPlrId]);
		ArrayPushCell(g_aUserMoneyArray, g_iUserMoney[iPlrId]);
		ArrayPushCell(g_aUserTcArray,    g_iRoundCount);
		ArrayPushCell(g_aUserTeamArray,  g_iUserTeam[iPlrId]);
	}
	
	g_iUserFrags[iPlrId] = 0;
	g_iUserDeaths[iPlrId] = 0;
	g_iUserMoney[iPlrId] = 0;
	
	g_bIsUserConnected[iPlrId] = false;
	g_bWasCheckPerformed[iPlrId] = false;
}

public Event_HLTV()
{
	for( new iPlrId=1; iPlrId<=32; iPlrId++ )
		g_fBlockSpawnAt[iPlrId] = 0.0;
	
	g_bBombPlanted = false;
	g_iRoundCount++;
}

public Event_TeamScore()
{
	new iTeamName[4];
	read_data(1, iTeamName, 4);
	
	g_bNullScore[equal(iTeamName, "CT")] = (read_data(2)?false:true);
	
	if( g_bNullScore[0] && g_bNullScore[1] ) // new round start after sv_restart 1 or some other event what reseted score
	{
		g_iRoundCount = 0;
		
		if( g_iDataSize )
		{
#if (DATA_SAVE & 1)
			ArrayClear(g_aUserSteam);
#endif

#if (DATA_SAVE & 2)
			ArrayClear(g_aUserIp);
#endif

#if (DATA_SAVE & 4)
#if !(DATA_SAVE & 2)
			ArrayClear(g_aUserIp);
#endif
#if !(DATA_SAVE & 16)
			ArrayClear(g_aUserName);
#endif
#endif
	
#if (DATA_SAVE & 8) && !(DATA_SAVE & 4) && !(DATA_SAVE & 2)
			ArrayClear(g_aUserIp);
#endif
	
#if (DATA_SAVE & 16)
			ArrayClear(g_aUserName);
#endif
			
			ArrayClear(g_aUserFragArray);
			ArrayClear(g_aUserDeathArray);
			ArrayClear(g_aUserMoneyArray);
			ArrayClear(g_aUserTcArray);
			ArrayClear(g_aUserTeamArray);
			
			g_iDataSize = 0;
		}
		
		g_bNullScore[0] = false;
		g_bNullScore[1] = false;
	}
}

public Event_TextMsg_t()
	CL_ManualTeamChange(0);

public Event_TextMsg_c()
	CL_ManualTeamChange(1);

CL_ManualTeamChange(iJoinedCt=0)
{
	new iName[26];
	if( read_data(3, iName, 26) )
	{
		new iPlrId = find_player("a", iName);
		
		if( iPlrId<33 && g_bIsUserConnected[clamp(iPlrId, 0, 32)] )
		{
			new bool:bTeamSwitch;
			
			if( !g_bWasCheckPerformed[iPlrId] ) // just joined some team so lets check was he connected before.
			{
				if( g_iDataSize )
				{
#if !(DATA_SAVE & 1) && !(DATA_SAVE & 2) && !(DATA_SAVE & 4) && !(DATA_SAVE & 8) && !(DATA_SAVE & 16)
					set_user_data_back(iPlrId, bTeamSwitch, iJoinedCt, iName, 0);
					return;
#else
					new iArrayItem, iClientData[26], iSavedData[26];
#if (DATA_SAVE & 1)
					get_user_authid(iPlrId, iClientData, 26);
					
					if( contain(iClientData, ":")==7 )
					{
						replace(iClientData, 26, "STEAM_", "");
						replace(iClientData, 26, "VALVE_", "");
						
						for( iArrayItem=0; iArrayItem<g_iDataSize; iArrayItem++ )
						{
							ArrayGetString(g_aUserSteam, iArrayItem, iSavedData, 16);
							
							if( equali(iClientData, iSavedData) )
							{
								ArrayDeleteItem(g_aUserSteam, iArrayItem);
								
								set_user_data_back(iPlrId, bTeamSwitch, iJoinedCt, iName, iArrayItem);
								
								return;
							}
						}
					}
#endif

#if (DATA_SAVE & 2)
					if( get_user_ip(iPlrId, iClientData[1], 26, 1)>15 )
						copy(iClientData, 26, "");
					else
					{
						for( iArrayItem=0; iArrayItem<g_iDataSize; iArrayItem++ )
						{
							ArrayGetString(g_aUserIp, iArrayItem, iSavedData, 16);
							
							if( equali(iClientData[1], iSavedData) )
							{
								ArrayDeleteItem(g_aUserIp, iArrayItem);
								
								set_user_data_back(iPlrId, bTeamSwitch, iJoinedCt, iName, iArrayItem);
								
								return;
							}
						}
					}
#endif
					
#if (DATA_SAVE & 4)
					new iSavedName[26];
#if !(DATA_SAVE & 2)
					if( get_user_ip(iPlrId, iClientData[1], 26, 1)>15 )
						copy(iClientData, 26, "");
#else
					iClientData[0] = contain(iClientData[1], ".");
					if( iClientData[0]<1 )
						copy(iClientData, 26, "");
					else
					{
						iClientData[0] += contain(iClientData[iClientData[0]+2], ".")+2;
						
						if( iClientData[0]>=4 && iClientData[0]<=8 && contain(iClientData[iClientData[0]+1], ".")>0 )
							copy(iClientData[iClientData[0]], (26-iClientData[0]), "");
						else
							copy(iClientData, 26, "");
					}
#endif
					if( iClientData[0] )
					{
						for( iArrayItem=0; iArrayItem<g_iDataSize; iArrayItem++ )
						{
							ArrayGetString(g_aUserName, iArrayItem, iSavedName, 26);
#if (DATA_SAVE & 2)
							ArrayGetString(g_aUserIp, iArrayItem, iSavedData, 16);
							if( equali(iSavedData, iClientData[1], iClientData[0]) && equali(iName, iSavedName) )
							{
#else
							ArrayGetString(g_aUserIp, iArrayItem, iSavedData, 8);
							if( equali(iClientData, iSavedData[1], iSavedData[0]) && equali(iName, iSavedName) )
							{
#endif
								ArrayDeleteItem(g_aUserIp,   iArrayItem);
								ArrayDeleteItem(g_aUserName, iArrayItem);
								
								set_user_data_back(iPlrId, bTeamSwitch, iJoinedCt, iName, iArrayItem);
								
								return;
							}
						}
					}
#endif
					
#if (DATA_SAVE & 8)
#if !(DATA_SAVE & 4)
#if !(DATA_SAVE & 2)
					if( get_user_ip(iPlrId, iClientData[1], 26, 1)>15 )
						copy(iClientData, 26, "");
#endif
					new iClientData[0] = contain(iClientData[1], ".");
					if( iClientData[0]<1 )
						copy(iClientData, 26, "");
					else
					{
						iClientData[0] += contain(iClientData[iClientData[0]+2], ".")+2;
						
						if( iClientData[0]>=4 && iClientData[0]<=8 && contain(iClientData[iClientData[0]+1], ".")>0 )
							copy(iClientData[iClientData[0]], (26-iClientData[0]), "");
						else
							copy(iClientData, 26, "");
					}
#endif
					if( iClientData[0] )
					{
						for( iArrayItem=0; iArrayItem<g_iDataSize; iArrayItem++ )
						{
#if (DATA_SAVE & 2)
							ArrayGetString(g_aUserIp, iArrayItem, iSavedData, 16);
							if( equali(iSavedData, iClientData[1], iClientData[0]) )
							{
#else
							ArrayGetString(g_aUserIp, iArrayItem, iSavedData, 8);
							if( equali(iClientData, iSavedData[1], iSavedData[0]) )
							{
#endif
								ArrayDeleteItem(g_aUserIp, iArrayItem);
								
								set_user_data_back(iPlrId, bTeamSwitch, iJoinedCt, iName, iArrayItem);
								
								return;
							}
						}
					}
#endif
					
#if (DATA_SAVE & 16)
					for( iArrayItem=0; iArrayItem<g_iDataSize; iArrayItem++ )
					{
						ArrayGetString(g_aUserName, iArrayItem, iSavedData, 16);
						
						if( equali(iName, iSavedData) )
						{
							ArrayDeleteItem(g_aUserName, iArrayItem);
							
							set_user_data_back(iPlrId, bTeamSwitch, iJoinedCt, iName, iArrayItem);
							
							return;
						}
					}
#endif
#endif
				}
				
				g_bWasCheckPerformed[iPlrId] = true;
			}
			
			g_iUserTeamChanged[iPlrId] = g_iRoundCount;
			g_iUserTeam[iPlrId] = (bTeamSwitch?(iJoinedCt?0:1):iJoinedCt);
		}
	}
}

set_user_data_back(iPlrId, bool:bTeamSwitch, iJoinedCt, iName[], iArrayItem=0)
{
	g_iUserFrags[iPlrId]  = ArrayGetCell(g_aUserFragArray,  0);
	g_iUserDeaths[iPlrId] = ArrayGetCell(g_aUserDeathArray, 0);
	g_iUserMoney[iPlrId]  = ArrayGetCell(g_aUserMoneyArray, 0);
	
	ArrayDeleteItem(g_aUserFragArray,  0);
	ArrayDeleteItem(g_aUserDeathArray, 0);
	ArrayDeleteItem(g_aUserMoneyArray, 0);
	
	set_user_frags(iPlrId, g_iUserFrags[iPlrId]);
	cs_set_user_deaths(iPlrId, g_iUserDeaths[iPlrId]);
	cs_set_user_money(iPlrId, g_iUserMoney[iPlrId], 0);
	
	if( ArrayGetCell(g_aUserTcArray, iArrayItem)==g_iRoundCount )
	{
		g_fBlockSpawnAt[iPlrId] = (get_gametime()+0.25);
		engclient_cmd(iPlrId, "menuselect", "5"); // speed up model select to block respawn
		
		if( ArrayGetCell(g_aUserTeamArray, iArrayItem)!=iJoinedCt )
		{
			bTeamSwitch = true;
			
			if( iJoinedCt )
			{
				switch( random_num(1, 4) )
				{
					case 1:  cs_set_user_team(iPlrId, CS_TEAM_T, CS_T_TERROR);
					case 2:  cs_set_user_team(iPlrId, CS_TEAM_T, CS_T_LEET);
					case 3:  cs_set_user_team(iPlrId, CS_TEAM_T, CS_T_ARCTIC);
					default: cs_set_user_team(iPlrId, CS_TEAM_T, CS_T_GUERILLA);
				}
			}
			else
			{
				switch( random_num(1, 4) )
				{
					case 1:  cs_set_user_team(iPlrId, CS_TEAM_CT, CS_CT_URBAN);
					case 2:  cs_set_user_team(iPlrId, CS_TEAM_CT, CS_CT_GSG9);
					case 3:  cs_set_user_team(iPlrId, CS_TEAM_CT, CS_CT_SAS);
					default: cs_set_user_team(iPlrId, CS_TEAM_CT, CS_CT_GIGN);
				}
			}
			
			static s_iMsgId_TextMsg;
			if( !s_iMsgId_TextMsg )
				s_iMsgId_TextMsg = get_user_msgid("TextMsg");
			
			message_begin(MSG_ONE_UNRELIABLE, s_iMsgId_TextMsg, _, iPlrId);
			write_byte(print_notify);
			write_string((iJoinedCt?"#Game_join_terrorist_auto":"#Game_join_ct_auto"));
			write_string(iName);
			message_end();
			
			message_begin(MSG_ONE_UNRELIABLE, s_iMsgId_TextMsg, _, iPlrId);
			write_byte(print_center);
			write_string("#Only_1_Team_Change");
			message_end();
		}
	}
	ArrayDeleteItem(g_aUserTcArray,   0);
	ArrayDeleteItem(g_aUserTeamArray, 0);
	
	client_print(iPlrId, print_chat, "* Your money frags and deaths have been restored."); // Please don't change it to your language... If you really care - create your own ML (it sux for people from other countries...)
	
	g_iDataSize--;
	
	g_bWasCheckPerformed[iPlrId] = true;
	
	g_iUserTeamChanged[iPlrId] = g_iRoundCount;
	g_iUserTeam[iPlrId] = (bTeamSwitch?(iJoinedCt?0:1):iJoinedCt);
	
	return 1;
}

public Event_ScoreInfo()
{
	new iPlrId = read_data(1);
	g_iUserFrags[iPlrId]  = read_data(2);
	g_iUserDeaths[iPlrId] = read_data(3);
}

public Event_Money(iPlrId)
	g_iUserMoney[iPlrId] = read_data(1);

public Event_ResetHUD(iPlrId)
{
	if( g_bWasCheckPerformed[iPlrId] && g_fBlockSpawnAt[iPlrId]>=get_gametime() )
	{
		g_fBlockSpawnAt[iPlrId] = 0.0;
		
		if( is_user_alive(iPlrId) )
		{
			new iPlayers[32], iPlayerNum;
			
			switch( cs_get_user_team(iPlrId) )
			{
				case CS_TEAM_T: get_players(iPlayers, iPlayerNum, "a", "CT");
				case CS_TEAM_CT:
				{
					get_players(iPlayers, iPlayerNum, "a", "TERRORIST");
					
					if( !iPlayerNum )
						iPlayerNum = (g_bBombPlanted?1:0);
				}
				default:
				{
					get_players(iPlayers, iPlayerNum, "a");
					
					if( !iPlayerNum )
						iPlayerNum = (g_bBombPlanted?1:0);
				}
			}
			
			if( iPlayerNum )
			{
				set_user_frags(iPlrId, ((g_iUserFrags[iPlrId]=get_user_frags(iPlrId))+1));
				cs_set_user_deaths(iPlrId, ((g_iUserDeaths[iPlrId]=cs_get_user_deaths(iPlrId))-1));
				
				static s_iMsgId_DeathMsg;
				if( !s_iMsgId_DeathMsg )
					s_iMsgId_DeathMsg = get_user_msgid("DeathMsg");
				new iMsgBlockType = get_msg_block(s_iMsgId_DeathMsg);
				set_msg_block(s_iMsgId_DeathMsg, BLOCK_ONCE);
				user_kill(iPlrId);
				set_msg_block(s_iMsgId_DeathMsg, iMsgBlockType);
			}
		}
	}
}

public LogEvent_PlantedTheBomb()
	g_bBombPlanted = true;

public LogEvent_RoundEnd()
	g_bBombPlanted = false;
