@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 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()); }
@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); }
/** * 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)); }
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)); } }
@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(); }
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)); }
@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) {} }
@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); } }
private ExecuteCommandResponse runCommand(SecurityContext context, ExecuteCommandRequest req) { String cmd = req.getCommand().split(" ")[0]; Optional<? extends CommandMapping> map = Sponge.getCommandManager().get(cmd); if (!map.isPresent()) { return new ExecuteCommandResponse(req.command, "Unknown command: " + cmd); } String name = map.get().getPrimaryAlias(); if (!context.hasPerms(name)) { return new ExecuteCommandResponse(req.getCommand(), "You are not allowed to execute '" + cmd + "'"); } final CommandSource src = new CommandSource(req.getName(), req.getWaitLines(), req.isHiddenInConsole()); try { WebAPI.runOnMain(() -> WebAPI.executeCommand(req.getCommand(), src)); if (req.getWaitLines() > 0 || req.getWaitTime() > 0) { synchronized (src) { src.wait(req.getWaitTime() > 0 ? req.getWaitTime() : CMD_WAIT_TIME); } } return new ExecuteCommandResponse(req.command, src.getLines()); } catch (InterruptedException e) { e.printStackTrace(); WebAPI.sentryCapture(e); return new ExecuteCommandResponse(req.command, e.getMessage()); } }
.filter(mapping -> "register".equalsIgnoreCase(mapping.getPrimaryAlias())) .forEach(commandManager::removeMapping);