From 5038f42af7babd34de90eb0272c171482adca6c2 Mon Sep 17 00:00:00 2001 From: apple502j <33279053+apple502j@users.noreply.github.com> Date: Fri, 8 Sep 2023 13:58:30 +0900 Subject: [PATCH 1/2] Pass handler to clientside ConfigurationPacketHandler --- .../networking/v1/ClientConfigurationNetworking.java | 11 ++++++----- .../NetworkingConfigurationClientTest.java | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java index a6e4fe0d25..78a5dbe044 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java @@ -105,10 +105,10 @@ public void receive(MinecraftClient client, ClientConfigurationNetworkHandler ne // Normally, packets are handled on the network IO thread - though it is // not guaranteed (for example, with 1.19.4 S2C packet bundling) // Since we're handling it right now, connection check is redundant. - handler.receive(packet, sender); + handler.receive(packet, networkHandler, sender); } else { client.execute(() -> { - if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, sender); + if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, networkHandler, sender); }); } } @@ -218,10 +218,10 @@ public void receive(MinecraftClient client, ClientConfigurationNetworkHandler ne // Normally, packets are handled on the network IO thread - though it is // not guaranteed (for example, with 1.19.4 S2C packet bundling) // Since we're handling it right now, connection check is redundant. - handler.receive(packet, sender); + handler.receive(packet, networkHandler, sender); } else { client.execute(() -> { - if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, sender); + if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, networkHandler, sender); }); } } @@ -451,9 +451,10 @@ public interface ConfigurationPacketHandler { * * * @param packet the packet + * @param networkHandler the network handler * @param responseSender the packet sender * @see FabricPacket */ - void receive(T packet, PacketSender responseSender); + void receive(T packet, ClientConfigurationNetworkHandler networkHandler, PacketSender responseSender); } } diff --git a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java index 3a1d4acb13..560ca70363 100644 --- a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java +++ b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java @@ -23,7 +23,7 @@ public class NetworkingConfigurationClientTest implements ClientModInitializer { @Override public void onInitializeClient() { - ClientConfigurationNetworking.registerGlobalReceiver(NetworkingConfigurationTest.ConfigurationPacket.PACKET_TYPE, (packet, responseSender) -> { + ClientConfigurationNetworking.registerGlobalReceiver(NetworkingConfigurationTest.ConfigurationPacket.PACKET_TYPE, (packet, handler, responseSender) -> { // Handle stuff here // Respond back to the server that the task is complete From 1cbc18ef4f8c35ef124c78ee9307ad241cde1c81 Mon Sep 17 00:00:00 2001 From: apple502j <33279053+apple502j@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:03:07 +0900 Subject: [PATCH 2/2] Add helper method for disconnection --- .../networking/v1/ClientConfigurationNetworking.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java index 78a5dbe044..829a76d8a4 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java @@ -27,6 +27,7 @@ import net.minecraft.network.PacketByteBuf; import net.minecraft.network.listener.ServerCommonPacketListener; import net.minecraft.network.packet.Packet; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.thread.ThreadExecutor; @@ -393,6 +394,14 @@ public static void send(T packet) { throw new IllegalStateException("Cannot send packet while not configuring!"); } + /** + * Disconnects from the server. + * @param handler the network handler + */ + public static void disconnect(ClientConfigurationNetworkHandler handler, Text reason) { + ((ClientCommonNetworkHandlerAccessor) handler).getConnection().disconnect(reason); + } + private ClientConfigurationNetworking() { }