3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +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
Ursprung f6e6f02a84
Commit 61480544f5
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: A60730A4A4ACE782
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.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.Command; import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandMeta; import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
@ -357,7 +358,10 @@ final class SuggestionsProvider<S> {
return CompletableFuture.allOf(futures).handle((unused, throwable) -> { return CompletableFuture.allOf(futures).handle((unused, throwable) -> {
final List<Suggestions> suggestions = new ArrayList<>(futures.length); final List<Suggestions> suggestions = new ArrayList<>(futures.length);
for (final CompletableFuture<Suggestions> future : futures) { 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()); suggestions.add(future.join());
} }
} }

Datei anzeigen

@ -224,7 +224,7 @@ public class VelocityCommandManager implements CommandManager {
Lists.transform(suggestions.getList(), Suggestion::getText)); Lists.transform(suggestions.getList(), Suggestion::getText));
} catch (final Throwable e) { } catch (final Throwable e) {
// Again, plugins are naughty // Again, plugins are naughty
return CompletableFutures.exceptionallyCompletedFuture( return CompletableFuture.failedFuture(
new RuntimeException("Unable to provide suggestions for " + cmdLine + " for " + source, e)); 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.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -308,7 +308,7 @@ public class BrigadierCommandTests extends CommandTestSuite {
.then(RequiredArgumentBuilder .then(RequiredArgumentBuilder
.<CommandSource, String>argument("child", word()) .<CommandSource, String>argument("child", word())
.suggests((context, builder) -> .suggests((context, builder) ->
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException()))) CompletableFuture.failedFuture(new RuntimeException())))
.build(); .build();
manager.register(new BrigadierCommand(node)); 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.mojang.brigadier.tree.RootCommandNode;
import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -82,6 +83,50 @@ public class CommandGraphInjectorTests extends CommandTestSuite {
assertEquals(1, callCount.get()); 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 @Test
void testInjectsBrigadierCommand() { void testInjectsBrigadierCommand() {
final LiteralCommandNode<CommandSource> node = LiteralArgumentBuilder final LiteralCommandNode<CommandSource> node = LiteralArgumentBuilder

Datei anzeigen

@ -18,7 +18,6 @@
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.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail; 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.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.spotify.futures.CompletableFutures;
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.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -333,7 +330,7 @@ public class RawCommandTests extends CommandTestSuite {
@Override @Override
public CompletableFuture<List<String>> suggestAsync(final Invocation invocation) { 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() { void testSuggestsMergesIgnoringHintsWhoseCustomSuggestionProviderFutureCompletesExceptionally() {
final var hint = RequiredArgumentBuilder final var hint = RequiredArgumentBuilder
.<CommandSource, String>argument("hint", word()) .<CommandSource, String>argument("hint", word())
.suggests((context, builder) -> .suggests((context, builder) -> CompletableFuture.failedFuture(new RuntimeException()))
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException()))
.build(); .build();
final var meta = manager.metaBuilder("hello") final var meta = manager.metaBuilder("hello")
.hint(hint) .hint(hint)

Datei anzeigen

@ -26,7 +26,6 @@ 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.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import java.util.Collections; import java.util.Collections;
@ -332,7 +331,7 @@ public class SimpleCommandTests extends CommandTestSuite {
@Override @Override
public CompletableFuture<List<String>> suggestAsync(final Invocation invocation) { 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() { void testSuggestsMergesIgnoringHintsWhoseCustomSuggestionProviderFutureCompletesExceptionally() {
final var hint = RequiredArgumentBuilder final var hint = RequiredArgumentBuilder
.<CommandSource, String>argument("hint", word()) .<CommandSource, String>argument("hint", word())
.suggests((context, builder) -> .suggests((context, builder) -> CompletableFuture.failedFuture(new RuntimeException()))
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException()))
.build(); .build();
final var meta = manager.metaBuilder("hello") final var meta = manager.metaBuilder("hello")
.hint(hint) .hint(hint)

Datei anzeigen

@ -23,11 +23,11 @@ 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.spotify.futures.CompletableFutures;
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 java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -214,8 +214,7 @@ public class SuggestionsProviderTests extends CommandTestSuite {
void testDoesNotSuggestHintIfHintSuggestionProviderFutureCompletesExceptionally() { void testDoesNotSuggestHintIfHintSuggestionProviderFutureCompletesExceptionally() {
final var hint = RequiredArgumentBuilder final var hint = RequiredArgumentBuilder
.<CommandSource, String>argument("hint", word()) .<CommandSource, String>argument("hint", word())
.suggests((context, builder) -> .suggests((context, builder) -> CompletableFuture.failedFuture(new RuntimeException()))
CompletableFutures.exceptionallyCompletedFuture(new RuntimeException()))
.build(); .build();
final var meta = manager.metaBuilder("hello") final var meta = manager.metaBuilder("hello")
.hint(hint) .hint(hint)