Add CommandExecuteEvent

This commit is contained in:
Leymooo
2020-04-27 00:04:12 +03:00
parent 957c0dd307
commit 6555e0e337
7 changed files with 226 additions and 19 deletions

View File

@@ -95,7 +95,7 @@ public class VelocityServer implements ProxyServer {
private @MonotonicNonNull VelocityConfiguration configuration;
private @MonotonicNonNull KeyPair serverKeyPair;
private final ServerMap servers;
private final VelocityCommandManager commandManager = new VelocityCommandManager();
private final VelocityCommandManager commandManager;
private final AtomicBoolean shutdownInProgress = new AtomicBoolean(false);
private boolean shutdown = false;
private final VelocityPluginManager pluginManager;
@@ -111,6 +111,7 @@ public class VelocityServer implements ProxyServer {
VelocityServer(final ProxyOptions options) {
pluginManager = new VelocityPluginManager(this);
eventManager = new VelocityEventManager(pluginManager);
commandManager = new VelocityCommandManager(eventManager);
scheduler = new VelocityScheduler(pluginManager);
console = new VelocityConsole(this);
cm = new ConnectionManager(this);

View File

@@ -7,16 +7,26 @@ 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 java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
public class VelocityCommandManager implements CommandManager {
private final Map<String, RawCommand> 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<CommandExecuteEvent> 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(' ');

View File

@@ -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<String> 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)

View File

@@ -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) {