Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public default void sendToDimension(final ResourceKey<Level> 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);
}
Expand Down
58 changes: 53 additions & 5 deletions src/main/java/com/ldtteam/common/network/PlayMessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@
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<T extends AbstractUnsidedPlayMessage>(ResourceLocation id,
FriendlyByteBuf.Reader<T> messageFactory,
BiFunction<FriendlyByteBuf, PlayMessageType<T>, T> messageFactory,
@Nullable Consumer<IDirectionAwarePayloadHandlerBuilder<T, IPlayPayloadHandler<T>>> payloadHandler)
{
/**
* Creates type for Server (sender) -> Client (receiver) message
*/
public static <T extends AbstractClientPlayMessage> PlayMessageType<T> forClient(final String modId,
final String messageName,
final FriendlyByteBuf.Reader<T> messageFactory)
final BiFunction<FriendlyByteBuf, PlayMessageType<T>, T> messageFactory)
{
return new PlayMessageType<T>(new ResourceLocation(modId, messageName), messageFactory, handlers -> {
handlers.client((payload, context) -> payload.onExecute(context, ensureClientPlayer(context, payload)));
Expand All @@ -35,7 +36,7 @@ public static <T extends AbstractClientPlayMessage> PlayMessageType<T> forClient
*/
public static <T extends AbstractServerPlayMessage> PlayMessageType<T> forServer(final String modId,
final String messageName,
final FriendlyByteBuf.Reader<T> messageFactory)
final BiFunction<FriendlyByteBuf, PlayMessageType<T>, T> messageFactory)
{
return new PlayMessageType<T>(new ResourceLocation(modId, messageName), messageFactory, handlers -> {
handlers.server((payload, context) -> payload.onExecute(context, ensureServerPlayer(context, payload)));
Expand All @@ -47,7 +48,7 @@ public static <T extends AbstractServerPlayMessage> PlayMessageType<T> forServer
*/
public static <T extends AbstractPlayMessage> PlayMessageType<T> forBothSides(final String modId,
final String messageName,
final FriendlyByteBuf.Reader<T> messageFactory)
final BiFunction<FriendlyByteBuf, PlayMessageType<T>, T> messageFactory)
{
return new PlayMessageType<T>(new ResourceLocation(modId, messageName), messageFactory, handlers -> {
handlers.client((payload, context) -> {
Expand All @@ -57,6 +58,48 @@ public static <T extends AbstractPlayMessage> PlayMessageType<T> forBothSides(fi
});
});
}
/**
* Creates type for Server (sender) -> Client (receiver) message.
* Allows null player argument
*/
public static <T extends AbstractClientPlayMessage> PlayMessageType<T> forClientAllowNullPlayer(final String modId,
final String messageName,
final BiFunction<FriendlyByteBuf, PlayMessageType<T>, T> messageFactory)
{
return new PlayMessageType<T>(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 <T extends AbstractServerPlayMessage> PlayMessageType<T> forServerAllowNullPlayer(final String modId,
final String messageName,
final BiFunction<FriendlyByteBuf, PlayMessageType<T>, T> messageFactory)
{
return new PlayMessageType<T>(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 <T extends AbstractPlayMessage> PlayMessageType<T> forBothSidesAllowNullPlayer(final String modId,
final String messageName,
final BiFunction<FriendlyByteBuf, PlayMessageType<T>, T> messageFactory)
{
return new PlayMessageType<T>(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:
Expand All @@ -76,7 +119,7 @@ public static <T extends AbstractPlayMessage> PlayMessageType<T> 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)
Expand All @@ -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)
Expand Down