From 81840abc86af98c89f61007e46d924364c911ee5 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 22 Jun 2019 21:13:20 -0400 Subject: [PATCH] Enforce at least one parameter in CommandManager#register() The old register method has been deprecated. --- .../api/command/CommandManager.java | 13 +++++++++++ .../velocitypowered/proxy/VelocityServer.java | 8 +++---- .../proxy/command/VelocityCommandManager.java | 22 +++++++++++++------ 3 files changed, 32 insertions(+), 11 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 69d1eb791..81a7b8297 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandManager.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandManager.java @@ -10,9 +10,22 @@ public interface CommandManager { * * @param command the command to register * @param aliases the alias to use + * + * @deprecated This method requires at least one alias, but this is only enforced at runtime. + * Prefer {@link #register(String, Command, String...)} instead. */ + @Deprecated void register(Command command, String... aliases); + /** + * Registers the specified command with the manager with the specified aliases. + * + * @param alias the first alias to register + * @param command the command to register + * @param otherAliases the other aliases to use + */ + void register(String alias, Command command, String... otherAliases); + /** * Unregisters a command. * diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index a8ee585b5..6e2765202 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -167,10 +167,10 @@ public class VelocityServer implements ProxyServer { cm.logChannelInformation(); // Initialize commands first - commandManager.register(new VelocityCommand(this), "velocity"); - commandManager.register(new ServerCommand(this), "server"); - commandManager.register(new ShutdownCommand(this), "shutdown", "end"); - commandManager.register(new GlistCommand(this), "glist"); + commandManager.register("velocity", new VelocityCommand(this)); + commandManager.register("server", new ServerCommand(this)); + commandManager.register("shutdown", new ShutdownCommand(this),"end"); + commandManager.register("glist", new GlistCommand(this)); try { Path configPath = Paths.get("velocity.toml"); 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 c60438c98..5e2718755 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java @@ -7,27 +7,35 @@ import com.velocitypowered.api.command.Command; import com.velocitypowered.api.command.CommandManager; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.RawCommand; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; -import org.checkerframework.checker.nullness.qual.NonNull; public class VelocityCommandManager implements CommandManager { private final Map commands = new HashMap<>(); @Override + @Deprecated public void register(final Command command, final String... aliases) { - Preconditions.checkNotNull(aliases, "aliases"); + Preconditions.checkArgument(aliases.length > 0, "no aliases provided"); + register(aliases[0], command, Arrays.copyOfRange(aliases, 1, aliases.length)); + } + + @Override + public void register(String alias, Command command, String... otherAliases) { + Preconditions.checkNotNull(alias, "alias"); + Preconditions.checkNotNull(otherAliases, "otherAliases"); Preconditions.checkNotNull(command, "executor"); - for (int i = 0, length = aliases.length; i < length; i++) { - final String alias = aliases[i]; - Preconditions.checkNotNull(alias, "alias at index %s", i); - this.commands.put(alias.toLowerCase(Locale.ENGLISH), command); + this.commands.put(alias.toLowerCase(Locale.ENGLISH), command); + + for (int i = 0, length = otherAliases.length; i < length; i++) { + final String alias1 = otherAliases[i]; + Preconditions.checkNotNull(alias1, "alias at index %s", i + 1); + this.commands.put(alias1.toLowerCase(Locale.ENGLISH), command); } }