From 6555e0e337add5c2fc66ad8367a95a1c0c48ecfc Mon Sep 17 00:00:00 2001 From: Leymooo Date: Mon, 27 Apr 2020 00:04:12 +0300 Subject: [PATCH 1/6] Add CommandExecuteEvent --- .../api/command/CommandManager.java | 11 ++ .../event/command/CommandExecuteEvent.java | 143 ++++++++++++++++++ .../api/event/player/PlayerChatEvent.java | 10 +- .../velocitypowered/proxy/VelocityServer.java | 3 +- .../proxy/command/VelocityCommandManager.java | 38 +++++ .../client/ClientPlaySessionHandler.java | 38 +++-- .../proxy/console/VelocityConsole.java | 2 +- 7 files changed, 226 insertions(+), 19 deletions(-) create mode 100644 api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java 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 81a7b8297..ce3452e40 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandManager.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandManager.java @@ -35,10 +35,21 @@ public interface CommandManager { /** * Attempts to execute a command from the specified {@code cmdLine}. + * CommandExecuteEvent will not called * * @param source the command's source * @param cmdLine the command to run * @return true if the command was found and executed, false if it was not */ boolean execute(CommandSource source, String cmdLine); + + /** + * Attempts to execute a command from the specified {@code cmdLine}. + * + * @param source the command's source + * @param cmdLine the command to run + * @param callEvent will CommandExecuteEvent called or not + * @return true if the command was found and executed, false if it was not + */ + boolean execute(CommandSource source, String cmdLine, boolean callEvent); } diff --git a/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java b/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java new file mode 100644 index 000000000..f92ed6892 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java @@ -0,0 +1,143 @@ +package com.velocitypowered.api.event.command; + +import com.google.common.base.Preconditions; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.event.ResultedEvent; +import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult; +import java.util.Optional; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * This event is fired when a player types in a chat message. + */ +public final class CommandExecuteEvent implements ResultedEvent { + + private final CommandSource commandSource; + private final String command; + private CommandResult result; + + /** + * Constructs a PlayerChatEvent. + * @param commandSource the source executing the command + * @param command the command being executed without first slash + */ + public CommandExecuteEvent(CommandSource commandSource, String command) { + this.commandSource = Preconditions.checkNotNull(commandSource, "commandSource"); + this.command = Preconditions.checkNotNull(command, "command"); + this.result = CommandResult.allowed(); + } + + public CommandSource getCommandSource() { + return commandSource; + } + + /** + * Gets the original command being executed without first slash. + * @return the original command being executed + */ + public String getCommand() { + return command; + } + + @Override + public CommandResult getResult() { + return result; + } + + @Override + public void setResult(CommandResult result) { + this.result = Preconditions.checkNotNull(result, "result"); + } + + @Override + public String toString() { + return "PlayerCommmandEvent{" + + "commandSource=" + commandSource + + ", command=" + command + + ", result=" + result + + '}'; + } + + /** + * Represents the result of the {@link CommandExecuteEvent}. + */ + public static final class CommandResult implements Result { + + private static final CommandResult ALLOWED = new CommandResult(true, false,null); + private static final CommandResult DENIED = new CommandResult(false, false,null); + private static final CommandResult FORWARD_TO_SERVER = new CommandResult(false, true, null); + + private @Nullable String command; + private final boolean status; + private final boolean forward; + + private CommandResult(boolean status, boolean forward, @Nullable String command) { + this.status = status; + this.forward = forward; + this.command = command; + } + + public Optional getCommand() { + return Optional.ofNullable(command); + } + + public boolean isForwardToServer() { + return forward; + } + + @Override + public boolean isAllowed() { + return status; + } + + @Override + public String toString() { + return status ? "allowed" : "denied"; + } + + /** + * Allows the command to be sent, without modification. + * @return the allowed result + */ + public static CommandResult allowed() { + return ALLOWED; + } + + /** + * Prevents the command from being executed. + * @return the denied result + */ + public static CommandResult denied() { + return DENIED; + } + + /** + * Prevents the command from being executed, but forward command to server. + * @return the forward result + */ + public static CommandResult forwardToServer() { + return FORWARD_TO_SERVER; + } + + /** + * Prevents the command from being executed on proxy, but forward command to server. + * @param newCommand the command without first slash to use instead + * @return a result with a new command being forwarded to server + */ + public static CommandResult forwardToServer(@NonNull String newCommand) { + Preconditions.checkNotNull(newCommand, "newCommand"); + return new CommandResult(false, true, newCommand); + } + + /** + * Allows the command to be executed, but silently replaced old command with another. + * @param newCommand the command to use instead without first slash + * @return a result with a new command + */ + public static CommandResult command(@NonNull String newCommand) { + Preconditions.checkNotNull(newCommand, "newCommand"); + return new CommandResult(true, false, newCommand); + } + } +} diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java index 6b8b7bba1..ef77c96a4 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java @@ -70,6 +70,10 @@ public final class PlayerChatEvent implements ResultedEvent getMessage() { + return Optional.ofNullable(message); + } + @Override public boolean isAllowed() { return status; @@ -96,10 +100,6 @@ public final class PlayerChatEvent implements ResultedEvent getMessage() { - return Optional.ofNullable(message); - } - /** * Allows the message to be sent, but silently replaced with another. * @param message the message to use instead @@ -110,6 +110,4 @@ public final class PlayerChatEvent implements ResultedEvent commands = new HashMap<>(); + private final EventManager eventManager; + + public VelocityCommandManager(EventManager eventManager) { + this.eventManager = eventManager; + } @Override @Deprecated @@ -47,11 +57,39 @@ public class VelocityCommandManager implements CommandManager { this.commands.remove(alias.toLowerCase(Locale.ENGLISH)); } + /** + * Calls CommandExecuteEvent. + * @param source the command's source + * @param cmd the command + * @return CompletableFuture of event + */ + public CompletableFuture callCommandEvent(CommandSource source, String cmd) { + Preconditions.checkNotNull(source, "invoker"); + Preconditions.checkNotNull(cmd, "cmd"); + return eventManager.fire(new CommandExecuteEvent(source, cmd)); + } + @Override public boolean execute(CommandSource source, String cmdLine) { Preconditions.checkNotNull(source, "invoker"); Preconditions.checkNotNull(cmdLine, "cmdLine"); + return execute(source, cmdLine, false); + } + + @Override + public boolean execute(CommandSource source, String cmdLine, boolean callEvent) { + Preconditions.checkNotNull(source, "invoker"); + Preconditions.checkNotNull(cmdLine, "cmdLine"); + + if (callEvent) { + CommandExecuteEvent event = callCommandEvent(source, cmdLine).join(); + CommandResult commandResult = event.getResult(); + if (commandResult.isForwardToServer() || !commandResult.isAllowed()) { + return false; + } + cmdLine = commandResult.getCommand().orElse(event.getCommand()); + } String alias = cmdLine; String args = ""; int firstSpace = cmdLine.indexOf(' '); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 35d149c24..f1bbaf1c8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -4,6 +4,7 @@ import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_13; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8; import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.constructChannelsPacket; +import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent; @@ -123,17 +124,32 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { String msg = packet.getMessage(); if (msg.startsWith("/")) { - try { - if (!server.getCommandManager().execute(player, msg.substring(1))) { - return false; - } - } catch (Exception e) { - logger.info("Exception occurred while running command for {}", player.getUsername(), - e); - player.sendMessage( - TextComponent.of("An error occurred while running this command.", TextColor.RED)); - return true; - } + + server.getCommandManager().callCommandEvent(player, msg.substring(1)) + .thenAcceptAsync(event -> { + CommandExecuteEvent.CommandResult commandResult = event.getResult(); + if (commandResult.isAllowed()) { + Optional eventCommand = event.getResult().getCommand(); + String command = eventCommand.orElse(event.getCommand()); + + if (commandResult.isForwardToServer()) { + smc.write(Chat.createServerbound(command)); + return; + } + + try { + if (!server.getCommandManager().execute(player, command)) { + smc.write(Chat.createServerbound(command)); + } + } catch (Exception e) { + logger.info("Exception occurred while running command for {}", player.getUsername(), + e); + player.sendMessage( + TextComponent.of("An error occurred while running this command.", + TextColor.RED)); + } + } + }); } else { PlayerChatEvent event = new PlayerChatEvent(player, msg); server.getEventManager().fire(event) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java index 4b9d38aee..0123db3e1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -91,7 +91,7 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons @Override protected void runCommand(String command) { try { - if (!this.server.getCommandManager().execute(this, command)) { + if (!this.server.getCommandManager().execute(this, command, true)) { sendMessage(TextComponent.of("Command not found.", TextColor.RED)); } } catch (Exception e) { From 1fb382e0f6b4fcb7c2100cabe0a284ce01862e89 Mon Sep 17 00:00:00 2001 From: Leymooo Date: Mon, 27 Apr 2020 00:09:25 +0300 Subject: [PATCH 2/6] fix logic --- .../client/ClientPlaySessionHandler.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index f1bbaf1c8..9c22e9c2c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -128,15 +128,13 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { server.getCommandManager().callCommandEvent(player, msg.substring(1)) .thenAcceptAsync(event -> { CommandExecuteEvent.CommandResult commandResult = event.getResult(); + Optional eventCommand = event.getResult().getCommand(); + String command = eventCommand.orElse(event.getCommand()); + if (commandResult.isForwardToServer()) { + smc.write(Chat.createServerbound(command)); + return; + } if (commandResult.isAllowed()) { - Optional eventCommand = event.getResult().getCommand(); - String command = eventCommand.orElse(event.getCommand()); - - if (commandResult.isForwardToServer()) { - smc.write(Chat.createServerbound(command)); - return; - } - try { if (!server.getCommandManager().execute(player, command)) { smc.write(Chat.createServerbound(command)); From fb64333813699b4f1ade5c9a0646f8d4f7460713 Mon Sep 17 00:00:00 2001 From: Leymooo Date: Mon, 27 Apr 2020 00:11:49 +0300 Subject: [PATCH 3/6] pass eventLoop to thenApplyAsync --- .../proxy/connection/client/ClientPlaySessionHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 9c22e9c2c..4864bcf22 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -147,7 +147,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { TextColor.RED)); } } - }); + }, smc.eventLoop()); } else { PlayerChatEvent event = new PlayerChatEvent(player, msg); server.getEventManager().fire(event) From c0b8e9d646cf94877c89f0b6d631ebb484f58ca2 Mon Sep 17 00:00:00 2001 From: Leymooo Date: Mon, 27 Apr 2020 01:05:57 +0300 Subject: [PATCH 4/6] add async methods, also add separate methods which will call event and which will not call event --- .../api/command/CommandManager.java | 32 +++++++++-- .../proxy/command/VelocityCommandManager.java | 57 ++++++++++++++----- .../client/ClientPlaySessionHandler.java | 2 +- .../proxy/console/VelocityConsole.java | 4 +- .../proxy/plugin/VelocityEventManager.java | 4 ++ 5 files changed, 79 insertions(+), 20 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 ce3452e40..af200cb74 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandManager.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandManager.java @@ -1,5 +1,7 @@ package com.velocitypowered.api.command; +import java.util.concurrent.CompletableFuture; + /** * Represents an interface to register a command executor with the proxy. */ @@ -34,8 +36,8 @@ public interface CommandManager { void unregister(String alias); /** - * Attempts to execute a command from the specified {@code cmdLine}. - * CommandExecuteEvent will not called + * Calls CommandExecuteEvent and attempts to execute a command from the specified {@code cmdLine} + * sync. * * @param source the command's source * @param cmdLine the command to run @@ -44,12 +46,32 @@ public interface CommandManager { boolean execute(CommandSource source, String cmdLine); /** - * Attempts to execute a command from the specified {@code cmdLine}. + * Attempts to execute a command from the specified {@code cmdLine} sync + * without calling CommandExecuteEvent. * * @param source the command's source * @param cmdLine the command to run - * @param callEvent will CommandExecuteEvent called or not * @return true if the command was found and executed, false if it was not */ - boolean execute(CommandSource source, String cmdLine, boolean callEvent); + boolean executeImmediately(CommandSource source, String cmdLine); + + /** + * Calls CommandExecuteEvent and attempts to execute a command from the specified {@code cmdLine} + * async. + * + * @param source the command's source + * @param cmdLine the command to run + * @return A future that will be completed with the result of the command execution + */ + CompletableFuture executeAsync(CommandSource source, String cmdLine); + + /** + * Attempts to execute a command from the specified {@code cmdLine} async + * without calling CommandExecuteEvent. + * + * @param source the command's source + * @param cmdLine the command to run + * @return A future that will be completed with the result of the command execution + */ + CompletableFuture executeImmediatelyAsync(CommandSource source, String cmdLine); } 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 93caa314b..3a8a53201 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java @@ -7,9 +7,9 @@ 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 com.velocitypowered.api.event.EventManager; import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult; +import com.velocitypowered.proxy.plugin.VelocityEventManager; import java.util.Arrays; import java.util.HashMap; @@ -22,9 +22,9 @@ import java.util.concurrent.CompletableFuture; public class VelocityCommandManager implements CommandManager { private final Map commands = new HashMap<>(); - private final EventManager eventManager; + private final VelocityEventManager eventManager; - public VelocityCommandManager(EventManager eventManager) { + public VelocityCommandManager(VelocityEventManager eventManager) { this.eventManager = eventManager; } @@ -74,22 +74,21 @@ public class VelocityCommandManager implements CommandManager { Preconditions.checkNotNull(source, "invoker"); Preconditions.checkNotNull(cmdLine, "cmdLine"); - return execute(source, cmdLine, false); + CommandExecuteEvent event = callCommandEvent(source, cmdLine).join(); + CommandResult commandResult = event.getResult(); + if (commandResult.isForwardToServer() || !commandResult.isAllowed()) { + return false; + } + cmdLine = commandResult.getCommand().orElse(event.getCommand()); + + return executeImmediately(source, cmdLine); } @Override - public boolean execute(CommandSource source, String cmdLine, boolean callEvent) { + public boolean executeImmediately(CommandSource source, String cmdLine) { Preconditions.checkNotNull(source, "invoker"); Preconditions.checkNotNull(cmdLine, "cmdLine"); - if (callEvent) { - CommandExecuteEvent event = callCommandEvent(source, cmdLine).join(); - CommandResult commandResult = event.getResult(); - if (commandResult.isForwardToServer() || !commandResult.isAllowed()) { - return false; - } - cmdLine = commandResult.getCommand().orElse(event.getCommand()); - } String alias = cmdLine; String args = ""; int firstSpace = cmdLine.indexOf(' '); @@ -113,6 +112,38 @@ public class VelocityCommandManager implements CommandManager { } } + + @Override + public CompletableFuture executeAsync(CommandSource source, String cmdLine) { + CompletableFuture result = new CompletableFuture<>(); + callCommandEvent(source, cmdLine).thenAccept(event -> { + CommandResult commandResult = event.getResult(); + if (commandResult.isForwardToServer() || !commandResult.isAllowed()) { + result.complete(false); + } + String command = commandResult.getCommand().orElse(event.getCommand()); + try { + result.complete(executeImmediately(source, command)); + } catch (Exception e) { + result.completeExceptionally(e); + } + }); + return result; + } + + @Override + public CompletableFuture executeImmediatelyAsync(CommandSource source, String cmdLine) { + CompletableFuture result = new CompletableFuture<>(); + eventManager.getService().execute(() -> { + try { + result.complete(executeImmediately(source, cmdLine)); + } catch (Exception e) { + result.completeExceptionally(e); + } + }); + return result; + } + public boolean hasCommand(String command) { return commands.containsKey(command); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 4864bcf22..e8b747bac 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -136,7 +136,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } if (commandResult.isAllowed()) { try { - if (!server.getCommandManager().execute(player, command)) { + if (!server.getCommandManager().executeImmediately(player, command)) { smc.write(Chat.createServerbound(command)); } } catch (Exception e) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java index 0123db3e1..1ccc0c101 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -8,6 +8,8 @@ import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.proxy.VelocityServer; import java.util.List; +import java.util.concurrent.CompletableFuture; + import net.kyori.text.Component; import net.kyori.text.TextComponent; import net.kyori.text.format.TextColor; @@ -91,7 +93,7 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons @Override protected void runCommand(String command) { try { - if (!this.server.getCommandManager().execute(this, command, true)) { + if (!this.server.getCommandManager().execute(this, command)) { sendMessage(TextComponent.of("Command not found.", TextColor.RED)); } } catch (Exception e) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java index 2ef08dade..87b0097b6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java @@ -185,6 +185,10 @@ public class VelocityEventManager implements EventManager { fireEvent(new ProxyShutdownEvent()); } + public ExecutorService getService() { + return service; + } + private static class VelocityMethodScanner implements MethodScanner { @Override From 26bf94f08f3c4c25304ad023392f348c1bf85170 Mon Sep 17 00:00:00 2001 From: Leymooo Date: Mon, 27 Apr 2020 13:09:04 +0300 Subject: [PATCH 5/6] fix typos, update javadocs --- .../api/command/CommandManager.java | 21 +++++++++++++------ .../event/command/CommandExecuteEvent.java | 4 ++-- .../proxy/command/VelocityCommandManager.java | 8 ++++--- 3 files changed, 22 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 af200cb74..f29777d6f 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandManager.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandManager.java @@ -36,22 +36,29 @@ public interface CommandManager { void unregister(String alias); /** - * Calls CommandExecuteEvent and attempts to execute a command from the specified {@code cmdLine} - * sync. + * Calls CommandExecuteEvent and attempts to execute a command using the specified {@code cmdLine} + * in a blocking fashion. * * @param source the command's source * @param cmdLine the command to run * @return true if the command was found and executed, false if it was not + * + * @deprecated This method will block current thread during event call and command execution. + * Prefer {@link #executeAsync(CommandSource, String)} instead. */ + @Deprecated boolean execute(CommandSource source, String cmdLine); /** - * Attempts to execute a command from the specified {@code cmdLine} sync - * without calling CommandExecuteEvent. + * Attempts to execute a command using the specified {@code cmdLine} in a blocking fashion without + * calling CommandExecuteEvent. * * @param source the command's source * @param cmdLine the command to run * @return true if the command was found and executed, false if it was not + * + * @deprecated This method will block current thread during event and command execution. + * Prefer {@link #executeImmediatelyAsync(CommandSource, String)} instead. */ boolean executeImmediately(CommandSource source, String cmdLine); @@ -61,7 +68,8 @@ public interface CommandManager { * * @param source the command's source * @param cmdLine the command to run - * @return A future that will be completed with the result of the command execution + * @return A future that will be completed with the result of the command execution. + * Can be completed exceptionally if exception was thrown during execution. */ CompletableFuture executeAsync(CommandSource source, String cmdLine); @@ -71,7 +79,8 @@ public interface CommandManager { * * @param source the command's source * @param cmdLine the command to run - * @return A future that will be completed with the result of the command execution + * @return A future that will be completed with the result of the command execution. + * Can be completed exceptionally if exception was thrown during execution. */ CompletableFuture executeImmediatelyAsync(CommandSource source, String cmdLine); } diff --git a/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java b/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java index f92ed6892..054699568 100644 --- a/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java @@ -18,7 +18,7 @@ public final class CommandExecuteEvent implements ResultedEvent { private CommandResult result; /** - * Constructs a PlayerChatEvent. + * Constructs a CommandExecuteEvent. * @param commandSource the source executing the command * @param command the command being executed without first slash */ @@ -52,7 +52,7 @@ public final class CommandExecuteEvent implements ResultedEvent { @Override public String toString() { - return "PlayerCommmandEvent{" + return "CommandExecuteEvent{" + "commandSource=" + commandSource + ", command=" + command + ", result=" + result 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 3a8a53201..2b3975f1a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java @@ -64,14 +64,14 @@ public class VelocityCommandManager implements CommandManager { * @return CompletableFuture of event */ public CompletableFuture callCommandEvent(CommandSource source, String cmd) { - Preconditions.checkNotNull(source, "invoker"); + Preconditions.checkNotNull(source, "source"); Preconditions.checkNotNull(cmd, "cmd"); return eventManager.fire(new CommandExecuteEvent(source, cmd)); } @Override public boolean execute(CommandSource source, String cmdLine) { - Preconditions.checkNotNull(source, "invoker"); + Preconditions.checkNotNull(source, "source"); Preconditions.checkNotNull(cmdLine, "cmdLine"); CommandExecuteEvent event = callCommandEvent(source, cmdLine).join(); @@ -86,7 +86,7 @@ public class VelocityCommandManager implements CommandManager { @Override public boolean executeImmediately(CommandSource source, String cmdLine) { - Preconditions.checkNotNull(source, "invoker"); + Preconditions.checkNotNull(source, "source"); Preconditions.checkNotNull(cmdLine, "cmdLine"); String alias = cmdLine; @@ -133,6 +133,8 @@ public class VelocityCommandManager implements CommandManager { @Override public CompletableFuture executeImmediatelyAsync(CommandSource source, String cmdLine) { + Preconditions.checkNotNull(source, "source"); + Preconditions.checkNotNull(cmdLine, "cmdLine"); CompletableFuture result = new CompletableFuture<>(); eventManager.getService().execute(() -> { try { From 9d6689f64aefafdcbc0c96c30ec3cc93d1a77283 Mon Sep 17 00:00:00 2001 From: Leymooo Date: Mon, 27 Apr 2020 13:12:03 +0300 Subject: [PATCH 6/6] forgot annotation --- .../java/com/velocitypowered/api/command/CommandManager.java | 1 + 1 file changed, 1 insertion(+) 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 f29777d6f..402167151 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandManager.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandManager.java @@ -60,6 +60,7 @@ public interface CommandManager { * @deprecated This method will block current thread during event and command execution. * Prefer {@link #executeImmediatelyAsync(CommandSource, String)} instead. */ + @Deprecated boolean executeImmediately(CommandSource source, String cmdLine); /**