/** * Returns a result indicating the command was processed with an * amount of affected blocks. * * @param count The amount of blocks affected * @return The result */ public static CommandResult affectedBlocks(int count) { return builder().affectedBlocks(count).build(); }
/** * Returns a result indicating the command was processed with a single * success. * * @param count The success count * @return The result */ public static CommandResult successCount(int count) { return builder().successCount(count).build(); }
/** * Returns a result indicating the command was processed with an * amount of queries. * * @param count The amount of queries * @return The result */ public static CommandResult queryResult(int count) { return builder().queryResult(count).build(); }
/** * Returns a result indicating the command was processed with an * amount of affected items. * * @param count The amount of items affected * @return The result */ public static CommandResult affectedItems(int count) { return builder().affectedItems(count).build(); }
/** * Returns a result indicating the command was processed with an * amount of affected entities. * * @param count The amount of entities affected * @return The result */ public static CommandResult affectedEntities(int count) { return builder().affectedEntities(count).build(); }
@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 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 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 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 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()); } }
@Test public void testFlaggedCommand() throws CommandException { CommandSpec command = CommandSpec.builder() .arguments(flags() .flag("a").valueFlag(integer(t("quot")), "q").buildWith(string(t("key")))) .executor((src, args) -> { assertEquals(true, args.getOne("a").get()); assertEquals(42, args.getOne("quot").get()); assertEquals("something", args.getOne("key").get()); return CommandResult.builder().successCount(3).build(); }) .build(); process(command, "-a -q 42 something"); process(command, "-aq 42 something"); process(command, "-a something -q 42"); }
/** * Returns a result indicating the command was processed with an * amount of affected items. * * @param count The amount of items affected * @return The result */ public static CommandResult affectedItems(int count) { return builder().affectedItems(count).build(); }
/** * Returns a result indicating the command was processed with an * amount of affected blocks. * * @param count The amount of blocks affected * @return The result */ public static CommandResult affectedBlocks(int count) { return builder().affectedBlocks(count).build(); }
/** * Returns a result indicating the command was processed with an * amount of queries. * * @param count The amount of queries * @return The result */ public static CommandResult queryResult(int count) { return builder().queryResult(count).build(); }
/** * Returns a result indicating the command was processed with a single success. * * @param count The success count * @return The result */ public static CommandResult successCount(int count) { return builder().successCount(count).build(); }
/** * Returns a result indicating the command was processed with an * amount of affected entities. * * @param count The amount of entities affected * @return The result */ public static CommandResult affectedEntities(int count) { return builder().affectedEntities(count).build(); }