    001package net.minecraft.command;
    002
    003import java.util.Iterator;
    004import java.util.List;
    005import java.util.regex.Matcher;
    006import java.util.regex.Pattern;
    007import net.minecraft.entity.player.EntityPlayerMP;
    008import net.minecraft.server.MinecraftServer;
    009import net.minecraft.server.management.BanEntry;
    010
    011public class CommandServerBanIp extends CommandBase
    012{
    013    public static final Pattern IPv4Pattern = Pattern.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
    014
    015    public String getCommandName()
    016    {
    017        return "ban-ip";
    018    }
    019
    020    /**
    021     * Return the required permission level for this command.
    022     */
    023    public int getRequiredPermissionLevel()
    024    {
    025        return 3;
    026    }
    027
    028    /**
    029     * Returns true if the given command sender is allowed to use this command.
    030     */
    031    public boolean canCommandSenderUseCommand(ICommandSender par1ICommandSender)
    032    {
    033        return MinecraftServer.getServer().getConfigurationManager().getBannedIPs().isListActive() && super.canCommandSenderUseCommand(par1ICommandSender);
    034    }
    035
    036    public String getCommandUsage(ICommandSender par1ICommandSender)
    037    {
    038        return par1ICommandSender.translateString("commands.banip.usage", new Object[0]);
    039    }
    040
    041    public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    042    {
    043        if (par2ArrayOfStr.length >= 1 && par2ArrayOfStr[0].length() > 1)
    044        {
    045            Matcher matcher = IPv4Pattern.matcher(par2ArrayOfStr[0]);
    046            String s = null;
    047
    048            if (par2ArrayOfStr.length >= 2)
    049            {
    050                s = func_82360_a(par1ICommandSender, par2ArrayOfStr, 1);
    051            }
    052
    053            if (matcher.matches())
    054            {
    055                this.banIP(par1ICommandSender, par2ArrayOfStr[0], s);
    056            }
    057            else
    058            {
    059                EntityPlayerMP entityplayermp = MinecraftServer.getServer().getConfigurationManager().getPlayerForUsername(par2ArrayOfStr[0]);
    060
    061                if (entityplayermp == null)
    062                {
    063                    throw new PlayerNotFoundException("commands.banip.invalid", new Object[0]);
    064                }
    065
    066                this.banIP(par1ICommandSender, entityplayermp.getPlayerIP(), s);
    067            }
    068        }
    069        else
    070        {
    071            throw new WrongUsageException("commands.banip.usage", new Object[0]);
    072        }
    073    }
    074
    075    /**
    076     * Adds the strings available in this command to the given list of tab completion options.
    077     */
    078    public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    079    {
    080        return par2ArrayOfStr.length == 1 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, MinecraftServer.getServer().getAllUsernames()) : null;
    081    }
    082
    083    /**
    084     * Actually does the banning work.
    085     */
    086    protected void banIP(ICommandSender par1ICommandSender, String par2Str, String par3Str)
    087    {
    088        BanEntry banentry = new BanEntry(par2Str);
    089        banentry.setBannedBy(par1ICommandSender.getCommandSenderName());
    090
    091        if (par3Str != null)
    092        {
    093            banentry.setBanReason(par3Str);
    094        }
    095
    096        MinecraftServer.getServer().getConfigurationManager().getBannedIPs().put(banentry);
    097        List list = MinecraftServer.getServer().getConfigurationManager().getPlayerList(par2Str);
    098        String[] astring = new String[list.size()];
    099        int i = 0;
    100        EntityPlayerMP entityplayermp;
    101
    102        for (Iterator iterator = list.iterator(); iterator.hasNext(); astring[i++] = entityplayermp.getEntityName())
    103        {
    104            entityplayermp = (EntityPlayerMP)iterator.next();
    105            entityplayermp.playerNetServerHandler.kickPlayerFromServer("You have been IP banned.");
    106        }
    107
    108        if (list.isEmpty())
    109        {
    110            notifyAdmins(par1ICommandSender, "commands.banip.success", new Object[] {par2Str});
    111        }
    112        else
    113        {
    114            notifyAdmins(par1ICommandSender, "commands.banip.success.players", new Object[] {par2Str, joinNiceString(astring)});
    115        }
    116    }
    117}