Skip to content

Commit

Permalink
Add MinecraftClient/Server instances to networking contexts (#3850)
Browse files Browse the repository at this point in the history
* change: add MinecraftClient/Server instances to networking contexts

Signed-off-by: Octol1ttle <[email protected]>

* style: optimize imports

Signed-off-by: Octol1ttle <[email protected]>

* change: use client instance from context in FabricRegistryClientInit

Signed-off-by: Octol1ttle <[email protected]>

* fix: remove unused import <_<

Signed-off-by: Octol1ttle <[email protected]>

* fix: update Javadocs in Networking classes

Signed-off-by: Octol1ttle <[email protected]>

---------

Signed-off-by: Octol1ttle <[email protected]>
  • Loading branch information
Octol1ttle authored and Su5eD committed Jul 4, 2024
1 parent 3f9c62b commit e5a91b9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -207,10 +208,11 @@ public interface ConfigurationPayloadHandler<T extends CustomPacketPayload> {
* <p>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}.
*
* <p>An example usage of this is to display an overlay message:
* <p>An example usage of this:
* <pre>{@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) -> {
*
* });
* }</pre>
*
Expand All @@ -223,6 +225,11 @@ public interface ConfigurationPayloadHandler<T extends CustomPacketPayload> {

@ApiStatus.NonExtendable
public interface Context {
/**
* @return The MinecraftClient instance
*/
Minecraft client();

/**
* @return The packet sender
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ public interface PlayPayloadHandler<T extends CustomPacketPayload> {
*
* <p>An example usage of this is to display an overlay message:
* <pre>{@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);
* });
* }</pre>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ public interface ConfigurationPacketHandler<T extends CustomPacketPayload> {
*
* <p>An example usage of this:
* <pre>{@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) -> {
*
* });
* }</pre>
Expand All @@ -267,6 +267,11 @@ public interface ConfigurationPacketHandler<T extends CustomPacketPayload> {

@ApiStatus.NonExtendable
public interface Context {
/**
* @return The MinecraftServer instance
*/
MinecraftServer server();

/**
* @return The ServerConfigurationNetworkHandler instance
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -307,13 +308,12 @@ public interface PlayPayloadHandler<T extends CustomPacketPayload> {
* <p>An example usage of this is to create an explosion where the player is looking:
* <pre>{@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());
* });
* }</pre>
*
* <p>The server and the network handler can be accessed via {@link ServerPlayer#server}
* and {@link ServerPlayer#connection}, respectively.
* <p>The network handler can be accessed via {@link ServerPlayer#connection}.
*
* @param payload the packet payload
* @param context the play networking context
Expand All @@ -324,6 +324,11 @@ public interface PlayPayloadHandler<T extends CustomPacketPayload> {

@ApiStatus.NonExtendable
public interface Context {
/**
* @return The MinecraftServer instance
*/
MinecraftServer server();

/**
* @return The player that received the packet
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e5a91b9

Please sign in to comment.