From 3173e2610939f5331d1b035019b0ea4d975680dc Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Fri, 17 May 2019 22:24:14 -0700 Subject: [PATCH] Fix //expand, improve //help --- .../worldedit/UnknownDirectionException.java | 1 + .../worldedit/command/ExpandCommands.java | 152 ++++++++++++++++++ .../worldedit/command/SelectionCommands.java | 58 ------- .../worldedit/command/UtilityCommands.java | 4 +- .../worldedit/command/WorldEditCommands.java | 4 +- .../command/argument/ExpandAmount.java | 51 ------ .../argument/ExpandAmountConverter.java | 70 -------- .../command/util/AsyncCommandBuilder.java | 1 - .../command/util/PrintCommandHelp.java | 25 +-- .../platform/PlatformCommandManager.java | 9 +- .../internal/command/CommandUtil.java | 8 + .../com/sk89q/worldedit/registry/Keyed.java | 1 - .../formatting/component/CommandUsageBox.java | 49 +++--- worldedit-libs/build.gradle | 2 +- 14 files changed, 209 insertions(+), 226 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/ExpandCommands.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmount.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmountConverter.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/UnknownDirectionException.java b/worldedit-core/src/main/java/com/sk89q/worldedit/UnknownDirectionException.java index 49c323d17..a5b7b6c56 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/UnknownDirectionException.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/UnknownDirectionException.java @@ -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; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ExpandCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ExpandCommands.java new file mode 100644 index 000000000..e931f5225 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ExpandCommands.java @@ -0,0 +1,152 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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 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)."); + } + +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java index bac02819a..155385c52 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java @@ -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 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" diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index af59e5adc..3b6217154 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -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 command) throws WorldEditException { - PrintCommandHelp.help(command, page, we, actor); + PrintCommandHelp.help(command, page, listSubCommands, we, actor); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java index 154f57f52..1a383a767 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java @@ -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 command) throws WorldEditException { - PrintCommandHelp.help(command, page, we, actor); + PrintCommandHelp.help(command, page, listSubCommands, we, actor); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmount.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmount.java deleted file mode 100644 index 062a330c9..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmount.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * 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 . - */ - -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"); - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmountConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmountConverter.java deleted file mode 100644 index b54311bee..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/ExpandAmountConverter.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * 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 . - */ - -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 { - - public static void register(CommandManager commandManager) { - commandManager.registerConverter(Key.of(ExpandAmount.class), new ExpandAmountConverter()); - } - - private final ArgumentConverter integerConverter = - ArgumentConverters.get(TypeToken.of(int.class)); - - private ExpandAmountConverter() { - } - - @Override - public Component describeAcceptableArguments() { - return TextComponent.of("`vert` or ").append(integerConverter.describeAcceptableArguments()); - } - - @Override - public List getSuggestions(String input) { - return limitByPrefix(Stream.concat( - Stream.of("vert"), integerConverter.getSuggestions(input).stream() - ), input); - } - - @Override - public ConversionResult 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); - } -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/AsyncCommandBuilder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/AsyncCommandBuilder.java index f79d3bdd1..d2f70a006 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/AsyncCommandBuilder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/AsyncCommandBuilder.java @@ -17,7 +17,6 @@ * along with this program. If not, see . */ - package com.sk89q.worldedit.command.util; import com.google.common.base.Strings; 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 aa911b705..ab21a39b9 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 @@ -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 commandPath, int page, WorldEdit we, Actor actor) throws InvalidComponentException { + public static void help(List 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 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 visited) { + return "/" + Joiner.on(" ").join(visited.stream().map(Command::getName).iterator()); + } + private static void printCommands(int page, Stream commandStream, Actor actor, List 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(); } 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 455ae0403..8708de678 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 @@ -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(), diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java index e3032fa95..b98236090 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java @@ -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 requireIV(Key type, String name, InjectedValueAccess injectedValueAccess) { + return injectedValueAccess.injectedValue(type).orElseThrow(() -> + new IllegalStateException("No injected value for " + name + " (type " + type + ")") + ); + } + private CommandUtil() { } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java index 5351ed36b..c5161bd90 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java @@ -17,7 +17,6 @@ * along with this program. If not, see . */ - package com.sk89q.worldedit.registry; /** 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 46347d107..e8abe0e38 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 @@ -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 commands, String commandString, @Nullable CommandParameters parameters) throws InvalidComponentException { checkNotNull(commands); checkNotNull(commandString); - Map subCommands = getSubCommands(Iterables.getLast(commands)); - if (subCommands.isEmpty()) { - attachCommandUsage(commands, commandString); - } else { - attachSubcommandUsage(subCommands, commandString, parameters); - } - } - - private void attachSubcommandUsage(Map 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 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 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()); } diff --git a/worldedit-libs/build.gradle b/worldedit-libs/build.gradle index ef2c30446..a1954e09d 100644 --- a/worldedit-libs/build.gradle +++ b/worldedit-libs/build.gradle @@ -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"