| 1 | package net.minecraft.command.server;
|
| 2 |
|
| 3 | import java.util.Date;
|
| 4 | import java.util.List;
|
| 5 | import java.util.regex.Matcher;
|
| 6 | import java.util.regex.Pattern;
|
| 7 | import net.minecraft.command.CommandBase;
|
| 8 | import net.minecraft.command.CommandException;
|
| 9 | import net.minecraft.command.ICommandSender;
|
| 10 | import net.minecraft.command.PlayerNotFoundException;
|
| 11 | import net.minecraft.command.WrongUsageException;
|
| 12 | import net.minecraft.entity.player.EntityPlayerMP;
|
| 13 | import net.minecraft.server.MinecraftServer;
|
| 14 | import net.minecraft.server.management.IPBanEntry;
|
| 15 | import net.minecraft.util.BlockPos;
|
| 16 | import net.minecraft.util.IChatComponent;
|
| 17 |
|
| 18 | public class CommandBanIp extends CommandBase
|
| 19 | {
|
| 20 | public static final Pattern field_147211_a = 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])$");
|
| 21 |
|
| 22 | /**
|
| 23 | * Gets the name of the command
|
| 24 | */
|
| 25 | public String getCommandName()
|
| 26 | {
|
| 27 | return "ban-ip";
|
| 28 | }
|
| 29 |
|
| 30 | /**
|
| 31 | * Return the required permission level for this command.
|
| 32 | */
|
| 33 | public int getRequiredPermissionLevel()
|
| 34 | {
|
| 35 | return 3;
|
| 36 | }
|
| 37 |
|
| 38 | /**
|
| 39 | * Returns true if the given command sender is allowed to use this command.
|
| 40 | */
|
| 41 | public boolean canCommandSenderUseCommand(ICommandSender sender)
|
| 42 | {
|
| 43 | return MinecraftServer.getServer().getConfigurationManager().getBannedIPs().isLanServer() && super.canCommandSenderUseCommand(sender);
|
| 44 | }
|
| 45 |
|
| 46 | /**
|
| 47 | * Gets the usage string for the command.
|
| 48 | */
|
| 49 | public String getCommandUsage(ICommandSender sender)
|
| 50 | {
|
| 51 | return "commands.banip.usage";
|
| 52 | }
|
| 53 |
|
| 54 | /**
|
| 55 | * Callback when the command is invoked
|
| 56 | */
|
| 57 | public void processCommand(ICommandSender sender, String[] args) throws CommandException
|
| 58 | {
|
| 59 | if (args.length >= 1 && args[0].length() > 1)
|
| 60 | {
|
| 61 | IChatComponent ichatcomponent = args.length >= 2 ? getChatComponentFromNthArg(sender, args, 1) : null;
|
| 62 | Matcher matcher = field_147211_a.matcher(args[0]);
|
| 63 |
|
| 64 | if (matcher.matches())
|
| 65 | {
|
| 66 | this.func_147210_a(sender, args[0], ichatcomponent == null ? null : ichatcomponent.getUnformattedText());
|
| 67 | }
|
| 68 | else
|
| 69 | {
|
| 70 | EntityPlayerMP entityplayermp = MinecraftServer.getServer().getConfigurationManager().getPlayerByUsername(args[0]);
|
| 71 |
|
| 72 | if (entityplayermp == null)
|
| 73 | {
|
| 74 | throw new PlayerNotFoundException("commands.banip.invalid", new Object[0]);
|
| 75 | }
|
| 76 |
|
| 77 | this.func_147210_a(sender, entityplayermp.getPlayerIP(), ichatcomponent == null ? null : ichatcomponent.getUnformattedText());
|
| 78 | }
|
| 79 | }
|
| 80 | else
|
| 81 | {
|
| 82 | throw new WrongUsageException("commands.banip.usage", new Object[0]);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
|
| 87 | {
|
| 88 | return args.length == 1 ? getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames()) : null;
|
| 89 | }
|
| 90 |
|
| 91 | protected void func_147210_a(ICommandSender sender, String address, String reason)
|
| 92 | {
|
| 93 | IPBanEntry ipbanentry = new IPBanEntry(address, (Date)null, sender.getName(), (Date)null, reason);
|
| 94 | MinecraftServer.getServer().getConfigurationManager().getBannedIPs().addEntry(ipbanentry);
|
| 95 | List<EntityPlayerMP> list = MinecraftServer.getServer().getConfigurationManager().getPlayersMatchingAddress(address);
|
| 96 | String[] astring = new String[list.size()];
|
| 97 | int i = 0;
|
| 98 |
|
| 99 | for (EntityPlayerMP entityplayermp : list)
|
| 100 | {
|
| 101 | entityplayermp.playerNetServerHandler.kickPlayerFromServer("You have been IP banned.");
|
| 102 | astring[i++] = entityplayermp.getName();
|
| 103 | }
|
| 104 |
|
| 105 | if (list.isEmpty())
|
| 106 | {
|
| 107 | notifyOperators(sender, this, "commands.banip.success", new Object[] {address});
|
| 108 | }
|
| 109 | else
|
| 110 | {
|
| 111 | notifyOperators(sender, this, "commands.banip.success.players", new Object[] {address, joinNiceString(astring)});
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|