Remove deprecated Velocity 1.0.0 Command API.

This commit is contained in:
Andrew Steinborn
2020-10-22 00:13:20 -04:00
parent cceeeb3490
commit 2f0ee15051
6 changed files with 25 additions and 285 deletions

View File

@@ -49,33 +49,6 @@ public interface CommandNodeFactory<T extends Command> {
}
};
CommandNodeFactory<Command> FALLBACK = (alias, command) ->
BrigadierUtils.buildRawArgumentsLiteral(alias,
context -> {
CommandSource source = context.getSource();
String[] args = BrigadierUtils.getSplitArguments(context);
if (!command.hasPermission(source, args)) {
return BrigadierCommand.FORWARD;
}
command.execute(source, args);
return 1;
},
(context, builder) -> {
String[] args = BrigadierUtils.getSplitArguments(context);
if (!command.hasPermission(context.getSource(), args)) {
return builder.buildFuture();
}
return command.suggestAsync(context.getSource(), args).thenApply(values -> {
for (String value : values) {
builder.suggest(value);
}
return builder.build();
});
});
/**
* Returns a Brigadier node for the execution of the given command.
*

View File

@@ -36,14 +36,12 @@ import com.velocitypowered.api.event.command.CommandExecuteEvent;
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
import com.velocitypowered.proxy.plugin.VelocityEventManager;
import com.velocitypowered.proxy.util.BrigadierUtils;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
public class VelocityCommandManager implements CommandManager {
@@ -68,20 +66,6 @@ public class VelocityCommandManager implements CommandManager {
return new VelocityCommandMeta.Builder(command.getNode().getName());
}
@Override
public void register(final Command command, final String... aliases) {
Preconditions.checkArgument(aliases.length > 0, "no aliases provided");
register(aliases[0], command, Arrays.copyOfRange(aliases, 1, aliases.length));
}
@Override
public void register(final String alias, final Command command, final String... otherAliases) {
Preconditions.checkNotNull(alias, "alias");
Preconditions.checkNotNull(command, "command");
Preconditions.checkNotNull(otherAliases, "otherAliases");
register(metaBuilder(alias).aliases(otherAliases).build(), command);
}
@Override
public void register(final BrigadierCommand command) {
Preconditions.checkNotNull(command, "command");
@@ -102,20 +86,10 @@ public class VelocityCommandManager implements CommandManager {
} else if (command instanceof SimpleCommand) {
node = CommandNodeFactory.SIMPLE.create(primaryAlias, (SimpleCommand) command);
} else if (command instanceof RawCommand) {
// This ugly hack will be removed in Velocity 2.0. Most if not all plugins
// have side-effect free #suggest methods. We rely on the newer RawCommand
// throwing UOE.
RawCommand asRaw = (RawCommand) command;
try {
asRaw.suggest(null, new String[0]);
} catch (final UnsupportedOperationException e) {
node = CommandNodeFactory.RAW.create(primaryAlias, asRaw);
} catch (final Exception ignored) {
// The implementation probably relies on a non-null source
}
}
if (node == null) {
node = CommandNodeFactory.FALLBACK.create(primaryAlias, command);
node = CommandNodeFactory.RAW.create(primaryAlias, (RawCommand) command);
} else {
throw new IllegalArgumentException("Unknown command implementation for "
+ command.getClass().getName());
}
if (!(command instanceof BrigadierCommand)) {

View File

@@ -125,20 +125,21 @@ public class CommandManagerTests {
VelocityCommandManager manager = createManager();
RawCommand command = new NoopRawCommand();
assertThrows(IllegalArgumentException.class, () -> manager.register(command),
"no aliases throws");
manager.register(command, "foO", "BAR");
manager.register("foO", command, "BAR");
assertTrue(manager.hasCommand("fOo"));
assertTrue(manager.hasCommand("bar"));
}
@Test
void testDeprecatedRegister() {
void testAlreadyRegisteredThrows() {
VelocityCommandManager manager = createManager();
Command command = new NoopDeprecatedCommand();
manager.register("foo", command);
assertTrue(manager.hasCommand("foO"));
manager.register("BAR", new NoopSimpleCommand());
assertThrows(IllegalArgumentException.class, () -> {
CommandMeta meta = manager.metaBuilder("baz")
.aliases("BAr")
.build();
manager.register(meta, new NoopRawCommand());
});
}
@Test
@@ -269,35 +270,6 @@ public class CommandManagerTests {
assertFalse(manager.executeImmediately(MockCommandSource.INSTANCE, "sendThem foo"));
}
@Test
void testDeprecatedExecute() {
VelocityCommandManager manager = createManager();
AtomicBoolean executed = new AtomicBoolean(false);
Command command = new Command() {
@Override
public void execute(final CommandSource source, final String @NonNull [] args) {
assertEquals(MockCommandSource.INSTANCE, source);
assertArrayEquals(new String[] { "boo", "123" }, args);
executed.set(true);
}
};
manager.register("foo", command);
assertTrue(manager.execute(MockCommandSource.INSTANCE, "foo boo 123"));
assertTrue(executed.get());
Command noPermsCommand = new Command() {
@Override
public boolean hasPermission(final CommandSource source, final String @NonNull [] args) {
return false;
}
};
manager.register("oof", noPermsCommand, "veryOof");
assertFalse(manager.execute(MockCommandSource.INSTANCE, "veryOOF"));
assertFalse(manager.executeImmediately(MockCommandSource.INSTANCE, "ooF boo 54321"));
}
@Test
void testSuggestions() {
VelocityCommandManager manager = createManager();
@@ -358,24 +330,8 @@ public class CommandManagerTests {
};
manager.register("raw", rawCommand);
Command deprecatedCommand = new Command() {
@Override
public List<String> suggest(
final CommandSource source, final String @NonNull [] currentArgs) {
switch (currentArgs.length) {
case 0:
return ImmutableList.of("boo", "scary");
case 1:
return ImmutableList.of("123", "456");
default:
return ImmutableList.of();
}
}
};
manager.register("deprecated", deprecatedCommand);
assertEquals(
ImmutableList.of("brigadier", "deprecated", "raw", "simple"),
ImmutableList.of("brigadier", "raw", "simple"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "").join(),
"literals are in alphabetical order");
assertEquals(
@@ -409,16 +365,6 @@ public class CommandManagerTests {
assertEquals(
ImmutableList.of("11", "13", "17"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "rAW bar ").join());
assertEquals(
ImmutableList.of("boo", "scary"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "deprecated ").join());
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "deprecated")
.join().isEmpty());
assertEquals(
ImmutableList.of("123", "456"),
manager.offerSuggestions(MockCommandSource.INSTANCE, "deprEcated foo").join());
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "deprecated foo 789 ")
.join().isEmpty());
}
@Test
@@ -528,29 +474,29 @@ public class CommandManagerTests {
}
};
manager.register(rawCommand, "foo");
manager.register("foo", rawCommand);
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo").get().isEmpty());
assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo bar").get().isEmpty());
Command oldCommand = new Command() {
Command oldCommand = new SimpleCommand() {
@Override
public void execute(CommandSource source, String @NonNull [] args) {
public void execute(Invocation invocation) {
fail("The Command should not be executed while testing suggestions");
}
@Override
public boolean hasPermission(CommandSource source, String @NonNull [] args) {
return args.length > 0;
public List<String> suggest(Invocation invocation) {
return ImmutableList.of("suggestion");
}
@Override
public List<String> suggest(CommandSource source, String @NonNull [] currentArgs) {
return ImmutableList.of("suggestion");
public boolean hasPermission(Invocation invocation) {
return invocation.arguments().length > 0;
}
};
manager.register(oldCommand, "bar");
manager.register("bar", oldCommand);
assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "bar").get().isEmpty());
assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "bar foo").get().isEmpty());
@@ -569,11 +515,4 @@ public class CommandManagerTests {
}
}
static class NoopDeprecatedCommand implements Command {
@Override
public void execute(final CommandSource source, final String @NonNull [] args) {
}
}
}