#3797: Expose sendPacketQueued to unsafe interface

This commit is contained in:
Outfluencer 2025-03-08 16:56:20 +11:00 committed by md_5
parent 47f8c29a7c
commit 9476ffccdb
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
6 changed files with 47 additions and 2 deletions

View File

@ -84,5 +84,15 @@ public interface Connection
* @param packet the packet to send
*/
void sendPacket(DefinedPacket packet);
/**
* Queue a packet to this connection.
* If the packet is not registered for the connections current encoder protocol, it will be queued until it is,
* otherwise it will be sent immediately.
*
* @param packet the packet to be queued
* @throws UnsupportedOperationException if used for a PendingConnection
*/
void sendPacketQueued(DefinedPacket packet);
}
}

View File

@ -156,7 +156,7 @@ public class BungeeTitle implements Title
{
if ( player.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_17 )
{
( (UserConnection) player ).sendPacketQueued( packet.newPacket );
player.unsafe().sendPacketQueued( packet.newPacket );
} else
{
player.unsafe().sendPacket( packet.oldPacket );

View File

@ -14,6 +14,7 @@ import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.packet.PluginMessage;
@RequiredArgsConstructor
@ -40,6 +41,18 @@ public class ServerConnection implements Server
{
ch.write( packet );
}
@Override
public void sendPacketQueued(DefinedPacket packet)
{
if ( ch.getEncodeVersion() >= ProtocolConstants.MINECRAFT_1_20_2 )
{
ServerConnection.this.sendPacketQueued( packet );
} else
{
sendPacket( packet );
}
}
};
public void sendPacketQueued(DefinedPacket packet)
@ -53,6 +66,8 @@ public class ServerConnection implements Server
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_SERVER.hasPacket( packet.getClass(), ch.getEncodeVersion() ) )
{
// we should limit this so bad api usage won't oom the server.
Preconditions.checkState( packetQueue.size() <= 4096, "too many queued packets" );
packetQueue.add( packet );
} else
{

View File

@ -150,6 +150,18 @@ public final class UserConnection implements ProxiedPlayer
{
ch.write( packet );
}
@Override
public void sendPacketQueued(DefinedPacket packet)
{
if ( pendingConnection.getVersion() >= ProtocolConstants.MINECRAFT_1_20_2 )
{
UserConnection.this.sendPacketQueued( packet );
} else
{
sendPacket( packet );
}
}
};
public boolean init()
@ -191,6 +203,8 @@ public final class UserConnection implements ProxiedPlayer
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) )
{
// we should limit this so bad api usage won't oom the server.
Preconditions.checkState( packetQueue.size() <= 4096, "too many queued packets" );
packetQueue.add( packet );
} else
{

View File

@ -122,6 +122,12 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{
ch.write( packet );
}
@Override
public void sendPacketQueued(DefinedPacket packet)
{
throw new UnsupportedOperationException( "Not supported" );
}
};
@Getter
private boolean onlineMode = BungeeCord.getInstance().config.isOnlineMode();

View File

@ -103,7 +103,7 @@ public class UpstreamBridge extends PacketHandler
if ( player.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_19_3 )
{
// need to queue, because players in config state could receive it
( (UserConnection) player ).sendPacketQueued( newPacket );
player.unsafe().sendPacketQueued( newPacket );
} else
{
player.unsafe().sendPacket( oldPacket );