From 76c682792602c9b4385b87932ed27efe7a55df15 Mon Sep 17 00:00:00 2001 From: Hugo Manrique Date: Sat, 24 Jul 2021 20:34:50 +0200 Subject: [PATCH] Expose registered aliases in API (#549) --- .../api/command/CommandManager.java | 11 +++++++++- .../proxy/command/VelocityCommandManager.java | 22 ++++++++++++++----- .../proxy/command/CommandManagerTests.java | 9 ++++++++ .../proxy/command/CommandTestSuite.java | 9 ++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandManager.java b/api/src/main/java/com/velocitypowered/api/command/CommandManager.java index 00a2ae2a1..9cc92a2e8 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandManager.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandManager.java @@ -8,6 +8,7 @@ package com.velocitypowered.api.command; import com.velocitypowered.api.event.command.CommandExecuteEvent; +import java.util.Collection; import java.util.concurrent.CompletableFuture; /** @@ -94,11 +95,19 @@ public interface CommandManager { */ CompletableFuture executeImmediatelyAsync(CommandSource source, String cmdLine); + /** + * Returns an immutable collection of the case-insensitive aliases registered + * on this manager. + * + * @return the registered aliases + */ + Collection getAliases(); + /** * Returns whether the given alias is registered on this manager. * * @param alias the command alias to check - * @return {@code true} if the alias is registered + * @return true if the alias is registered; false otherwise */ boolean hasCommand(String alias); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java index 0fc0ea8b8..45c259d8d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java @@ -24,6 +24,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestion; +import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.RootCommandNode; import com.spotify.futures.CompletableFutures; import com.velocitypowered.api.command.BrigadierCommand; @@ -38,11 +39,13 @@ import com.velocitypowered.proxy.command.registrar.CommandRegistrar; import com.velocitypowered.proxy.command.registrar.RawCommandRegistrar; import com.velocitypowered.proxy.command.registrar.SimpleCommandRegistrar; import com.velocitypowered.proxy.event.VelocityEventManager; +import java.util.Collection; import java.util.List; import java.util.Locale; import java.util.concurrent.CompletableFuture; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.stream.Collectors; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -249,12 +252,19 @@ public class VelocityCommandManager implements CommandManager { } } - /** - * Returns whether the given alias is registered on this manager. - * - * @param alias the command alias to check - * @return true if the alias is registered; false otherwise - */ + @Override + public Collection getAliases() { + lock.readLock().lock(); + try { + // A RootCommandNode may only contain LiteralCommandNode children instances + return dispatcher.getRoot().getChildren().stream() + .map(CommandNode::getName) + .collect(ImmutableList.toImmutableList()); + } finally { + lock.readLock().unlock(); + } + } + @Override public boolean hasCommand(final String alias) { Preconditions.checkNotNull(alias, "alias"); diff --git a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java index e977c12f0..bc5c826b4 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java @@ -17,11 +17,13 @@ package com.velocitypowered.proxy.command; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import com.google.common.collect.ImmutableList; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.CommandSource; @@ -41,6 +43,7 @@ public class CommandManagerTests extends CommandTestSuite { manager.register(meta, DummyCommand.INSTANCE); assertTrue(manager.hasCommand("hello")); + assertRegisteredAliases("hello"); } @Test @@ -55,6 +58,7 @@ public class CommandManagerTests extends CommandTestSuite { assertTrue(manager.hasCommand("bar")); assertTrue(manager.hasCommand("baz")); assertTrue(manager.hasCommand("qux")); + assertRegisteredAliases("foo", "bar", "baz", "qux"); } @Test @@ -66,6 +70,7 @@ public class CommandManagerTests extends CommandTestSuite { assertTrue(manager.hasCommand("foo")); assertTrue(manager.hasCommand("bar")); + assertRegisteredAliases("foo", "bar"); } @Test @@ -76,6 +81,7 @@ public class CommandManagerTests extends CommandTestSuite { manager.register(new BrigadierCommand(node)); assertTrue(manager.hasCommand("hello")); + assertRegisteredAliases("hello"); } @Test @@ -125,6 +131,7 @@ public class CommandManagerTests extends CommandTestSuite { manager.unregister("hello"); assertFalse(manager.hasCommand("hello")); + assertRegisteredAliases(); } @Test @@ -133,6 +140,7 @@ public class CommandManagerTests extends CommandTestSuite { manager.unregister("hello"); assertFalse(manager.hasCommand("hello")); + assertRegisteredAliases(); } @Test @@ -145,6 +153,7 @@ public class CommandManagerTests extends CommandTestSuite { assertFalse(manager.hasCommand("bar")); assertTrue(manager.hasCommand("foo")); + assertRegisteredAliases("foo"); } // Execution diff --git a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java index 45c479747..5e0a11960 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java @@ -30,6 +30,7 @@ import com.velocitypowered.api.proxy.Player; import com.velocitypowered.proxy.event.MockEventManager; import com.velocitypowered.proxy.event.VelocityEventManager; import java.util.Arrays; +import java.util.Collection; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -80,4 +81,12 @@ abstract class CommandTestSuite { final var actual = manager.offerSuggestions(player, input).join(); assertEquals(Arrays.asList(expectedSuggestions), actual); } + + final void assertRegisteredAliases(final String... expected) { + final Collection actual = manager.getAliases(); + assertEquals(expected.length, actual.size()); + final Collection asList = Arrays.asList(expected); + assertTrue(asList.containsAll(actual)); + assertTrue(actual.containsAll(asList)); + } }