mirror of
https://github.com/PaperMC/Velocity.git
synced 2024-11-27 06:30:35 +08:00
Expose registered aliases in API (#549)
This commit is contained in:
parent
0bad9199dc
commit
76c6827926
@ -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<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.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
@ -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<String> 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");
|
||||
|
@ -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
|
||||
|
@ -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<String> actual = manager.getAliases();
|
||||
assertEquals(expected.length, actual.size());
|
||||
final Collection<String> asList = Arrays.asList(expected);
|
||||
assertTrue(asList.containsAll(actual));
|
||||
assertTrue(actual.containsAll(asList));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user