13
0
geforkt von Mirrors/Velocity

add async methods, also add separate methods which will call event and which will not call event

Dieser Commit ist enthalten in:
Leymooo 2020-04-27 01:05:57 +03:00
Ursprung fb64333813
Commit c0b8e9d646
5 geänderte Dateien mit 79 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -1,5 +1,7 @@
package com.velocitypowered.api.command; package com.velocitypowered.api.command;
import java.util.concurrent.CompletableFuture;
/** /**
* Represents an interface to register a command executor with the proxy. * Represents an interface to register a command executor with the proxy.
*/ */
@ -34,8 +36,8 @@ public interface CommandManager {
void unregister(String alias); void unregister(String alias);
/** /**
* Attempts to execute a command from the specified {@code cmdLine}. * Calls CommandExecuteEvent and attempts to execute a command from the specified {@code cmdLine}
* CommandExecuteEvent will not called * sync.
* *
* @param source the command's source * @param source the command's source
* @param cmdLine the command to run * @param cmdLine the command to run
@ -44,12 +46,32 @@ public interface CommandManager {
boolean execute(CommandSource source, String cmdLine); 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 source the command's source
* @param cmdLine the command to run * @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 * @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<Boolean> 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<Boolean> executeImmediatelyAsync(CommandSource source, String cmdLine);
} }

Datei anzeigen

@ -7,9 +7,9 @@ import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandManager; import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand; 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;
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult; import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
import com.velocitypowered.proxy.plugin.VelocityEventManager;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -22,9 +22,9 @@ import java.util.concurrent.CompletableFuture;
public class VelocityCommandManager implements CommandManager { public class VelocityCommandManager implements CommandManager {
private final Map<String, RawCommand> commands = new HashMap<>(); private final Map<String, RawCommand> commands = new HashMap<>();
private final EventManager eventManager; private final VelocityEventManager eventManager;
public VelocityCommandManager(EventManager eventManager) { public VelocityCommandManager(VelocityEventManager eventManager) {
this.eventManager = eventManager; this.eventManager = eventManager;
} }
@ -74,22 +74,21 @@ public class VelocityCommandManager implements CommandManager {
Preconditions.checkNotNull(source, "invoker"); Preconditions.checkNotNull(source, "invoker");
Preconditions.checkNotNull(cmdLine, "cmdLine"); 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(); CommandExecuteEvent event = callCommandEvent(source, cmdLine).join();
CommandResult commandResult = event.getResult(); CommandResult commandResult = event.getResult();
if (commandResult.isForwardToServer() || !commandResult.isAllowed()) { if (commandResult.isForwardToServer() || !commandResult.isAllowed()) {
return false; return false;
} }
cmdLine = commandResult.getCommand().orElse(event.getCommand()); cmdLine = commandResult.getCommand().orElse(event.getCommand());
return executeImmediately(source, cmdLine);
} }
@Override
public boolean executeImmediately(CommandSource source, String cmdLine) {
Preconditions.checkNotNull(source, "invoker");
Preconditions.checkNotNull(cmdLine, "cmdLine");
String alias = cmdLine; String alias = cmdLine;
String args = ""; String args = "";
int firstSpace = cmdLine.indexOf(' '); int firstSpace = cmdLine.indexOf(' ');
@ -113,6 +112,38 @@ public class VelocityCommandManager implements CommandManager {
} }
} }
@Override
public CompletableFuture<Boolean> executeAsync(CommandSource source, String cmdLine) {
CompletableFuture<Boolean> 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<Boolean> executeImmediatelyAsync(CommandSource source, String cmdLine) {
CompletableFuture<Boolean> 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) { public boolean hasCommand(String command) {
return commands.containsKey(command); return commands.containsKey(command);
} }

Datei anzeigen

@ -136,7 +136,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
} }
if (commandResult.isAllowed()) { if (commandResult.isAllowed()) {
try { try {
if (!server.getCommandManager().execute(player, command)) { if (!server.getCommandManager().executeImmediately(player, command)) {
smc.write(Chat.createServerbound(command)); smc.write(Chat.createServerbound(command));
} }
} catch (Exception e) { } catch (Exception e) {

Datei anzeigen

@ -8,6 +8,8 @@ import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.api.proxy.ConsoleCommandSource;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
import net.kyori.text.format.TextColor; import net.kyori.text.format.TextColor;
@ -91,7 +93,7 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons
@Override @Override
protected void runCommand(String command) { protected void runCommand(String command) {
try { try {
if (!this.server.getCommandManager().execute(this, command, true)) { if (!this.server.getCommandManager().execute(this, command)) {
sendMessage(TextComponent.of("Command not found.", TextColor.RED)); sendMessage(TextComponent.of("Command not found.", TextColor.RED));
} }
} catch (Exception e) { } catch (Exception e) {

Datei anzeigen

@ -185,6 +185,10 @@ public class VelocityEventManager implements EventManager {
fireEvent(new ProxyShutdownEvent()); fireEvent(new ProxyShutdownEvent());
} }
public ExecutorService getService() {
return service;
}
private static class VelocityMethodScanner implements MethodScanner<Object> { private static class VelocityMethodScanner implements MethodScanner<Object> {
@Override @Override