13
0
geforkt von Mirrors/Velocity

Just import the entire command manager from Polymer, without changing any names

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-05-23 13:58:27 -04:00
Ursprung 590f18dbe0
Commit 569bb4a16b
2 geänderte Dateien mit 105 neuen und 107 gelöschten Zeilen

Datei anzeigen

@ -124,7 +124,7 @@ public class VelocityCommandManager implements CommandManager {
* @return the {@link CompletableFuture} of the event * @return the {@link CompletableFuture} of the event
*/ */
public CompletableFuture<CommandExecuteEvent> callCommandEvent(final CommandSource source, public CompletableFuture<CommandExecuteEvent> callCommandEvent(final CommandSource source,
final String cmdLine) { final String cmdLine) {
Preconditions.checkNotNull(source, "source"); Preconditions.checkNotNull(source, "source");
Preconditions.checkNotNull(cmdLine, "cmdLine"); Preconditions.checkNotNull(cmdLine, "cmdLine");
return eventManager.fire(new CommandExecuteEvent(source, cmdLine)); return eventManager.fire(new CommandExecuteEvent(source, cmdLine));
@ -139,7 +139,7 @@ public class VelocityCommandManager implements CommandManager {
return dispatcher.execute(results) != BrigadierCommand.FORWARD; return dispatcher.execute(results) != BrigadierCommand.FORWARD;
} catch (final CommandSyntaxException e) { } catch (final CommandSyntaxException e) {
boolean isSyntaxError = !e.getType().equals( boolean isSyntaxError = !e.getType().equals(
CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()); CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand());
if (isSyntaxError) { if (isSyntaxError) {
source.sendMessage(Identity.nil(), Component.text(e.getMessage(), NamedTextColor.RED)); source.sendMessage(Identity.nil(), Component.text(e.getMessage(), NamedTextColor.RED));
// This is, of course, a lie, but the API will need to change... // This is, of course, a lie, but the API will need to change...
@ -169,7 +169,7 @@ public class VelocityCommandManager implements CommandManager {
@Override @Override
public CompletableFuture<Boolean> executeImmediately( public CompletableFuture<Boolean> executeImmediately(
final CommandSource source, final String cmdLine) { final CommandSource source, final String cmdLine) {
Preconditions.checkNotNull(source, "source"); Preconditions.checkNotNull(source, "source");
Preconditions.checkNotNull(cmdLine, "cmdLine"); Preconditions.checkNotNull(cmdLine, "cmdLine");
@ -186,17 +186,17 @@ public class VelocityCommandManager implements CommandManager {
* possibly empty * possibly empty
*/ */
public CompletableFuture<List<String>> offerSuggestions(final CommandSource source, public CompletableFuture<List<String>> offerSuggestions(final CommandSource source,
final String cmdLine) { final String cmdLine) {
Preconditions.checkNotNull(source, "source"); Preconditions.checkNotNull(source, "source");
Preconditions.checkNotNull(cmdLine, "cmdLine"); Preconditions.checkNotNull(cmdLine, "cmdLine");
ParseResults<CommandSource> parse = parse(cmdLine, source, false); ParseResults<CommandSource> parse = parse(cmdLine, source, false);
return dispatcher.getCompletionSuggestions(parse) return dispatcher.getCompletionSuggestions(parse)
.thenApply(suggestions -> Lists.transform(suggestions.getList(), Suggestion::getText)); .thenApply(suggestions -> Lists.transform(suggestions.getList(), Suggestion::getText));
} }
private ParseResults<CommandSource> parse(final String cmdLine, final CommandSource source, private ParseResults<CommandSource> parse(final String cmdLine, final CommandSource source,
final boolean trim) { final boolean trim) {
String normalized = BrigadierUtils.normalizeInput(cmdLine, trim); String normalized = BrigadierUtils.normalizeInput(cmdLine, trim);
return dispatcher.parse(normalized, source); return dispatcher.parse(normalized, source);
} }
@ -216,4 +216,4 @@ public class VelocityCommandManager implements CommandManager {
public CommandDispatcher<CommandSource> getDispatcher() { public CommandDispatcher<CommandSource> getDispatcher() {
return dispatcher; return dispatcher;
} }
} }

Datei anzeigen

@ -34,7 +34,6 @@ import com.mojang.brigadier.tree.ArgumentCommandNode;
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.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.BrigadierCommand;
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;
import com.velocitypowered.api.command.RawCommand; import com.velocitypowered.api.command.RawCommand;
@ -80,8 +79,8 @@ public class CommandManagerTests {
void testBrigadierRegister() { void testBrigadierRegister() {
VelocityCommandManager manager = createManager(); VelocityCommandManager manager = createManager();
LiteralCommandNode<CommandSource> node = LiteralArgumentBuilder LiteralCommandNode<CommandSource> node = LiteralArgumentBuilder
.<CommandSource>literal("foo") .<CommandSource>literal("foo")
.build(); .build();
BrigadierCommand command = new BrigadierCommand(node); BrigadierCommand command = new BrigadierCommand(node);
manager.register(command); manager.register(command);
@ -89,12 +88,12 @@ public class CommandManagerTests {
assertTrue(manager.hasCommand("fOo")); assertTrue(manager.hasCommand("fOo"));
LiteralCommandNode<CommandSource> barNode = LiteralArgumentBuilder LiteralCommandNode<CommandSource> barNode = LiteralArgumentBuilder
.<CommandSource>literal("bar") .<CommandSource>literal("bar")
.build(); .build();
BrigadierCommand aliasesCommand = new BrigadierCommand(barNode); BrigadierCommand aliasesCommand = new BrigadierCommand(barNode);
CommandMeta meta = manager.metaBuilder(aliasesCommand) CommandMeta meta = manager.metaBuilder(aliasesCommand)
.aliases("baZ") .aliases("baZ")
.build(); .build();
assertEquals(ImmutableSet.of("bar", "baz"), meta.getAliases()); assertEquals(ImmutableSet.of("bar", "baz"), meta.getAliases());
assertTrue(meta.getHints().isEmpty()); assertTrue(meta.getHints().isEmpty());
@ -135,36 +134,36 @@ public class CommandManagerTests {
AtomicBoolean executed = new AtomicBoolean(false); AtomicBoolean executed = new AtomicBoolean(false);
AtomicBoolean checkedRequires = new AtomicBoolean(false); AtomicBoolean checkedRequires = new AtomicBoolean(false);
LiteralCommandNode<CommandSource> node = LiteralArgumentBuilder LiteralCommandNode<CommandSource> node = LiteralArgumentBuilder
.<CommandSource>literal("buy") .<CommandSource>literal("buy")
.executes(context -> { .executes(context -> {
assertEquals(MockCommandSource.INSTANCE, context.getSource()); assertEquals(MockCommandSource.INSTANCE, context.getSource());
assertEquals("buy", context.getInput()); assertEquals("buy", context.getInput());
executed.set(true); executed.set(true);
return 1; return 1;
}) })
.build(); .build();
CommandNode<CommandSource> quantityNode = RequiredArgumentBuilder CommandNode<CommandSource> quantityNode = RequiredArgumentBuilder
.<CommandSource, Integer>argument("quantity", IntegerArgumentType.integer(12, 16)) .<CommandSource, Integer>argument("quantity", IntegerArgumentType.integer(12, 16))
.requires(source -> { .requires(source -> {
assertEquals(MockCommandSource.INSTANCE, source); assertEquals(MockCommandSource.INSTANCE, source);
checkedRequires.set(true); checkedRequires.set(true);
return true; return true;
}) })
.executes(context -> { .executes(context -> {
int argument = IntegerArgumentType.getInteger(context, "quantity"); int argument = IntegerArgumentType.getInteger(context, "quantity");
assertEquals(14, argument); assertEquals(14, argument);
executed.set(true); executed.set(true);
return 1; return 1;
}) })
.build(); .build();
CommandNode<CommandSource> productNode = RequiredArgumentBuilder CommandNode<CommandSource> productNode = RequiredArgumentBuilder
.<CommandSource, String>argument("product", StringArgumentType.string()) .<CommandSource, String>argument("product", StringArgumentType.string())
.requires(source -> { .requires(source -> {
checkedRequires.set(true); checkedRequires.set(true);
return false; return false;
}) })
.executes(context -> fail("was executed")) .executes(context -> fail("was executed"))
.build(); .build();
quantityNode.addChild(productNode); quantityNode.addChild(productNode);
node.addChild(quantityNode); node.addChild(quantityNode);
manager.register(new BrigadierCommand(node)); manager.register(new BrigadierCommand(node));
@ -174,9 +173,9 @@ public class CommandManagerTests {
assertTrue(manager.executeImmediately(MockCommandSource.INSTANCE, "buy 14").join()); assertTrue(manager.executeImmediately(MockCommandSource.INSTANCE, "buy 14").join());
assertTrue(checkedRequires.compareAndSet(true, false)); assertTrue(checkedRequires.compareAndSet(true, false));
assertTrue(executed.get()); assertTrue(executed.get());
assertFalse(manager.execute(MockCommandSource.INSTANCE, "buy 9").join(), assertTrue(manager.execute(MockCommandSource.INSTANCE, "buy 9").join(),
"Invalid arg returns false"); "Invalid arg returns false");
assertFalse(manager.executeImmediately(MockCommandSource.INSTANCE, "buy 12 bananas") assertTrue(manager.executeImmediately(MockCommandSource.INSTANCE, "buy 12 bananas")
.join()); .join());
assertTrue(checkedRequires.get()); assertTrue(checkedRequires.get());
} }
@ -266,15 +265,15 @@ public class CommandManagerTests {
VelocityCommandManager manager = createManager(); VelocityCommandManager manager = createManager();
LiteralCommandNode<CommandSource> brigadierNode = LiteralArgumentBuilder LiteralCommandNode<CommandSource> brigadierNode = LiteralArgumentBuilder
.<CommandSource>literal("brigadier") .<CommandSource>literal("brigadier")
.build(); .build();
CommandNode<CommandSource> nameNode = RequiredArgumentBuilder CommandNode<CommandSource> nameNode = RequiredArgumentBuilder
.<CommandSource, String>argument("name", StringArgumentType.string()) .<CommandSource, String>argument("name", StringArgumentType.string())
.build(); .build();
CommandNode<CommandSource> numberNode = RequiredArgumentBuilder CommandNode<CommandSource> numberNode = RequiredArgumentBuilder
.<CommandSource, Integer>argument("quantity", IntegerArgumentType.integer()) .<CommandSource, Integer>argument("quantity", IntegerArgumentType.integer())
.suggests((context, builder) -> builder.suggest(2).suggest(3).buildFuture()) .suggests((context, builder) -> builder.suggest(2).suggest(3).buildFuture())
.build(); .build();
nameNode.addChild(numberNode); nameNode.addChild(numberNode);
brigadierNode.addChild(nameNode); brigadierNode.addChild(nameNode);
manager.register(new BrigadierCommand(brigadierNode)); manager.register(new BrigadierCommand(brigadierNode));
@ -322,53 +321,53 @@ public class CommandManagerTests {
manager.register("raw", rawCommand); manager.register("raw", rawCommand);
assertEquals( assertEquals(
ImmutableList.of("brigadier", "raw", "simple"), ImmutableList.of("brigadier", "raw", "simple"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "").join(), manager.offerSuggestions(MockCommandSource.INSTANCE, "").join(),
"literals are in alphabetical order"); "literals are in alphabetical order");
assertEquals( assertEquals(
ImmutableList.of("brigadier"), ImmutableList.of("brigadier"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "briga").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "briga").join());
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier") assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier")
.join().isEmpty()); .join().isEmpty());
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier ") assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier ")
.join().isEmpty()); .join().isEmpty());
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier foo") assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier foo")
.join().isEmpty()); .join().isEmpty());
assertEquals( assertEquals(
ImmutableList.of("2", "3"), ImmutableList.of("2", "3"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier foo ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "brigadier foo ").join());
assertEquals( assertEquals(
ImmutableList.of("bar", "foo"), ImmutableList.of("bar", "foo"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "simple ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "simple ").join());
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "simple") assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "simple")
.join().isEmpty()); .join().isEmpty());
assertEquals( assertEquals(
ImmutableList.of("123"), ImmutableList.of("123"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "simPle foo").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "simPle foo").join());
assertEquals( assertEquals(
ImmutableList.of("baz", "foo"), ImmutableList.of("baz", "foo"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "raw ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "raw ").join());
assertEquals( assertEquals(
ImmutableList.of("2", "3", "5", "7"), ImmutableList.of("2", "3", "5", "7"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "raw foo ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "raw foo ").join());
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "raw foo") assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "raw foo")
.join().isEmpty()); .join().isEmpty());
assertEquals( assertEquals(
ImmutableList.of("11", "13", "17"), ImmutableList.of("11", "13", "17"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "rAW bar ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "rAW bar ").join());
} }
@Test @Test
void testBrigadierSuggestionPermissions() { void testBrigadierSuggestionPermissions() {
VelocityCommandManager manager = createManager(); VelocityCommandManager manager = createManager();
LiteralCommandNode<CommandSource> manageNode = LiteralArgumentBuilder LiteralCommandNode<CommandSource> manageNode = LiteralArgumentBuilder
.<CommandSource>literal("manage") .<CommandSource>literal("manage")
.requires(source -> false) .requires(source -> false)
.build(); .build();
CommandNode<CommandSource> idNode = RequiredArgumentBuilder CommandNode<CommandSource> idNode = RequiredArgumentBuilder
.<CommandSource, Integer>argument("id", IntegerArgumentType.integer(0)) .<CommandSource, Integer>argument("id", IntegerArgumentType.integer(0))
.suggests((context, builder) -> fail("called suggestion builder")) .suggests((context, builder) -> fail("called suggestion builder"))
.build(); .build();
manageNode.addChild(idNode); manageNode.addChild(idNode);
manager.register(new BrigadierCommand(manageNode)); manager.register(new BrigadierCommand(manageNode));
@ -376,12 +375,11 @@ public class CommandManagerTests {
// However, it won't query children if the source doesn't pass the parent // However, it won't query children if the source doesn't pass the parent
// #requires predicate. // #requires predicate.
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "manage ") assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "manage ")
.join().isEmpty()); .join().isEmpty());
} }
// TODO: Hug needs to fix this test!
@Disabled
@Test @Test
@Disabled
void testHinting() { void testHinting() {
VelocityCommandManager manager = createManager(); VelocityCommandManager manager = createManager();
AtomicBoolean executed = new AtomicBoolean(false); AtomicBoolean executed = new AtomicBoolean(false);
@ -401,25 +399,25 @@ public class CommandManagerTests {
}; };
CommandNode<CommandSource> barHint = LiteralArgumentBuilder CommandNode<CommandSource> barHint = LiteralArgumentBuilder
.<CommandSource>literal("bar") .<CommandSource>literal("bar")
.executes(context -> fail("hints don't get executed")) .executes(context -> fail("hints don't get executed"))
.build(); .build();
ArgumentCommandNode<CommandSource, Integer> numberArg = RequiredArgumentBuilder ArgumentCommandNode<CommandSource, Integer> numberArg = RequiredArgumentBuilder
.<CommandSource, Integer>argument("number", IntegerArgumentType.integer()) .<CommandSource, Integer>argument("number", IntegerArgumentType.integer())
.suggests((context, builder) -> { .suggests((context, builder) -> {
calledSuggestionProvider.set(true); calledSuggestionProvider.set(true);
return builder.suggest("456").buildFuture(); return builder.suggest("456").buildFuture();
}) })
.build(); .build();
barHint.addChild(numberArg); barHint.addChild(numberArg);
CommandNode<CommandSource> bazHint = LiteralArgumentBuilder CommandNode<CommandSource> bazHint = LiteralArgumentBuilder
.<CommandSource>literal("baz") .<CommandSource>literal("baz")
.build(); .build();
CommandMeta meta = manager.metaBuilder("foo") CommandMeta meta = manager.metaBuilder("foo")
.aliases("foo2") .aliases("foo2")
.hint(barHint) .hint(barHint)
.hint(bazHint) .hint(bazHint)
.build(); .build();
manager.register(meta, command); manager.register(meta, command);
expectedArgs.set("notBarOrBaz"); expectedArgs.set("notBarOrBaz");
@ -436,13 +434,13 @@ public class CommandManagerTests {
assertTrue(executed.compareAndSet(true, false)); assertTrue(executed.compareAndSet(true, false));
assertEquals(ImmutableList.of("bar", "baz", "raw"), assertEquals(ImmutableList.of("bar", "baz", "raw"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "foo ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "foo ").join());
assertFalse(calledSuggestionProvider.get()); assertFalse(calledSuggestionProvider.get());
assertEquals(ImmutableList.of("456"), assertEquals(ImmutableList.of("456"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "foo bar ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "foo bar ").join());
assertTrue(calledSuggestionProvider.compareAndSet(true, false)); assertTrue(calledSuggestionProvider.compareAndSet(true, false));
assertEquals(ImmutableList.of(), assertEquals(ImmutableList.of(),
manager.offerSuggestions(MockCommandSource.INSTANCE, "foo2 baz ").join()); manager.offerSuggestions(MockCommandSource.INSTANCE, "foo2 baz ").join());
} }
@Test @Test
@ -470,20 +468,20 @@ public class CommandManagerTests {
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo").get().isEmpty()); assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo").get().isEmpty());
assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo bar").get().isEmpty()); assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo bar").get().isEmpty());
Command oldCommand = new SimpleCommand() { SimpleCommand oldCommand = new SimpleCommand() {
@Override @Override
public void execute(Invocation invocation) { public void execute(Invocation invocation) {
fail("The Command should not be executed while testing suggestions"); fail("The Command should not be executed while testing suggestions");
} }
@Override @Override
public List<String> suggest(Invocation invocation) { public boolean hasPermission(Invocation invocation) {
return ImmutableList.of("suggestion"); return invocation.arguments().length > 0;
} }
@Override @Override
public boolean hasPermission(Invocation invocation) { public List<String> suggest(Invocation invocation) {
return invocation.arguments().length > 0; return ImmutableList.of("suggestion");
} }
}; };
@ -506,4 +504,4 @@ public class CommandManagerTests {
} }
} }
} }