Implement basic Forge suggestions

This commit is contained in:
Kenzie Togami 2019-05-05 22:49:48 -07:00 committed by Kenzie Togami
parent 8a3e6a12b9
commit 067a570df0
3 changed files with 51 additions and 4 deletions

View File

@ -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(

View File

@ -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<String> aliases = ImmutableList.builder();
aliases.add(command.getName()).addAll(command.getAliases());
for (String alias : aliases.build()) {
LiteralArgumentBuilder<CommandSource> base = literal(alias)
.executes(FAKE_COMMAND);
LiteralArgumentBuilder<CommandSource> 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<Suggestions> suggest(CommandContext<CommandSource> context,
SuggestionsBuilder builder) throws CommandSyntaxException {
CommandSuggestionEvent event = new CommandSuggestionEvent(
ForgeAdapter.adaptPlayer(context.getSource().asPlayer()),
builder.getInput()
);
WorldEdit.getInstance().getEventBus().post(event);
List<Substring> suggestions = event.getSuggestions();
ImmutableList.Builder<Suggestion> 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())
);
}
}

View File

@ -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"