Skip to content

Commit 9d1332d

Browse files
committed
Add PluginMessageEvents for configuration phase
1 parent 83c1749 commit 9d1332d

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

proxy/src/main/java/com/velocitypowered/proxy/connection/backend/ConfigSessionHandler.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
package com.velocitypowered.proxy.connection.backend;
1919

20+
import com.velocitypowered.api.event.connection.PluginMessageEvent;
2021
import com.velocitypowered.api.event.connection.PreTransferEvent;
2122
import com.velocitypowered.api.event.player.CookieRequestEvent;
2223
import com.velocitypowered.api.event.player.CookieStoreEvent;
2324
import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent;
2425
import com.velocitypowered.api.event.player.ServerResourcePackRemoveEvent;
2526
import com.velocitypowered.api.event.player.ServerResourcePackSendEvent;
2627
import com.velocitypowered.api.network.ProtocolVersion;
28+
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
2729
import com.velocitypowered.api.proxy.player.ResourcePackInfo;
2830
import com.velocitypowered.proxy.VelocityServer;
2931
import com.velocitypowered.proxy.connection.MinecraftConnection;
@@ -54,6 +56,8 @@
5456
import com.velocitypowered.proxy.protocol.packet.config.StartUpdatePacket;
5557
import com.velocitypowered.proxy.protocol.packet.config.TagsUpdatePacket;
5658
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
59+
import io.netty.buffer.ByteBufUtil;
60+
import io.netty.buffer.Unpooled;
5761
import java.io.IOException;
5862
import java.net.InetSocketAddress;
5963
import java.util.concurrent.CompletableFuture;
@@ -261,7 +265,29 @@ public boolean handle(PluginMessagePacket packet) {
261265
PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion(),
262266
serverConn.getPlayer().getProtocolVersion()));
263267
} else {
264-
serverConn.getPlayer().getConnection().write(packet.retain());
268+
byte[] bytes = ByteBufUtil.getBytes(packet.content());
269+
ChannelIdentifier id = this.server.getChannelRegistrar().getFromId(packet.getChannel());
270+
271+
if (id == null) {
272+
serverConn.getPlayer().getConnection().write(packet.retain());
273+
return true;
274+
}
275+
276+
// Handling this stuff async means that we should probably pause
277+
// the connection while we toss this off into another pool
278+
this.serverConn.getConnection().setAutoReading(false);
279+
this.server.getEventManager()
280+
.fire(new PluginMessageEvent(serverConn, serverConn.getPlayer(), id, bytes))
281+
.thenAcceptAsync(pme -> {
282+
if (pme.getResult().isAllowed() && !serverConn.getPlayer().getConnection().isClosed()) {
283+
serverConn.getPlayer().getConnection().write(new PluginMessagePacket(
284+
pme.getIdentifier().getId(), Unpooled.wrappedBuffer(bytes)));
285+
}
286+
this.serverConn.getConnection().setAutoReading(true);
287+
}, serverConn.ensureConnected().eventLoop()).exceptionally((ex) -> {
288+
logger.error("Exception while handling plugin message {}", packet, ex);
289+
return null;
290+
});
265291
}
266292
return true;
267293
}

proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717

1818
package com.velocitypowered.proxy.connection.client;
1919

20+
import com.velocitypowered.api.event.connection.PluginMessageEvent;
2021
import com.velocitypowered.api.event.player.CookieReceiveEvent;
2122
import com.velocitypowered.api.event.player.PlayerClientBrandEvent;
2223
import com.velocitypowered.api.event.player.configuration.PlayerConfigurationEvent;
2324
import com.velocitypowered.api.event.player.configuration.PlayerFinishConfigurationEvent;
2425
import com.velocitypowered.api.event.player.configuration.PlayerFinishedConfigurationEvent;
26+
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
2527
import com.velocitypowered.proxy.VelocityServer;
2628
import com.velocitypowered.proxy.connection.MinecraftConnection;
2729
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
30+
import com.velocitypowered.proxy.connection.backend.BungeeCordMessageResponder;
2831
import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
2932
import com.velocitypowered.proxy.connection.player.resourcepack.ResourcePackResponseBundle;
3033
import com.velocitypowered.proxy.protocol.MinecraftPacket;
@@ -41,6 +44,7 @@
4144
import com.velocitypowered.proxy.protocol.packet.config.KnownPacksPacket;
4245
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
4346
import io.netty.buffer.ByteBuf;
47+
import io.netty.buffer.ByteBufUtil;
4448
import io.netty.buffer.Unpooled;
4549
import java.util.concurrent.CompletableFuture;
4650
import java.util.concurrent.TimeUnit;
@@ -123,8 +127,32 @@ public boolean handle(final PluginMessagePacket packet) {
123127
brandChannel = packet.getChannel();
124128
// Client sends `minecraft:brand` packet immediately after Login,
125129
// but at this time the backend server may not be ready
130+
} else if (BungeeCordMessageResponder.isBungeeCordMessage(packet)) {
131+
return true;
126132
} else if (serverConn != null) {
127-
serverConn.ensureConnected().write(packet.retain());
133+
byte[] bytes = ByteBufUtil.getBytes(packet.content());
134+
ChannelIdentifier id = this.server.getChannelRegistrar().getFromId(packet.getChannel());
135+
136+
if (id == null) {
137+
serverConn.getPlayer().getConnection().write(packet.retain());
138+
return true;
139+
}
140+
141+
// Handling this stuff async means that we should probably pause
142+
// the connection while we toss this off into another pool
143+
serverConn.getPlayer().getConnection().setAutoReading(false);
144+
this.server.getEventManager()
145+
.fire(new PluginMessageEvent(serverConn.getPlayer(), serverConn, id, bytes))
146+
.thenAcceptAsync(pme -> {
147+
if (pme.getResult().isAllowed() && serverConn.getConnection() != null) {
148+
serverConn.ensureConnected().write(new PluginMessagePacket(
149+
pme.getIdentifier().getId(), Unpooled.wrappedBuffer(bytes)));
150+
}
151+
serverConn.getPlayer().getConnection().setAutoReading(true);
152+
}, player.getConnection().eventLoop()).exceptionally((ex) -> {
153+
logger.error("Exception while handling plugin message packet for {}", player, ex);
154+
return null;
155+
});
128156
}
129157
return true;
130158
}

0 commit comments

Comments
 (0)