@Override public synchronized Set<String> getPrimaryAliases() { Set<String> aliases = new HashSet<>(); for (CommandMapping mapping : this.commands.values()) { aliases.add(mapping.getPrimaryAlias()); } return Collections.unmodifiableSet(aliases); }
@Override public synchronized Set<String> getAliases() { Set<String> aliases = new HashSet<>(); for (CommandMapping mapping : this.commands.values()) { aliases.addAll(mapping.getAllAliases()); } return Collections.unmodifiableSet(aliases); }
@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 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()); }
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 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 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 boolean testPermission(CommandSource source) { for (CommandMapping mapping : this.commands.values()) { if (mapping.getCallable().testPermission(source)) { return true; } } return false; }
@Override public synchronized Set<String> getAliases() { Set<String> aliases = new HashSet<>(); for (CommandMapping mapping : this.commands.values()) { aliases.addAll(mapping.getAllAliases()); } return Collections.unmodifiableSet(aliases); }
@Override public synchronized Set<String> getPrimaryAliases() { Set<String> aliases = new HashSet<>(); for (CommandMapping mapping : this.commands.values()) { aliases.add(mapping.getPrimaryAlias()); } return Collections.unmodifiableSet(aliases); }
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(); }
/** * Gets the passed command as a cached object. This method first tries to get the command from the cache, * and if it is not found uses the {@link #updateCommand(CommandMapping)} method to convert it into a * cached object. * @param command The command which is returned in it's cached form. * @return The cached version of the specified command. */ public CachedCommand getCommand(CommandMapping command) { Optional<CachedCommand> e = getCommand(command.getPrimaryAlias()); return e.orElseGet(() -> updateCommand(command)); }
@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); }
public void updateCommands() { assert Sponge.getServer().isMainThread(); commands.clear(); Collection<CommandMapping> newCommands = Sponge.getCommandManager().getAll().values(); for (CommandMapping cmd : newCommands) { if (commands.containsKey(cmd.getPrimaryAlias())) continue; commands.put(cmd.getPrimaryAlias(), new CachedCommand(cmd)); } }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.commands, input -> input.getCallable().testPermission(src)).keys().elementSet(); }
@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(); }
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) {
private void actOnCommandEvent(SendCommandEvent event) { String commandName = event.getCommand(); boolean logUnknownCommands = config.isTrue(DataGatheringSettings.LOG_UNKNOWN_COMMANDS); boolean combineCommandAliases = config.isTrue(DataGatheringSettings.COMBINE_COMMAND_ALIASES); if (!logUnknownCommands || combineCommandAliases) { Optional<? extends CommandMapping> existingCommand = Sponge.getCommandManager().get(commandName); if (!existingCommand.isPresent()) { if (!logUnknownCommands) { return; } } else if (combineCommandAliases) { commandName = existingCommand.get().getPrimaryAlias(); } } processing.submit(processors.commandProcessor(commandName)); }
private Set<String> filterCommands(final CommandSource src) { return Multimaps.filterValues(this.dispatcher.getAll(), input -> input != null && input.getCallable().testPermission(src) ) .keys() .elementSet(); }
@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); } }