First stab at tab complete of jail commands, #77

Right now, the first option of the jail command completes and checks for
proper permission and all that good stuff. Good way to get started with
commands but that's all that has been completed right now.

Plan is that the command interface will get a tabComplete method which
all the classes that implement it will handle. Then if a tabComplete
happens on that command we'll let the command handle it, so the command
handlers don't get cluttered.
This commit is contained in:
graywolf336 2015-05-29 16:56:02 -05:00
parent ea50ecea6b
commit d3d4572ca4
7 changed files with 81 additions and 4 deletions

View File

@ -1,5 +1,7 @@
package com.graywolf336.jail;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.bukkit.command.Command;
@ -196,6 +198,23 @@ public class JailMain extends JavaPlugin {
return true;//Always return true here, that way we can handle the help and command usage ourself.
}
public List<String> onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args) {
if(jh == null || cmdHand == null) {
sender.sendMessage(Lang.PLUGINNOTLOADED.get());
getServer().getConsoleSender().sendMessage(Lang.PLUGINNOTLOADED.get());
}else {
debug("Tab Complete Args (" + args.length + ") for '" + commandLabel + "': " + Util.getStringFromArray(", ", args));
if(commandLabel.equalsIgnoreCase("jail") || commandLabel.equalsIgnoreCase("j")) {
return jh.parseTabComplete(jm, sender, args);
}else {
//cmdHand.handleCommand(jm, sender, command.getName().toLowerCase(), args);
//unjail,etc
}
}
return Collections.emptyList();
}
public void reloadEverything() throws Exception {
//Reload the configuration file
reloadConfig();

View File

@ -91,6 +91,28 @@ public class Util {
return false;
}
public static String getStringFromArray(String separator, String... list) {
StringBuilder result = new StringBuilder();
for(String s : list) {
if(result.length() != 0) result.append(separator);
result.append(s);
}
return result.toString();
}
public static String getStringFromList(String separator, List<String> list) {
StringBuilder result = new StringBuilder();
for(String s : list) {
if(result.length() != 0) result.append(separator);
result.append(s);
}
return result.toString();
}
/** Returns a colorful message from the color codes. */
public static String getColorfulMessage(String message) {

View File

@ -1,6 +1,7 @@
package com.graywolf336.jail.command;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
@ -47,6 +48,41 @@ public class JailHandler {
plugin.debug("Loaded " + commands.size() + " sub-commands of /jail.");
}
public List<String> parseTabComplete(JailManager jm, CommandSender sender, String[] args) {
if(args[0].equalsIgnoreCase("")) {
List<String> results = new ArrayList<String>();
boolean hasJailPermission = false;
for(Command c : commands.values()) {
CommandInfo i = c.getClass().getAnnotation(CommandInfo.class);
//Skip the jail subcommand instance, since it is unique
if(i.pattern().equalsIgnoreCase("jail|j")) {
hasJailPermission = sender.hasPermission(i.permission());
continue;
}
//Skip if the command requires a player and the sender isn't a player
if(i.needsPlayer() && !(sender instanceof Player)) continue;
if(sender.hasPermission(i.permission())) {
results.add(i.pattern().split("\\|")[0]);
}
}
//Sort the results before adding the player names
Collections.sort(results);
//Don't send out all the players if they don't have jail permission
if(hasJailPermission)
for(Player p : jm.getPlugin().getServer().getOnlinePlayers())
results.add(p.getName());
return results;
}
return Collections.emptyList();
}
/**
* Handles the given command and checks that the command is in valid form.

View File

@ -13,7 +13,7 @@ import com.graywolf336.jail.command.CommandInfo;
maxArgs = 2,
minimumArgs = 1,
needsPlayer = true,
pattern = "createcell|createcells|cellcreate|cellscreate|cc",
pattern = "createcells|createcell|cellcreate|cellscreate|cc",
permission = "jail.command.jailcreatecells",
usage = "/jail createcell [jail] (cellname)"
)

View File

@ -13,7 +13,7 @@ import com.graywolf336.jail.enums.Lang;
maxArgs = 2,
minimumArgs = 1,
needsPlayer = false,
pattern = "telein|teleportin",
pattern = "teleportin|telein",
permission = "jail.command.jailtelein",
usage = "/jail telein [jail] (name)"
)

View File

@ -13,7 +13,7 @@ import com.graywolf336.jail.enums.Lang;
maxArgs = 2,
minimumArgs = 1,
needsPlayer = false,
pattern = "teleout|teleportout",
pattern = "teleportout|teleout",
permission = "jail.command.jailteleout",
usage = "/jail teleout [jail] (name)"
)

View File

@ -15,7 +15,7 @@ import com.graywolf336.jail.enums.Lang;
maxArgs = 2,
minimumArgs = 2,
needsPlayer = false,
pattern = "transferall|transall",
pattern = "transferall|transall|ta",
permission = "jail.command.jailtransferall",
usage = "/jail transferall [current] [target]"
)