
public admin_print(id,const message[],...)
{
	static buffer_message[512];
	vformat(buffer_message,511,message,3);

	if(id == 0)
	{
		server_print(buffer_message);
	}
	else
	{
		client_print(id,print_console,buffer_message);
	}
}

public admin_purge(id)
{
	if(get_playersnum())
	{
		admin_print(id,"%L",id,"L_SERVER_NO_EMPTY",PLUGIN_TAG);
		return PLUGIN_HANDLED;
	}
	
	if(get_pcvar_num(cvar_savexp) == 1)
	{
		admin_print(id,"%L",id,"L_VAULT_FILE_DELETED",PLUGIN_TAG);
		new data_path[64] = "%s/vault/%s.VAULT";
		format(data_path,63,data_path,amxx_data_dir,PLUGIN_VAULT);
		delete_file(data_path);
	}
	else if(get_pcvar_num(cvar_savexp) == 2)
	{
		admin_print(id,"%L",id,"L_SQL_TABLE_EMPTY",PLUGIN_TAG);
		database_truncate();
	}
	else
	{
		admin_print(id,"%L",id,"L_TURN_ON_SAVEXP",PLUGIN_TAG);
	}
	
	return PLUGIN_HANDLED;
}

public admin_reset_player(id,szTarget[],class)
{
	if(class > 2)
	{
		admin_print(id,"%L",id,"L_INVALID_CLASS",PLUGIN_TAG);
		return PLUGIN_HANDLED;
	}
	
	new victim = 0,bool:target_found = false, class_old;
	while ((victim = find_target(victim,szTarget)) > 0)
	{
		class_old=g_player_class[victim];
		
		if(class == 1)
		{
			g_player_class[victim]=1;
		}
		else if(class == 2)
		{
			g_player_class[victim]=2;
		}
		else
		{
			continue;
		}
		
		client_print(victim,print_chat,"%L",victim,"L_ADMIN_RESET_USER",PLUGIN_TAG,g_bleach_classes[g_player_class[victim]]);
		
		load_player(victim);
		
		reset_player(victim);
		reset_player_attributes(victim);
		reset_player_skills(victim);
		
		restore_cooldowns(victim);
		restore_player(victim);
		
		save_player(victim);
		
		g_player_class[victim]=class_old;
		
		load_player(victim);
		print_hud_player(victim);
		
		target_found = true;
	}
	
	if (!target_found)
	{
		no_target_found(id,szTarget,(victim == -2));
	}
	
	return PLUGIN_HANDLED;
}

//---------------------------------------------------------------------

public admin_give_xp(id,szTarget[],amount)
{
	new victim = 0,bool:target_found = false;

	while ((victim = find_target(victim,szTarget)) > 0)
	{
		client_print(victim,print_chat,"%L",victim,"L_ADMIN_XP_GAVE",PLUGIN_TAG,amount);
		target_found = true;
		xp_set(victim,g_player_experience[victim]+amount);
	}
	
	if (!target_found)
	{
		no_target_found(id,szTarget,(victim == -2));
	}
}

public admin_set_xp(id,szTarget[],amount)
{
	new victim = 0,bool:target_found = false;

	while ( ( victim = find_target( victim, szTarget ) ) > 0 )
	{
		client_print(victim,print_chat,"%L",victim,"L_ADMIN_XP_SET",PLUGIN_TAG,amount);
		target_found = true;
		xp_set(victim,amount);
	}
	
	if (!target_found)
	{
		no_target_found(id,szTarget,(victim == -2));
	}
}

//---------------------------------------------------------------------

public admin_give_spirit(id,szTarget[],amount)
{
	new victim = 0,bool:target_found = false;

	while ((victim = find_target(victim,szTarget)) > 0)
	{
		client_print(victim,print_chat,"%L",victim,"L_ADMIN_SPR_GAVE",PLUGIN_TAG,amount);
		target_found = true;
		spirit_set(victim,g_player_spirit[victim]+amount);
	}
	
	if (!target_found)
	{
		no_target_found(id,szTarget,(victim == -2));
	}
}

public admin_set_spirit(id,szTarget[],amount)
{
	new victim = 0,bool:target_found = false;

	while ( ( victim = find_target( victim, szTarget ) ) > 0 )
	{
		client_print(victim,print_chat,"%L",victim,"L_ADMIN_SPR_SET",PLUGIN_TAG,amount);
		target_found = true;
		spirit_set(victim,amount);
	}
	
	if (!target_found)
	{
		no_target_found(id,szTarget,(victim == -2));
	}
}

