From 0960f70e6b50d875df82415d094d5c5ffc4d4be0 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Sat, 27 Apr 2019 03:33:13 -0700 Subject: [PATCH] Update to latest Piston changes --- .../worldedit/command/SchematicCommands.java | 8 ++- .../command/util/PrintCommandHelp.java | 14 +++-- .../platform/PlatformCommandManager.java | 28 +++++++--- .../command/WorldEditExceptionConverter.java | 56 ++++++++++--------- .../formatting/component/CommandUsageBox.java | 26 +++++---- 5 files changed, 77 insertions(+), 55 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java index 5fd4c6d98..c06fe851f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java @@ -39,6 +39,7 @@ import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.session.ClipboardHolder; +import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.io.Closer; import com.sk89q.worldedit.util.io.file.FilenameException; import org.enginehub.piston.annotation.Command; @@ -159,7 +160,7 @@ public class SchematicCommands { boolean overwrite = f.exists(); if (overwrite) { if (!player.hasPermission("worldedit.schematic.delete")) { - throw new StopExecutionException("That schematic already exists!"); + throw new StopExecutionException(TextComponent.of("That schematic already exists!")); } if (!allowOverwrite) { player.printError("That schematic already exists. Use the -f flag to overwrite it."); @@ -186,7 +187,8 @@ public class SchematicCommands { File parent = f.getParentFile(); if (parent != null && !parent.exists()) { if (!parent.mkdirs()) { - throw new StopExecutionException("Could not create folder for schematics!"); + throw new StopExecutionException(TextComponent.of( + "Could not create folder for schematics!")); } } @@ -277,7 +279,7 @@ public class SchematicCommands { @Switch(name = 'n', desc = "Sort by date, newest first") boolean newFirst) { if (oldFirst && newFirst) { - throw new StopExecutionException("Cannot sort by oldest and newest."); + throw new StopExecutionException(TextComponent.of("Cannot sort by oldest and newest.")); } File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().saveDir); List fileList = allFiles(dir); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java index b1971b0a9..b59578a49 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PrintCommandHelp.java @@ -83,13 +83,13 @@ public class PrintCommandHelp { return; } - List visited = new ArrayList<>(); + List visited = new ArrayList<>(); 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; } - visited.add(commandPath.get(0)); + visited.add(currentCommand); // Drill down to the command for (int i = 1; i < commandPath.size(); i++) { @@ -103,8 +103,8 @@ public class PrintCommandHelp { } if (subCommands.containsKey(subCommand)) { - visited.add(subCommand); currentCommand = subCommands.get(subCommand); + visited.add(currentCommand); } else { actor.printError(String.format("The sub-command '%s' under '%s' could not be found.", subCommand, Joiner.on(" ").join(visited))); @@ -116,7 +116,8 @@ public class PrintCommandHelp { if (subCommands.isEmpty()) { // Create the message - CommandUsageBox box = new CommandUsageBox(currentCommand, String.join(" ", visited)); + CommandUsageBox box = new CommandUsageBox(visited, visited.stream() + .map(Command::getName).collect(Collectors.joining(" "))); actor.print(box.create()); } else { printAllCommands(page, perPage, subCommands.values().stream(), actor, visited); @@ -124,7 +125,7 @@ public class PrintCommandHelp { } private static void printAllCommands(int page, int perPage, Stream commandStream, Actor actor, - List commandList) { + List commandList) { // Get a list of aliases List commands = commandStream .sorted(byCleanName()) @@ -151,7 +152,8 @@ public class PrintCommandHelp { // Add each command for (Command mapping : list) { String alias = (commandList.isEmpty() ? "/" : "") + mapping.getName(); - String command = Stream.concat(commandList.stream(), Stream.of(mapping.getName())) + String command = Stream.concat(commandList.stream(), Stream.of(mapping)) + .map(Command::getName) .collect(Collectors.joining(" ", "/", "")); box.appendCommand(alias, mapping.getDescription(), command); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index c8f363a11..93ba549dd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -94,9 +94,11 @@ import com.sk89q.worldedit.util.command.parametric.ExceptionConverter; import com.sk89q.worldedit.util.eventbus.Subscribe; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import com.sk89q.worldedit.util.formatting.text.format.TextColor; import com.sk89q.worldedit.util.logging.DynamicStreamHandler; import com.sk89q.worldedit.util.logging.LogFormat; 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.DefaultCommandManagerService; @@ -112,6 +114,7 @@ import org.enginehub.piston.inject.MapBackedValueStore; import org.enginehub.piston.inject.MemoizingValueAccess; import org.enginehub.piston.inject.MergedValueAccess; import org.enginehub.piston.part.SubCommandPart; +import org.enginehub.piston.util.HelpGenerator; import org.enginehub.piston.util.ValueProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -499,21 +502,28 @@ public final class PlatformCommandManager { actor.printError("You are not permitted to do that. Are you in the right mode?"); } } catch (UsageException e) { - String message = e.getMessage(); - actor.printError(message != null ? message : "The command was not used properly (no more help available)."); + actor.print(TextComponent.builder("") + .color(TextColor.RED) + .append(e.getRichMessage()) + .build()); + ImmutableList cmd = e.getCommands(); + if (cmd.size() > 0) { + actor.print(TextComponent.builder("Usage: ") + .color(TextColor.RED) + .append(TextComponent.of("/", ColorConfig.getMainText())) + .append(HelpGenerator.create(cmd).getUsage()) + .build()); + } } catch (CommandExecutionException e) { Throwable t = e.getCause(); actor.printError("Please report this error: [See console]"); actor.printRaw(t.getClass().getName() + ": " + t.getMessage()); log.error("An unexpected error while handling a WorldEdit command", t); } catch (CommandException e) { - String message = e.getMessage(); - if (message != null) { - actor.printError(e.getMessage()); - } else { - actor.printError("An unknown error has occurred! Please see console."); - log.error("An unknown error occurred", e); - } + actor.print(TextComponent.builder("") + .color(TextColor.RED) + .append(e.getRichMessage()) + .build()); } finally { Optional editSessionOpt = context.snapshotMemory().injectedValue(Key.of(EditSession.class)); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditExceptionConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditExceptionConverter.java index b75d1c5b3..a11294527 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditExceptionConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditExceptionConverter.java @@ -21,6 +21,7 @@ package com.sk89q.worldedit.internal.command; import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.DisallowedItemException; import com.sk89q.worldedit.EmptyClipboardException; import com.sk89q.worldedit.IncompleteRegionException; @@ -39,6 +40,7 @@ import com.sk89q.worldedit.internal.expression.ExpressionException; import com.sk89q.worldedit.regions.RegionOperationException; import com.sk89q.worldedit.util.command.parametric.ExceptionConverterHelper; import com.sk89q.worldedit.util.command.parametric.ExceptionMatch; +import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.io.file.FileSelectionAbortedException; import com.sk89q.worldedit.util.io.file.FilenameResolutionException; import com.sk89q.worldedit.util.io.file.InvalidFilenameException; @@ -61,110 +63,114 @@ public class WorldEditExceptionConverter extends ExceptionConverterHelper { this.worldEdit = worldEdit; } + private CommandException newCommandException(String message, Throwable cause) { + return new CommandException(TextComponent.of(message), cause, ImmutableList.of()); + } + @ExceptionMatch public void convert(NumberFormatException e) throws CommandException { final Matcher matcher = numberFormat.matcher(e.getMessage()); if (matcher.matches()) { - throw new CommandException("Number expected; string \"" + matcher.group(1) - + "\" given.", e, null); + throw newCommandException("Number expected; string \"" + matcher.group(1) + + "\" given.", e); } else { - throw new CommandException("Number expected; string given.", e, null); + throw newCommandException("Number expected; string given.", e); } } @ExceptionMatch public void convert(IncompleteRegionException e) throws CommandException { - throw new CommandException("Make a region selection first.", e, null); + throw newCommandException("Make a region selection first.", e); } @ExceptionMatch public void convert(UnknownItemException e) throws CommandException { - throw new CommandException("Block name '" + e.getID() + "' was not recognized.", e, null); + throw newCommandException("Block name '" + e.getID() + "' was not recognized.", e); } @ExceptionMatch public void convert(InvalidItemException e) throws CommandException { - throw new CommandException(e.getMessage(), e, null); + throw newCommandException(e.getMessage(), e); } @ExceptionMatch public void convert(DisallowedItemException e) throws CommandException { - throw new CommandException("Block '" + e.getID() - + "' not allowed (see WorldEdit configuration).", e, null); + throw newCommandException("Block '" + e.getID() + + "' not allowed (see WorldEdit configuration).", e); } @ExceptionMatch public void convert(MaxChangedBlocksException e) throws CommandException { - throw new CommandException("Max blocks changed in an operation reached (" - + e.getBlockLimit() + ").", e, null); + throw newCommandException("Max blocks changed in an operation reached (" + + e.getBlockLimit() + ").", e); } @ExceptionMatch public void convert(MaxBrushRadiusException e) throws CommandException { - throw new CommandException("Maximum brush radius (in configuration): " + worldEdit.getConfiguration().maxBrushRadius, e, null); + throw newCommandException("Maximum brush radius (in configuration): " + worldEdit.getConfiguration().maxBrushRadius, e); } @ExceptionMatch public void convert(MaxRadiusException e) throws CommandException { - throw new CommandException("Maximum radius (in configuration): " + worldEdit.getConfiguration().maxRadius, e, null); + throw newCommandException("Maximum radius (in configuration): " + worldEdit.getConfiguration().maxRadius, e); } @ExceptionMatch public void convert(UnknownDirectionException e) throws CommandException { - throw new CommandException("Unknown direction: " + e.getDirection(), e, null); + throw newCommandException("Unknown direction: " + e.getDirection(), e); } @ExceptionMatch public void convert(InsufficientArgumentsException e) throws CommandException { - throw new CommandException(e.getMessage(), e, null); + throw newCommandException(e.getMessage(), e); } @ExceptionMatch public void convert(RegionOperationException e) throws CommandException { - throw new CommandException(e.getMessage(), e, null); + throw newCommandException(e.getMessage(), e); } @ExceptionMatch public void convert(ExpressionException e) throws CommandException { - throw new CommandException(e.getMessage(), e, null); + throw newCommandException(e.getMessage(), e); } @ExceptionMatch public void convert(EmptyClipboardException e) throws CommandException { - throw new CommandException("Your clipboard is empty. Use //copy first.", e, null); + throw newCommandException("Your clipboard is empty. Use //copy first.", e); } @ExceptionMatch public void convert(InvalidFilenameException e) throws CommandException { - throw new CommandException("Filename '" + e.getFilename() + "' invalid: " - + e.getMessage(), e, null); + throw newCommandException("Filename '" + e.getFilename() + "' invalid: " + + e.getMessage(), e); } @ExceptionMatch public void convert(FilenameResolutionException e) throws CommandException { - throw new CommandException( - "File '" + e.getFilename() + "' resolution error: " + e.getMessage(), e, null); + throw newCommandException( + "File '" + e.getFilename() + "' resolution error: " + e.getMessage(), e); } @ExceptionMatch public void convert(InvalidToolBindException e) throws CommandException { - throw new CommandException("Can't bind tool to " + e.getItemType().getName() + ": " + e.getMessage(), e, null); + throw newCommandException("Can't bind tool to " + e.getItemType().getName() + ": " + e.getMessage(), e); } @ExceptionMatch public void convert(FileSelectionAbortedException e) throws CommandException { - throw new CommandException("File selection aborted.", e, null); + throw newCommandException("File selection aborted.", e); } @ExceptionMatch public void convert(WorldEditException e) throws CommandException { - throw new CommandException(e.getMessage(), e, null); + throw newCommandException(e.getMessage(), e); } @ExceptionMatch public void convert(IllegalArgumentException e) throws CommandException { - throw new CommandException(e.getMessage(), e, null); + throw newCommandException(e.getMessage(), e); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java index 69d5339f7..6b6314b73 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java @@ -19,8 +19,10 @@ package com.sk89q.worldedit.util.formatting.component; +import com.google.common.collect.Iterables; import org.enginehub.piston.Command; import org.enginehub.piston.CommandParameters; +import org.enginehub.piston.util.HelpGenerator; import javax.annotation.Nullable; import java.util.List; @@ -39,26 +41,26 @@ public class CommandUsageBox extends TextComponentProducer { /** * Create a new usage box. * - * @param command the command to describe - * @param commandString the command that was used, such as "/we" or "/brush sphere" + * @param commands the commands to describe + * @param commandString the commands that were used, such as "/we" or "/brush sphere" */ - public CommandUsageBox(Command command, String commandString) { - this(command, commandString, null); + public CommandUsageBox(List commands, String commandString) { + this(commands, commandString, null); } /** * Create a new usage box. * - * @param command the command to describe - * @param commandString the command that was used, such as "/we" or "/brush sphere" + * @param commands the commands to describe + * @param commandString the commands that were used, such as "/we" or "/brush sphere" * @param parameters list of parameters to use */ - public CommandUsageBox(Command command, String commandString, @Nullable CommandParameters parameters) { - checkNotNull(command); + public CommandUsageBox(List commands, String commandString, @Nullable CommandParameters parameters) { + checkNotNull(commands); checkNotNull(commandString); - Map subCommands = getSubCommands(command); + Map subCommands = getSubCommands(Iterables.getLast(commands)); if (subCommands.isEmpty()) { - attachCommandUsage(command, commandString); + attachCommandUsage(commands, commandString); } else { attachSubcommandUsage(subCommands, commandString, parameters); } @@ -81,9 +83,9 @@ public class CommandUsageBox extends TextComponentProducer { append(box.create()); } - private void attachCommandUsage(Command description, String commandString) { + private void attachCommandUsage(List commands, String commandString) { MessageBox box = new MessageBox("Help for " + commandString, - new TextComponentProducer().append(description.getFullHelp())); + new TextComponentProducer().append(HelpGenerator.create(commands).getFullHelp())); append(box.create()); }