9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch
Dreeam 52477a91ae Updated Upstream (Paper/Gale)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@f4f27551 [ci/skip] Clarify BlockFadeEvent#getNewState javadocs (#12250)
PaperMC/Paper@102c8bbc Add config for updating equipment on player actions (#12275)
PaperMC/Paper@f0388e2f Call EntityPushedByEntityAttackEvent for Mace AoE (#12257)
PaperMC/Paper@c37b890c More deferred requireNonNull message creation
PaperMC/Paper@310f5229 Add unsupported config option and internal API to simplify remote item matching
PaperMC/Paper@6ea42025 Send all attributes on respawn (#12274)
PaperMC/Paper@2d3a1385 [ci/skip] Refine recipe management API documentation. (#12287)
PaperMC/Paper@aaaeb4e1 [ci/skip] Make compilation logs actually readable (#12276)
PaperMC/Paper@bb1beda6 feat: add event to wind charge explode (#12248)

Gale Changes:
Dreeam-qwq/Gale@43bfa513 Updated Upstream (Paper)
Dreeam-qwq/Gale@ab0a7189 Updated Upstream ()
Dreeam-qwq/Gale@27ddb2fa [ci/skip] Added merge announcement
Dreeam-qwq/Gale@523e7745 [ci/skip] Fix announcement format
Dreeam-qwq/Gale@d91ed070 [ci/skip] Update annocement
2025-03-17 14:30:27 -04:00

117 lines
5.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Fri, 21 Feb 2025 15:52:42 +0100
Subject: [PATCH] Rewrite queue on Connection.flushQueue
diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java
index 00a82873d226f113278632a53c0faca420dd67d4..9aeadf792eb7c6c39044df00fa7ff80017ba72d0 100644
--- a/net/minecraft/network/Connection.java
+++ b/net/minecraft/network/Connection.java
@@ -85,7 +85,7 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
private static final ProtocolInfo<ServerHandshakePacketListener> INITIAL_PROTOCOL = HandshakeProtocols.SERVERBOUND;
private final PacketFlow receiving;
private volatile boolean sendLoginDisconnect = true;
- private final Queue<WrappedConsumer> pendingActions = Queues.newConcurrentLinkedQueue(); // Paper - Optimize network
+ private final Queue<WrappedConsumer> pendingActions = org.dreeam.leaf.config.modules.network.ConnectionFlushQueueRewrite.enabled ? new java.util.ArrayDeque<>() : Queues.newConcurrentLinkedQueue(); // Paper - Optimize network // Leaf - Rewrite queue on Connection.flushQueue
public Channel channel;
public SocketAddress address;
// Spigot start
@@ -541,9 +541,17 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
if (io.papermc.paper.util.MCUtil.isMainThread()) {
return this.processQueue();
} else if (this.isPending) {
- // Should only happen during login/status stages
- synchronized (this.pendingActions) {
- return this.processQueue();
+ // Leaf start - Rewrite queue on Connection.flushQueue
+ if (org.dreeam.leaf.config.modules.network.ConnectionFlushQueueRewrite.enabled) {
+ // Submit to the event loop to ensure thread confinement
+ this.channel.eventLoop().execute(this::processQueue);
+ return false;
+ } else {
+ // Original Paper behavior
+ synchronized (this.pendingActions) {
+ return this.processQueue();
+ // Leaf end - Rewrite queue on Connection.flushQueue
+ }
}
}
return false;
@@ -554,6 +562,24 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
return true;
}
+ // Leaf start - Rewrite queue on Connection.flushQueue
+ if (org.dreeam.leaf.config.modules.network.ConnectionFlushQueueRewrite.enabled) {
+ WrappedConsumer queued;
+ while ((queued = this.pendingActions.poll()) != null) {
+ if (queued instanceof PacketSendAction packetSendAction) {
+ final Packet<?> packet = packetSendAction.packet;
+ if (!packet.isReady()) {
+ // Re-add to the front and exit
+ this.pendingActions.add(queued);
+ return false;
+ }
+ }
+
+ if (queued.tryMarkConsumed()) {
+ queued.accept(this);
+ }
+ }
+ } else {
// If we are on main, we are safe here in that nothing else should be processing queue off main anymore
// But if we are not on main due to login/status, the parent is synchronized on packetQueue
final java.util.Iterator<WrappedConsumer> iterator = this.pendingActions.iterator();
@@ -581,6 +607,8 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
queued.accept(this);
}
}
+ }
+ // Leaf end - Rewrite queue on Connection.flushQueue
return true;
}
// Paper end - Optimize network
@@ -913,6 +941,31 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
// Paper start - Optimize network
public void clearPacketQueue() {
final net.minecraft.server.level.ServerPlayer player = getPlayer();
+
+ // Leaf start - Rewrite queue on Connection.flushQueue
+ if (org.dreeam.leaf.config.modules.network.ConnectionFlushQueueRewrite.enabled) {
+ // When using Leaf's queue rewrite, ensure thread safety via event loop
+ if (this.channel != null && !this.channel.eventLoop().inEventLoop()) {
+ this.channel.eventLoop().execute(() -> this.clearPacketQueue());
+ return;
+ }
+
+ // Take a snapshot to avoid ConcurrentModificationException
+ java.util.List<Consumer<Connection>> queueSnapshot = new java.util.ArrayList<>(this.pendingActions);
+
+ for (Consumer<Connection> queuedAction : queueSnapshot) {
+ if (queuedAction instanceof PacketSendAction packetSendAction) {
+ final Packet<?> packet = packetSendAction.packet;
+ if (packet.hasFinishListener()) {
+ packet.onPacketDispatchFinish(player, null);
+ }
+ }
+ }
+
+ this.pendingActions.clear();
+ } else {
+ // Original Paper behavior - use synchronization
+ synchronized (this.pendingActions) {
for (final Consumer<Connection> queuedAction : this.pendingActions) {
if (queuedAction instanceof PacketSendAction packetSendAction) {
final Packet<?> packet = packetSendAction.packet;
@@ -922,6 +975,9 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
}
}
this.pendingActions.clear();
+ }
+ }
+ // Leaf end - Rewrite queue on Connection.flushQueue
}
private static class InnerUtil { // Attempt to hide these methods from ProtocolLib, so it doesn't accidently pick them up.