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 3c8faedd0..4bf48a994 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 @@ -22,6 +22,7 @@ import java.util.Set; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import net.minecraft.client.MinecraftClient; import net.minecraft.network.packet.CustomPayload; import net.minecraft.util.Identifier; import net.minecraft.util.thread.ThreadExecutor; @@ -257,10 +258,11 @@ public final class ClientConfigurationNetworking { *

Unlike {@link ClientPlayNetworking.PlayPayloadHandler} this method is executed on {@linkplain io.netty.channel.EventLoop netty's event loops}. * Modification to the game should be {@linkplain ThreadExecutor#submit(Runnable) scheduled}. * - *

An example usage of this is to display an overlay message: + *

An example usage of this: *

{@code
-		 * // See FabricPacket for creating the packet
-		 * ClientConfigurationNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (packet, responseSender) -> {
+		 * // use PayloadTypeRegistry for registering the payload
+		 * ClientConfigurationNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (payload, context) -> {
+		 *
 		 * });
 		 * }
* @@ -273,6 +275,11 @@ public final class ClientConfigurationNetworking { @ApiStatus.NonExtendable public interface Context { + /** + * @return The MinecraftClient instance + */ + MinecraftClient client(); + /** * @return The packet sender */ diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java index 7601a6f28..a0ceaec56 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java @@ -266,9 +266,9 @@ public final class ClientPlayNetworking { * *

An example usage of this is to display an overlay message: *

{@code
-		 * // See FabricPacket for creating the payload
-		 * ClientPlayNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (player, payload, responseSender) -> {
-		 * 	MinecraftClient.getInstance().inGameHud.setOverlayMessage(payload.message(), true);
+		 * // use PayloadTypeRegistry for registering the payload
+		 * ClientPlayNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (payload, context) -> {
+		 * 	context.client().inGameHud.setOverlayMessage(payload.message(), true);
 		 * });
 		 * }
* 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 2d4fb7726..d391223bb 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 @@ -44,7 +44,7 @@ public final class ClientConfigurationNetworkAddon extends ClientCommonNetworkAd public ClientConfigurationNetworkAddon(ClientConfigurationNetworkHandler handler, MinecraftClient client) { super(ClientNetworkingImpl.CONFIGURATION, ((ClientCommonNetworkHandlerAccessor) handler).getConnection(), "ClientPlayNetworkAddon for " + ((ClientConfigurationNetworkHandlerAccessor) handler).getProfile().getName(), handler, client); - this.context = new ContextImpl(this); + this.context = new ContextImpl(client, this); // Must register pending channels via lateinit this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION); @@ -128,8 +128,9 @@ public final class ClientConfigurationNetworkAddon extends ClientCommonNetworkAd return (ChannelInfoHolder) ((ClientCommonNetworkHandlerAccessor) handler).getConnection(); } - private record ContextImpl(PacketSender responseSender) implements ClientConfigurationNetworking.Context { + private record ContextImpl(MinecraftClient client, PacketSender responseSender) implements ClientConfigurationNetworking.Context { private ContextImpl { + Objects.requireNonNull(client, "client"); Objects.requireNonNull(responseSender, "responseSender"); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java index a83495ca3..7f6c36cef 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java @@ -252,8 +252,8 @@ public final class ServerConfigurationNetworking { * *

An example usage of this: *

{@code
-		 * // See FabricPacket for creating the packet
-		 * ServerConfigurationNetworking.registerReceiver(BOOM_PACKET_TYPE, (packet, responseSender) -> {
+		 * // use PayloadTypeRegistry for registering the payload
+		 * ServerConfigurationNetworking.registerReceiver(BOOM_PACKET_TYPE, (payload, context) -> {
 		 *
 		 * });
 		 * }
@@ -268,6 +268,11 @@ public final class ServerConfigurationNetworking { @ApiStatus.NonExtendable public interface Context { + /** + * @return The MinecraftServer instance + */ + MinecraftServer server(); + /** * @return The ServerConfigurationNetworkHandler instance */ diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java index addf7991a..23d415919 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java @@ -25,6 +25,7 @@ import org.jetbrains.annotations.Nullable; import net.minecraft.network.listener.ClientCommonPacketListener; import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.Packet; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; @@ -308,13 +309,12 @@ public final class ServerPlayNetworking { *

An example usage of this is to create an explosion where the player is looking: *

{@code
 		 * // use PayloadTypeRegistry for registering the payload
-		 * ServerPlayNetworking.registerReceiver(BoomPayload.ID, (payload, player, responseSender) -> {
-		 * 	ModPacketHandler.createExplosion(player, payload.fire());
+		 * ServerPlayNetworking.registerReceiver(BoomPayload.ID, (payload, context) -> {
+		 * 	ModPacketHandler.createExplosion(context.player(), payload.fire());
 		 * });
 		 * }
* - *

The server and the network handler can be accessed via {@link ServerPlayerEntity#server} - * and {@link ServerPlayerEntity#networkHandler}, respectively. + *

The network handler can be accessed via {@link ServerPlayerEntity#networkHandler}. * * @param payload the packet payload * @param context the play networking context @@ -325,6 +325,11 @@ public final class ServerPlayNetworking { @ApiStatus.NonExtendable public interface Context { + /** + * @return The MinecraftServer instance + */ + MinecraftServer server(); + /** * @return The player that received the packet */ diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java index 896825e53..c70e7dbdc 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java @@ -49,7 +49,7 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw super(ServerNetworkingImpl.CONFIGURATION, ((ServerCommonNetworkHandlerAccessor) handler).getConnection(), "ServerConfigurationNetworkAddon for " + handler.getDebugProfile().getName()); this.handler = handler; this.server = server; - this.context = new ContextImpl(handler, this); + this.context = new ContextImpl(server, handler, this); // Must register pending channels via lateinit this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION); @@ -180,8 +180,9 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw return (ChannelInfoHolder) ((ServerCommonNetworkHandlerAccessor) handler).getConnection(); } - private record ContextImpl(ServerConfigurationNetworkHandler networkHandler, PacketSender responseSender) implements ServerConfigurationNetworking.Context { + private record ContextImpl(MinecraftServer server, ServerConfigurationNetworkHandler networkHandler, PacketSender responseSender) implements ServerConfigurationNetworking.Context { private ContextImpl { + Objects.requireNonNull(server, "server"); Objects.requireNonNull(networkHandler, "networkHandler"); Objects.requireNonNull(responseSender, "responseSender"); } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java index 6f27923ad..2406133a0 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java @@ -48,7 +48,7 @@ public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon< super(ServerNetworkingImpl.PLAY, connection, "ServerPlayNetworkAddon for " + handler.player.getDisplayName()); this.handler = handler; this.server = server; - this.context = new ContextImpl(handler, this); + this.context = new ContextImpl(server, handler, this); // Must register pending channels via lateinit this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.PLAY); @@ -129,8 +129,9 @@ public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon< return NetworkingImpl.isReservedCommonChannel(channelName); } - private record ContextImpl(ServerPlayNetworkHandler handler, PacketSender responseSender) implements ServerPlayNetworking.Context { + private record ContextImpl(MinecraftServer server, ServerPlayNetworkHandler handler, PacketSender responseSender) implements ServerPlayNetworking.Context { private ContextImpl { + Objects.requireNonNull(server, "server"); Objects.requireNonNull(handler, "handler"); Objects.requireNonNull(responseSender, "responseSender"); } diff --git a/fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java b/fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java index f5a1a1a73..f9425016c 100644 --- a/fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java +++ b/fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java @@ -40,12 +40,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientConfigurationNetworkHandler; import net.minecraft.network.NetworkPhase; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.CustomPayload; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.minecraft.util.Identifier; @@ -130,8 +132,23 @@ public class CommonPacketTests { ClientNetworkingImpl.setClientConfigurationAddon(clientAddon); - clientContext = () -> packetSender; + clientContext = new ClientConfigurationNetworking.Context() { + @Override + public MinecraftClient client() { + return null; + } + + @Override + public PacketSender responseSender() { + return packetSender; + } + }; serverContext = new ServerConfigurationNetworking.Context() { + @Override + public MinecraftServer server() { + return null; + } + @Override public ServerConfigurationNetworkHandler networkHandler() { return serverNetworkHandler; diff --git a/fabric-registry-sync-v0/src/client/java/net/fabricmc/fabric/impl/client/registry/sync/FabricRegistryClientInit.java b/fabric-registry-sync-v0/src/client/java/net/fabricmc/fabric/impl/client/registry/sync/FabricRegistryClientInit.java index 5fe4a864f..6591b66d9 100644 --- a/fabric-registry-sync-v0/src/client/java/net/fabricmc/fabric/impl/client/registry/sync/FabricRegistryClientInit.java +++ b/fabric-registry-sync-v0/src/client/java/net/fabricmc/fabric/impl/client/registry/sync/FabricRegistryClientInit.java @@ -21,7 +21,6 @@ import java.util.concurrent.CompletionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; import net.fabricmc.api.ClientModInitializer; @@ -41,13 +40,11 @@ public class FabricRegistryClientInit implements ClientModInitializer { private void registerSyncPacketReceiver(RegistryPacketHandler packetHandler) { ClientConfigurationNetworking.registerGlobalReceiver(packetHandler.getPacketId(), (payload, context) -> { - MinecraftClient client = MinecraftClient.getInstance(); - - RegistrySyncManager.receivePacket(client, packetHandler, payload, RegistrySyncManager.DEBUG || !client.isInSingleplayer()) + RegistrySyncManager.receivePacket(context.client(), packetHandler, payload, RegistrySyncManager.DEBUG || !context.client().isInSingleplayer()) .whenComplete((complete, throwable) -> { if (throwable != null) { LOGGER.error("Registry remapping failed!", throwable); - client.execute(() -> context.responseSender().disconnect(getText(throwable))); + context.client().execute(() -> context.responseSender().disconnect(getText(throwable))); return; }