Expose registered aliases in API (#549)

This commit is contained in:
Hugo Manrique 2021-07-24 20:34:50 +02:00 committed by GitHub
parent 0bad9199dc
commit 76c6827926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 7 deletions

View File

@ -8,6 +8,7 @@
package com.velocitypowered.api.command; package com.velocitypowered.api.command;
import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.command.CommandExecuteEvent;
import java.util.Collection;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
/** /**
@ -94,11 +95,19 @@ public interface CommandManager {
*/ */
CompletableFuture<Boolean> executeImmediatelyAsync(CommandSource source, String cmdLine); CompletableFuture<Boolean> executeImmediatelyAsync(CommandSource source, String cmdLine);
/**
* Returns an immutable collection of the case-insensitive aliases registered
* on this manager.
*
* @return the registered aliases
*/
Collection<String> getAliases();
/** /**
* Returns whether the given alias is registered on this manager. * Returns whether the given alias is registered on this manager.
* *
* @param alias the command alias to check * @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); boolean hasCommand(String alias);
} }

View File

@ -24,6 +24,7 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode; import com.mojang.brigadier.tree.RootCommandNode;
import com.spotify.futures.CompletableFutures; import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.BrigadierCommand; 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.RawCommandRegistrar;
import com.velocitypowered.proxy.command.registrar.SimpleCommandRegistrar; import com.velocitypowered.proxy.command.registrar.SimpleCommandRegistrar;
import com.velocitypowered.proxy.event.VelocityEventManager; import com.velocitypowered.proxy.event.VelocityEventManager;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
import net.kyori.adventure.identity.Identity; import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -249,12 +252,19 @@ public class VelocityCommandManager implements CommandManager {
} }
} }
/** @Override
* Returns whether the given alias is registered on this manager. public Collection<String> getAliases() {
* lock.readLock().lock();
* @param alias the command alias to check try {
* @return true if the alias is registered; false otherwise // A RootCommandNode may only contain LiteralCommandNode children instances
*/ return dispatcher.getRoot().getChildren().stream()
.map(CommandNode::getName)
.collect(ImmutableList.toImmutableList());
} finally {
lock.readLock().unlock();
}
}
@Override @Override
public boolean hasCommand(final String alias) { public boolean hasCommand(final String alias) {
Preconditions.checkNotNull(alias, "alias"); Preconditions.checkNotNull(alias, "alias");

View File

@ -17,11 +17,13 @@
package com.velocitypowered.proxy.command; 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; 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.LiteralArgumentBuilder;
import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
@ -41,6 +43,7 @@ public class CommandManagerTests extends CommandTestSuite {
manager.register(meta, DummyCommand.INSTANCE); manager.register(meta, DummyCommand.INSTANCE);
assertTrue(manager.hasCommand("hello")); assertTrue(manager.hasCommand("hello"));
assertRegisteredAliases("hello");
} }
@Test @Test
@ -55,6 +58,7 @@ public class CommandManagerTests extends CommandTestSuite {
assertTrue(manager.hasCommand("bar")); assertTrue(manager.hasCommand("bar"));
assertTrue(manager.hasCommand("baz")); assertTrue(manager.hasCommand("baz"));
assertTrue(manager.hasCommand("qux")); assertTrue(manager.hasCommand("qux"));
assertRegisteredAliases("foo", "bar", "baz", "qux");
} }
@Test @Test
@ -66,6 +70,7 @@ public class CommandManagerTests extends CommandTestSuite {
assertTrue(manager.hasCommand("foo")); assertTrue(manager.hasCommand("foo"));
assertTrue(manager.hasCommand("bar")); assertTrue(manager.hasCommand("bar"));
assertRegisteredAliases("foo", "bar");
} }
@Test @Test
@ -76,6 +81,7 @@ public class CommandManagerTests extends CommandTestSuite {
manager.register(new BrigadierCommand(node)); manager.register(new BrigadierCommand(node));
assertTrue(manager.hasCommand("hello")); assertTrue(manager.hasCommand("hello"));
assertRegisteredAliases("hello");
} }
@Test @Test
@ -125,6 +131,7 @@ public class CommandManagerTests extends CommandTestSuite {
manager.unregister("hello"); manager.unregister("hello");
assertFalse(manager.hasCommand("hello")); assertFalse(manager.hasCommand("hello"));
assertRegisteredAliases();
} }
@Test @Test
@ -133,6 +140,7 @@ public class CommandManagerTests extends CommandTestSuite {
manager.unregister("hello"); manager.unregister("hello");
assertFalse(manager.hasCommand("hello")); assertFalse(manager.hasCommand("hello"));
assertRegisteredAliases();
} }
@Test @Test
@ -145,6 +153,7 @@ public class CommandManagerTests extends CommandTestSuite {
assertFalse(manager.hasCommand("bar")); assertFalse(manager.hasCommand("bar"));
assertTrue(manager.hasCommand("foo")); assertTrue(manager.hasCommand("foo"));
assertRegisteredAliases("foo");
} }
// Execution // Execution

View File

@ -30,6 +30,7 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.proxy.event.MockEventManager; import com.velocitypowered.proxy.event.MockEventManager;
import com.velocitypowered.proxy.event.VelocityEventManager; import com.velocitypowered.proxy.event.VelocityEventManager;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -80,4 +81,12 @@ abstract class CommandTestSuite {
final var actual = manager.offerSuggestions(player, input).join(); final var actual = manager.offerSuggestions(player, input).join();
assertEquals(Arrays.asList(expectedSuggestions), actual); assertEquals(Arrays.asList(expectedSuggestions), actual);
} }
final void assertRegisteredAliases(final String... expected) {
final Collection<String> actual = manager.getAliases();
assertEquals(expected.length, actual.size());
final Collection<String> asList = Arrays.asList(expected);
assertTrue(asList.containsAll(actual));
assertTrue(actual.containsAll(asList));
}
} }