mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-24 16:48:02 -05:00
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>
(cherry picked from commit ab7edbac5e
)
This commit is contained in:
parent
4179102586
commit
a916f7c5d0
9 changed files with 58 additions and 24 deletions
|
@ -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 {
|
|||
* <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}.
|
||||
*
|
||||
* <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>
|
||||
*
|
||||
|
@ -273,6 +275,11 @@ public final class ClientConfigurationNetworking {
|
|||
|
||||
@ApiStatus.NonExtendable
|
||||
public interface Context {
|
||||
/**
|
||||
* @return The MinecraftClient instance
|
||||
*/
|
||||
MinecraftClient client();
|
||||
|
||||
/**
|
||||
* @return The packet sender
|
||||
*/
|
||||
|
|
|
@ -266,9 +266,9 @@ public final class ClientPlayNetworking {
|
|||
*
|
||||
* <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>
|
||||
*
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -252,8 +252,8 @@ public final class ServerConfigurationNetworking {
|
|||
*
|
||||
* <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>
|
||||
|
@ -268,6 +268,11 @@ public final class ServerConfigurationNetworking {
|
|||
|
||||
@ApiStatus.NonExtendable
|
||||
public interface Context {
|
||||
/**
|
||||
* @return The MinecraftServer instance
|
||||
*/
|
||||
MinecraftServer server();
|
||||
|
||||
/**
|
||||
* @return The ServerConfigurationNetworkHandler instance
|
||||
*/
|
||||
|
|
|
@ -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 {
|
|||
* <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 ServerPlayerEntity#server}
|
||||
* and {@link ServerPlayerEntity#networkHandler}, respectively.
|
||||
* <p>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
|
||||
*/
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 <T extends RegistryPacketHandler.RegistrySyncPayload> void registerSyncPacketReceiver(RegistryPacketHandler<T> 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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue