Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 31f21cc

Browse files
committed
Merge branch 'update-api'
2 parents 255a819 + 26b332e commit 31f21cc

17 files changed

+155
-801
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# ViaBungee
22

3-
> [!WARNING]
4-
> This currently only works if **both client and server are below 1.20.2**.
5-
6-
See https://github.com/ViaVersion/ViaVersion for the main project. Note that we recommend putting ViaVersion on your
7-
backend servers, or switching to [Velocity](https://github.com/PaperMC/Velocity) or [ViaProxy](https://github.com/ViaVersion/ViaProxy) with [BungeeViaProxy plugin](https://github.com/ViaVersionAddons/BungeeViaProxy) as ViaBungee is fairly unstable due to platform limitations.
3+
See https://github.com/ViaVersion/ViaVersion for the main project.
84

95
## To get started
106

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111
compileOnly("com.viaversion:viabackwards-common:5.2.1")
1212
compileOnly("com.viaversion:viarewind-common:4.0.5")
1313
compileOnly("com.viaversion:viaaprilfools-common:4.0.0")
14-
compileOnly("net.md-5:bungeecord-api:1.20-R0.3-SNAPSHOT")
14+
compileOnly("net.md-5:bungeecord-api:1.21-R0.1-SNAPSHOT")
1515
implementation("net.lenni0451:Reflect:1.4.0")
1616
}
1717

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ rootProject.name = "viabungee"
33
dependencyResolutionManagement {
44
repositories {
55
maven("https://repo.viaversion.com")
6+
maven("https://oss.sonatype.org/content/groups/public")
67
maven("https://repo.papermc.io/repository/maven-public/")
78
maven("https://maven.lenni0451.net/everything")
89
mavenCentral()

src/main/java/com/viaversion/bungee/ViaBungeePlatform.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import net.md_5.bungee.api.ProxyServer;
5252
import net.md_5.bungee.api.connection.ProxiedPlayer;
5353
import net.md_5.bungee.api.plugin.Plugin;
54-
import net.md_5.bungee.protocol.ProtocolConstants;
5554

5655
public final class ViaBungeePlatform implements ViaServerProxyPlatform<ProxiedPlayer> {
5756
private final ProtocolDetectorService protocolDetectorService = new ProtocolDetectorService();
@@ -63,8 +62,8 @@ public final class ViaBungeePlatform implements ViaServerProxyPlatform<ProxiedPl
6362
public ViaBungeePlatform(final ViaBungeePlugin plugin, final File pluginFolder) {
6463
this.plugin = plugin;
6564
try {
66-
ProtocolConstants.class.getField("MINECRAFT_1_21");
67-
} catch (final NoSuchFieldException e) {
65+
ProxyServer.getInstance().unsafe();
66+
} catch (final NoSuchMethodError e) {
6867
getLogger().warning(" / \\");
6968
getLogger().warning(" / \\");
7069
getLogger().warning(" / | \\");
@@ -74,9 +73,6 @@ public ViaBungeePlatform(final ViaBungeePlugin plugin, final File pluginFolder)
7473
getLogger().warning("/_____________\\");
7574
}
7675

77-
getLogger().warning("ViaVersion does not work as intended across many different server versions, especially the more recent ones. " +
78-
"Consider moving Via plugins to your backend server or switching to Velocity.");
79-
8076
api = new BungeeViaAPI();
8177
viaConfig = new BungeeViaConfig(getDataFolder(), getLogger());
8278
bungeeConfig = new ViaBungeeConfig(new File(pluginFolder, "config.yml"), getLogger());

src/main/java/com/viaversion/bungee/handlers/BungeeChannelInitializer.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/main/java/com/viaversion/bungee/handlers/BungeeDecodeHandler.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.viaversion.viaversion.api.connection.UserConnection;
2121
import com.viaversion.viaversion.exception.CancelCodecException;
2222
import com.viaversion.viaversion.exception.CancelDecoderException;
23+
import com.viaversion.viaversion.util.ByteBufUtil;
24+
import com.viaversion.viaversion.util.PipelineUtil;
2325
import io.netty.buffer.ByteBuf;
2426
import io.netty.channel.ChannelHandler;
2527
import io.netty.channel.ChannelHandlerContext;
@@ -28,36 +30,40 @@
2830

2931
@ChannelHandler.Sharable
3032
public class BungeeDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
31-
private final UserConnection info;
3233

33-
public BungeeDecodeHandler(UserConnection info) {
34-
this.info = info;
34+
private final UserConnection user;
35+
36+
public BungeeDecodeHandler(UserConnection user) {
37+
this.user = user;
3538
}
3639

3740
@Override
38-
protected void decode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
39-
if (!ctx.channel().isActive()) {
41+
protected void decode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) {
42+
if (!user.checkServerboundPacket()) {
4043
throw CancelDecoderException.generate(null);
4144
}
42-
43-
if (!info.checkServerboundPacket()) throw CancelDecoderException.generate(null);
44-
if (!info.shouldTransformPacket()) {
45+
if (!user.shouldTransformPacket()) {
4546
out.add(bytebuf.retain());
4647
return;
4748
}
4849

49-
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
50+
ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf);
5051
try {
51-
info.transformServerbound(transformedBuf, CancelDecoderException::generate);
52+
user.transformIncoming(transformedBuf, CancelDecoderException::generate);
5253
out.add(transformedBuf.retain());
5354
} finally {
5455
transformedBuf.release();
5556
}
5657
}
5758

5859
@Override
59-
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
60-
if (cause instanceof CancelCodecException) return;
61-
super.exceptionCaught(ctx, cause);
60+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
61+
try {
62+
super.channelRead(ctx, msg);
63+
} catch (Throwable e) {
64+
if (!PipelineUtil.containsCause(e, CancelCodecException.class)) {
65+
throw e;
66+
}
67+
}
6268
}
6369
}

src/main/java/com/viaversion/bungee/handlers/BungeeEncodeHandler.java

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,96 +17,61 @@
1717
*/
1818
package com.viaversion.bungee.handlers;
1919

20-
import com.viaversion.bungee.util.BungeePipelineUtil;
2120
import com.viaversion.viaversion.api.connection.UserConnection;
2221
import com.viaversion.viaversion.exception.CancelCodecException;
2322
import com.viaversion.viaversion.exception.CancelEncoderException;
23+
import com.viaversion.viaversion.util.ByteBufUtil;
24+
import com.viaversion.viaversion.util.PipelineUtil;
2425
import io.netty.buffer.ByteBuf;
2526
import io.netty.channel.ChannelHandler;
2627
import io.netty.channel.ChannelHandlerContext;
28+
import io.netty.channel.ChannelPromise;
2729
import io.netty.handler.codec.MessageToMessageEncoder;
2830
import java.util.List;
2931

3032
@ChannelHandler.Sharable
3133
public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
32-
private final UserConnection info;
33-
private boolean handledCompression;
3434

35-
public BungeeEncodeHandler(UserConnection info) {
36-
this.info = info;
35+
private final UserConnection connection;
36+
37+
public BungeeEncodeHandler(UserConnection connection) {
38+
this.connection = connection;
3739
}
3840

3941
@Override
40-
protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
41-
if (!ctx.channel().isActive()) {
42+
protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) {
43+
if (!connection.checkClientboundPacket()) {
4244
throw CancelEncoderException.generate(null);
4345
}
44-
45-
if (!info.checkClientboundPacket()) throw CancelEncoderException.generate(null);
46-
if (!info.shouldTransformPacket()) {
46+
if (!connection.shouldTransformPacket()) {
4747
out.add(bytebuf.retain());
4848
return;
4949
}
5050

51-
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
51+
ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf);
5252
try {
53-
boolean needsCompress = handleCompressionOrder(ctx, transformedBuf);
54-
info.transformClientbound(transformedBuf, CancelEncoderException::generate);
55-
56-
if (needsCompress) {
57-
recompress(ctx, transformedBuf);
58-
}
59-
53+
connection.transformOutgoing(transformedBuf, CancelEncoderException::generate);
6054
out.add(transformedBuf.retain());
6155
} finally {
6256
transformedBuf.release();
6357
}
6458
}
6559

66-
private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) {
67-
boolean needsCompress = false;
68-
if (!handledCompression && ctx.pipeline().names().indexOf("compress") > ctx.pipeline().names().indexOf("via-encoder")) {
69-
// Need to decompress this packet due to bad order
70-
ByteBuf decompressed = BungeePipelineUtil.decompress(ctx, buf);
71-
72-
// Ensure the buffer wasn't reused
73-
if (buf != decompressed) {
74-
try {
75-
buf.clear().writeBytes(decompressed);
76-
} finally {
77-
decompressed.release();
78-
}
79-
}
80-
81-
// Reorder the pipeline
82-
ChannelHandler decoder = ctx.pipeline().get("via-decoder");
83-
ChannelHandler encoder = ctx.pipeline().get("via-encoder");
84-
ctx.pipeline().remove(decoder);
85-
ctx.pipeline().remove(encoder);
86-
ctx.pipeline().addAfter("decompress", "via-decoder", decoder);
87-
ctx.pipeline().addAfter("compress", "via-encoder", encoder);
88-
needsCompress = true;
89-
handledCompression = true;
90-
}
91-
return needsCompress;
92-
}
9360

94-
private void recompress(ChannelHandlerContext ctx, ByteBuf buf) {
95-
ByteBuf compressed = BungeePipelineUtil.compress(ctx, buf);
61+
@Override
62+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
9663
try {
97-
buf.clear().writeBytes(compressed);
98-
} finally {
99-
compressed.release();
64+
super.write(ctx, msg, promise);
65+
} catch (Throwable e) {
66+
if (!PipelineUtil.containsCause(e, CancelCodecException.class)) {
67+
throw e;
68+
} else {
69+
promise.setSuccess();
70+
}
10071
}
10172
}
10273

103-
@Override
104-
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
105-
if (cause instanceof CancelCodecException) return;
106-
super.exceptionCaught(ctx, cause);
107-
}
108-
10974
public UserConnection connection() {
110-
return info;
75+
return connection;
11176
}
11277
}

0 commit comments

Comments
 (0)