Port utility commands

This commit is contained in:
Kenzie Togami
2019-04-25 19:36:22 -07:00
parent c05e1ed0cc
commit 9b0fda9f83
9 changed files with 574 additions and 513 deletions

View File

@ -0,0 +1,53 @@
/*
* 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.util.command;
import com.sk89q.worldedit.extension.platform.PlatformCommandMananger;
import org.enginehub.piston.Command;
import org.enginehub.piston.part.SubCommandPart;
import java.util.Comparator;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CommandUtil {
public static Map<String, Command> getSubCommands(Command currentCommand) {
return currentCommand.getParts().stream()
.filter(p -> p instanceof SubCommandPart)
.flatMap(p -> ((SubCommandPart) p).getCommands().stream())
.collect(Collectors.toMap(Command::getName, Function.identity()));
}
private static String clean(String input) {
return PlatformCommandMananger.COMMAND_CLEAN_PATTERN.matcher(input).replaceAll("");
}
private static final Comparator<Command> BY_CLEAN_NAME =
Comparator.comparing(c -> clean(c.getName()));
public static Comparator<Command> byCleanName() {
return BY_CLEAN_NAME;
}
private CommandUtil() {
}
}

View File

@ -19,21 +19,18 @@
package com.sk89q.worldedit.util.formatting.component;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.extension.platform.PlatformCommandMananger;
import com.sk89q.worldedit.util.command.CommandCallable;
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Description;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.util.command.PrimaryAliasComparator;
import com.sk89q.worldedit.util.formatting.StyledFragment;
import java.util.ArrayList;
import java.util.List;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandParameters;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.util.command.CommandUtil.byCleanName;
import static com.sk89q.worldedit.util.command.CommandUtil.getSubCommands;
/**
* A box to describe usage of a command.
@ -46,7 +43,7 @@ public class CommandUsageBox extends StyledFragment {
* @param command the command to describe
* @param commandString the command that was used, such as "/we" or "/brush sphere"
*/
public CommandUsageBox(CommandCallable command, String commandString) {
public CommandUsageBox(Command command, String commandString) {
this(command, commandString, null);
}
@ -55,54 +52,40 @@ public class CommandUsageBox extends StyledFragment {
*
* @param command the command to describe
* @param commandString the command that was used, such as "/we" or "/brush sphere"
* @param locals list of locals to use
* @param parameters list of parameters to use
*/
public CommandUsageBox(CommandCallable command, String commandString, @Nullable CommandLocals locals) {
public CommandUsageBox(Command command, String commandString, @Nullable CommandParameters parameters) {
checkNotNull(command);
checkNotNull(commandString);
if (command instanceof Dispatcher) {
attachDispatcherUsage((Dispatcher) command, commandString, locals);
Map<String, Command> subCommands = getSubCommands(command);
if (subCommands.isEmpty()) {
attachCommandUsage(command, commandString);
} else {
attachCommandUsage(command.getDescription(), commandString);
attachSubcommandUsage(subCommands, commandString, parameters);
}
}
private void attachDispatcherUsage(Dispatcher dispatcher, String commandString, @Nullable CommandLocals locals) {
private void attachSubcommandUsage(Map<String, Command> dispatcher, String commandString, @Nullable CommandParameters parameters) {
CommandListBox box = new CommandListBox("Subcommands");
String prefix = !commandString.isEmpty() ? commandString + " " : "";
List<CommandMapping> list = new ArrayList<>(dispatcher.getCommands());
list.sort(new PrimaryAliasComparator(PlatformCommandMananger.COMMAND_CLEAN_PATTERN));
List<Command> list = dispatcher.values().stream()
.sorted(byCleanName())
.collect(Collectors.toList());
for (CommandMapping mapping : list) {
if (locals == null || mapping.getCallable().testPermission(locals)) {
box.appendCommand(prefix + mapping.getPrimaryAlias(), mapping.getDescription().getDescription());
for (Command mapping : list) {
if (parameters == null || mapping.getCondition().satisfied(parameters)) {
box.appendCommand(prefix + mapping.getName(), mapping.getDescription());
}
}
append(box);
}
private void attachCommandUsage(Description description, String commandString) {
private void attachCommandUsage(Command description, String commandString) {
MessageBox box = new MessageBox("Help for " + commandString);
StyledFragment contents = box.getContents();
if (description.getUsage() != null) {
contents.append(new Label().append("Usage: "));
contents.append(description.getUsage());
} else {
contents.append(new Subtle().append("Usage information is not available."));
}
contents.newLine();
if (description.getHelp() != null) {
contents.append(description.getHelp());
} else if (description.getDescription() != null) {
contents.append(description.getDescription());
} else {
contents.append(new Subtle().append("No further help is available."));
}
box.getContents().append(description.getFullHelp());
append(box);
}