9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-26 10:29:13 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0238-Async-switch-connection-state.patch
Dreeam 3c25377465 Drop some unused patches
ClassInstanceMultiMap belongs to Minecraft vanilla entity storage.
And is unused, since replaced by spottedleaf's entity storage (rewrite chunk system).
However these patches might be useful for vanilla entity storage if is used.
2025-07-09 04:20:02 +08:00

149 lines
9.5 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: hayanesuru <hayanesuru@outlook.jp>
Date: Fri, 9 May 2025 16:55:34 +0900
Subject: [PATCH] Async switch connection state
diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java
index 01fb1bf0eba70b740b12731c79feadefc8fe96f0..a6077984e139be5cbcc00f8f0828f1992d3604a6 100644
--- a/net/minecraft/network/Connection.java
+++ b/net/minecraft/network/Connection.java
@@ -343,6 +343,11 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
if (protocolInfo.flow() != this.getReceiving()) {
throw new IllegalStateException("Invalid inbound protocol: " + protocolInfo.id());
} else {
+ // Leaf start - Async switch connection state
+ if (org.dreeam.leaf.config.modules.network.AlternativeJoin.enabled && ca.spottedleaf.moonrise.common.util.TickThread.isTickThread()) {
+ this.channel.config().setAutoRead(false);
+ }
+ // Leaf end - Async switch connection state
this.packetListener = packetInfo;
this.disconnectListener = null;
UnconfiguredPipelineHandler.InboundConfigurationTask inboundConfigurationTask = UnconfiguredPipelineHandler.setupInboundProtocol(protocolInfo);
@@ -352,7 +357,14 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
inboundConfigurationTask = inboundConfigurationTask.andThen(context -> context.pipeline().addAfter("decoder", "bundler", packetBundlePacker));
}
- syncAfterConfigurationChange(this.channel.writeAndFlush(inboundConfigurationTask));
+ // Leaf start - Async switch connection state
+ var cf = this.channel.writeAndFlush(inboundConfigurationTask);
+ if (org.dreeam.leaf.config.modules.network.AlternativeJoin.enabled && ca.spottedleaf.moonrise.common.util.TickThread.isTickThread()) {
+ cf.addListener((ChannelFutureListener) Connection::syncAfterConfigurationChange);
+ return;
+ }
+ syncAfterConfigurationChange(cf);
+ // Leaf end - Async switch connection state
}
}
@@ -374,6 +386,31 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
}
}
+ // Leaf start - Async switch connection state
+ public @Nullable ChannelFuture setupOutboundProtocolAsync(ProtocolInfo<?> protocolInfo) {
+ if (protocolInfo.flow() != this.getSending()) {
+ throw new IllegalStateException("Invalid outbound protocol: " + protocolInfo.id());
+ } else {
+ UnconfiguredPipelineHandler.OutboundConfigurationTask outboundConfigurationTask = UnconfiguredPipelineHandler.setupOutboundProtocol(protocolInfo);
+ BundlerInfo bundlerInfo = protocolInfo.bundlerInfo();
+ if (bundlerInfo != null) {
+ PacketBundleUnpacker packetBundleUnpacker = new PacketBundleUnpacker(bundlerInfo);
+ outboundConfigurationTask = outboundConfigurationTask.andThen(
+ context -> context.pipeline().addAfter("encoder", "unbundler", packetBundleUnpacker)
+ );
+ }
+
+ boolean flag = protocolInfo.id() == ConnectionProtocol.LOGIN;
+ var cf = this.channel.writeAndFlush(outboundConfigurationTask.andThen(context -> this.sendLoginDisconnect = flag));
+ if (org.dreeam.leaf.config.modules.network.AlternativeJoin.enabled) {
+ cf.addListener((ChannelFutureListener) Connection::syncAfterConfigurationChange);
+ return cf;
+ }
+ return null;
+ }
+ }
+ // Leaf end - Async switch connection state
+
public void setListenerForServerboundHandshake(PacketListener packetListener) {
if (this.packetListener != null) {
throw new IllegalStateException("Listener already set");
diff --git a/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java b/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java
index ea892dd1d23d0f16c11381d1e8d75164a2791a3d..25c6e9f1b88ea7bfa2c7f1d130d56c581202960e 100644
--- a/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java
+++ b/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java
@@ -152,11 +152,34 @@ public class ServerConfigurationPacketListenerImpl extends ServerCommonPacketLis
}
}
+ private volatile boolean changingState = false; // Leaf - Async switch connection state
@Override
public void handleConfigurationFinished(ServerboundFinishConfigurationPacket packet) {
+ // Leaf start - Async switch connection state
+ if (org.dreeam.leaf.config.modules.network.AlternativeJoin.enabled && !changingState) {
+ changingState = true;
+ this.finishCurrentTask(JoinWorldTask.TYPE);
+ this.connection.setupOutboundProtocolAsync(GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess()))).addListener(l -> {
+ try {
+ PacketUtils.ensureRunningOnSameThread(packet, this, this.server);
+ } catch (net.minecraft.server.RunningOnDifferentThreadException ignored) {
+ } catch (
+ io.papermc.paper.util.ServerStopRejectedExecutionException ignored) { // Paper - do not prematurely disconnect players on stop
+ } catch (java.util.concurrent.RejectedExecutionException var6) {
+ this.connection.disconnect(Component.translatable("multiplayer.disconnect.server_shutdown"));
+ } catch (ClassCastException var7) {
+ LOGGER.error("Received {} that couldn't be processed", packet.getClass(), var7);
+ this.connection.disconnect(Component.translatable("multiplayer.disconnect.invalid_packet"));
+ }
+ });
+ return;
+ }
PacketUtils.ensureRunningOnSameThread(packet, this, this.server);
- this.finishCurrentTask(JoinWorldTask.TYPE);
- this.connection.setupOutboundProtocol(GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess())));
+ if (!org.dreeam.leaf.config.modules.network.AlternativeJoin.enabled) {
+ this.finishCurrentTask(JoinWorldTask.TYPE);
+ this.connection.setupOutboundProtocol(GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess())));
+ }
+ // Leaf end - Async switch connection state
try {
PlayerList playerList = this.server.getPlayerList();
diff --git a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
index 2e014cd543a6a790a0023835199b0e956e49fd1f..72115a4d2c54834f9edef02c83ed79f0fe380e78 100644
--- a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
+++ b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
@@ -459,11 +459,31 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener,
this.disconnect(ServerCommonPacketListenerImpl.DISCONNECT_UNEXPECTED_QUERY);
}
+ private volatile boolean changingState = false; // Leaf - Async switch connection state
@Override
public void handleLoginAcknowledgement(ServerboundLoginAcknowledgedPacket packet) {
+ // Leaf start - Async switch connection state
+ if (org.dreeam.leaf.config.modules.network.AlternativeJoin.enabled && !changingState) {
+ changingState = true;
+ this.connection.setupOutboundProtocolAsync(ConfigurationProtocols.CLIENTBOUND).addListener(l -> {
+ try {
+ net.minecraft.network.protocol.PacketUtils.ensureRunningOnSameThread(packet, this, this.server);
+ } catch (net.minecraft.server.RunningOnDifferentThreadException ignored) {
+ } catch (
+ io.papermc.paper.util.ServerStopRejectedExecutionException ignored) { // Paper - do not prematurely disconnect players on stop
+ } catch (java.util.concurrent.RejectedExecutionException var6) {
+ this.connection.disconnect(Component.translatable("multiplayer.disconnect.server_shutdown"));
+ } catch (ClassCastException var7) {
+ LOGGER.error("Received {} that couldn't be processed", packet.getClass(), var7);
+ this.connection.disconnect(Component.translatable("multiplayer.disconnect.invalid_packet"));
+ }
+ });
+ return;
+ }
+ // Leaf end - Async switch connection state
net.minecraft.network.protocol.PacketUtils.ensureRunningOnSameThread(packet, this, this.server); // CraftBukkit
Validate.validState(this.state == ServerLoginPacketListenerImpl.State.PROTOCOL_SWITCHING, "Unexpected login acknowledgement packet");
- this.connection.setupOutboundProtocol(ConfigurationProtocols.CLIENTBOUND);
+ if (!org.dreeam.leaf.config.modules.network.AlternativeJoin.enabled) this.connection.setupOutboundProtocol(ConfigurationProtocols.CLIENTBOUND); // Leaf - Async switch connection state
CommonListenerCookie commonListenerCookie = CommonListenerCookie.createInitial(Objects.requireNonNull(this.authenticatedProfile), this.transferred);
ServerConfigurationPacketListenerImpl serverConfigurationPacketListenerImpl = new ServerConfigurationPacketListenerImpl(
this.server, this.connection, commonListenerCookie