@Override public Optional<CommandMapping> get(String alias) { return get(alias, null); }
@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 Text getUsage(final CommandSource source) { final Text.Builder build = Text.builder(); Iterable<String> filteredCommands = filterCommands(source).stream() .filter(input -> { if (input == null) { return false; } final Optional<CommandMapping> ret = get(input, source); return ret.isPresent() && ret.get().getPrimaryAlias().equals(input); }) .collect(Collectors.toList()); for (Iterator<String> it = filteredCommands.iterator(); it.hasNext();) { build.append(Text.of(it.next())); if (it.hasNext()) { build.append(CommandMessageFormatting.PIPE_TEXT); } } return build.build(); }
@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()); }
Optional<CommandMapping> child = this.dispatcher.get(commandComponent.get(), src); if (!child.isPresent()) { return ImmutableList.of();
@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(); } }
Optional<CommandMapping> optionalCommandMapping = this.dispatcher.get(key, source); if (optionalCommandMapping.isPresent()) { final CommandMapping mapping = optionalCommandMapping.get();
@Override public Optional<CommandMapping> get(String alias) { return get(alias, null); }
@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 protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { final String key = args.next(); final Optional<CommandMapping> mapping = this.dispatcher.get(key, source); if (!mapping.isPresent()) { throw args.createError(t("Input command %s was not a valid subcommand!", key)); } return mapping.get(); }
@Override public Text getUsage(final CommandSource source) { final Text.Builder build = Text.builder(); Iterable<String> filteredCommands = filterCommands(source).stream() .filter(input -> { if (input == null) { return false; } final Optional<CommandMapping> ret = get(input, source); return ret.isPresent() && ret.get().getPrimaryAlias().equals(input); }) .collect(Collectors.toList()); for (Iterator<String> it = filteredCommands.iterator(); it.hasNext();) { build.append(Text.of(it.next())); if (it.hasNext()) { build.append(CommandMessageFormatting.PIPE_TEXT); } } return build.build(); }
@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())); } }
if (commandComponent.isPresent()) { if (args.hasNext()) { Optional<CommandMapping> child = this.dispatcher.get(commandComponent.get(), src); if (!child.isPresent()) { return ImmutableList.of();
@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()); }