Skip to content

Commit

Permalink
Configuration networking fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Jun 5, 2024
1 parent 98a7dbb commit 24c1c88
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public final class ClientConfigurationConnectionEvents {
/**
* Event indicating a connection entering the CONFIGURATION state, ready for registering channel handlers.
*
* <p>No packets should be sent when this event is invoked.
*
* @see ClientConfigurationNetworking#registerReceiver(CustomPayload.Id, ClientConfigurationNetworking.ConfigurationPayloadHandler)
*/
public static final Event<ClientConfigurationConnectionEvents.Init> INIT = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Init.class, callbacks -> (handler, client) -> {
Expand All @@ -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.
*
* <p>Packets may be sent during this event.
*/
public static final Event<ClientConfigurationConnectionEvents.Start> 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.
*
* <p>No packets should be sent when this event is invoked.
*/
public static final Event<ClientConfigurationConnectionEvents.Ready> READY = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Ready.class, callbacks -> (handler, client) -> {
for (ClientConfigurationConnectionEvents.Ready callback : callbacks) {
callback.onConfigurationReady(handler, client);
public static final Event<ClientConfigurationConnectionEvents.Complete> COMPLETE = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Complete.class, callbacks -> (handler, client) -> {
for (ClientConfigurationConnectionEvents.Complete callback : callbacks) {
callback.onConfigurationComplete(handler, client);
}
});

Expand All @@ -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<ClientConfigurationConnectionEvents.Ready> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -84,7 +89,8 @@ protected void invokeUnregisterEvent(List<Identifier> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public static void setClientPlayAddon(ClientPlayNetworkAddon addon) {
}

public static void setClientConfigurationAddon(ClientConfigurationNetworkAddon addon) {
assert addon == null || currentPlayAddon == null;
currentConfigurationAddon = addon;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -102,4 +117,18 @@ public Id<? extends CustomPayload> getId() {
return ID;
}
}

public static class ConfigurationStartPacket implements CustomPayload {
public static final ConfigurationStartPacket INSTANCE = new ConfigurationStartPacket();
public static final CustomPayload.Id<ConfigurationStartPacket> ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "configure_start"));
public static final PacketCodec<PacketByteBuf, ConfigurationStartPacket> CODEC = PacketCodec.unit(INSTANCE);

private ConfigurationStartPacket() {
}

@Override
public Id<? extends CustomPayload> getId() {
return ID;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> {
Expand All @@ -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);
});
}
}

0 comments on commit 24c1c88

Please sign in to comment.