diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java index 42cac04f13..4ca3bd05bc 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java @@ -30,6 +30,8 @@ public final class ClientConfigurationConnectionEvents { /** * Event indicating a connection entering the CONFIGURATION state, ready for registering channel handlers. * + *

No packets should be sent when this event is invoked. + * * @see ClientConfigurationNetworking#registerReceiver(CustomPayload.Id, ClientConfigurationNetworking.ConfigurationPayloadHandler) */ public static final Event INIT = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Init.class, callbacks -> (handler, client) -> { @@ -38,14 +40,25 @@ public final class ClientConfigurationConnectionEvents { } }); + /** + * An event called after the connection has been initialized and is ready to start sending and receiving configuration packets. + * + *

Packets may be sent during this event. + */ + public static final Event START = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Start.class, callbacks -> (handler, client) -> { + for (ClientConfigurationConnectionEvents.Start callback : callbacks) { + callback.onConfigurationStart(handler, client); + } + }); + /** * An event called after the ReadyS2CPacket has been received, just before switching to the PLAY state. * *

No packets should be sent when this event is invoked. */ - public static final Event READY = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Ready.class, callbacks -> (handler, client) -> { - for (ClientConfigurationConnectionEvents.Ready callback : callbacks) { - callback.onConfigurationReady(handler, client); + public static final Event COMPLETE = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Complete.class, callbacks -> (handler, client) -> { + for (ClientConfigurationConnectionEvents.Complete callback : callbacks) { + callback.onConfigurationComplete(handler, client); } }); @@ -69,12 +82,38 @@ public interface Init { } @FunctionalInterface - public interface Ready { - void onConfigurationReady(ClientConfigurationNetworkHandler handler, MinecraftClient client); + public interface Start { + void onConfigurationStart(ClientConfigurationNetworkHandler handler, MinecraftClient client); + } + + @FunctionalInterface + public interface Complete { + void onConfigurationComplete(ClientConfigurationNetworkHandler handler, MinecraftClient client); } @FunctionalInterface public interface Disconnect { void onConfigurationDisconnect(ClientConfigurationNetworkHandler handler, MinecraftClient client); } + + // Deprecated: + + /** + * @deprecated replaced by {@link #COMPLETE} + */ + @Deprecated + public static final Event READY = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Ready.class, callbacks -> (handler, client) -> { + for (ClientConfigurationConnectionEvents.Ready callback : callbacks) { + callback.onConfigurationReady(handler, client); + } + }); + + /** + * @deprecated replaced by {@link ClientConfigurationConnectionEvents.Complete} + */ + @Deprecated + @FunctionalInterface + public interface Ready { + void onConfigurationReady(ClientConfigurationNetworkHandler handler, MinecraftClient client); + } } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java index 0e3a6e3505..6cf951409f 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java @@ -53,6 +53,11 @@ protected void invokeInitEvent() { ClientConfigurationConnectionEvents.INIT.invoker().onConfigurationInit(this.handler, this.client); } + @Override + public void onServerReady() { + ClientConfigurationConnectionEvents.START.invoker().onConfigurationStart(this.handler, this.client); + } + @Override protected void receiveRegistration(boolean register, RegistrationPayload payload) { super.receiveRegistration(register, payload); @@ -84,7 +89,8 @@ protected void invokeUnregisterEvent(List ids) { C2SConfigurationChannelEvents.UNREGISTER.invoker().onChannelUnregister(this.handler, this, this.client, ids); } - public void handleReady() { + public void handleComplete() { + ClientConfigurationConnectionEvents.COMPLETE.invoker().onConfigurationComplete(this.handler, this.client); ClientConfigurationConnectionEvents.READY.invoker().onConfigurationReady(this.handler, this.client); ClientNetworkingImpl.setClientConfigurationAddon(null); } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java index 0ed32b04e2..c2f5bea6a2 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java @@ -127,7 +127,6 @@ public static void setClientPlayAddon(ClientPlayNetworkAddon addon) { } public static void setClientConfigurationAddon(ClientConfigurationNetworkAddon addon) { - assert addon == null || currentPlayAddon == null; currentConfigurationAddon = addon; } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java index 94621972c9..2261d9a3b0 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java @@ -52,8 +52,8 @@ private void initAddon(CallbackInfo ci) { } @Inject(method = "onReady", at = @At(value = "NEW", target = "(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/network/ClientConnection;Lnet/minecraft/client/network/ClientConnectionState;)Lnet/minecraft/client/network/ClientPlayNetworkHandler;")) - public void onReady(ReadyS2CPacket packet, CallbackInfo ci) { - this.addon.handleReady(); + public void handleComplete(ReadyS2CPacket packet, CallbackInfo ci) { + this.addon.handleComplete(); } @Override diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java index 0fb8f69763..22af2c7853 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java @@ -27,9 +27,11 @@ import net.minecraft.client.network.ClientConnectionState; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.network.ClientConnection; +import net.minecraft.network.packet.s2c.play.EnterReconfigurationS2CPacket; import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; +import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon; import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl; import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon; @@ -56,6 +58,12 @@ private void handleServerPlayReady(GameJoinS2CPacket packet, CallbackInfo ci) { this.addon.onServerReady(); } + @Inject(method = "onEnterReconfiguration", at = @At("RETURN")) + public void onEnterReconfiguration(EnterReconfigurationS2CPacket packet, CallbackInfo ci) { + NetworkHandlerExtensions networkHandlerExtensions = (NetworkHandlerExtensions) connection.getPacketListener(); + ((ClientConfigurationNetworkAddon) networkHandlerExtensions.getAddon()).onServerReady(); + } + @Override public ClientPlayNetworkAddon getAddon() { return this.addon; diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java index d84f819330..f2f3314283 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java @@ -18,15 +18,20 @@ import java.util.function.Consumer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.minecraft.network.PacketByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.Packet; +import net.minecraft.server.command.DebugConfigCommand; import net.minecraft.server.network.ServerPlayerConfigurationTask; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; @@ -36,10 +41,13 @@ * Also see NetworkingConfigurationClientTest. */ public class NetworkingConfigurationTest implements ModInitializer { + private static final Logger LOGGER = LoggerFactory.getLogger(NetworkingConfigurationTest.class); + @Override public void onInitialize() { PayloadTypeRegistry.configurationS2C().register(ConfigurationPacket.ID, ConfigurationPacket.CODEC); PayloadTypeRegistry.configurationC2S().register(ConfigurationCompletePacket.ID, ConfigurationCompletePacket.CODEC); + PayloadTypeRegistry.configurationC2S().register(ConfigurationStartPacket.ID, ConfigurationStartPacket.CODEC); ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { // You must check to see if the client can handle your config task @@ -54,6 +62,13 @@ public void onInitialize() { ServerConfigurationNetworking.registerGlobalReceiver(ConfigurationCompletePacket.ID, (packet, context) -> { context.networkHandler().completeTask(TestConfigurationTask.KEY); }); + + ServerConfigurationNetworking.registerGlobalReceiver(ConfigurationStartPacket.ID, (packet, context) -> { + LOGGER.info("Received configuration start packet from client"); + }); + + // Enable the vanilla debugconfig command + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> DebugConfigCommand.register(dispatcher)); } public record TestConfigurationTask(String data) implements ServerPlayerConfigurationTask { @@ -102,4 +117,18 @@ public Id getId() { return ID; } } + + public static class ConfigurationStartPacket implements CustomPayload { + public static final ConfigurationStartPacket INSTANCE = new ConfigurationStartPacket(); + public static final CustomPayload.Id ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "configure_start")); + public static final PacketCodec CODEC = PacketCodec.unit(INSTANCE); + + private ConfigurationStartPacket() { + } + + @Override + public Id getId() { + return ID; + } + } } 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 01055ab12c..dd8a243b26 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 @@ -16,11 +16,17 @@ package net.fabricmc.fabric.test.networking.client.configuration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; import net.fabricmc.fabric.test.networking.configuration.NetworkingConfigurationTest; public class NetworkingConfigurationClientTest implements ClientModInitializer { + private static final Logger LOGGER = LoggerFactory.getLogger(NetworkingConfigurationTest.class); + @Override public void onInitializeClient() { ClientConfigurationNetworking.registerGlobalReceiver(NetworkingConfigurationTest.ConfigurationPacket.ID, (packet, context) -> { @@ -29,5 +35,10 @@ public void onInitializeClient() { // Respond back to the server that the task is complete context.responseSender().sendPacket(NetworkingConfigurationTest.ConfigurationCompletePacket.INSTANCE); }); + + ClientConfigurationConnectionEvents.START.register((handler, client) -> { + LOGGER.info("Sending configuration start packet to server"); + ClientConfigurationNetworking.send(NetworkingConfigurationTest.ConfigurationStartPacket.INSTANCE); + }); } }