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