Skip to content

Commit

Permalink
Fixed admin command tab complete for Ops
Browse files Browse the repository at this point in the history
Tab complete values are sorted now too.
  • Loading branch information
tastybento committed Jul 12, 2018
1 parent 31455a0 commit c64ecf7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.World;
Expand Down Expand Up @@ -438,14 +439,15 @@ public Command setUsage(String usage) {

@Override
public List<String> tabComplete(final CommandSender sender, final String alias, final String[] args) {
Arrays.stream(args).forEach(plugin::log);// DEBUG
List<String> options = new ArrayList<>();
// Get command object based on args entered so far
CompositeCommand cmd = getCommandFromArgs(args);
// Check for console and permissions
if (cmd.onlyPlayer && !(sender instanceof Player)) {
return options;
}
if (!cmd.getPermission().isEmpty() && !sender.hasPermission(cmd.getPermission())) {
if (!cmd.getPermission().isEmpty() && !sender.hasPermission(cmd.getPermission()) && !sender.isOp()) {
return options;
}
// Add any tab completion from the subcommand
Expand All @@ -472,7 +474,7 @@ public List<String> tabComplete(final CommandSender sender, final String alias,

String lastArg = args.length != 0 ? args[args.length - 1] : "";

return Util.tabLimit(options, lastArg);
return Util.tabLimit(options, lastArg).stream().sorted().collect(Collectors.toList());
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/bskyblock/TestBSkyBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,23 @@ public void testCommandAPI() {
}
}
String[] args = {""};
assertEquals(Arrays.asList("sub1","sub2", "help"), testCommand.tabComplete(player, "test", args));
assertNotSame(Arrays.asList("sub1","sub2", "help"), testCommand.tabComplete(sender, "test", args));
// Results are alphabetically sorted
assertEquals(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(player, "test", args));
assertNotSame(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(sender, "test", args));
args[0] = "su";
assertEquals(Arrays.asList("sub1","sub2"), testCommand.tabComplete(player, "test", args));
args[0] = "d";
assertNotSame(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(player, "test", args));
args[0] = "sub1";
assertEquals(Collections.emptyList(), testCommand.tabComplete(player, "test", args));
String[] args2 = {"sub2",""};
assertEquals(Arrays.asList("subsub", "help"), testCommand.tabComplete(player, "test", args2));
assertEquals(Arrays.asList("help", "subsub"), testCommand.tabComplete(player, "test", args2));
args2[1] = "s";
assertEquals(Collections.singletonList("subsub"), testCommand.tabComplete(player, "test", args2));
String[] args3 = {"sub2","subsub", ""};
assertEquals(Arrays.asList("subsubsub", "help"), testCommand.tabComplete(player, "test", args3));
assertEquals(Arrays.asList("help", "subsubsub"), testCommand.tabComplete(player, "test", args3));
// Test for overridden tabcomplete
assertEquals(Arrays.asList("Florian", "Ben", "Bill", "Ted", "help"),
assertEquals(Arrays.asList("Ben", "Bill", "Florian", "Ted", "help"),
testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", ""}));
// Test for partial word
assertEquals(Arrays.asList("Ben", "Bill"),
Expand Down

0 comments on commit c64ecf7

Please sign in to comment.