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