Add support for command tab completion in the console. Adds BUKKIT-4168

This commit corrects tab-completion logic to consider non-player command
senders.

By: Phillip Schichtel <quick_wango@code-infection.de>
This commit is contained in:
Bukkit/Spigot 2013-08-17 18:51:10 -06:00
parent 5462a33b20
commit f60d6710d5
2 changed files with 8 additions and 5 deletions

View file

@ -78,18 +78,18 @@ public abstract class Command {
Validate.notNull(args, "Arguments cannot be null"); Validate.notNull(args, "Arguments cannot be null");
Validate.notNull(alias, "Alias cannot be null"); Validate.notNull(alias, "Alias cannot be null");
if (!(sender instanceof Player) || args.length == 0) { if (args.length == 0) {
return ImmutableList.of(); return ImmutableList.of();
} }
String lastWord = args[args.length - 1]; String lastWord = args[args.length - 1];
Player senderPlayer = (Player) sender; Player senderPlayer = sender instanceof Player ? (Player) sender : null;
ArrayList<String> matchedPlayers = new ArrayList<String>(); ArrayList<String> matchedPlayers = new ArrayList<String>();
for (Player player : sender.getServer().getOnlinePlayers()) { for (Player player : sender.getServer().getOnlinePlayers()) {
String name = player.getName(); String name = player.getName();
if (senderPlayer.canSee(player) && StringUtil.startsWithIgnoreCase(name, lastWord)) { if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, lastWord)) {
matchedPlayers.add(name); matchedPlayers.add(name);
} }
} }

View file

@ -16,6 +16,7 @@ import java.util.regex.Pattern;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.defaults.*; import org.bukkit.command.defaults.*;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil; import org.bukkit.util.StringUtil;
public class SimpleCommandMap implements CommandMap { public class SimpleCommandMap implements CommandMap {
@ -226,6 +227,8 @@ public class SimpleCommandMap implements CommandMap {
ArrayList<String> completions = new ArrayList<String>(); ArrayList<String> completions = new ArrayList<String>();
Map<String, Command> knownCommands = this.knownCommands; Map<String, Command> knownCommands = this.knownCommands;
final String prefix = (sender instanceof Player ? "/" : "");
for (VanillaCommand command : fallbackCommands) { for (VanillaCommand command : fallbackCommands) {
String name = command.getName(); String name = command.getName();
@ -241,7 +244,7 @@ public class SimpleCommandMap implements CommandMap {
continue; continue;
} }
completions.add('/' + name); completions.add(prefix + name);
} }
for (Map.Entry<String, Command> commandEntry : knownCommands.entrySet()) { for (Map.Entry<String, Command> commandEntry : knownCommands.entrySet()) {
@ -254,7 +257,7 @@ public class SimpleCommandMap implements CommandMap {
String name = commandEntry.getKey(); // Use the alias, not command name String name = commandEntry.getKey(); // Use the alias, not command name
if (StringUtil.startsWithIgnoreCase(name, cmdLine)) { if (StringUtil.startsWithIgnoreCase(name, cmdLine)) {
completions.add('/' + name); completions.add(prefix + name);
} }
} }