//---------------------------------------------------------------------

public admin_item_list(id)
{
	admin_print(id,"%L",id,"L_INVALID_ITEM_ID");
	for (new i = 1; i <= BLEACH_MAX_ITEMS; ++i)
	{
			admin_print(id,"%d = %s",i,g_item_names[i]);
	}
}

public admin_give_item(id,szTarget[],item)
{
	if((item <= 0) || (item > BLEACH_MAX_ITEMS))
	{
		admin_item_list(id);
	}
	else
	{
		new victim = 0,bool:target_found = false;
		
		while ( ( victim = find_target( victim, szTarget ) ) > 0 )
		{
			if(g_player_item[victim] == 0)
			{
				client_print(victim,print_chat,"%L",victim,"L_ADMIN_ITEM_GAVE",PLUGIN_TAG,g_item_names[item]);
				target_found = true;
				g_player_item[victim]=item;
				item_passive(victim);
			}
		}
		
		if (!target_found)
		{
			no_target_found(id,szTarget,(victim == -2));
		}
	}
}

public admin_drop_item(id,item)
{
	if((item > 0) && (item <= BLEACH_MAX_ITEMS))
	{
		item_drop(id,item);
		admin_print(id,"%L",id,"L_ADMIN_DROPPED_ITEM",PLUGIN_TAG,g_item_names[item]);
	}
	else
	{
		admin_item_list(id);
	}
}

//---------------------------------------------------------------------

public find_target( iLastID, szTarget[] )
{
	new iTarget = -1;

	if ( equali( szTarget, "@ALL" ) )
	{
		new players[32], iTotalPlayers, i;
		get_players( players, iTotalPlayers );
		
		for ( i = 0; i < iTotalPlayers; i++ )
		{
			if ( players[i] == iLastID && i + 1 != iTotalPlayers )
			{
				iTarget = players[i+1];
			}
		}

		if ( iTotalPlayers > 0 && iLastID == 0 )
		{
			iTarget = players[0];
		}
	}

	else if ( szTarget[0] == '@' )
	{
		new iTeam = -1;
		
		if ( equali( szTarget, "@T" ) )
		{
			iTeam = TEAM_T;
		}
		else if ( equali( szTarget, "@CT" ) )
		{
				iTeam = TEAM_CT;
		}

		if ( iTeam != -1 )
		{
			new players[32], iTotalPlayers, i, iFirstPlayer = -1, bool:bSaveNext = false;
			get_players( players, iTotalPlayers );

			for ( i = 0; i < iTotalPlayers; i++ )
			{			
				if ( iTeam == get_user_team( players[i] ) )
				{
					
					if ( bSaveNext )
					{
						iTarget = players[i];
						break;
					}

					if ( players[i] == iLastID )
					{
						bSaveNext = true;
					}
					
					if ( iFirstPlayer == -1 )
					{
						iFirstPlayer = players[i];
					}
				}
			}

			if ( iLastID == 0 )
			{
				iTarget = iFirstPlayer;
			}
		}
	}

	else
	{
		new iPlayer = find_player( "a", szTarget );
		
		if ( !iPlayer )
		{
			iPlayer = find_player( "bl", szTarget );

			if ( iPlayer )
			{

				if ( iPlayer != find_player( "blj", szTarget ) )
				{
					return -2;
				}
			}
		}

		if ( !iPlayer )
		{
			iPlayer = find_player( "c" , szTarget );
		}

		if ( !iPlayer )
		{
			if ( szTarget[0] == '#' && szTarget[1] )
			{
				iPlayer = find_player( "k", str_to_num( szTarget[1] ) );
			}
		}
		
		if ( iPlayer && iLastID != iPlayer )
		{
			iTarget = iPlayer;
		}
	}

	return iTarget;
}

public no_target_found(id,szTarget[],bool:multi)
{
	if(multi)
	{
		admin_print(id,"%L",id,"L_ADMIN_FIND_TARGETS",PLUGIN_TAG,szTarget);
	}
	else
	{
		admin_print(id,"%L",id,"L_ADMIN_NO_TARGET",PLUGIN_TAG,szTarget);
		admin_print(id,"%L",id,"L_ADMIN_AVAILABLE_TARGETS",PLUGIN_TAG);
	}
}
