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