Merge pull request #299 from Leymooo/command-event
Add event for commands
This commit is contained in:
@@ -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);
|
||||
|
@@ -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.command.CommandExecuteEvent;
|
||||
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
|
||||
import com.velocitypowered.proxy.plugin.VelocityEventManager;
|
||||
|
||||
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 VelocityEventManager eventManager;
|
||||
|
||||
public VelocityCommandManager(VelocityEventManager eventManager) {
|
||||
this.eventManager = eventManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
@@ -47,9 +57,36 @@ 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, "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();
|
||||
CommandResult commandResult = event.getResult();
|
||||
if (commandResult.isForwardToServer() || !commandResult.isAllowed()) {
|
||||
return false;
|
||||
}
|
||||
cmdLine = commandResult.getCommand().orElse(event.getCommand());
|
||||
|
||||
return executeImmediately(source, cmdLine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeImmediately(CommandSource source, String cmdLine) {
|
||||
Preconditions.checkNotNull(source, "source");
|
||||
Preconditions.checkNotNull(cmdLine, "cmdLine");
|
||||
|
||||
String alias = cmdLine;
|
||||
@@ -75,6 +112,40 @@ 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) {
|
||||
Preconditions.checkNotNull(source, "source");
|
||||
Preconditions.checkNotNull(cmdLine, "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);
|
||||
}
|
||||
|
@@ -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,30 @@ 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();
|
||||
Optional<String> eventCommand = event.getResult().getCommand();
|
||||
String command = eventCommand.orElse(event.getCommand());
|
||||
if (commandResult.isForwardToServer()) {
|
||||
smc.write(Chat.createServerbound(command));
|
||||
return;
|
||||
}
|
||||
if (commandResult.isAllowed()) {
|
||||
try {
|
||||
if (!server.getCommandManager().executeImmediately(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));
|
||||
}
|
||||
}
|
||||
}, smc.eventLoop());
|
||||
} else {
|
||||
PlayerChatEvent event = new PlayerChatEvent(player, msg);
|
||||
server.getEventManager().fire(event)
|
||||
|
@@ -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;
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user