Fix //expand, improve //help

This commit is contained in:
Kenzie Togami 2019-05-17 22:24:14 -07:00
parent 71df3716dd
commit 3173e26109
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
14 changed files with 209 additions and 226 deletions

View File

@ -32,6 +32,7 @@ public class UnknownDirectionException extends WorldEditException {
* @param dir the input that was tried
*/
public UnknownDirectionException(String dir) {
super("Unknown direction: " + dir);
this.dir = dir;
}

View File

@ -0,0 +1,152 @@
/*
* 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;
import com.google.common.collect.ImmutableSet;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.command.util.PermissionCondition;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.internal.annotation.Direction;
import com.sk89q.worldedit.internal.annotation.MultiDirection;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionOperationException;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandManagerService;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.inject.Key;
import org.enginehub.piston.part.SubCommandPart;
import java.util.List;
import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION;
import static com.sk89q.worldedit.internal.command.CommandUtil.requireIV;
/**
* Extracted from {@link SelectionCommands} to allow importing of {@link Command}.
*/
@CommandContainer
public class ExpandCommands {
public static void register(CommandRegistrationHandler registration,
CommandManager commandManager,
CommandManagerService commandManagerService) {
// Collect the general expand command
CommandManager collect = commandManagerService.newCommandManager();
registration.register(
collect,
ExpandCommandsRegistration.builder(),
new ExpandCommands()
);
Command expandBaseCommand = collect.getCommand("/expand")
.orElseThrow(() -> new IllegalStateException("No /expand command"));
commandManager.register("/expand", command -> {
command.condition(new PermissionCondition(ImmutableSet.of("worldedit.selection.expand")));
command.addPart(SubCommandPart.builder(
TranslatableComponent.of("vert"),
TextComponent.of("Vertical expansion sub-command")
)
.withCommands(ImmutableSet.of(createVertCommand(commandManager)))
.optional()
.build());
command.addParts(expandBaseCommand.getParts());
command.action(expandBaseCommand.getAction());
command.description(expandBaseCommand.getDescription());
});
}
private static Command createVertCommand(CommandManager commandManager) {
return commandManager.newCommand("vert")
.description(TextComponent.of("Vertically expand the selection to world limits."))
.action(parameters -> {
expandVert(
requireIV(Key.of(LocalSession.class), "localSession", parameters),
requireIV(Key.of(Player.class), "localSession", parameters)
);
return 1;
})
.build();
}
private static void expandVert(LocalSession session, Player player) throws IncompleteRegionException {
Region region = session.getSelection(player.getWorld());
try {
int oldSize = region.getArea();
region.expand(
BlockVector3.at(0, (player.getWorld().getMaxY() + 1), 0),
BlockVector3.at(0, -(player.getWorld().getMaxY() + 1), 0));
session.getRegionSelector(player.getWorld()).learnChanges();
int newSize = region.getArea();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);
player.print("Region expanded " + (newSize - oldSize)
+ " blocks [top-to-bottom].");
} catch (RegionOperationException e) {
player.printError(e.getMessage());
}
}
@org.enginehub.piston.annotation.Command(
name = "/expand",
desc = "Expand the selection area"
)
@Logging(REGION)
public void expand(Player player, LocalSession session,
@Arg(desc = "Amount to expand the selection by, can be `vert` to expand to the whole vertical column")
int amount,
@Arg(desc = "Amount to expand the selection by in the other direction", def = "0")
int reverseAmount,
@Arg(desc = "Direction to expand", def = Direction.AIM)
@MultiDirection
List<BlockVector3> direction) throws WorldEditException {
Region region = session.getSelection(player.getWorld());
int oldSize = region.getArea();
if (reverseAmount == 0) {
for (BlockVector3 dir : direction) {
region.expand(dir.multiply(amount));
}
} else {
for (BlockVector3 dir : direction) {
region.expand(dir.multiply(amount), dir.multiply(-reverseAmount));
}
}
session.getRegionSelector(player.getWorld()).learnChanges();
int newSize = region.getArea();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);
player.print("Region expanded " + (newSize - oldSize) + " block(s).");
}
}

View File

