@Override public List<String> getSuggestions(CommandSource src, final String arguments, @Nullable Location<World> targetPosition) throws CommandException { final String[] argSplit = arguments.split(" ", 2); Optional<CommandMapping> cmdOptional = get(argSplit[0], src); if (argSplit.length == 1) { return filterCommands(src, argSplit[0]).stream().collect(ImmutableList.toImmutableList()); } else if (!cmdOptional.isPresent()) { return ImmutableList.of(); } return cmdOptional.get().getCallable().getSuggestions(src, argSplit[1], targetPosition); }
@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, 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(); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { CommandMapping mapping = args.<CommandMapping>getOne(getUntranslatedKey()).orElse(null); if (mapping == null) { if (this.fallbackExecutor == null) { throw new CommandException(t("Invalid subcommand state -- no more than one mapping may be provided for child arg %s", getKey())); } return this.fallbackExecutor.execute(src, args); } if (mapping.getCallable() instanceof CommandSpec) { CommandSpec spec = ((CommandSpec) mapping.getCallable()); spec.checkPermission(src); return spec.getExecutor().execute(src, args); } final String arguments = args.<String>getOne(getUntranslatedKey() + "_args").orElse(""); return mapping.getCallable().process(src, arguments); }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.commands, input -> input.getCallable().testPermission(src)).keys().elementSet(); }
return ImmutableList.of(); if (child.get().getCallable() instanceof CommandSpec) { return ((CommandSpec) child.get().getCallable()).complete(src, args, context); .getCallable() .getSuggestions(src, arguments, context.<Location<World>>getOne(CommandContext.TARGET_BLOCK_ARG).orElse(null)); } catch (CommandException e) {
@Override public Optional<Text> getHelp(CommandSource source) { if (this.commands.isEmpty()) { return Optional.empty(); } Text.Builder build = t("Available commands:\n").toBuilder(); for (Iterator<String> it = filterCommands(source).iterator(); it.hasNext();) { final Optional<CommandMapping> mappingOpt = get(it.next(), source); if (!mappingOpt.isPresent()) { continue; } CommandMapping mapping = mappingOpt.get(); final Optional<Text> description = mapping.getCallable().getShortDescription(source); build.append(Text.builder(mapping.getPrimaryAlias()) .color(TextColors.GREEN) .style(TextStyles.UNDERLINE) .onClick(TextActions.suggestCommand("/" + mapping.getPrimaryAlias())).build(), SPACE_TEXT, description.orElse(mapping.getCallable().getUsage(source))); if (it.hasNext()) { build.append(Text.NEW_LINE); } } return Optional.of(build.build()); }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.dispatcher.getAll(), input -> input != null && input.getCallable().testPermission(src) ) .keys() .elementSet(); }
@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(); } }
final CommandMapping mapping = optionalCommandMapping.get(); try { if ((mapping.getCallable() instanceof CommandSpec)) { CommandSpec spec = ((CommandSpec) mapping.getCallable()); spec.populateContext(source, args, context); } else { throw new ArgumentParseException.WithUsage(ex, Text.of(key, " ", mapping.getCallable().getUsage(source)));
@Override public List<String> getSuggestions(CommandSource src, final String arguments, @Nullable Location<World> targetPosition) throws CommandException { final String[] argSplit = arguments.split(" ", 2); Optional<CommandMapping> cmdOptional = get(argSplit[0], src); if (argSplit.length == 1) { return filterCommands(src).stream().filter(new StartsWithPredicate(argSplit[0])).collect(GuavaCollectors.toImmutableList()); } else if (!cmdOptional.isPresent()) { return ImmutableList.of(); } return cmdOptional.get().getCallable().getSuggestions(src, argSplit[1], targetPosition); }
@Override public boolean testPermission(CommandSource source) { for (CommandMapping mapping : this.commands.values()) { if (mapping.getCallable().testPermission(source)) { return true; } } return false; }
public CachedCommand(CommandMapping cmd) { super(null); this.name = cmd.getPrimaryAlias(); this.aliases = cmd.getAllAliases().toArray(new String[cmd.getAllAliases().size()]); try { this.usage = cmd.getCallable().getUsage(instance).toBuilder().build(); this.description = cmd.getCallable().getShortDescription(instance).orElse(Text.EMPTY).toBuilder().build(); this.help = cmd.getCallable().getHelp(instance).orElse(Text.EMPTY).toBuilder().build(); } catch (Exception ignored) {} }
@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] : ""; final CommandCallable spec = cmdOptional.get().getCallable(); try { return spec.process(source, arguments); } catch (CommandNotFoundException e) { throw new CommandException(t("No such child command: %s", e.getCommand())); } }
return ImmutableList.of(); if (child.get().getCallable() instanceof CommandSpec) { return ((CommandSpec) child.get().getCallable()).complete(src, args, context); } else { args.nextIfPresent(); return child.get().getCallable().getSuggestions(src, arguments, context.<Location<World>>getOne(CommandContext.TARGET_BLOCK_ARG).orElse(null)); } catch (CommandException e) { Text eText = e.getText();
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { CommandMapping mapping = args.<CommandMapping>getOne(getUntranslatedKey()).orElse(null); if (mapping == null) { if (this.fallbackExecutor != null) { return this.fallbackExecutor.execute(src, args); } else { throw new CommandException(t("Invalid subcommand state -- no more than one mapping may be provided for child arg %s", getKey())); } } if (mapping.getCallable() instanceof CommandSpec) { CommandSpec spec = ((CommandSpec) mapping.getCallable()); spec.checkPermission(src); return spec.getExecutor().execute(src, args); } else { final String arguments = args.<String>getOne(getUntranslatedKey() + "_args").orElse(""); return mapping.getCallable().process(src, arguments); } }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.commands, input -> input.getCallable().testPermission(src)).keys().elementSet(); }
@Override public Optional<Text> getHelp(CommandSource source) { if (this.commands.isEmpty()) { return Optional.empty(); } Text.Builder build = t("Available commands:\n").toBuilder(); for (Iterator<String> it = filterCommands(source).iterator(); it.hasNext();) { final Optional<CommandMapping> mappingOpt = get(it.next(), source); if (!mappingOpt.isPresent()) { continue; } CommandMapping mapping = mappingOpt.get(); final Optional<Text> description = mapping.getCallable().getShortDescription(source); build.append(Text.builder(mapping.getPrimaryAlias()) .color(TextColors.GREEN) .style(TextStyles.UNDERLINE) .onClick(TextActions.suggestCommand("/" + mapping.getPrimaryAlias())).build(), SPACE_TEXT, description.orElse(mapping.getCallable().getUsage(source))); if (it.hasNext()) { build.append(Text.NEW_LINE); } } return Optional.of(build.build()); }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { super.parse(source, args, context); final CommandMapping mapping = context.<CommandMapping>getOne(getUntranslatedKey()).get(); if ((mapping.getCallable() instanceof CommandSpec)) { CommandSpec spec = ((CommandSpec) mapping.getCallable()); spec.populateContext(source, args, context); } else { if (args.hasNext()) { args.next(); } context.putArg(getUntranslatedKey() + "_args", args.getRaw().substring(args.getRawPosition())); while (args.hasNext()) { args.next(); } } }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.dispatcher.getAll(), input -> input != null && input.getCallable().testPermission(src)).keys().elementSet(); }