From 0c07abff29354226b68d9ce829f0941f7040650b Mon Sep 17 00:00:00 2001 From: Nightenom <17338378+Nightenom@users.noreply.github.com> Date: Thu, 8 Feb 2024 18:04:37 +0100 Subject: [PATCH] Make network abstract better --- .../network/AbstractClientPlayMessage.java | 5 +- .../common/network/AbstractPlayMessage.java | 5 +- .../network/AbstractServerPlayMessage.java | 5 +- .../network/IClientboundDistributor.java | 2 +- .../common/network/PlayMessageType.java | 58 +++++++++++++++++-- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/ldtteam/common/network/AbstractClientPlayMessage.java b/src/main/java/com/ldtteam/common/network/AbstractClientPlayMessage.java index a6f97e0f..5b2c363d 100644 --- a/src/main/java/com/ldtteam/common/network/AbstractClientPlayMessage.java +++ b/src/main/java/com/ldtteam/common/network/AbstractClientPlayMessage.java @@ -10,6 +10,8 @@ public abstract class AbstractClientPlayMessage extends AbstractUnsidedPlayMessage implements IClientboundDistributor { /** + * This constructor should be called from message call site, ie. the code where you instantiate the message to send it to client + * * @param type message type */ public AbstractClientPlayMessage(final PlayMessageType type) @@ -22,8 +24,9 @@ public AbstractClientPlayMessage(final PlayMessageType type) * * @param buf received network payload * @param type message type + * @apiNote you can keep this protected to reduce visibility */ - public AbstractClientPlayMessage(final FriendlyByteBuf buf, final PlayMessageType type) + protected AbstractClientPlayMessage(final FriendlyByteBuf buf, final PlayMessageType type) { super(type); } diff --git a/src/main/java/com/ldtteam/common/network/AbstractPlayMessage.java b/src/main/java/com/ldtteam/common/network/AbstractPlayMessage.java index 1d9e0130..a81b9b08 100644 --- a/src/main/java/com/ldtteam/common/network/AbstractPlayMessage.java +++ b/src/main/java/com/ldtteam/common/network/AbstractPlayMessage.java @@ -13,6 +13,8 @@ public abstract class AbstractPlayMessage extends AbstractUnsidedPlayMessage imp IServerboundDistributor { /** + * This constructor should be called from message call site, ie. the code where you instantiate the message to send it to the other side + * * @param type message type */ public AbstractPlayMessage(final PlayMessageType type) @@ -25,8 +27,9 @@ public AbstractPlayMessage(final PlayMessageType type) * * @param buf received network payload * @param type message type + * @apiNote you can keep this protected to reduce visibility */ - public AbstractPlayMessage(final FriendlyByteBuf buf, final PlayMessageType type) + protected AbstractPlayMessage(final FriendlyByteBuf buf, final PlayMessageType type) { super(type); } diff --git a/src/main/java/com/ldtteam/common/network/AbstractServerPlayMessage.java b/src/main/java/com/ldtteam/common/network/AbstractServerPlayMessage.java index 678d4b7f..8484494c 100644 --- a/src/main/java/com/ldtteam/common/network/AbstractServerPlayMessage.java +++ b/src/main/java/com/ldtteam/common/network/AbstractServerPlayMessage.java @@ -10,6 +10,8 @@ public abstract class AbstractServerPlayMessage extends AbstractUnsidedPlayMessage implements IServerboundDistributor { /** + * This constructor should be called from message call site, ie. the code where you instantiate the message to send it to server + * * @param type message type */ public AbstractServerPlayMessage(final PlayMessageType type) @@ -22,8 +24,9 @@ public AbstractServerPlayMessage(final PlayMessageType type) * * @param buf received network payload * @param type message type + * @apiNote you can keep this protected to reduce visibility */ - public AbstractServerPlayMessage(final FriendlyByteBuf buf, final PlayMessageType type) + protected AbstractServerPlayMessage(final FriendlyByteBuf buf, final PlayMessageType type) { super(type); } diff --git a/src/main/java/com/ldtteam/common/network/IClientboundDistributor.java b/src/main/java/com/ldtteam/common/network/IClientboundDistributor.java index 488b6346..2bf3a7b8 100644 --- a/src/main/java/com/ldtteam/common/network/IClientboundDistributor.java +++ b/src/main/java/com/ldtteam/common/network/IClientboundDistributor.java @@ -36,7 +36,7 @@ public default void sendToDimension(final ResourceKey dimensionKey) PacketDistributor.DIMENSION.with(dimensionKey).send(this); } - public default void sendToSpherePoint(final TargetPoint point) + public default void sendToTargetPoint(final TargetPoint point) { PacketDistributor.NEAR.with(point).send(this); } diff --git a/src/main/java/com/ldtteam/common/network/PlayMessageType.java b/src/main/java/com/ldtteam/common/network/PlayMessageType.java index 585ac6db..3d8d03f6 100644 --- a/src/main/java/com/ldtteam/common/network/PlayMessageType.java +++ b/src/main/java/com/ldtteam/common/network/PlayMessageType.java @@ -9,13 +9,14 @@ import net.neoforged.neoforge.network.registration.IDirectionAwarePayloadHandlerBuilder; import net.neoforged.neoforge.network.registration.IPayloadRegistrar; import org.jetbrains.annotations.Nullable; +import java.util.function.BiFunction; import java.util.function.Consumer; /** * Class to connect message type with proper sided registration. */ public record PlayMessageType(ResourceLocation id, - FriendlyByteBuf.Reader messageFactory, + BiFunction, T> messageFactory, @Nullable Consumer>> payloadHandler) { /** @@ -23,7 +24,7 @@ public record PlayMessageType(ResourceLoca */ public static PlayMessageType forClient(final String modId, final String messageName, - final FriendlyByteBuf.Reader messageFactory) + final BiFunction, T> messageFactory) { return new PlayMessageType(new ResourceLocation(modId, messageName), messageFactory, handlers -> { handlers.client((payload, context) -> payload.onExecute(context, ensureClientPlayer(context, payload))); @@ -35,7 +36,7 @@ public static PlayMessageType forClient */ public static PlayMessageType forServer(final String modId, final String messageName, - final FriendlyByteBuf.Reader messageFactory) + final BiFunction, T> messageFactory) { return new PlayMessageType(new ResourceLocation(modId, messageName), messageFactory, handlers -> { handlers.server((payload, context) -> payload.onExecute(context, ensureServerPlayer(context, payload))); @@ -47,7 +48,7 @@ public static PlayMessageType forServer */ public static PlayMessageType forBothSides(final String modId, final String messageName, - final FriendlyByteBuf.Reader messageFactory) + final BiFunction, T> messageFactory) { return new PlayMessageType(new ResourceLocation(modId, messageName), messageFactory, handlers -> { handlers.client((payload, context) -> { @@ -57,6 +58,48 @@ public static PlayMessageType forBothSides(fi }); }); } + /** + * Creates type for Server (sender) -> Client (receiver) message. + * Allows null player argument + */ + public static PlayMessageType forClientAllowNullPlayer(final String modId, + final String messageName, + final BiFunction, T> messageFactory) + { + return new PlayMessageType(new ResourceLocation(modId, messageName), messageFactory, handlers -> { + handlers.client((payload, context) -> payload.onExecute(context, context.player().orElse(null))); + }); + } + + /** + * Creates type for Client (sender) -> Server (receiver) message. + * Allows null player argument + */ + public static PlayMessageType forServerAllowNullPlayer(final String modId, + final String messageName, + final BiFunction, T> messageFactory) + { + return new PlayMessageType(new ResourceLocation(modId, messageName), messageFactory, handlers -> { + handlers.server((payload, context) -> payload.onExecute(context, getServerPlayer(context))); + }); + } + + /** + * Creates type for bidirectional message. + * Allows null player argument + */ + public static PlayMessageType forBothSidesAllowNullPlayer(final String modId, + final String messageName, + final BiFunction, T> messageFactory) + { + return new PlayMessageType(new ResourceLocation(modId, messageName), messageFactory, handlers -> { + handlers.client((payload, context) -> { + payload.onClientExecute(context, context.player().orElse(null)); + }).server((payload, context) -> { + payload.onServerExecute(context, getServerPlayer(context)); + }); + }); + } /** * Call this in following code: @@ -76,7 +119,7 @@ public static PlayMessageType forBothSides(fi */ public void register(final IPayloadRegistrar registry) { - registry.play(id, messageFactory, payloadHandler); + registry.play(id, buf -> messageFactory.apply(buf, this), payloadHandler); } private static Player ensureClientPlayer(final PlayPayloadContext context, final AbstractUnsidedPlayMessage payload) @@ -91,6 +134,11 @@ private static ServerPlayer ensureServerPlayer(final PlayPayloadContext context, .orElseThrow(() -> wrongPlayerException(context, context.player().orElse(null), payload)); } + private static ServerPlayer getServerPlayer(final PlayPayloadContext context) + { + return context.player().map(player -> player instanceof final ServerPlayer serverPlayer ? serverPlayer : null).orElse(null); + } + private static RuntimeException wrongPlayerException(final PlayPayloadContext context, final Player player, final AbstractUnsidedPlayMessage payload)