@ -24,7 +24,6 @@ import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.command.argument.ExpandAmount;
import com.sk89q.worldedit.command.argument.SelectorChoice;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
@ -267,63 +266,6 @@ public class SelectionCommands {
}
}
@Command(
name = "/expand",
desc = "Expand the selection area"
)
@Logging(REGION)
@CommandPermissions("worldedit.selection.expand")
public void expand(Player player, LocalSession session,
@Arg(desc = "Amount to expand the selection by, can be `vert` to expand to the whole vertical column")
ExpandAmount amount,
@Arg(desc = "Amount to expand the selection by in the other direction", def = "0")
int reverseAmount,
@Arg(desc = "Direction to expand", def = Direction.AIM)
@MultiDirection
List<BlockVector3> direction) throws WorldEditException {
// Special syntax (//expand vert) to expand the selection between
// sky and bedrock.
if (amount.isVert()) {
Region region = session.getSelection(player.getWorld());
try {
int oldSize = region.getArea();
region.expand(
BlockVector3.at(0, (player.getWorld().getMaxY() + 1), 0),
BlockVector3.at(0, -(player.getWorld().getMaxY() + 1), 0));
session.getRegionSelector(player.getWorld()).learnChanges();
int newSize = region.getArea();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);
player.print("Region expanded " + (newSize - oldSize)
+ " blocks [top-to-bottom].");
} catch (RegionOperationException e) {
player.printError(e.getMessage());
}
return;
}
Region region = session.getSelection(player.getWorld());
int oldSize = region.getArea();
if (reverseAmount == 0) {
for (BlockVector3 dir : direction) {
region.expand(dir.multiply(amount.getAmount()));
}
} else {
for (BlockVector3 dir : direction) {
region.expand(dir.multiply(amount.getAmount()), dir.multiply(-reverseAmount));
}
}
session.getRegionSelector(player.getWorld()).learnChanges();
int newSize = region.getArea();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);
player.print("Region expanded " + (newSize - oldSize) + " block(s).");
}
@Command(
name = "/contract",
desc = "Contract the selection area"

View File

@ -515,11 +515,13 @@ public class UtilityCommands {
)
@CommandPermissions("worldedit.help")
public void help(Actor actor,
@Switch(name = 's', desc = "List sub-commands of the given command, if applicable")
boolean listSubCommands,
@Arg(desc = "The page to retrieve", def = "1")
int page,
@Arg(desc = "The command to retrieve help for", def = "", variable = true)
List<String> command) throws WorldEditException {
PrintCommandHelp.help(command, page, we, actor);
PrintCommandHelp.help(command, page, listSubCommands, we, actor);
}
}

View File

@ -157,10 +157,12 @@ public class WorldEditCommands {
)
@CommandPermissions("worldedit.help")
public void help(Actor actor,
@Switch(name = 's', desc = "List sub-commands of the given command, if applicable")
boolean listSubCommands,
@Arg(desc = "The page to retrieve", def = "1")
int page,
@Arg(desc = "The command to retrieve help for", def = "", variable = true)
List<String> command) throws WorldEditException {
PrintCommandHelp.help(command, page, we, actor);
PrintCommandHelp.help(command, page, listSubCommands, we, actor);
}
}

View File

