3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

Check permissions before providing suggestions (#430)

Dieser Commit ist enthalten in:
David Mayr 2021-02-17 01:13:48 +01:00 committet von GitHub
Ursprung 1a768bda9d
Commit 648624d333
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 56 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -46,6 +46,10 @@ public interface CommandNodeFactory<T extends Command> {
},
(context, builder) -> {
String[] args = BrigadierUtils.getSplitArguments(context);
if (!command.hasPermission(context.getSource(), args)) {
return builder.buildFuture();
}
return command.suggestAsync(context.getSource(), args).thenApply(values -> {
for (String value : values) {
builder.suggest(value);
@ -82,6 +86,9 @@ public interface CommandNodeFactory<T extends Command> {
(context, builder) -> {
I invocation = createInvocation(context);
if (!command.hasPermission(invocation)) {
return builder.buildFuture();
}
return command.suggestAsync(invocation).thenApply(values -> {
for (String value : values) {
builder.suggest(value);

Datei anzeigen

@ -25,6 +25,7 @@ import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.proxy.plugin.MockEventManager;
import com.velocitypowered.proxy.plugin.VelocityEventManager;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -487,6 +488,54 @@ public class CommandManagerTests {
manager.offerSuggestions(MockCommandSource.INSTANCE, "foo2 baz ").join());
}
@Test
void testSuggestionPermissions() throws ExecutionException, InterruptedException {
VelocityCommandManager manager = createManager();
RawCommand rawCommand = new RawCommand() {
@Override
public void execute(final Invocation invocation) {
fail("The Command should not be executed while testing suggestions");
}
@Override
public boolean hasPermission(Invocation invocation) {
return invocation.arguments().length() > 0;
}
@Override
public List<String> suggest(final Invocation invocation) {
return ImmutableList.of("suggestion");
}
};
manager.register(rawCommand, "foo");
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo").get().isEmpty());
assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo bar").get().isEmpty());
Command oldCommand = new Command() {
@Override
public void execute(CommandSource source, String @NonNull [] args) {
fail("The Command should not be executed while testing suggestions");
}
@Override
public boolean hasPermission(CommandSource source, String @NonNull [] args) {
return args.length > 0;
}
@Override
public List<String> suggest(CommandSource source, String @NonNull [] currentArgs) {
return ImmutableList.of("suggestion");
}
};
manager.register(oldCommand, "bar");
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "bar").get().isEmpty());
assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "bar foo").get().isEmpty());
}
static class NoopSimpleCommand implements SimpleCommand {
@Override
public void execute(final Invocation invocation) {