
CommandMap contains a method that will auto-complete commands appropriately. Before the first space, it searches for commands of which the sender has permission. After the first space, it delegates to the individual command. Vanilla commands contain implementations to mimic vanilla implementation. Exception would be give, that allows for name matching; a feature we already allowed as part of the command is now supported for auto-complete as well. Plugin commands can get a tab completer set to delegate the completion for. If no tab completer is set, it can check the executor to see if it implements the tab completion interface. It will also attempt to chain calls if null gets returned from these interfaces. Plugins also implement the new TabCompleter interface, to add ease-of-use for plugin developers, similar to the onCommand() method. The default command implementation simply searches for player names. To help facilitate command completion, a utility class was added with two functions. One checks two strings, to see if the specified string starts with (ignoring case) the second. The other method uses the first to selectively copy elements from one collection to another.
74 lines
2.4 KiB
Java
74 lines
2.4 KiB
Java
package org.bukkit.command.defaults;
|
|
|
|
import java.util.List;
|
|
import java.util.regex.Pattern;
|
|
|
|
import org.apache.commons.lang.Validate;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
public class BanIpCommand extends VanillaCommand {
|
|
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])$");
|
|
|
|
public BanIpCommand() {
|
|
super("ban-ip");
|
|
this.description = "Prevents the specified IP address from using this server";
|
|
this.usageMessage = "/ban-ip <address|player> [reason ...]";
|
|
this.setPermission("bukkit.command.ban.ip");
|
|
}
|
|
|
|
@Override
|
|
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
|
|
if (!testPermission(sender)) return true;
|
|
if (args.length < 1) {
|
|
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
|
|
return false;
|
|
}
|
|
|
|
// TODO: Ban Reason support
|
|
if (ipValidity.matcher(args[0]).matches()) {
|
|
processIPBan(args[0], sender);
|
|
} else {
|
|
Player player = Bukkit.getPlayer(args[0]);
|
|
|
|
if (player == null) {
|
|
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
|
|
return false;
|
|
}
|
|
|
|
processIPBan(player.getAddress().getAddress().getHostAddress(), sender);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void processIPBan(String ip, CommandSender sender) {
|
|
// TODO: Kick on ban
|
|
Bukkit.banIP(ip);
|
|
|
|
Command.broadcastCommandMessage(sender, "Banned IP Address " + ip);
|
|
}
|
|
|
|
@Override
|
|
public boolean matches(String input) {
|
|
return input.equalsIgnoreCase("ban-ip");
|
|
}
|
|
|
|
@Override
|
|
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
|
Validate.notNull(sender, "Sender cannot be null");
|
|
Validate.notNull(args, "Arguments cannot be null");
|
|
Validate.notNull(alias, "Alias cannot be null");
|
|
|
|
if (args.length == 1) {
|
|
return super.tabComplete(sender, alias, args);
|
|
}
|
|
return ImmutableList.of();
|
|
}
|
|
}
|