From 9c507b8dd2a84f769dfe5aeab6ced6515ff8c6db Mon Sep 17 00:00:00 2001 From: Morphan1 Date: Wed, 12 Nov 2014 20:36:22 -0500 Subject: [PATCH] Add tab completion procedures to command scripts --- .../core/CommandScriptContainer.java | 40 ++++++++++++++++++- .../denizen/utilities/DenizenCommand.java | 37 +++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/CommandScriptContainer.java b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/CommandScriptContainer.java index 3e43d3da37..5be4147d56 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/CommandScriptContainer.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/CommandScriptContainer.java @@ -60,7 +60,19 @@ public class CommandScriptContainer extends ScriptContainer { // # should be allowed to view the help for this command. // # Available context: returns whether the server is viewing the help (a player if false). // allowed help: - // - determine + // - determine > + // + // # The procedure-based script that will run when a player uses tab completion to + // # predict words. This should return a dList of words that the player can tab through, + // # based on the arguments they have already typed. Leaving this node out will result + // # in using Bukkit's built-in tab completion. + // # Available context: returns a list of input arguments. + // # returns all the arguments as raw text. + // # returns whether the server is using tab completion (a player if false). + // # returns the command alias being used. + // tab complete: + // - if !> queue clear + // - determine ]]> // // # The script that will run when the command is executed. // # No, you do not need '- determine fulfilled' or anything of the sort, since @@ -69,12 +81,13 @@ public class CommandScriptContainer extends ScriptContainer { // # returns all the arguments as raw text. // # returns whether the server is running the command (a player if false). // script: - // - if ! { + // - if !> { // - narrate "You do not have permission for that command." // - queue clear // } // - narrate "Yay!" // - narrate "My command worked!" + // - narrate "And I typed ''!" // // // --> @@ -132,7 +145,30 @@ public boolean runAllowedHelpProcedure(dPlayer player, dNPC npc, Map runTabCompleteProcedure(dPlayer player, dNPC npc, Map context) { + // Add the reqId to each of the entries for the determine command + List entries = getEntries(new BukkitScriptEntryData(player, npc), "TAB COMPLETE"); + long id = DetermineCommand.getNewId(); + ScriptBuilder.addObjectToEntries(entries, "ReqId", id); + + ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(getName())).setReqId(id).addEntries(entries); + if (context != null) { + for (Map.Entry entry : context.entrySet()) { + queue.addContext(entry.getKey(), entry.getValue()); + } + } + queue.start(); + if (DetermineCommand.hasOutcome(id)) + return dList.valueOf(DetermineCommand.getOutcome(id)); + else + return new ArrayList(); + } + public boolean hasAllowedHelpProcedure() { return contains("ALLOWED HELP"); } + + public boolean hasTabCompleteProcedure() { + return contains("TAB COMPLETE"); + } } diff --git a/src/main/java/net/aufdemrand/denizen/utilities/DenizenCommand.java b/src/main/java/net/aufdemrand/denizen/utilities/DenizenCommand.java index 3fd6a3787d..7e151fbe3d 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/DenizenCommand.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/DenizenCommand.java @@ -77,4 +77,41 @@ public boolean execute(CommandSender commandSender, String commandLabel, String[ public boolean isRegistered() { return true; } + + @Override + public List tabComplete(CommandSender commandSender, String alias, String[] arguments) { + if (!script.hasTabCompleteProcedure()) return super.tabComplete(commandSender, alias, arguments); + Map context = new HashMap(); + String raw_args = ""; + if (arguments.length > 0) { + StringBuilder rawArgsBuilder = new StringBuilder(); + for (String arg : arguments) { + rawArgsBuilder.append(arg).append(' '); + } + raw_args = rawArgsBuilder.substring(0, rawArgsBuilder.length() - 1); + } + List args = Arrays.asList(aH.buildArgs(raw_args)); + context.put("args", new dList(args)); + context.put("raw_args", new Element(raw_args)); + context.put("alias", new Element(alias)); + dPlayer player = null; + dNPC npc = null; + if (commandSender instanceof Player) { + Player pl = (Player) commandSender; + if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(pl)) + npc = dNPC.fromEntity(pl); + else + player = dPlayer.mirrorBukkitPlayer(pl); + context.put("server", Element.FALSE); + } + else { + context.put("server", Element.TRUE); + } + if (Depends.citizens != null && npc == null) { + NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender); + if (citizen != null) + npc = dNPC.mirrorCitizensNPC(citizen); + } + return script.runTabCompleteProcedure(player, npc, context); + } }