Fix SimpleCommand suggestion offset (#1664)

* Fix command suggestion offset

* fix length error

* add test

* checkstyle

---------

Co-authored-by: Ross <2086824-trashp@users.noreply.gitlab.com>
This commit is contained in:
Ross
2025-10-13 21:41:33 +02:00
committed by GitHub
parent 806b386cdb
commit 5753548b44
3 changed files with 32 additions and 3 deletions

View File

@@ -103,13 +103,17 @@ abstract class InvocableCommandRegistrar<T extends InvocableCommand<I>,
.requiresWithContext((context, reader) -> requirement.test(context)) .requiresWithContext((context, reader) -> requirement.test(context))
.executes(callback) .executes(callback)
.suggests((context, builder) -> { .suggests((context, builder) -> {
// Offset the suggestion to the last space seperated word
int lastSpace = builder.getRemaining().lastIndexOf(' ') + 1;
final var offsetBuilder = builder.createOffset(builder.getStart() + lastSpace);
final I invocation = invocationFactory.create(context); final I invocation = invocationFactory.create(context);
return command.suggestAsync(invocation).thenApply(suggestions -> { return command.suggestAsync(invocation).thenApply(suggestions -> {
for (String value : suggestions) { for (String value : suggestions) {
Preconditions.checkNotNull(value, "suggestion"); Preconditions.checkNotNull(value, "suggestion");
builder.suggest(value); offsetBuilder.suggest(value);
} }
return builder.build(); return offsetBuilder.build();
}); });
}) })
.build(); .build();

View File

@@ -724,7 +724,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
TabCompleteResponsePacket resp = new TabCompleteResponsePacket(); TabCompleteResponsePacket resp = new TabCompleteResponsePacket();
resp.setTransactionId(packet.getTransactionId()); resp.setTransactionId(packet.getTransactionId());
resp.setStart(startPos + 1); resp.setStart(startPos + 1);
resp.setLength(packet.getCommand().length() - startPos); resp.setLength(packet.getCommand().length() - startPos - 1);
resp.getOffers().addAll(offers); resp.getOffers().addAll(offers);
player.getConnection().write(resp); player.getConnection().write(resp);
} }

View File

@@ -18,14 +18,17 @@
package com.velocitypowered.proxy.command; package com.velocitypowered.proxy.command;
import static com.mojang.brigadier.arguments.StringArgumentType.word; import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.suggestion.Suggestions;
import com.velocitypowered.api.command.Command; import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand; import com.velocitypowered.api.command.RawCommand;
import com.velocitypowered.api.command.SimpleCommand;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -286,4 +289,26 @@ public class SuggestionsProviderTests extends CommandTestSuite {
return ImmutableList.of(); return ImmutableList.of();
} }
} }
@Test
void testSuggestionOffset() {
final var meta = manager.metaBuilder("offset").build();
manager.register(meta, new SimpleCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public List<String> suggest(final Invocation invocation) {
return List.of("bump");
}
});
assertSuggestions("offset bu", "bump");
for (int i = 10; i < 20; i++) {
final Suggestions suggestions = manager.offerBrigadierSuggestions(source, "offset " + "bump ".repeat(i)).join();
assertEquals(7 + 5 * i, suggestions.getList().get(0).getRange().getStart());
}
}
} }