diff options
Diffstat (limited to 'dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java')
-rw-r--r-- | dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java | 103 |
1 files changed, 67 insertions, 36 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java b/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java index 6d38174..7577808 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java @@ -123,8 +123,6 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom @Override public ModifiableCommandAddress getCommandTarget(CommandSender sender, ArgumentBuffer buffer) { - //System.out.println("Buffer cursor upon getCommandTarget: " + buffer.getCursor()); - ModifiableCommandAddress cur = this; ChildCommandAddress child; while (buffer.hasNext()) { @@ -139,16 +137,25 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom cur = child; } - /* - if (!cur.hasCommand() && cur.hasHelpCommand()) { - cur = cur.getHelpCommand(); - } else { - while (!cur.hasCommand() && cur.hasParent()) { - cur = cur.getParent(); + return cur; + } + + @Override + public ModifiableCommandAddress getCommandTarget(ExecutionContext context, ArgumentBuffer buffer) throws CommandException { + CommandSender sender = context.getSender(); + ModifiableCommandAddress cur = this; + ChildCommandAddress child; + while (buffer.hasNext()) { + child = cur.getChild(buffer.next(), context); + if (child == null + || (child.hasCommand() && !child.getCommand().isVisibleTo(sender)) + || (cur.hasCommand() && cur.getCommand().takePrecedenceOverSubcommand(buffer.peekPrevious(), buffer.getUnaffectingCopy()))) { buffer.rewind(); + break; } + + cur = child; } - */ return cur; } @@ -165,18 +172,32 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom @Override public boolean dispatchCommand(CommandSender sender, ArgumentBuffer buffer) { - ModifiableCommandAddress targetAddress = getCommandTarget(sender, buffer); - Command target = targetAddress.getCommand(); - - if (target == null || target instanceof DefaultGroupCommand) { - if (targetAddress.hasHelpCommand()) { - target = targetAddress.getHelpCommand().getCommand(); - } else if (target == null){ - return false; + ExecutionContext context = new ExecutionContext(sender, false); + + ModifiableCommandAddress targetAddress = null; + + try { + targetAddress = getCommandTarget(context, buffer); + Command target = targetAddress.getCommand(); + + if (target == null || target instanceof DefaultGroupCommand) { + if (targetAddress.hasHelpCommand()) { + target = targetAddress.getHelpCommand().getCommand(); + } else if (target == null){ + return false; + } + } + + context.targetAcquired(targetAddress, target, buffer); + target.executeWithContext(context); + + } catch (Throwable t) { + if (targetAddress == null) { + targetAddress = this; } + targetAddress.getChatController().handleException(sender, context, t); } - target.execute(sender, targetAddress, buffer); return true; } @@ -192,28 +213,38 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom @Override public List<String> getTabCompletions(CommandSender sender, Location location, ArgumentBuffer buffer) { - ICommandAddress target = getCommandTarget(sender, buffer); - List<String> out = target.hasCommand() ? target.getCommand().tabComplete(sender, target, location, buffer.getUnaffectingCopy()) : Collections.emptyList(); - - int cursor = buffer.getCursor(); - String input; - if (cursor >= buffer.size()) { - input = ""; - } else { - input = buffer.get(cursor).toLowerCase(); - } + ExecutionContext context = new ExecutionContext(sender, true); + + try { + ICommandAddress target = getCommandTarget(context, buffer); + List<String> out = target.hasCommand() + ? target.getCommand().tabComplete(sender, target, location, buffer.getUnaffectingCopy()) + : Collections.emptyList(); + + int cursor = buffer.getCursor(); + String input; + if (cursor >= buffer.size()) { + input = ""; + } else { + input = buffer.get(cursor).toLowerCase(); + } - boolean wrapped = false; - for (String child : target.getChildren().keySet()) { - if (child.toLowerCase().startsWith(input)) { - if (!wrapped) { - out = new ArrayList<>(out); - wrapped = true; + boolean wrapped = false; + for (String child : target.getChildren().keySet()) { + if (child.toLowerCase().startsWith(input)) { + if (!wrapped) { + out = new ArrayList<>(out); + wrapped = true; + } + out.add(child); } - out.add(child); } + + return out; + + } catch (CommandException ex) { + return Collections.emptyList(); } - return out; } } |