@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 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 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 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(); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { String id = args.<String>getOne("id").get(); String namespace = args.<String>getOne("namespace").orElse(((Player) src).getUniqueId().toString()); boolean personal = namespace.equals(((Player) src).getUniqueId().toString()); if (!personal && !area.commandDeleteOtherPermissions.hasPermission(src)) { src.sendMessage(Text.of("You do not have permission to use this namespace!")); return CommandResult.empty(); } id = id.toLowerCase(Locale.ENGLISH); File folder = new File(new File(CraftBookPlugin.inst().getWorkingDirectory(), "areas"), namespace); File file = new File(folder, id + CopyManager.getFileSuffix()); if (!folder.exists() || !file.exists()) { src.sendMessage(Text.of(TextColors.RED, "This area does not exist!")); return CommandResult.empty(); } file.delete(); src.sendMessage(Text.of(TextColors.YELLOW, "Area deleted!")); return CommandResult.success(); } }
@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(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { String namespace = args.<String>getOne("namespace").orElse(((Player) src).getUniqueId().toString()); if (!namespace.equals(((Player) src).getUniqueId().toString()) && !area.commandListOtherPermissions.hasPermission(src)) { src.sendMessage(Text.of(TextColors.RED, "You don't have permission to list areas of other namespaces!")); return CommandResult.empty(); } PaginationService paginationService = Sponge.getServiceManager().provide(PaginationService.class).get(); PaginationList.Builder builder = paginationService.builder(); String[] files = new File(new File(CraftBookPlugin.inst().getWorkingDirectory(), "areas"), namespace).list(); List<Text> contents = new ArrayList<>(); for (String file : files) if (file != null) contents.add(Text.of(file)); builder.contents(contents).title(Text.of("Areas")).sendTo(src); return CommandResult.success(); } }
@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(); } variables.removeVariable(namespace, key); src.sendMessage(Text.of(TextColors.YELLOW, "Removed variable " + key)); return CommandResult.success(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) { if (!(src instanceof Player)) { plugin.sendMessage(src, "no-console"); return CommandResult.empty(); } String skinName = args.<String>getOne("skinName").get().toLowerCase().replace("skin-", ""); try { int targetId = Integer.parseInt(skinName); Player receiver = (Player) src; Task.builder().async().execute(new SkinSelector(plugin, receiver, targetId)).submit(plugin); } catch (NumberFormatException numberFormatException) { plugin.sendMessage(src, "invalid-skin-name"); } return CommandResult.success(); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { Optional<BlockOperation> op = args.getOne("uuid"); if (!op.isPresent()) { src.sendMessage(Text.builder("Invalid block operation uuid!") .color(TextColors.DARK_RED) .build()); return CommandResult.empty(); } if (op.get().getStatus() == BlockOperationStatus.RUNNING) { op.get().pause(); } else { op.get().start(); } return CommandResult.success(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { return CommandResult.empty(); } boolean currentState = treeLopper.disabledPlayers.getValue().contains(((Player) src).getUniqueId()); if (args.hasAny("state")) { currentState = args.<Boolean>getOne("state").get(); } else { currentState = !currentState; } treeLopper.disabledPlayers.getValue().remove(((Player) src).getUniqueId()); if (!currentState) { treeLopper.disabledPlayers.getValue().add(((Player) src).getUniqueId()); } return CommandResult.success(); } }
@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(); } if(!Variables.isValidVariableKey(key)) { src.sendMessage(Text.of(TextColors.RED, "Key contains invalid characters!")); return CommandResult.empty(); } String value = args.<String>getOne("value").get(); variables.addVariable(namespace, key, value); src.sendMessage(Text.of(TextColors.YELLOW, "Set variable " + key + " to value " + variables.getVariable(namespace, key))); return CommandResult.success(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { 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(); } List<Text> variableList = new ArrayList<>(); for (Map.Entry<String, String> entry : variables.getVariables(namespace).entrySet()) { variableList.add(Text.of(TextColors.YELLOW, entry.getKey(), TextColors.GRAY, ": ", TextColors.GREEN, entry.getValue())); } PaginationList.builder() .title(Text.of(TextColors.YELLOW, "Variable List")) .header(Text.of(TextColors.GREEN, namespace)) .contents(variableList) .build().sendTo(src); return CommandResult.success(); } }
Optional<String> optUsername = args.getOne("username"); if (!optUsername.isPresent()) { return CommandResult.empty(); return CommandResult.empty(); if (!optUser.isPresent()) { src.sendMessage(Text.builder("Could not find user '" + username + "'").color(TextColors.RED).build()); return CommandResult.empty();
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { Optional<String> optUsername = args.getOne("username"); if (!optUsername.isPresent()) { return CommandResult.empty(); } String username = optUsername.get(); Optional<UserPermissionStruct> optUser = WebAPI.getUserService().removeUser(username); if (!optUser.isPresent()) { src.sendMessage(Text.builder("Couldn't find user to remove " + username) .color(TextColors.RED).build()); return CommandResult.empty(); } src.sendMessage(Text.builder("Removed user ") .append(Text.builder(username).color(TextColors.GOLD).build()) .build()); return CommandResult.success(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) { if (!(src instanceof Player)) { plugin.sendMessage(src, "no-console"); return CommandResult.empty(); } Player receiver = (Player) src; Task.builder().async().execute(new SkinInvalidator(plugin, receiver)).submit(plugin); return CommandResult.success(); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { Optional<BlockOperation> op = args.getOne("uuid"); if (!op.isPresent()) { src.sendMessage(Text.builder("Invalid block operation uuid!") .color(TextColors.DARK_RED) .build()); return CommandResult.empty(); } op.get().stop(null); src.sendMessage(Text.builder("Successfully cancelled block operation") .color(TextColors.DARK_GREEN) .build()); return CommandResult.success(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (args.<String>getOne("channel").isPresent() && args.<String>getOne("message").isPresent()) { String channel = args.<String>getOne("channel").get(); String message = args.<String>getOne("message").get(); for (TextChannel discordChannel : MagiBridge.jda.getTextChannels()) { if (discordChannel.getName().equals(channel)) { for (TextChannel ch : MagiBridge.jda.getTextChannelsByName(channel, true)) { ch.sendMessage(message).queue(); } } } src.sendMessage(Text.builder("Message sent!").color(TextColors.GREEN).build()); return CommandResult.success(); } src.sendMessage(Text.builder("Could not send message! Are you sure the channel exists?").color(TextColors.RED).build()); return CommandResult.empty(); } })
@Listener(order = Order.FIRST, beforeModifications = true) public void onCommand(SendCommandEvent commandEvent, @First Player player) { String command = commandEvent.getCommand(); Optional<? extends CommandMapping> commandOpt = commandManager.get(command); if (commandOpt.isPresent()) { CommandMapping mapping = commandOpt.get(); command = mapping.getPrimaryAlias(); //do not blacklist our own commands if (commandManager.getOwner(mapping) .map(pc -> pc.getId().equals(PomData.ARTIFACT_ID)) .orElse(false)) { return; } } commandEvent.setResult(CommandResult.empty()); if (settings.getGeneral().isCommandOnlyProtection()) { List<String> protectedCommands = settings.getGeneral().getProtectedCommands(); if (protectedCommands.contains(command) && !plugin.getDatabase().isLoggedIn(player)) { player.sendMessage(settings.getText().getProtectedCommand()); commandEvent.setCancelled(true); } } else { checkLoginStatus(commandEvent, player); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { plugin.sendMessage(src, "no-console"); return CommandResult.empty(); } UUID uniqueId = ((Player) src).getUniqueId(); Task.builder().async() .execute(() -> { UserPreference preferences = plugin.getCore().getStorage().getPreferences(uniqueId); Task.builder().execute(() -> sendSkinDetails(uniqueId, preferences)).submit(plugin); }) .submit(plugin); return null; }