3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Fire PluginMessageEvent correctly if it's sent when a connection is established

Fixes #774
Dieser Commit ist enthalten in:
Andrew Steinborn 2023-01-08 13:34:44 -05:00
Ursprung 022ae0fc00
Commit 586cd56c05
2 geänderte Dateien mit 40 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -193,7 +193,7 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
return; return;
} }
mcConnection.setSessionHandler(new InitialConnectSessionHandler(player)); mcConnection.setSessionHandler(new InitialConnectSessionHandler(player, server));
server.getEventManager().fire(new PostLoginEvent(player)) server.getEventManager().fire(new PostLoginEvent(player))
.thenCompose((ignored) -> connectToInitialServer(player)) .thenCompose((ignored) -> connectToInitialServer(player))
.exceptionally((ex) -> { .exceptionally((ex) -> {

Datei anzeigen

@ -17,11 +17,18 @@
package com.velocitypowered.proxy.connection.client; package com.velocitypowered.proxy.connection.client;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.connection.backend.BungeeCordMessageResponder; import com.velocitypowered.proxy.connection.backend.BungeeCordMessageResponder;
import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
import com.velocitypowered.proxy.protocol.packet.PluginMessage; import com.velocitypowered.proxy.protocol.packet.PluginMessage;
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/** /**
* Handles the play state between exiting the login phase and establishing the first connection * Handles the play state between exiting the login phase and establishing the first connection
@ -29,10 +36,15 @@ import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
*/ */
public class InitialConnectSessionHandler implements MinecraftSessionHandler { public class InitialConnectSessionHandler implements MinecraftSessionHandler {
private static final Logger logger = LogManager.getLogger(InitialConnectSessionHandler.class);
private final ConnectedPlayer player; private final ConnectedPlayer player;
InitialConnectSessionHandler(ConnectedPlayer player) { private final VelocityServer server;
InitialConnectSessionHandler(ConnectedPlayer player, VelocityServer server) {
this.player = player; this.player = player;
this.server = server;
} }
@Override @Override
@ -45,12 +57,37 @@ public class InitialConnectSessionHandler implements MinecraftSessionHandler {
if (PluginMessageUtil.isRegister(packet)) { if (PluginMessageUtil.isRegister(packet)) {
player.getKnownChannels().addAll(PluginMessageUtil.getChannels(packet)); player.getKnownChannels().addAll(PluginMessageUtil.getChannels(packet));
serverConn.ensureConnected().write(packet.retain());
return true;
} else if (PluginMessageUtil.isUnregister(packet)) { } else if (PluginMessageUtil.isUnregister(packet)) {
player.getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet)); player.getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet));
serverConn.ensureConnected().write(packet.retain());
return true;
} else if (BungeeCordMessageResponder.isBungeeCordMessage(packet)) { } else if (BungeeCordMessageResponder.isBungeeCordMessage(packet)) {
return true; return true;
} }
serverConn.ensureConnected().write(packet.retain());
ChannelIdentifier id = server.getChannelRegistrar().getFromId(packet.getChannel());
if (id == null) {
serverConn.ensureConnected().write(packet.retain());
return true;
}
byte[] copy = ByteBufUtil.getBytes(packet.content());
PluginMessageEvent event = new PluginMessageEvent(serverConn, serverConn.getPlayer(), id,
copy);
server.getEventManager().fire(event)
.thenAcceptAsync(pme -> {
if (pme.getResult().isAllowed() && serverConn.isActive()) {
PluginMessage copied = new PluginMessage(packet.getChannel(),
Unpooled.wrappedBuffer(copy));
serverConn.ensureConnected().write(copied);
}
}, player.getConnection().eventLoop())
.exceptionally((ex) -> {
logger.error("Exception while handling plugin message {}", packet, ex);
return null;
});
} }
return true; return true;
} }