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 963c673ff..1628e75cd 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 @@ -24,6 +24,7 @@ import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.client.Minecraft; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.thread.BlockableEventLoop; @@ -207,10 +208,11 @@ public interface ConfigurationPayloadHandler { *

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 BlockableEventLoop#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) -> {
+		 *
 		 * });
 		 * }
* @@ -223,6 +225,11 @@ public interface ConfigurationPayloadHandler { @ApiStatus.NonExtendable public interface Context { + /** + * @return The MinecraftClient instance + */ + Minecraft 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 fcf409a5b..0039865dc 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 @@ -235,9 +235,9 @@ public interface PlayPayloadHandler { * *

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/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 b931eeb1f..0a1c53924 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 @@ -251,8 +251,8 @@ public interface ConfigurationPacketHandler { * *

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) -> {
 		 *
 		 * });
 		 * }
@@ -267,6 +267,11 @@ public interface ConfigurationPacketHandler { @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 d6f79a9de..ceca9db27 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 @@ -26,6 +26,7 @@ import net.minecraft.network.protocol.common.ClientCommonPacketListener; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; import org.sinytra.fabric.networking_api.server.NeoServerPlayNetworking; @@ -307,13 +308,12 @@ public interface PlayPayloadHandler { *

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 ServerPlayer#server} - * and {@link ServerPlayer#connection}, respectively. + *

The network handler can be accessed via {@link ServerPlayer#connection}. * * @param payload the packet payload * @param context the play networking context @@ -324,6 +324,11 @@ public interface PlayPayloadHandler { @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/org/sinytra/fabric/networking_api/client/NeoClientConfigurationNetworking.java b/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/client/NeoClientConfigurationNetworking.java index 8ae5930c8..3e11a5812 100644 --- a/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/client/NeoClientConfigurationNetworking.java +++ b/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/client/NeoClientConfigurationNetworking.java @@ -3,13 +3,13 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl; +import net.minecraft.client.Minecraft; import net.minecraft.network.ConnectionProtocol; import net.minecraft.network.protocol.PacketFlow; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.neoforged.neoforge.common.extensions.ICommonPacketListener; import net.neoforged.neoforge.network.handling.IPayloadContext; -import net.neoforged.neoforge.network.registration.NetworkRegistry; import org.sinytra.fabric.networking_api.NeoCommonNetworking; import java.util.Objects; @@ -79,6 +79,11 @@ public static void setClientConfigurationAddon(ICommonPacketListener listener) { } private record ClientConfigNeoContextWrapper(IPayloadContext context) implements ClientConfigurationNetworking.Context { + @Override + public Minecraft client() { + return Minecraft.getInstance(); + } + @Override public PacketSender responseSender() { return new NeoClientPacketSender(context.connection()); diff --git a/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerConfigurationNetworking.java b/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerConfigurationNetworking.java index d9a49f4e2..75b4fc8ad 100644 --- a/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerConfigurationNetworking.java +++ b/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerConfigurationNetworking.java @@ -7,6 +7,7 @@ import net.minecraft.network.protocol.PacketFlow; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerConfigurationPacketListenerImpl; import net.neoforged.neoforge.network.handling.IPayloadContext; import net.neoforged.neoforge.network.registration.NetworkRegistry; @@ -55,6 +56,11 @@ public static PacketSender getSender(ServerConfigurationPacketListenerImpl handl } private record ServerConfigNeoContextWrapper(IPayloadContext context) implements ServerConfigurationNetworking.Context { + @Override + public MinecraftServer server() { + return networkHandler().server; + } + @Override public ServerConfigurationPacketListenerImpl networkHandler() { return (ServerConfigurationPacketListenerImpl) context.listener(); diff --git a/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerPlayNetworking.java b/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerPlayNetworking.java index e6ddc51bc..c3d67ee30 100644 --- a/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerPlayNetworking.java +++ b/fabric-networking-api-v1/src/main/java/org/sinytra/fabric/networking_api/server/NeoServerPlayNetworking.java @@ -8,6 +8,7 @@ import net.minecraft.network.protocol.PacketFlow; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; import net.neoforged.neoforge.network.handling.IPayloadContext; @@ -60,6 +61,11 @@ public static void onClientReady(ServerPlayer player) { } private record ServerNeoContextWrapper(IPayloadContext context) implements ServerPlayNetworking.Context { + @Override + public MinecraftServer server() { + return player().getServer(); + } + @Override public ServerPlayer player() { return (ServerPlayer) context.player();