Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Server-Side ViaFabric compatibility #1163

Open
wants to merge 14 commits into
base: 2.0
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix Via support; consolidate module code
ViaFabric support tested on 1.21.4 server. 1.20.2+ clients work. ViaFabric breaks with 1.20.1 clients on 1.21.4 servers, with the connection timing out. This is a Via issue and occurs without PacketEvents or GrimAC present.
Axionize committed Mar 11, 2025
commit dfb7fc765afd555cf903e397e1689f847979b5e8
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
import io.github.retrooper.packetevents.handler.PacketDecoder;
import io.github.retrooper.packetevents.handler.PacketEncoder;
import io.github.retrooper.packetevents.util.FabricInjectionUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelPipeline;
@@ -43,48 +44,13 @@
@Mixin(net.minecraft.network.Connection.class)
public class ConnectionMixin {

// doesn't account for mods like ViaFabric
@Unique
private static final ClientVersion CLIENT_VERSION =
ClientVersion.getById(SharedConstants.getProtocolVersion());

@Inject(
method = "configureSerialization",
at = @At("TAIL")
)
private static void configureSerialization(
ChannelPipeline pipeline, PacketFlow flow, CallbackInfo ci
) {
PacketSide pipelineSide = switch (flow) {
case CLIENTBOUND -> PacketSide.CLIENT;
case SERVERBOUND -> PacketSide.SERVER;
};
PacketSide apiSide = PacketEvents.getAPI().getInjector().getPacketSide();
if (pipelineSide != apiSide) {
// if pipeline side doesn't match api side, don't inject into
// this pipeline - it probably means this is the pipeline from
// integrated server to minecraft client, which is currently unsupported
PacketEvents.getAPI().getLogManager().debug("Skipped pipeline injection on " + pipelineSide);
return;
}

PacketEvents.getAPI().getLogManager().debug("Game connected!");

Channel channel = pipeline.channel();
User user = new User(channel, ConnectionState.HANDSHAKING,
CLIENT_VERSION, new UserProfile(null, null));
ProtocolManager.USERS.put(channel.pipeline(), user);

UserConnectEvent connectEvent = new UserConnectEvent(user);
PacketEvents.getAPI().getEventManager().callEvent(connectEvent);
if (connectEvent.isCancelled()) {
channel.unsafe().closeForcibly();
return;
}

channel.pipeline().addBefore("decoder", PacketEvents.DECODER_NAME, new PacketDecoder(apiSide, user));
channel.pipeline().addBefore("encoder", PacketEvents.ENCODER_NAME, new PacketEncoder(apiSide, user));
channel.closeFuture().addListener((ChannelFutureListener) future ->
PacketEventsImplHelper.handleDisconnection(user.getChannel(), user.getUUID()));
FabricInjectionUtil.injectAtPipelineBuilder(pipeline, flow);
}
}
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import java.util.Arrays;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.ApiStatus;

@@ -49,7 +50,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
return;
}
PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player,
msg, false, this.side);
msg, true, this.side);
if (msg.isReadable()) {
out.add(msg.retain());
}
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
private @Nullable ProtocolPacketEvent handlePacket(ChannelHandlerContext ctx, ByteBuf buffer, ChannelPromise promise) throws Exception {
// Process the packet using PacketEventsImplHelper (similar to Spigot)
ProtocolPacketEvent protocolPacketEvent = PacketEventsImplHelper.handlePacket(
ctx.channel(), this.user, this.player, buffer, false, this.side
ctx.channel(), this.user, this.player, buffer, true, this.side
);

// Execute post-send tasks (required for cross-platform support)
Original file line number Diff line number Diff line change
@@ -101,7 +101,6 @@ public static void reorderHandlers(ChannelHandlerContext ctx) {
}
}

// Log the pipeline for debugging
PacketEvents.getAPI().getLogManager().debug("Pipeline after reorder: " + pipeline.names());
}
}