/** * Register a child command for a given set of aliases. * * @param callable The command to register * @param aliases The aliases to register it as * @return The child command's mapping, if present */ public Optional<CommandMapping> register(CommandCallable callable, List<String> aliases) { return this.dispatcher.register(callable, aliases); }
/** * Register a child command for a given set of aliases. * * @param callable The command to register * @param aliases The aliases to register it as * @return The child command's mapping, if present */ public Optional<CommandMapping> register(CommandCallable callable, String... aliases) { return this.dispatcher.register(callable, aliases); }
/** * Register a given command using the given list of aliases. * * <p>If there is a conflict with one of the aliases (i.e. that alias * is already assigned to another command), then the alias will be skipped. * It is possible for there to be no alias to be available out of * the provided list of aliases, which would mean that the command would not * be assigned to any aliases.</p> * * <p>The first non-conflicted alias becomes the "primary alias."</p> * * @param callable The command * @param aliases A list of aliases * @return The registered command mapping, unless no aliases could be * registered */ public Optional<CommandMapping> register(CommandCallable callable, List<String> aliases) { return register(callable, aliases, Function.identity()); }
/** * Register a given command using the given list of aliases. * * <p>If there is a conflict with one of the aliases (i.e. that alias * is already assigned to another command), then the alias will be skipped. * It is possible for there to be no alias to be available out of * the provided list of aliases, which would mean that the command would not * be assigned to any aliases.</p> * * <p>The first non-conflicted alias becomes the "primary alias."</p> * * @param callable The command * @param alias An array of aliases * @return The registered command mapping, unless no aliases could be * registered */ public Optional<CommandMapping> register(CommandCallable callable, String... alias) { checkNotNull(alias, "alias"); return register(callable, Arrays.asList(alias)); }
@Test public void testEmptyChildrenWorks() throws CommandException { final AtomicBoolean parent = new AtomicBoolean(); final CommandSpec spec = CommandSpec.builder() .children(ImmutableMap.<List<String>, CommandSpec>of()) .executor((s, c) -> { parent.set(true); return CommandResult.success(); }) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "emptyparent"); execute.process(mock(CommandSource.class), "emptyparent"); assertTrue(parent.get()); }
@Test public void testNoArgsFunctional() throws CommandException, NoSuchFieldException, IllegalAccessException { CommandManager cm = mock(CommandManager.class); TestHooks.setInstance("commandManager", cm); CommandSpec cmd = CommandSpec.builder() .executor((src, args) -> CommandResult.empty()) .build(); final SimpleDispatcher dispatcher = new SimpleDispatcher(); dispatcher.register(cmd, "cmd"); dispatcher.process(Mockito.mock(CommandSource.class), "cmd"); }
@Test public void testSimpleChildCommand() throws CommandException { final AtomicBoolean childExecuted = new AtomicBoolean(); final CommandSpec spec = CommandSpec.builder() .children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder() .executor((src, args) -> { childExecuted.set(true); return CommandResult.builder().successCount(1).build(); }) .build())) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "parent"); execute.process(mock(CommandSource.class), "parent child"); assertTrue(childExecuted.get()); }
@Test public void testEmptyChildrenWorksWithArgument() throws CommandException { final AtomicBoolean parent = new AtomicBoolean(); final CommandSpec spec = CommandSpec.builder() .arguments(GenericArguments.optional(GenericArguments.string(Text.of("a")))) .children(ImmutableMap.<List<String>, CommandSpec>of()) .executor((s, c) -> { parent.set(true); return CommandResult.success(); }) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "emptyparentwith"); execute.process(mock(CommandSource.class), "emptyparentwith child"); assertTrue(parent.get()); }
@Test public void testEmptyChildrenWorksWithOptionalArgument() throws CommandException { final AtomicBoolean parent = new AtomicBoolean(); final CommandSpec spec = CommandSpec.builder() .arguments(GenericArguments.optional(GenericArguments.string(Text.of("b")))) .children(ImmutableMap.<List<String>, CommandSpec>builder() .put(Lists.newArrayList("aaa"), CommandSpec.builder().executor((s, c) -> CommandResult.empty()).build()).build()) .executor((s, c) -> { parent.set(true); return CommandResult.success(); }) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "emptyparentwithopt"); execute.process(mock(CommandSource.class), "emptyparentwithopt"); assertTrue(parent.get()); }
@Test public void testSimpleChildCommandIsSuppressedOnError() throws CommandException { final AtomicBoolean parentExecuted = new AtomicBoolean(); final AtomicBoolean childExecuted = new AtomicBoolean(); final CommandSpec spec = CommandSpec.builder() .children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder() .arguments(GenericArguments.literal(Text.of("test"), "test")) .executor((src, args) -> { childExecuted.set(true); return CommandResult.builder().successCount(1).build(); }) .build())) .arguments(GenericArguments.literal(Text.of("t"), "child")) .executor((src, args) -> { parentExecuted.set(true); return CommandResult.success(); }) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "parent"); execute.process(mock(CommandSource.class), "parent child"); assertFalse(childExecuted.get()); assertTrue(parentExecuted.get()); }
@Test public void testErrorOnNonExistentChildWithNoExecutor() throws CommandException { final CommandSpec spec = CommandSpec.builder() .children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder() .executor((src, args) -> CommandResult.builder().successCount(1).build()) .build())) .childArgumentParseExceptionFallback(false) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "parent"); try { execute.process(mock(CommandSource.class), "parent wrong"); } catch (ArgumentParseException ex) { assertEquals("Input command wrong was not a valid subcommand!\nwrong\n^", ex.getMessage()); } }
@Test public void testSimpleChildCommandIsThrownOnErrorWhenSelected() throws CommandException { final AtomicBoolean parentExecuted = new AtomicBoolean(); final AtomicBoolean childExecuted = new AtomicBoolean(); final CommandSpec spec = CommandSpec.builder() .children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder() .arguments(GenericArguments.literal(Text.of("test"), "test")) .executor((src, args) -> { childExecuted.set(true); return CommandResult.builder().successCount(1).build(); }) .build())) .arguments(GenericArguments.literal(Text.of("t"), "child")) .executor((src, args) -> { parentExecuted.set(true); return CommandResult.success(); }) .childArgumentParseExceptionFallback(false) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "parent"); try { execute.process(mock(CommandSource.class), "parent child"); } catch (ArgumentParseException ex) { // ignored - we check this with the booleans } assertFalse(childExecuted.get()); assertFalse(parentExecuted.get()); }
@Test public void testErrorOnNonExistentChildWithNoOtherParameters() throws CommandException { final CommandSpec spec = CommandSpec.builder() .children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder() .executor((src, args) -> CommandResult.builder().successCount(1).build()) .build())) .childArgumentParseExceptionFallback(false) .executor((src, args) -> CommandResult.success()) .build(); final SimpleDispatcher execute = new SimpleDispatcher(); execute.register(spec, "parent"); try { execute.process(mock(CommandSource.class), "parent wrong"); } catch (ArgumentParseException ex) { assertEquals("Input command wrong was not a valid subcommand!\nwrong\n^", ex.getMessage()); } }
/** * Register a child command for a given set of aliases. * * @param callable The command to register * @param aliases The aliases to register it as * @return The child command's mapping, if present */ public Optional<CommandMapping> register(CommandCallable callable, List<String> aliases) { return this.dispatcher.register(callable, aliases); }
/** * Register a child command for a given set of aliases. * * @param callable The command to register * @param aliases The aliases to register it as * @return The child command's mapping, if present */ public Optional<CommandMapping> register(CommandCallable callable, String... aliases) { return this.dispatcher.register(callable, aliases); }
/** * Register a given command using the given list of aliases. * * <p>If there is a conflict with one of the aliases (i.e. that alias * is already assigned to another command), then the alias will be skipped. * It is possible for there to be no alias to be available out of * the provided list of aliases, which would mean that the command would not * be assigned to any aliases.</p> * * <p>The first non-conflicted alias becomes the "primary alias."</p> * * @param callable The command * @param aliases A list of aliases * @return The registered command mapping, unless no aliases could be registered */ public Optional<CommandMapping> register(CommandCallable callable, List<String> aliases) { return register(callable, aliases, Function.identity()); }
/** * Register a given command using the given list of aliases. * * <p>If there is a conflict with one of the aliases (i.e. that alias * is already assigned to another command), then the alias will be skipped. * It is possible for there to be no alias to be available out of * the provided list of aliases, which would mean that the command would not * be assigned to any aliases.</p> * * <p>The first non-conflicted alias becomes the "primary alias."</p> * * @param callable The command * @param alias An array of aliases * @return The registered command mapping, unless no aliases could be registered */ public Optional<CommandMapping> register(CommandCallable callable, String... alias) { checkNotNull(alias, "alias"); return register(callable, Arrays.asList(alias)); }