    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 var3 = IPv4Pattern.matcher(par2ArrayOfStr[0]);
    046            String var4 = null;
    047
    048            if (par2ArrayOfStr.length >= 2)
    049            {
    050                var4 = func_82360_a(par1ICommandSender, par2ArrayOfStr, 1);
    051            }
    052
    053            if (var3.matches())
    054            {
    055                this.banIP(par1ICommandSender, par2ArrayOfStr[0], var4);
    056            }
    057            else
    058            {
    059                EntityPlayerMP var5 = MinecraftServer.getServer().getConfigurationManager().getPlayerForUsername(par2ArrayOfStr[0]);
    060
    061                if (var5 == null)
    062                {
    063                    throw new PlayerNotFoundException("commands.banip.invalid", new Object[0]);
    064                }
    065
    066                this.banIP(par1ICommandSender, var5.func_71114_r(), var4);
    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 var4 = new BanEntry(par2Str);
    089        var4.setBannedBy(par1ICommandSender.getCommandSenderName());
    090
    091        if (par3Str != null)
    092        {
    093            var4.setBanReason(par3Str);
    094        }
    095
    096        MinecraftServer.getServer().getConfigurationManager().getBannedIPs().put(var4);
    097        List var5 = MinecraftServer.getServer().getConfigurationManager().getPlayerList(par2Str);
    098        String[] var6 = new String[var5.size()];
    099        int var7 = 0;
    100        EntityPlayerMP var9;
    101
    102        for (Iterator var8 = var5.iterator(); var8.hasNext(); var6[var7++] = var9.getEntityName())
    103        {
    104            var9 = (EntityPlayerMP)var8.next();
    105            var9.playerNetServerHandler.kickPlayerFromServer("You have been IP banned.");
    106        }
    107
    108        if (var5.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(var6)});
    115        }
    116    }
    117}