Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-12-23 23:00:35 +01:00
add async methods, also add separate methods which will call event and which will not call event
Dieser Commit ist enthalten in:
Ursprung
fb64333813
Commit
c0b8e9d646
@ -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<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);
|
||||
}
|
||||
|
@ -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<String, RawCommand> 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<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) {
|
||||
return commands.containsKey(command);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -185,6 +185,10 @@ public class VelocityEventManager implements EventManager {
|
||||
fireEvent(new ProxyShutdownEvent());
|
||||
}
|
||||
|
||||
public ExecutorService getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
private static class VelocityMethodScanner implements MethodScanner<Object> {
|
||||
|
||||
@Override
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren