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

Apply suggested future-related changes

Co-authored-by: A248 <theanandbeh@gmail.com>
Dieser Commit ist enthalten in:
Hugo Manrique 2021-06-11 14:03:38 +02:00 committet von Andrew Steinborn
Ursprung 27dfa13b5f
Commit 009abe4cd3
7 geänderte Dateien mit 59 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -29,6 +29,7 @@ import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
@ -357,7 +358,10 @@ final class SuggestionsProvider<S> {
return CompletableFuture.allOf(futures).handle((unused, throwable) -> {
final List<Suggestions> suggestions = new ArrayList<>(futures.length);
for (final CompletableFuture<Suggestions> future : futures) {
if (!future.isCompletedExceptionally()) {
if (future.isCompletedExceptionally()) {
final Throwable exception = CompletableFutures.getException(future);
LOGGER.error("Node cannot provide suggestions", exception);
} else {
suggestions.add(future.join());
}
}

Datei anzeigen

@ -227,7 +227,7 @@ public class VelocityCommandManager implements CommandManager {
Lists.transform(suggestions.getList(), Suggestion::getText));
} catch (final Throwable e) {
// Again, plugins are naughty
return CompletableFutures.exceptionallyCompletedFuture(
return CompletableFuture.failedFuture(
new RuntimeException("Unable to provide suggestions for " + cmdLine + " for " + source, e));
}
}

Datei anzeigen

@ -27,9 +27,9 @@ import static org.junit.jupiter.api.Assertions.fail;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
@ -308,7 +308,7 @@ public class BrigadierCommandTests extends CommandTestSuite {
.then(RequiredArgumentBuilder
.<CommandSource, String>argument("child", word())
.suggests((context, builder) ->
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException())))
CompletableFuture.failedFuture(new RuntimeException())))
.build();
manager.register(new BrigadierCommand(node));

Datei anzeigen

@ -30,6 +30,7 @@ import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand;
import com.velocitypowered.api.command.SimpleCommand;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.BeforeEach;
@ -82,6 +83,50 @@ public class CommandGraphInjectorTests extends CommandTestSuite {
assertEquals(1, callCount.get());
}
@Test
void testInjectsHintsOfInvocableCommand() {
final var hint = LiteralArgumentBuilder
.<CommandSource>literal("hint")
.build();
final var meta = manager.metaBuilder("hello")
.hint(hint)
.build();
manager.register(meta, (SimpleCommand) invocation -> fail());
manager.getInjector().inject(dest, source);
// Preserves hint node
final var expected = manager.getRoot();
assertEquals(expected, dest);
}
@Test
void testFiltersHintsOfImpermissibleAlias() {
final var callCount = new AtomicInteger();
final var hint = LiteralArgumentBuilder
.<CommandSource>literal("hint")
.build();
final var meta = manager.metaBuilder("hello")
.hint(hint)
.build();
manager.register(meta, new RawCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public boolean hasPermission(final Invocation invocation) {
callCount.incrementAndGet();
return false;
}
});
manager.getInjector().inject(dest, source);
assertTrue(dest.getChildren().isEmpty());
assertEquals(1, callCount.get()); // does not call hasPermission for hints
}
@Test
void testInjectsBrigadierCommand() {
final LiteralCommandNode<CommandSource> node = LiteralArgumentBuilder

Datei anzeigen

@ -18,7 +18,6 @@
package com.velocitypowered.proxy.command;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
@ -26,10 +25,8 @@ import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand;
import com.velocitypowered.api.command.SimpleCommand;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@ -333,7 +330,7 @@ public class RawCommandTests extends CommandTestSuite {
@Override
public CompletableFuture<List<String>> suggestAsync(final Invocation invocation) {
return CompletableFutures.exceptionallyCompletedFuture(new RuntimeException());
return CompletableFuture.failedFuture(new RuntimeException());
}
});
@ -437,8 +434,7 @@ public class RawCommandTests extends CommandTestSuite {
void testSuggestsMergesIgnoringHintsWhoseCustomSuggestionProviderFutureCompletesExceptionally() {
final var hint = RequiredArgumentBuilder
.<CommandSource, String>argument("hint", word())
.suggests((context, builder) ->
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException()))
.suggests((context, builder) -> CompletableFuture.failedFuture(new RuntimeException()))
.build();
final var meta = manager.metaBuilder("hello")
.hint(hint)

Datei anzeigen

@ -26,7 +26,6 @@ import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import java.util.Collections;
@ -332,7 +331,7 @@ public class SimpleCommandTests extends CommandTestSuite {
@Override
public CompletableFuture<List<String>> suggestAsync(final Invocation invocation) {
return CompletableFutures.exceptionallyCompletedFuture(new RuntimeException());
return CompletableFuture.failedFuture(new RuntimeException());
}
});
@ -436,8 +435,7 @@ public class SimpleCommandTests extends CommandTestSuite {
void testSuggestsMergesIgnoringHintsWhoseCustomSuggestionProviderFutureCompletesExceptionally() {
final var hint = RequiredArgumentBuilder
.<CommandSource, String>argument("hint", word())
.suggests((context, builder) ->
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException()))
.suggests((context, builder) -> CompletableFuture.failedFuture(new RuntimeException()))
.build();
final var meta = manager.metaBuilder("hello")
.hint(hint)

Datei anzeigen

@ -23,11 +23,11 @@ import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test;
/**
@ -214,8 +214,7 @@ public class SuggestionsProviderTests extends CommandTestSuite {
void testDoesNotSuggestHintIfHintSuggestionProviderFutureCompletesExceptionally() {
final var hint = RequiredArgumentBuilder
.<CommandSource, String>argument("hint", word())
.suggests((context, builder) ->
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException()))
.suggests((context, builder) -> CompletableFuture.failedFuture(new RuntimeException()))
.build();
final var meta = manager.createMetaBuilder("hello")
.hint(hint)