Add MinecraftClient/Server instances to networking contexts (#3850)

* change: add MinecraftClient/Server instances to networking contexts

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* style: optimize imports

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* change: use client instance from context in FabricRegistryClientInit

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* fix: remove unused import <_<

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* fix: update Javadocs in Networking classes

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2024-06-17 22:10:28 +05:00 committed by GitHub
parent 48fb158648
commit ab7edbac5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 58 additions and 24 deletions

View file

@ -22,6 +22,7 @@ import java.util.Set;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import net.minecraft.client.MinecraftClient;
import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.thread.ThreadExecutor; import net.minecraft.util.thread.ThreadExecutor;
@ -257,10 +258,11 @@ public final class ClientConfigurationNetworking {
* <p>Unlike {@link ClientPlayNetworking.PlayPayloadHandler} this method is executed on {@linkplain io.netty.channel.EventLoop netty's event loops}. * <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 ThreadExecutor#submit(Runnable) scheduled}. * Modification to the game should be {@linkplain ThreadExecutor#submit(Runnable) scheduled}.
* *
* <p>An example usage of this is to display an overlay message: * <p>An example usage of this:
* <pre>{@code * <pre>{@code
* // See FabricPacket for creating the packet * // use PayloadTypeRegistry for registering the payload
* ClientConfigurationNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (packet, responseSender) -> { * ClientConfigurationNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (payload, context) -> {
*
* }); * });
* }</pre> * }</pre>
* *
@ -273,6 +275,11 @@ public final class ClientConfigurationNetworking {
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
public interface Context { public interface Context {
/**
* @return The MinecraftClient instance
*/
MinecraftClient client();
/** /**
* @return The packet sender * @return The packet sender
*/ */

View file

@ -266,9 +266,9 @@ public final class ClientPlayNetworking {
* *
* <p>An example usage of this is to display an overlay message: * <p>An example usage of this is to display an overlay message:
* <pre>{@code * <pre>{@code
* // See FabricPacket for creating the payload * // use PayloadTypeRegistry for registering the payload
* ClientPlayNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (player, payload, responseSender) -> { * ClientPlayNetworking.registerReceiver(OVERLAY_PACKET_TYPE, (payload, context) -> {
* MinecraftClient.getInstance().inGameHud.setOverlayMessage(payload.message(), true); * context.client().inGameHud.setOverlayMessage(payload.message(), true);
* }); * });
* }</pre> * }</pre>
* *

View file

@ -44,7 +44,7 @@ public final class ClientConfigurationNetworkAddon extends ClientCommonNetworkAd
public ClientConfigurationNetworkAddon(ClientConfigurationNetworkHandler handler, MinecraftClient client) { public ClientConfigurationNetworkAddon(ClientConfigurationNetworkHandler handler, MinecraftClient client) {
super(ClientNetworkingImpl.CONFIGURATION, ((ClientCommonNetworkHandlerAccessor) handler).getConnection(), "ClientPlayNetworkAddon for " + ((ClientConfigurationNetworkHandlerAccessor) handler).getProfile().getName(), handler, 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 // Must register pending channels via lateinit
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION); this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION);
@ -128,8 +128,9 @@ public final class ClientConfigurationNetworkAddon extends ClientCommonNetworkAd
return (ChannelInfoHolder) ((ClientCommonNetworkHandlerAccessor) handler).getConnection(); 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 { private ContextImpl {
Objects.requireNonNull(client, "client");
Objects.requireNonNull(responseSender, "responseSender"); Objects.requireNonNull(responseSender, "responseSender");
} }
} }

View file

@ -252,8 +252,8 @@ public final class ServerConfigurationNetworking {
* *
* <p>An example usage of this: * <p>An example usage of this:
* <pre>{@code * <pre>{@code
* // See FabricPacket for creating the packet * // use PayloadTypeRegistry for registering the payload
* ServerConfigurationNetworking.registerReceiver(BOOM_PACKET_TYPE, (packet, responseSender) -> { * ServerConfigurationNetworking.registerReceiver(BOOM_PACKET_TYPE, (payload, context) -> {
* *
* }); * });
* }</pre> * }</pre>
@ -268,6 +268,11 @@ public final class ServerConfigurationNetworking {
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
public interface Context { public interface Context {
/**
* @return The MinecraftServer instance
*/
MinecraftServer server();
/** /**
* @return The ServerConfigurationNetworkHandler instance * @return The ServerConfigurationNetworkHandler instance
*/ */

View file

@ -25,6 +25,7 @@ import org.jetbrains.annotations.Nullable;
import net.minecraft.network.listener.ClientCommonPacketListener; import net.minecraft.network.listener.ClientCommonPacketListener;
import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
@ -308,13 +309,12 @@ public final class ServerPlayNetworking {
* <p>An example usage of this is to create an explosion where the player is looking: * <p>An example usage of this is to create an explosion where the player is looking:
* <pre>{@code * <pre>{@code
* // use PayloadTypeRegistry for registering the payload * // use PayloadTypeRegistry for registering the payload
* ServerPlayNetworking.registerReceiver(BoomPayload.ID, (payload, player, responseSender) -> { * ServerPlayNetworking.registerReceiver(BoomPayload.ID, (payload, context) -> {
* ModPacketHandler.createExplosion(player, payload.fire()); * ModPacketHandler.createExplosion(context.player(), payload.fire());
* }); * });
* }</pre> * }</pre>
* *
* <p>The server and the network handler can be accessed via {@link ServerPlayerEntity#server} * <p>The network handler can be accessed via {@link ServerPlayerEntity#networkHandler}.
* and {@link ServerPlayerEntity#networkHandler}, respectively.
* *
* @param payload the packet payload * @param payload the packet payload
* @param context the play networking context * @param context the play networking context
@ -325,6 +325,11 @@ public final class ServerPlayNetworking {
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
public interface Context { public interface Context {
/**
* @return The MinecraftServer instance
*/
MinecraftServer server();
/** /**
* @return The player that received the packet * @return The player that received the packet
*/ */

View file

@ -49,7 +49,7 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw
super(ServerNetworkingImpl.CONFIGURATION, ((ServerCommonNetworkHandlerAccessor) handler).getConnection(), "ServerConfigurationNetworkAddon for " + handler.getDebugProfile().getName()); super(ServerNetworkingImpl.CONFIGURATION, ((ServerCommonNetworkHandlerAccessor) handler).getConnection(), "ServerConfigurationNetworkAddon for " + handler.getDebugProfile().getName());
this.handler = handler; this.handler = handler;
this.server = server; this.server = server;
this.context = new ContextImpl(handler, this); this.context = new ContextImpl(server, handler, this);
// Must register pending channels via lateinit // Must register pending channels via lateinit
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION); this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION);
@ -180,8 +180,9 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw
return (ChannelInfoHolder) ((ServerCommonNetworkHandlerAccessor) handler).getConnection(); 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 { private ContextImpl {
Objects.requireNonNull(server, "server");
Objects.requireNonNull(networkHandler, "networkHandler"); Objects.requireNonNull(networkHandler, "networkHandler");
Objects.requireNonNull(responseSender, "responseSender"); Objects.requireNonNull(responseSender, "responseSender");
} }

View file

@ -48,7 +48,7 @@ public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon<
super(ServerNetworkingImpl.PLAY, connection, "ServerPlayNetworkAddon for " + handler.player.getDisplayName()); super(ServerNetworkingImpl.PLAY, connection, "ServerPlayNetworkAddon for " + handler.player.getDisplayName());
this.handler = handler; this.handler = handler;
this.server = server; this.server = server;
this.context = new ContextImpl(handler, this); this.context = new ContextImpl(server, handler, this);
// Must register pending channels via lateinit // Must register pending channels via lateinit
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.PLAY); this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.PLAY);
@ -129,8 +129,9 @@ public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon<
return NetworkingImpl.isReservedCommonChannel(channelName); 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 { private ContextImpl {
Objects.requireNonNull(server, "server");
Objects.requireNonNull(handler, "handler"); Objects.requireNonNull(handler, "handler");
Objects.requireNonNull(responseSender, "responseSender"); Objects.requireNonNull(responseSender, "responseSender");
} }

View file

@ -40,12 +40,14 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientConfigurationNetworkHandler; import net.minecraft.client.network.ClientConfigurationNetworkHandler;
import net.minecraft.network.NetworkPhase; import net.minecraft.network.NetworkPhase;
import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.CustomPayload;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
@ -130,8 +132,23 @@ public class CommonPacketTests {
ClientNetworkingImpl.setClientConfigurationAddon(clientAddon); 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() { serverContext = new ServerConfigurationNetworking.Context() {
@Override
public MinecraftServer server() {
return null;
}
@Override @Override
public ServerConfigurationNetworkHandler networkHandler() { public ServerConfigurationNetworkHandler networkHandler() {
return serverNetworkHandler; return serverNetworkHandler;

View file

@ -21,7 +21,6 @@ import java.util.concurrent.CompletionException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ClientModInitializer;
@ -41,13 +40,11 @@ public class FabricRegistryClientInit implements ClientModInitializer {
private <T extends RegistryPacketHandler.RegistrySyncPayload> void registerSyncPacketReceiver(RegistryPacketHandler<T> packetHandler) { private <T extends RegistryPacketHandler.RegistrySyncPayload> void registerSyncPacketReceiver(RegistryPacketHandler<T> packetHandler) {
ClientConfigurationNetworking.registerGlobalReceiver(packetHandler.getPacketId(), (payload, context) -> { ClientConfigurationNetworking.registerGlobalReceiver(packetHandler.getPacketId(), (payload, context) -> {
MinecraftClient client = MinecraftClient.getInstance(); RegistrySyncManager.receivePacket(context.client(), packetHandler, payload, RegistrySyncManager.DEBUG || !context.client().isInSingleplayer())
RegistrySyncManager.receivePacket(client, packetHandler, payload, RegistrySyncManager.DEBUG || !client.isInSingleplayer())
.whenComplete((complete, throwable) -> { .whenComplete((complete, throwable) -> {
if (throwable != null) { if (throwable != null) {
LOGGER.error("Registry remapping failed!", throwable); LOGGER.error("Registry remapping failed!", throwable);
client.execute(() -> context.responseSender().disconnect(getText(throwable))); context.client().execute(() -> context.responseSender().disconnect(getText(throwable)));
return; return;
} }