diff --git a/src/main/java/com/massivecraft/massivecore/cmd/MassiveCoreBukkitCommand.java b/src/main/java/com/massivecraft/massivecore/cmd/MassiveCoreBukkitCommand.java index 0477c0d1..dffc8615 100644 --- a/src/main/java/com/massivecraft/massivecore/cmd/MassiveCoreBukkitCommand.java +++ b/src/main/java/com/massivecraft/massivecore/cmd/MassiveCoreBukkitCommand.java @@ -39,6 +39,16 @@ public class MassiveCoreBukkitCommand extends Command implements PluginIdentifia this.massiveCommand = massiveCommand; } + // -------------------------------------------- // + // OVERRIDE: PLUGIN IDENTIFIABLE COMMAND + // -------------------------------------------- // + + @Override + public Plugin getPlugin() + { + return massiveCommand.getRegisteredPlugin(); + } + // -------------------------------------------- // // OVERRIDE: EXECUTE // -------------------------------------------- // @@ -78,14 +88,37 @@ public class MassiveCoreBukkitCommand extends Command implements PluginIdentifia @Override public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { + Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); + String tokenlc = args[args.length - 1].toLowerCase(); + + // First we need a command to find the subcommands in. + MassiveCommand cmd = this.getSubCommand(args, massiveCommand); + + // So if we found a command, and it has subcommands + // we will suggest the aliases instead of senders. + if (cmd != null && cmd.getSubCommands().size() > 0) + { + // If we compiled in Java 8, I could have used streams :( + for (MassiveCommand sub : cmd.getSubCommands()) + { + if ( ! sub.isVisibleTo(sender)) continue; + for (String subAlias : sub.getAliases()) + { + if ( ! subAlias.toLowerCase().startsWith(tokenlc)) continue; + ret.add(subAlias); + } + } + + // return, so senders is not suggested. + return new ArrayList(ret); + } + + // If subcommands didn't work, we will try with senders. List superRet = super.tabComplete(sender, alias, args); if (args.length == 0) return superRet; - Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); ret.addAll(superRet); - String tokenlc = args[args.length - 1].toLowerCase(); - // Add names of all online senders that match and isn't added yet. for (String senderName : IdUtil.getOnlineNames()) { @@ -98,13 +131,43 @@ public class MassiveCoreBukkitCommand extends Command implements PluginIdentifia } // -------------------------------------------- // - // OVERRIDE: PLUGIN IDENTIFIABLE COMMAND + // PRIVATE: TAB COMPLETE // -------------------------------------------- // - @Override - public Plugin getPlugin() - { - return massiveCommand.getRegisteredPlugin(); + private MassiveCommand getSubCommand(String[] args, MassiveCommand cmd) + { + // First we look in the basecommand, then in its subcommands, + // and the next subcommand and so on. + // Until we run out of args or no subcommand is found. + for (int i = 0; i < args.length-1; i++) + { + String arg = args[i]; + // If no subcommand is found we will not look further. + if (cmd == null) break; + + // We have to loop through the subcommands to see if any match the arg. + // if none exists we will get null, thus we break in case of null. + cmd = this.getSubCommand(arg, cmd); + } + + return cmd; } + private MassiveCommand getSubCommand(String arg, MassiveCommand cmd) + { + if (cmd == null || arg == null) return null; + + // We will look in all its subcommands + for (MassiveCommand sub : cmd.getSubCommands()) + { + // and in all those look for an alias that matches + for (String subAlias : sub.getAliases()) + { + // If it matched we had success + if (subAlias.equalsIgnoreCase(arg)) return sub; + } + } + return null; + } + }