Queue packets when client isn't ready

This commit is contained in:
Adrian Bergqvist 2023-04-08 19:26:33 +02:00
parent e0df9a5a48
commit 8beb59a627
No known key found for this signature in database
GPG Key ID: 3B3DA43224B79417
8 changed files with 52 additions and 6 deletions

View File

@ -5,10 +5,14 @@ plugins {
}
group = "org.adde0109"
version = "1.3.1-beta-rc4"
version = "1.3.1-beta"
repositories {
mavenCentral()
maven {
name = "papermc"
url = uri("https://repo.papermc.io/repository/maven-public/")
}
}
dependencies {

View File

@ -7,6 +7,7 @@ public class ForgeConstants {
public static final String MARKER_ADDER = "FML2/3 Marker Adder";
public static final String RESET_LISTENER = "ambassador-reset-listener";
public static final String SERVER_SUCCESS_LISTENER = "ambassador-server-success-listener";
public static final String PLUGIN_PACKET_QUEUE = "ambassador-plugin-generated-packet-queue";
public static final String FORGE_HANDSHAKE_HOLDER = "ambassador-forge-handshake-holder";
public static final String FORGE_HANDSHAKE_DECODER = "ambassador-forge-decoder";
public static final String FORGE_HANDSHAKE_HANDLER = "ambassador-forge-handler";

View File

@ -16,7 +16,7 @@ import org.adde0109.ambassador.forge.packet.IForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.packet.ModListReplyPacket;
import org.adde0109.ambassador.forge.pipeline.ForgeLoginWrapperDecoder;
import org.adde0109.ambassador.velocity.client.FML2CRPMResetCompleteDecoder;
import org.adde0109.ambassador.velocity.client.OutboundForgeHandshakeHolder;
import org.adde0109.ambassador.velocity.client.OutboundForgeHandshakeQueue;
import org.adde0109.ambassador.velocity.client.OutboundSuccessHolder;
import java.util.concurrent.TimeUnit;
@ -50,7 +50,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
player.getConnectionInFlight().getConnection().getChannel().config().setAutoRead(false);
connection.getChannel().pipeline().addBefore(Connections.MINECRAFT_DECODER, ForgeConstants.RESET_LISTENER,new FML2CRPMResetCompleteDecoder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER, ForgeConstants.FORGE_HANDSHAKE_HOLDER,new OutboundForgeHandshakeHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER, ForgeConstants.FORGE_HANDSHAKE_HOLDER,new OutboundForgeHandshakeQueue());
((ForgeLoginWrapperDecoder) connection.getChannel().pipeline().get(ForgeConstants.FORGE_HANDSHAKE_DECODER)).registerLoginWrapperID(98);
connection.write(new PluginMessage("fml:handshake", Unpooled.wrappedBuffer(ForgeHandshakeUtils.generatePluginResetPacket())));

View File

@ -11,6 +11,7 @@ import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.network.Connections;
import com.velocitypowered.proxy.protocol.StateRegistry;
import net.kyori.adventure.text.Component;
import org.adde0109.ambassador.Ambassador;
import org.adde0109.ambassador.forge.ForgeConstants;
import org.adde0109.ambassador.forge.VelocityForgeClientConnectionPhase;
@ -47,6 +48,7 @@ public class VelocityEventHandler {
public void onPostLoginEvent(PostLoginEvent event, Continuation continuation) {
if (((ConnectedPlayer) event.getPlayer()).getPhase() instanceof VelocityForgeClientConnectionPhase)
((VelocityServer) Ambassador.getInstance().server).unregisterConnection((ConnectedPlayer) event.getPlayer());
event.getPlayer().sendMessage(Component.text("test"));
continuation.resume();
}

View File

@ -57,6 +57,7 @@ public class ForgeLoginSessionHandler implements MinecraftSessionHandler {
((OutboundSuccessHolder) connection.getChannel().pipeline().get(ForgeConstants.SERVER_SUCCESS_LISTENER))
.sendPacket();
connection.setState(StateRegistry.PLAY);
connection.getChannel().pipeline().remove(ForgeConstants.PLUGIN_PACKET_QUEUE);
((VelocityServer) Ambassador.getInstance().server).registerConnection(player);
}

View File

@ -1,10 +1,9 @@
package org.adde0109.ambassador.velocity.client;
import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage;
import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccess;
import io.netty.channel.*;
public class OutboundForgeHandshakeHolder extends ChannelOutboundHandlerAdapter {
public class OutboundForgeHandshakeQueue extends ChannelOutboundHandlerAdapter {
PendingWriteQueue writeQueue;

View File

@ -0,0 +1,38 @@
package org.adde0109.ambassador.velocity.client;
import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder;
import io.netty.channel.*;
import io.netty.handler.codec.EncoderException;
public class PluginLoginPacketQueue extends ChannelOutboundHandlerAdapter {
private PendingWriteQueue queue;
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
queue = new PendingWriteQueue(ctx);
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
MinecraftEncoder encoder = ctx.pipeline().get(MinecraftEncoder.class);
try {
encoder.write(ctx, msg, promise);
} catch (EncoderException e) {
if (e.getCause() instanceof IllegalArgumentException) {
queue.add(msg,promise);
}
}
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
if (ctx.channel().isActive()) {
queue.removeAndWriteAll();
ctx.flush();
} else {
queue.removeAndFailAll(new ChannelException());
}
}
}

View File

@ -10,7 +10,6 @@ import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.Handshake;
import io.netty.buffer.ByteBuf;
import org.adde0109.ambassador.forge.ForgeConstants;
import org.adde0109.ambassador.velocity.client.OutboundSuccessHolder;
public class VelocityHandshakeSessionHandler implements MinecraftSessionHandler {
private final HandshakeSessionHandler original;
@ -31,10 +30,12 @@ public class VelocityHandshakeSessionHandler implements MinecraftSessionHandler
case "FML2":
connection.setType(ForgeConstants.ForgeFML2);
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new PluginLoginPacketQueue());
break;
case "FML3":
connection.setType(ForgeConstants.ForgeFML3);
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new PluginLoginPacketQueue());
break;
}
}