Skip to content

Commit

Permalink
Add tab completion procedures to command scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Nov 13, 2014
1 parent f636fac commit 9c507b8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Expand Up @@ -60,7 +60,19 @@ public class CommandScriptContainer extends ScriptContainer {
// # should be allowed to view the help for this command.
// # Available context: <context.server> returns whether the server is viewing the help (a player if false).
// allowed help:
// - determine <player.is_op>
// - determine <player.is_op||<context.server>>
//
// # 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: <context.args> returns a list of input arguments.
// # <context.raw_args> returns all the arguments as raw text.
// # <context.server> returns whether the server is using tab completion (a player if false).
// # <context.alias> returns the command alias being used.
// tab complete:
// - if !<player.is_op||<context.server>> queue clear
// - determine <server.list_online_players.parse[name].include[pizza|potato|anchovy].filter[starts_with[<context.args.last>]]>
//
// # The script that will run when the command is executed.
// # No, you do not need '- determine fulfilled' or anything of the sort, since
Expand All @@ -69,12 +81,13 @@ public class CommandScriptContainer extends ScriptContainer {
// # <context.raw_args> returns all the arguments as raw text.
// # <context.server> returns whether the server is running the command (a player if false).
// script:
// - if !<player.is_op> {
// - if !<player.is_op||<context.server>> {
// - narrate "<red>You do not have permission for that command."
// - queue clear
// }
// - narrate "Yay!"
// - narrate "My command worked!"
// - narrate "And I typed '<context.raw_args>'!"
// </code>
//
// -->
Expand Down Expand Up @@ -132,7 +145,30 @@ public boolean runAllowedHelpProcedure(dPlayer player, dNPC npc, Map<String, dOb
return DetermineCommand.getOutcome(id).equalsIgnoreCase("true");
}

public List<String> runTabCompleteProcedure(dPlayer player, dNPC npc, Map<String, dObject> context) {
// Add the reqId to each of the entries for the determine command
List<ScriptEntry> 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<String, dObject> 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<String>();
}

public boolean hasAllowedHelpProcedure() {
return contains("ALLOWED HELP");
}

public boolean hasTabCompleteProcedure() {
return contains("TAB COMPLETE");
}
}
37 changes: 37 additions & 0 deletions src/main/java/net/aufdemrand/denizen/utilities/DenizenCommand.java
Expand Up @@ -77,4 +77,41 @@ public boolean execute(CommandSender commandSender, String commandLabel, String[
public boolean isRegistered() {
return true;
}

@Override
public List<String> tabComplete(CommandSender commandSender, String alias, String[] arguments) {
if (!script.hasTabCompleteProcedure()) return super.tabComplete(commandSender, alias, arguments);
Map<String, dObject> context = new HashMap<String, dObject>();
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<String> 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);
}
}

0 comments on commit 9c507b8

Please sign in to comment.