@Override public CommandResult process(CommandSource source, String arguments) throws org.spongepowered.api.command.CommandException { CommandEvent weEvent = new CommandEvent(SpongeWorldEdit.inst().wrapCommandSource(source), command.getPrimaryAlias() + " " + arguments); WorldEdit.getInstance().getEventBus().post(weEvent); return weEvent.isCancelled() ? CommandResult.success() : CommandResult.empty(); }
@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()); }
/** * Builds the {@link CommandResult}. * * @return A CommandResult with the specified settings */ public CommandResult build() { return new CommandResult(this.successCount, this.affectedBlocks, this.affectedEntities, this.affectedItems, this.queryResult); } }
@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"); }
/** * 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(); }
public CachedCommandResult(CommandResult result) { super(null); this.affectedBlocks = result.getAffectedBlocks().orElse(null); this.affectedEntities = result.getAffectedEntities().orElse(null); this.affectedItems = result.getAffectedItems().orElse(null); this.queryResult = result.getQueryResult().orElse(null); this.successCount = result.getSuccessCount().orElse(null); }
/** * Tests that if a {@link CommandSource} that is not a player is provided, they can execute a standard command. * * @throws CommandException */ @Test public void testThatCommandSourcesCanExecuteStandardCommand() throws CommandException { setupSpongeMock(); BasicCommand cmd = new BasicCommand(); getInjector().injectMembers(cmd); cmd.postInit(); CommandSource mock = getMockCommandSource(); CommandResult result = cmd.process(mock, ""); Assert.assertTrue("There should have been one success!", result.getSuccessCount().orElse(0) == 1); }
@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()); }
@Override public CommandResult process(CommandSource source, String arguments) { if (!testPermission(source)) { source.sendMessage(Text.builder("You do not have permission to use this command.").color(TextColors.RED).build()); return CommandResult.empty(); } this.sparkPlatform.executeCommand(source, arguments.split(" ")); return CommandResult.empty(); }
/** * 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(); }
/** * Tests that if a {@link Player} is provided, they can execute a command for players. * * @throws CommandException */ @Test public void testThatPlayersCanExecutePlayerCommand() throws CommandException { setupSpongeMock(); PlayerCommand cmd = new PlayerCommand(); getInjector().injectMembers(cmd); cmd.postInit(); Player mock = getMockPlayer(); CommandResult result = cmd.process(mock, ""); Assert.assertTrue("There should have been one success!", result.getSuccessCount().orElse(0) == 1); }
@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()); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { src.sendMessage(docsText); return CommandResult.success(); } }
@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()); }
/** * 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(); }
/** * Tests that if a {@link Player} is provided, they can execute a standard command. * * @throws CommandException */ @Test public void testThatPlayerSourcesCanExecuteStandardCommand() throws CommandException { setupSpongeMock(); BasicCommand cmd = new BasicCommand(); getInjector().injectMembers(cmd); cmd.postInit(); Player mock = getMockPlayer(); CommandResult result = cmd.process(mock, ""); Assert.assertTrue("There should have been one success!", result.getSuccessCount().orElse(0) == 1); }
/** * Builds the {@link CommandResult}. * * @return A CommandResult with the specified settings */ public CommandResult build() { return new CommandResult(this.successCount, this.affectedBlocks, this.affectedEntities, this.affectedItems, this.queryResult); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { String key = args.<String>getOne("key").get(); boolean global = args.hasAny("g"); String namespace = global ? Variables.GLOBAL_NAMESPACE : args.<String>getOne("namespace").orElse(((Player) src).getUniqueId().toString()); if (namespace.equals(Variables.GLOBAL_NAMESPACE) && !global) { src.sendMessage(Text.of(TextColors.RED, "Invalid namespace!")); return CommandResult.empty(); } src.sendMessage(Text.of(TextColors.YELLOW, "Variable " + key + " is set to " + variables.getVariable(namespace, key))); return CommandResult.success(); } }