Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
BanIpCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.List;
4 import java.util.regex.Pattern;
5 
6 import org.apache.commons.lang.StringUtils;
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.BanList;
9 import org.bukkit.Bukkit;
10 import org.bukkit.ChatColor;
11 import org.bukkit.command.Command;
13 import org.bukkit.entity.Player;
14 
15 import com.google.common.collect.ImmutableList;
16 
17 public class BanIpCommand extends VanillaCommand {
18  public static final Pattern ipValidity = 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])$");
19 
20  public BanIpCommand() {
21  super("ban-ip");
22  this.description = "Prevents the specified IP address from using this server";
23  this.usageMessage = "/ban-ip <address|player> [reason ...]";
24  this.setPermission("bukkit.command.ban.ip");
25  }
26 
27  @Override
28  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
29  if (!testPermission(sender)) return true;
30  if (args.length < 1) {
31  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
32  return false;
33  }
34 
35  String reason = args.length > 0 ? StringUtils.join(args, ' ', 1, args.length) : null;
36 
37  if (ipValidity.matcher(args[0]).matches()) {
38  processIPBan(args[0], sender, reason);
39  } else {
40  Player player = Bukkit.getPlayer(args[0]);
41 
42  if (player == null) {
43  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
44  return false;
45  }
46 
47  processIPBan(player.getAddress().getAddress().getHostAddress(), sender, reason);
48  }
49 
50  return true;
51  }
52 
53  private void processIPBan(String ip, CommandSender sender, String reason) {
54  Bukkit.getBanList(BanList.Type.IP).addBan(ip, reason, null, sender.getName());
55 
56  // Find all matching players and kick
57  for (Player player : Bukkit.getOnlinePlayers()) {
58  if (player.getAddress().getAddress().getHostAddress().equals(ip)) {
59  player.kickPlayer("You have been IP banned.");
60  }
61  }
62 
63  Command.broadcastCommandMessage(sender, "Banned IP Address " + ip);
64  }
65 
66  @Override
67  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
68  Validate.notNull(sender, "Sender cannot be null");
69  Validate.notNull(args, "Arguments cannot be null");
70  Validate.notNull(alias, "Alias cannot be null");
71 
72  if (args.length == 1) {
73  return super.tabComplete(sender, alias, args);
74  }
75  return ImmutableList.of();
76  }
77 }
boolean testPermission(CommandSender target)
InetSocketAddress getAddress()
void sendMessage(String message)
void setPermission(String permission)
static BanList getBanList(BanList.Type type)
Definition: Bukkit.java:471
BanEntry addBan(String target, String reason, Date expires, String source)
static Player[] getOnlinePlayers()
Definition: Bukkit.java:94
static Player getPlayer(String name)
Definition: Bukkit.java:186