From 0e9fee3b6044bd2bde3f5c5af04ff52bee636ff6 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Sun, 8 Sep 2019 17:42:57 -0700 Subject: [PATCH] Add /tool back, deprecate global tool commands --- .../sk89q/worldedit/command/ToolCommands.java | 75 ++++++++++++++++++- .../platform/PlatformCommandManager.java | 11 +-- .../internal/command/CommandUtil.java | 63 ++++++++++++++++ 3 files changed, 137 insertions(+), 12 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java index 5c8de085c..f3ef732b4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.command; +import com.google.common.collect.Collections2; import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; @@ -38,16 +39,82 @@ import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.function.pattern.Pattern; +import com.sk89q.worldedit.internal.command.CommandRegistrationHandler; +import com.sk89q.worldedit.internal.command.CommandUtil; import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.util.TreeGenerator; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.item.ItemType; +import org.enginehub.piston.CommandManager; +import org.enginehub.piston.CommandManagerService; +import org.enginehub.piston.CommandMetadata; +import org.enginehub.piston.CommandParameters; import org.enginehub.piston.annotation.Command; import org.enginehub.piston.annotation.CommandContainer; import org.enginehub.piston.annotation.param.Arg; +import org.enginehub.piston.part.SubCommandPart; + +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; @CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class) public class ToolCommands { + + public static void register(CommandRegistrationHandler registration, + CommandManager commandManager, + CommandManagerService commandManagerService, + WorldEdit worldEdit) { + // Collect the tool commands + CommandManager collect = commandManagerService.newCommandManager(); + + registration.register( + collect, + ToolCommandsRegistration.builder(), + new ToolCommands(worldEdit) + ); + + // Register deprecated global commands + Set commands = collect.getAllCommands() + .collect(Collectors.toSet()); + for (org.enginehub.piston.Command command : commands) { + commandManager.register(CommandUtil.deprecate( + command, "Using global tool names is deprecated " + + "and will be removed in WorldEdit 8", ToolCommands::asNonGlobal + )); + } + + // Remove aliases with / in them, since it doesn't make sense for sub-commands. + Set nonGlobalCommands = commands.stream() + .map(command -> + command.toBuilder().aliases( + Collections2.filter(command.getAliases(), alias -> !alias.startsWith("/")) + ).build() + ) + .collect(Collectors.toSet()); + commandManager.register("tool", command -> { + command.addPart(SubCommandPart.builder( + TranslatableComponent.of("tool"), + TextComponent.of("The tool to bind") + ) + .withCommands(nonGlobalCommands) + .required() + .build()); + command.description(TextComponent.of("Binds a tool to the item in your hand")); + }); + } + + private static String asNonGlobal(org.enginehub.piston.Command oldCommand, + CommandParameters oldParameters) { + String name = Optional.ofNullable(oldParameters.getMetadata()) + .map(CommandMetadata::getCalledName) + .filter(n -> !n.startsWith("/")) + .orElseGet(oldCommand::getName); + return "/tool " + name; + } + private final WorldEdit we; public ToolCommands(WorldEdit we) { @@ -65,8 +132,8 @@ public class ToolCommands { } @Command( - name = "/selwand", - aliases = "selwand", + name = "selwand", + aliases = "/selwand", desc = "Selection wand tool" ) @CommandPermissions("worldedit.setwand") @@ -78,8 +145,8 @@ public class ToolCommands { } @Command( - name = "/navwand", - aliases = "navwand", + name = "navwand", + aliases = "/navwand", desc = "Navigation wand tool" ) @CommandPermissions("worldedit.setwand") 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 296dfd1c3..2913a277d 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 @@ -19,8 +19,6 @@ package com.sk89q.worldedit.extension.platform; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.reflect.TypeToken; @@ -64,7 +62,6 @@ import com.sk89q.worldedit.command.SnapshotUtilCommandsRegistration; import com.sk89q.worldedit.command.SuperPickaxeCommands; import com.sk89q.worldedit.command.SuperPickaxeCommandsRegistration; import com.sk89q.worldedit.command.ToolCommands; -import com.sk89q.worldedit.command.ToolCommandsRegistration; import com.sk89q.worldedit.command.ToolUtilCommands; import com.sk89q.worldedit.command.ToolUtilCommandsRegistration; import com.sk89q.worldedit.command.UtilityCommands; @@ -141,6 +138,8 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.google.common.base.Preconditions.checkNotNull; + /** * Handles the registration and invocation of commands. * @@ -399,11 +398,7 @@ public final class PlatformCommandManager { SnapshotUtilCommandsRegistration.builder(), new SnapshotUtilCommands(worldEdit) ); - this.registration.register( - commandManager, - ToolCommandsRegistration.builder(), - new ToolCommands(worldEdit) - ); + ToolCommands.register(registration, commandManager, commandManagerService, worldEdit); this.registration.register( commandManager, ToolUtilCommandsRegistration.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 bdcd8e541..d1690fc24 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 @@ -21,11 +21,17 @@ package com.sk89q.worldedit.internal.command; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.internal.util.Substring; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.event.ClickEvent; +import com.sk89q.worldedit.util.formatting.text.format.TextColor; +import com.sk89q.worldedit.util.formatting.text.format.TextDecoration; import org.enginehub.piston.Command; +import org.enginehub.piston.CommandParameters; +import org.enginehub.piston.NoInputCommandParameters; import org.enginehub.piston.exception.CommandException; import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.Key; @@ -43,6 +49,63 @@ import static java.util.stream.Collectors.toList; public class CommandUtil { + private static Component makeDeprecatedFooter(Component newCommand) { + return TextComponent.builder("This command is deprecated. Use ", TextColor.GOLD) + .decoration(TextDecoration.ITALIC, true) + .append(newCommand) + .append(" instead.") + .build(); + } + + public interface NewCommandGenerator { + + String newCommand(Command oldCommand, CommandParameters oldParameters); + + } + + public static Command deprecate(Command command, String reason, + NewCommandGenerator newCommandGenerator) { + Component deprecatedWarning = makeDeprecatedFooter( + newCommandSuggestion(newCommandGenerator, + NoInputCommandParameters.builder().build(), + command) + ); + return command.toBuilder() + .action(parameters -> + deprecatedCommandWarning(parameters, command, reason, newCommandGenerator)) + .footer(command.getFooter() + .map(existingFooter -> existingFooter + .append(TextComponent.newline()).append(deprecatedWarning)) + .orElse(deprecatedWarning)) + .build(); + } + + private static int deprecatedCommandWarning( + CommandParameters parameters, + Command command, + String reason, + NewCommandGenerator generator + ) throws Exception { + parameters.injectedValue(Key.of(Actor.class)) + .ifPresent(actor -> { + Component suggestion = newCommandSuggestion(generator, parameters, command); + actor.print(TextComponent.of(reason + ". Please use ", TextColor.GOLD) + .append(suggestion) + .append(TextComponent.of(" instead.")) + ); + }); + return command.getAction().run(parameters); + } + + private static Component newCommandSuggestion(NewCommandGenerator generator, + CommandParameters parameters, + Command command) { + String suggestedCommand = generator.newCommand(command, parameters); + return TextComponent.of(suggestedCommand) + .decoration(TextDecoration.UNDERLINED, true) + .clickEvent(ClickEvent.suggestCommand(suggestedCommand)); + } + public static Map getSubCommands(Command currentCommand) { return currentCommand.getParts().stream() .filter(p -> p instanceof SubCommandPart)