@ -1,51 +0,0 @@
/*
* 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.argument;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
public final class ExpandAmount {
public static ExpandAmount vert() {
return new ExpandAmount(null);
}
public static ExpandAmount from(int amount) {
return new ExpandAmount(amount);
}
@Nullable
private final Integer amount;
private ExpandAmount(@Nullable Integer amount) {
this.amount = amount;
}
public boolean isVert() {
return amount == null;
}
public int getAmount() {
return checkNotNull(amount, "This amount is vertical, i.e. undefined");
}
}

View File

@ -1,70 +0,0 @@
/*
* 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.argument;
import com.google.common.reflect.TypeToken;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.converter.ArgumentConverter;
import org.enginehub.piston.converter.ArgumentConverters;
import org.enginehub.piston.converter.ConversionResult;
import org.enginehub.piston.converter.SuccessfulConversion;
import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.Key;
import java.util.List;
import java.util.stream.Stream;
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix;
public class ExpandAmountConverter implements ArgumentConverter<ExpandAmount> {
public static void register(CommandManager commandManager) {
commandManager.registerConverter(Key.of(ExpandAmount.class), new ExpandAmountConverter());
}
private final ArgumentConverter<Integer> integerConverter =
ArgumentConverters.get(TypeToken.of(int.class));
private ExpandAmountConverter() {
}
@Override
public Component describeAcceptableArguments() {
return TextComponent.of("`vert` or ").append(integerConverter.describeAcceptableArguments());
}
@Override
public List<String> getSuggestions(String input) {
return limitByPrefix(Stream.concat(
Stream.of("vert"), integerConverter.getSuggestions(input).stream()
), input);
}
@Override
public ConversionResult<ExpandAmount> convert(String argument, InjectedValueAccess context) {
if (argument.equalsIgnoreCase("vert")
|| argument.equalsIgnoreCase("vertical")) {
return SuccessfulConversion.fromSingle(ExpandAmount.vert());
}
return integerConverter.convert(argument, context).mapSingle(ExpandAmount::from);
}
}

View File

@ -17,7 +17,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.command.util;
import com.google.common.base.Strings;

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit.command.util;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.formatting.component.CommandListBox;
@ -66,7 +67,7 @@ public class PrintCommandHelp {
return mapping.orElse(null);
}
public static void help(List<String> commandPath, int page, WorldEdit we, Actor actor) throws InvalidComponentException {
public static void help(List<String> commandPath, int page, boolean listSubCommands, WorldEdit we, Actor actor) throws InvalidComponentException {
CommandManager manager = we.getPlatformManager().getPlatformCommandManager().getCommandManager();
if (commandPath.isEmpty()) {
@ -89,7 +90,7 @@ public class PrintCommandHelp {
if (subCommands.isEmpty()) {
actor.printError(String.format("'%s' has no sub-commands. (Maybe '%s' is for a parameter?)",
Joiner.on(" ").join(visited.stream().map(Command::getName).iterator()), subCommand));
toCommandString(visited), subCommand));
// full help for single command
CommandUsageBox box = new CommandUsageBox(visited, visited.stream()
.map(Command::getName).collect(Collectors.joining(" ")));
@ -102,27 +103,28 @@ public class PrintCommandHelp {
visited.add(currentCommand);
} else {
actor.printError(String.format("The sub-command '%s' under '%s' could not be found.",
subCommand, Joiner.on(" ").join(visited.stream().map(Command::getName).iterator())));
subCommand, toCommandString(visited)));
// list subcommands for currentCommand
CommandUsageBox box = new CommandUsageBox(visited, visited.stream()
.map(Command::getName).collect(Collectors.joining(" ")));
actor.print(box.create());
printCommands(page, getSubCommands(Iterables.getLast(visited)).values().stream(), actor, visited);
return;
}
}
Map<String, Command> subCommands = getSubCommands(currentCommand);
if (subCommands.isEmpty()) {
if (subCommands.isEmpty() || !listSubCommands) {
// Create the message
CommandUsageBox box = new CommandUsageBox(visited, visited.stream()
.map(Command::getName).collect(Collectors.joining(" ")));
CommandUsageBox box = new CommandUsageBox(visited, toCommandString(visited));
actor.print(box.create());
} else {
printCommands(page, subCommands.values().stream(), actor, visited);
}
}
private static String toCommandString(List<Command> visited) {
return "/" + Joiner.on(" ").join(visited.stream().map(Command::getName).iterator());
}
private static void printCommands(int page, Stream<Command> commandStream, Actor actor,
List<Command> commandList) throws InvalidComponentException {
// Get a list of aliases
@ -130,11 +132,10 @@ public class PrintCommandHelp {
.sorted(byCleanName())
.collect(toList());
String used = commandList.isEmpty() ? null
: Joiner.on(" ").join(commandList.stream().map(Command::getName).iterator());
String used = commandList.isEmpty() ? null : toCommandString(commandList);
CommandListBox box = new CommandListBox(
(used == null ? "Help" : "Subcommands: " + used),
"//help %page%" + (used == null ? "" : " " + used));
"//help -s %page%" + (used == null ? "" : " " + used));
if (!actor.isPlayer()) {
box.formatForConsole();
}

View File

@ -36,6 +36,7 @@ import com.sk89q.worldedit.command.ChunkCommands;
import com.sk89q.worldedit.command.ChunkCommandsRegistration;
import com.sk89q.worldedit.command.ClipboardCommands;
import com.sk89q.worldedit.command.ClipboardCommandsRegistration;
import com.sk89q.worldedit.command.ExpandCommands;
import com.sk89q.worldedit.command.GeneralCommands;
import com.sk89q.worldedit.command.GeneralCommandsRegistration;
import com.sk89q.worldedit.command.GenerationCommands;
@ -73,7 +74,6 @@ import com.sk89q.worldedit.command.argument.CommaSeparatedValuesConverter;
import com.sk89q.worldedit.command.argument.DirectionConverter;
import com.sk89q.worldedit.command.argument.EntityRemoverConverter;
import com.sk89q.worldedit.command.argument.EnumConverter;
import com.sk89q.worldedit.command.argument.ExpandAmountConverter;
import com.sk89q.worldedit.command.argument.FactoryConverter;
import com.sk89q.worldedit.command.argument.RegionFactoryConverter;
import com.sk89q.worldedit.command.argument.RegistryConverter;
@ -104,6 +104,7 @@ import com.sk89q.worldedit.world.World;
import org.enginehub.piston.ColorConfig;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.TextConfig;
import org.enginehub.piston.converter.ArgumentConverters;
import org.enginehub.piston.exception.CommandException;
import org.enginehub.piston.exception.CommandExecutionException;
@ -148,6 +149,10 @@ public final class PlatformCommandManager {
private static final java.util.logging.Logger COMMAND_LOG =
java.util.logging.Logger.getLogger("com.sk89q.worldedit.CommandLog");
static {
TextConfig.setCommandPrefix("/");
}
private final WorldEdit worldEdit;
private final PlatformManager platformManager;
private final CommandManagerServiceImpl commandManagerService;
@ -206,7 +211,6 @@ public final class PlatformCommandManager {
VectorConverter.register(commandManager);
EnumConverter.register(commandManager);
RegistryConverter.register(commandManager);
ExpandAmountConverter.register(commandManager);
ZonedDateTimeConverter.register(commandManager);
BooleanConverter.register(commandManager);
EntityRemoverConverter.register(commandManager);
@ -360,6 +364,7 @@ public final class PlatformCommandManager {
SelectionCommandsRegistration.builder(),
new SelectionCommands(worldEdit)
);
ExpandCommands.register(registration, commandManager, commandManagerService);
this.registration.register(
commandManager,
SnapshotUtilCommandsRegistration.builder(),

View File

@ -27,6 +27,8 @@ import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import org.enginehub.piston.Command;
import org.enginehub.piston.exception.CommandException;
import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.Key;
import org.enginehub.piston.part.SubCommandPart;
import java.util.Comparator;
@ -120,6 +122,12 @@ public class CommandUtil {
}
}
public static <T> T requireIV(Key<T> type, String name, InjectedValueAccess injectedValueAccess) {
return injectedValueAccess.injectedValue(type).orElseThrow(() ->
new IllegalStateException("No injected value for " + name + " (type " + type + ")")
);
}
private CommandUtil() {
}
}

View File

@ -17,7 +17,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.registry;
/**

View File

@ -20,17 +20,19 @@
package com.sk89q.worldedit.util.formatting.component;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
import com.sk89q.worldedit.util.formatting.text.format.TextDecoration;
import org.enginehub.piston.ColorConfig;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandParameters;
import org.enginehub.piston.util.HelpGenerator;
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.internal.command.CommandUtil.byCleanName;
import static com.sk89q.worldedit.internal.command.CommandUtil.getSubCommands;
/**
@ -58,35 +60,26 @@ public class CommandUsageBox extends TextComponentProducer {
public CommandUsageBox(List<Command> commands, String commandString, @Nullable CommandParameters parameters) throws InvalidComponentException {
checkNotNull(commands);
checkNotNull(commandString);
Map<String, Command> subCommands = getSubCommands(Iterables.getLast(commands));
if (subCommands.isEmpty()) {
attachCommandUsage(commands, commandString);
} else {
attachSubcommandUsage(subCommands, commandString, parameters);
}
}
private void attachSubcommandUsage(Map<String, Command> dispatcher, String commandString, @Nullable CommandParameters parameters) throws InvalidComponentException {
CommandListBox box = new CommandListBox(commandString.isEmpty() ? "Help" : "Subcommands:" + commandString,
"//help %page%" + (commandString.isEmpty() ? "" : " " + commandString));
String prefix = !commandString.isEmpty() ? commandString + " " : "";
List<Command> list = dispatcher.values().stream()
.sorted(byCleanName())
.collect(Collectors.toList());
for (Command mapping : list) {
if (parameters == null || mapping.getCondition().satisfied(parameters)) {
box.appendCommand(prefix + mapping.getName(), mapping.getDescription());
}
}
append(box.create(1));
attachCommandUsage(commands, commandString);
}
private void attachCommandUsage(List<Command> commands, String commandString) {
TextComponentProducer boxContent = new TextComponentProducer()
.append(HelpGenerator.create(commands).getFullHelp());
if (getSubCommands(Iterables.getLast(commands)).size() > 0) {
boxContent.append(TextComponent.newline())
.append(TextComponent.builder("> ")
.color(ColorConfig.getHelpText())
.append(TextComponent.builder("List Subcommands")
.color(ColorConfig.getMainText())
.decoration(TextDecoration.ITALIC, true)
.clickEvent(ClickEvent.runCommand("//help -s " + commandString))
.hoverEvent(HoverEvent.showText(TextComponent.of("List all subcommands of this command")))
.build())
.build());
}
MessageBox box = new MessageBox("Help for " + commandString,
new TextComponentProducer().append(HelpGenerator.create(commands).getFullHelp()));
boxContent);
append(box.create());
}

View File

@ -91,7 +91,7 @@ configure(subprojects + project("core:ap")) {
def textExtrasVersion = "3.0.2"
project("core") {
def textVersion = "3.0.0"
def pistonVersion = '0.2.4'
def pistonVersion = '0.3.0'
dependencies {
shade "net.kyori:text-api:$textVersion"