/** * 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 a single * success. * * @param count The success count * @return The result */ public static CommandResult successCount(int count) { return builder().successCount(count).build(); }
/** * Perform a permissions check, throwing an exception if the required * permissions are not present. * * @param commander the source to check against * @param permission The permission to check * @throws CommandException if the source does not have permission */ public void checkPermission(CommandSource commander, String permission) throws CommandException { if (!commander.hasPermission(permission)) { throw new CommandException(t("You do not have permission to use this command!")); } }
@Override public CommandResult process(CommandSource source, String commandLine) throws CommandException { final String[] argSplit = commandLine.split(" ", 2); Optional<CommandMapping> cmdOptional = get(argSplit[0], source); if (!cmdOptional.isPresent()) { throw new CommandNotFoundException(t("commands.generic.notFound"), argSplit[0]); // TODO: Fix properly to use a SpongeTranslation?? } final String arguments = argSplit.length > 1 ? argSplit[1] : ""; CommandMapping mapping = cmdOptional.get(); Optional<PluginContainer> pluginOwner = Sponge.getCommandManager().getOwner(mapping); pluginOwner.ifPresent(pluginContainer -> Sponge.getCauseStackManager().pushCause(pluginContainer)); final CommandCallable spec = mapping.getCallable(); Sponge.getCauseStackManager().pushCause(spec); try { return spec.process(source, arguments); } catch (CommandNotFoundException e) { throw new CommandException(t("No such child command: %s", e.getCommand())); } finally { if (pluginOwner.isPresent()) { Sponge.getCauseStackManager().popCause(); } Sponge.getCauseStackManager().popCause(); } }
@Override public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) { if (!src.hasPermission(this.permission)) { return ImmutableList.of(); } return this.element.complete(src, args, context); }
@Override public void printRaw(String msg) { for (String part : msg.split("\n")) { sender.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(part)); } }
@Override public boolean testPermission(CommandSource source) { return this.permission == null || source.hasPermission(this.permission); }
/** * 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 entities. * * @param count The amount of entities affected * @return The result */ public static CommandResult affectedEntities(int count) { return builder().affectedEntities(count).build(); }
@Override public String getName() { return sender.getName(); }
@Override public boolean testPermission(CommandSource source) { for (CommandMapping mapping : this.commands.values()) { if (mapping.getCallable().testPermission(source)) { return true; } } return false; }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.dispatcher.getAll(), input -> input != null && input.getCallable().testPermission(src) ) .keys() .elementSet(); }
/** * Check the relevant permission for this command with the provided source, * throwing an exception if the source does not have permission to use * the command. * * @param source The source to check * @throws CommandException if the source does not have permission */ public void checkPermission(CommandSource source) throws CommandException { checkNotNull(source, "source"); if (!testPermission(source)) { throw new CommandPermissionException(); } }
/** * 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); } }
/** * Returns a {@link Builder}. * * @return A new command result builder */ public static Builder builder() { return new Builder(); }
private Text getSuperText() { return super.getText(); }
@Override public boolean testPermission(CommandSource source) { for (String perm : command.getDescription().getPermissions()) { if (!source.hasPermission(perm)) { return false; } } return true; }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.commands, input -> input.getCallable().testPermission(src)).keys().elementSet(); }
private Set<String> filterCommands(final CommandSource src, String start) { ListMultimap<String, CommandMapping> map = Multimaps.filterKeys(this.commands, input -> input != null && input.toLowerCase().startsWith(start.toLowerCase())); return Multimaps.filterValues(map, input -> input.getCallable().testPermission(src)).keys().elementSet(); }