From 067a570df0359695d24f0419787241726e13f591 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Sun, 5 May 2019 22:49:48 -0700 Subject: [PATCH] Implement basic Forge suggestions --- .../platform/PlatformCommandManager.java | 3 +- .../sk89q/worldedit/forge/CommandWrapper.java | 50 ++++++++++++++++++- worldedit-libs/build.gradle | 2 +- 3 files changed, 51 insertions(+), 4 deletions(-) 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 bdd904226..b56877590 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 @@ -567,8 +567,9 @@ public final class PlatformCommandManager { event.setSuggestions(suggestions.stream() .map(suggestion -> { + int noSlashLength = arguments.length() - 1; Substring original = suggestion.getReplacedArgument() == split.size() - ? Substring.from(arguments, arguments.length() - 1) + ? Substring.from(arguments, noSlashLength, noSlashLength) : split.get(suggestion.getReplacedArgument()); // increase original points by 1, for removed `/` in `parseArgs` return Substring.wrap( diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java index 2f4c650b9..6a15e51dd 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java @@ -22,15 +22,28 @@ package com.sk89q.worldedit.forge; import com.google.common.collect.ImmutableList; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.context.StringRange; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.Suggestion; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.command.util.PermissionCondition; +import com.sk89q.worldedit.event.platform.CommandSuggestionEvent; +import com.sk89q.worldedit.internal.util.Substring; import net.minecraft.command.CommandSource; import net.minecraft.entity.player.EntityPlayerMP; +import java.util.List; import java.util.Set; +import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; import java.util.stream.Stream; +import static net.minecraft.command.Commands.argument; import static net.minecraft.command.Commands.literal; public final class CommandWrapper { @@ -41,8 +54,10 @@ public final class CommandWrapper { ImmutableList.Builder aliases = ImmutableList.builder(); aliases.add(command.getName()).addAll(command.getAliases()); for (String alias : aliases.build()) { - LiteralArgumentBuilder base = literal(alias) - .executes(FAKE_COMMAND); + LiteralArgumentBuilder base = literal(alias).executes(FAKE_COMMAND) + .then(argument("args", StringArgumentType.greedyString()) + .suggests(CommandWrapper::suggest) + .executes(FAKE_COMMAND)); if (command.getCondition().as(PermissionCondition.class) .filter(p -> p.getPermissions().size() > 0).isPresent()) { base.requires(requirementsFor(command)); @@ -73,4 +88,35 @@ public final class CommandWrapper { }; } + private static CompletableFuture suggest(CommandContext context, + SuggestionsBuilder builder) throws CommandSyntaxException { + CommandSuggestionEvent event = new CommandSuggestionEvent( + ForgeAdapter.adaptPlayer(context.getSource().asPlayer()), + builder.getInput() + ); + WorldEdit.getInstance().getEventBus().post(event); + List suggestions = event.getSuggestions(); + + ImmutableList.Builder result = ImmutableList.builder(); + + for (Substring suggestion : suggestions) { + String suggestionText = suggestion.getSubstring(); + // If at end, we are actually suggesting the next argument + // Ensure there is a space! + if (suggestion.getStart() == suggestion.getEnd() + && suggestion.getEnd() == builder.getInput().length() + && !builder.getInput().endsWith(" ")) { + suggestionText = " " + suggestionText; + } + result.add(new Suggestion( + StringRange.between(suggestion.getStart(), suggestion.getEnd()), + suggestionText + )); + } + + return CompletableFuture.completedFuture( + Suggestions.create(builder.getInput(), result.build()) + ); + } + } diff --git a/worldedit-libs/build.gradle b/worldedit-libs/build.gradle index f684475e2..6549fa81c 100644 --- a/worldedit-libs/build.gradle +++ b/worldedit-libs/build.gradle @@ -90,7 +90,7 @@ configure(subprojects + project("core:ap")) { def textVersion = "3.0.0" project("core") { - def pistonVersion = '0.2.1' + def pistonVersion = '0.2.2' dependencies { shade "net.kyori:text-api:$textVersion"