2019-04-26 02:36:22 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.command.util;
|
|
|
|
|
|
|
|
import com.google.common.base.Joiner;
|
2019-04-26 06:02:23 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2019-05-18 05:24:14 +00:00
|
|
|
import com.google.common.collect.Iterables;
|
2019-04-26 02:36:22 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
|
|
|
import com.sk89q.worldedit.util.formatting.component.CommandListBox;
|
|
|
|
import com.sk89q.worldedit.util.formatting.component.CommandUsageBox;
|
2019-04-28 05:12:05 +00:00
|
|
|
import com.sk89q.worldedit.util.formatting.component.InvalidComponentException;
|
2019-04-26 02:36:22 +00:00
|
|
|
import org.enginehub.piston.Command;
|
|
|
|
import org.enginehub.piston.CommandManager;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
2019-04-26 06:02:23 +00:00
|
|
|
import java.util.stream.Collectors;
|
2019-04-26 02:36:22 +00:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
2019-04-30 22:03:18 +00:00
|
|
|
import static com.sk89q.worldedit.internal.command.CommandUtil.byCleanName;
|
|
|
|
import static com.sk89q.worldedit.internal.command.CommandUtil.getSubCommands;
|
2019-04-26 02:36:22 +00:00
|
|
|
import static java.util.stream.Collectors.toList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of the //help command.
|
|
|
|
*/
|
2019-04-28 05:12:05 +00:00
|
|
|
// Stored in a separate class to prevent import conflicts, and because it's aliased via /we help.
|
2019-04-26 02:36:22 +00:00
|
|
|
public class PrintCommandHelp {
|
|
|
|
|
|
|
|
private static Command detectCommand(CommandManager manager, String command) {
|
|
|
|
Optional<Command> mapping;
|
|
|
|
|
|
|
|
// First try the command as entered
|
|
|
|
mapping = manager.getCommand(command);
|
|
|
|
if (mapping.isPresent()) {
|
|
|
|
return mapping.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If tried with slashes, try dropping a slash
|
|
|
|
if (command.startsWith("/")) {
|
|
|
|
mapping = manager.getCommand(command.substring(1));
|
|
|
|
return mapping.orElse(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, check /command, since that's common
|
|
|
|
mapping = manager.getCommand("/" + command);
|
|
|
|
return mapping.orElse(null);
|
|
|
|
}
|
|
|
|
|
2019-05-18 05:24:14 +00:00
|
|
|
public static void help(List<String> commandPath, int page, boolean listSubCommands, WorldEdit we, Actor actor) throws InvalidComponentException {
|
2019-04-26 08:18:02 +00:00
|
|
|
CommandManager manager = we.getPlatformManager().getPlatformCommandManager().getCommandManager();
|
2019-04-26 02:36:22 +00:00
|
|
|
|
|
|
|
if (commandPath.isEmpty()) {
|
2019-04-28 05:12:05 +00:00
|
|
|
printCommands(page, manager.getAllCommands(), actor, ImmutableList.of());
|
2019-04-26 02:36:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-27 10:33:13 +00:00
|
|
|
List<Command> visited = new ArrayList<>();
|
2019-04-26 02:36:22 +00:00
|
|
|
Command currentCommand = detectCommand(manager, commandPath.get(0));
|
|
|
|
if (currentCommand == null) {
|
|
|
|
actor.printError(String.format("The command '%s' could not be found.", commandPath.get(0)));
|
|
|
|
return;
|
|
|
|
}
|
2019-04-27 10:33:13 +00:00
|
|
|
visited.add(currentCommand);
|
2019-04-26 02:36:22 +00:00
|
|
|
|
|
|
|
// Drill down to the command
|
|
|
|
for (int i = 1; i < commandPath.size(); i++) {
|
|
|
|
String subCommand = commandPath.get(i);
|
|
|
|
Map<String, Command> subCommands = getSubCommands(currentCommand);
|
|
|
|
|
|
|
|
if (subCommands.isEmpty()) {
|
|
|
|
actor.printError(String.format("'%s' has no sub-commands. (Maybe '%s' is for a parameter?)",
|
2019-05-18 05:24:14 +00:00
|
|
|
toCommandString(visited), subCommand));
|
2019-04-28 05:12:05 +00:00
|
|
|
// full help for single command
|
|
|
|
CommandUsageBox box = new CommandUsageBox(visited, visited.stream()
|
|
|
|
.map(Command::getName).collect(Collectors.joining(" ")));
|
|
|
|
actor.print(box.create());
|
2019-04-26 02:36:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subCommands.containsKey(subCommand)) {
|
|
|
|
currentCommand = subCommands.get(subCommand);
|
2019-04-27 10:33:13 +00:00
|
|
|
visited.add(currentCommand);
|
2019-04-26 02:36:22 +00:00
|
|
|
} else {
|
|
|
|
actor.printError(String.format("The sub-command '%s' under '%s' could not be found.",
|
2019-05-18 05:24:14 +00:00
|
|
|
subCommand, toCommandString(visited)));
|
2019-04-28 05:12:05 +00:00
|
|
|
// list subcommands for currentCommand
|
2019-05-18 05:24:14 +00:00
|
|
|
printCommands(page, getSubCommands(Iterables.getLast(visited)).values().stream(), actor, visited);
|
2019-04-26 02:36:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, Command> subCommands = getSubCommands(currentCommand);
|
|
|
|
|
2019-05-18 05:24:14 +00:00
|
|
|
if (subCommands.isEmpty() || !listSubCommands) {
|
2019-04-26 02:36:22 +00:00
|
|
|
// Create the message
|
2019-05-18 05:24:14 +00:00
|
|
|
CommandUsageBox box = new CommandUsageBox(visited, toCommandString(visited));
|
2019-04-26 05:11:46 +00:00
|
|
|
actor.print(box.create());
|
2019-04-26 02:36:22 +00:00
|
|
|
} else {
|
2019-04-28 05:12:05 +00:00
|
|
|
printCommands(page, subCommands.values().stream(), actor, visited);
|
2019-04-26 02:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 05:24:14 +00:00
|
|
|
private static String toCommandString(List<Command> visited) {
|
|
|
|
return "/" + Joiner.on(" ").join(visited.stream().map(Command::getName).iterator());
|
|
|
|
}
|
|
|
|
|
2019-04-28 05:12:05 +00:00
|
|
|
private static void printCommands(int page, Stream<Command> commandStream, Actor actor,
|
|
|
|
List<Command> commandList) throws InvalidComponentException {
|
2019-04-26 02:36:22 +00:00
|
|
|
// Get a list of aliases
|
|
|
|
List<Command> commands = commandStream
|
|
|
|
.sorted(byCleanName())
|
|
|
|
.collect(toList());
|
|
|
|
|
2019-05-18 05:24:14 +00:00
|
|
|
String used = commandList.isEmpty() ? null : toCommandString(commandList);
|
2019-04-28 05:12:05 +00:00
|
|
|
CommandListBox box = new CommandListBox(
|
|
|
|
(used == null ? "Help" : "Subcommands: " + used),
|
2019-05-18 05:24:14 +00:00
|
|
|
"//help -s %page%" + (used == null ? "" : " " + used));
|
2019-04-28 05:12:05 +00:00
|
|
|
if (!actor.isPlayer()) {
|
|
|
|
box.formatForConsole();
|
|
|
|
}
|
2019-04-26 02:36:22 +00:00
|
|
|
|
2019-04-28 05:12:05 +00:00
|
|
|
for (Command mapping : commands) {
|
|
|
|
String alias = (commandList.isEmpty() ? "/" : "") + mapping.getName();
|
|
|
|
String command = Stream.concat(commandList.stream(), Stream.of(mapping))
|
|
|
|
.map(Command::getName)
|
|
|
|
.collect(Collectors.joining(" ", "/", ""));
|
|
|
|
box.appendCommand(alias, mapping.getDescription(), command);
|
2019-04-26 02:36:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-28 05:12:05 +00:00
|
|
|
actor.print(box.create(page));
|
2019-04-26 02:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private PrintCommandHelp() {
|
|
|
|
}
|
|
|
|
}
|