Skip to content

Commit

Permalink
split type and receiver registration for clientbound packet
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jan 19, 2024
1 parent c4dda20 commit 85091b8
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 165 deletions.
28 changes: 0 additions & 28 deletions src/main/java/lol/bai/badpackets/api/C2SPacketReceiver.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/main/java/lol/bai/badpackets/api/PacketSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public interface PacketSender {
* <p>
* <b>Only available when on game.</b>
*
* @see PlayPackets#registerServerReceiver(ResourceLocation, ServerPlayPacketReceiver)
* @see PlayPackets#registerServerReceiver(CustomPacketPayload.Type, StreamCodec, ServerPlayPacketReceiver)
* @see PlayPackets#registerServerChannel(ResourceLocation, ServerPlayPacketReceiver)
* @see PlayPackets#registerServerChannel(CustomPacketPayload.Type, StreamCodec, ServerPlayPacketReceiver)
*/
@ApiSide.ClientOnly
static PacketSender c2s() {
Expand All @@ -39,8 +39,8 @@ static PacketSender c2s() {
*
* @param player the player that we want to send packets to.
*
* @see PlayPackets#registerClientReceiver(ResourceLocation, ClientPlayPacketReceiver)
* @see PlayPackets#registerClientReceiver(CustomPacketPayload.Type, StreamCodec, ClientPlayPacketReceiver)
* @see PlayPackets#registerClientChannel(ResourceLocation, ClientPlayPacketReceiver)
* @see PlayPackets#registerClientChannel(CustomPacketPayload.Type, StreamCodec, ClientPlayPacketReceiver)
*/
@ApiSide.ServerOnly
static PacketSender s2c(ServerPlayer player) {
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/lol/bai/badpackets/api/S2CPacketReceiver.java

This file was deleted.

50 changes: 40 additions & 10 deletions src/main/java/lol/bai/badpackets/api/config/ConfigPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static void registerTask(ResourceLocation id, ConfigTaskExecutor executor
*
* @see ServerConfigPacketReceiver#receive
*/
public static void registerServerReceiver(ResourceLocation id, ServerConfigPacketReceiver<FriendlyByteBuf> receiver) {
registerServerReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (server, handler, payload, responseSender, taskFinisher) ->
receiver.receive(server, handler, payload.buffer(), responseSender, taskFinisher));
public static void registerServerChannel(ResourceLocation id, ServerConfigPacketReceiver<FriendlyByteBuf> receiver) {
ChannelRegistry.CONFIG_C2S.registerCodec(id, UntypedPayload.codec(id));
ChannelRegistry.CONFIG_C2S.registerReceiver(id, (server, handler, payload, responseSender, taskFinisher) -> receiver.receive(server, handler, ((UntypedPayload) payload).buffer(), responseSender, taskFinisher));
}

/**
Expand All @@ -61,8 +61,9 @@ public static void registerServerReceiver(ResourceLocation id, ServerConfigPacke
* @see ServerConfigPacketReceiver#receive
*/
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerServerReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super FriendlyByteBuf, P> codec, ServerConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_C2S.register(type, codec, (ServerConfigPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerServerChannel(CustomPacketPayload.Type<P> type, StreamCodec<? super FriendlyByteBuf, P> codec, ServerConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_C2S.registerCodec(type.id(), codec);
ChannelRegistry.CONFIG_C2S.registerReceiver(type.id(), (ServerConfigPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand All @@ -80,6 +81,35 @@ public static void registerServerReadyCallback(ServerConfigPacketReadyCallback c
/**
* Register a server-to-client packet receiver.
* <p>
* This method needs to be called on <b>all sides</b>.
* <p>
* Register the receiver on <b>client side</b> with {@link #registerClientReceiver(ResourceLocation, ClientConfigPacketReceiver)}
*
* @param id the packet id
*/
public static void registerClientChannel(ResourceLocation id) {
ChannelRegistry.CONFIG_S2C.registerCodec(id, UntypedPayload.codec(id));
}

/**
* Register a server-to-client packet receiver.
* <p>
* This method needs to be called on <b>all sides</b>.
* <p>
* Register the receiver on <b>client side</b> with {@link #registerClientReceiver(CustomPacketPayload.Type, ClientConfigPacketReceiver)}
*
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
*/
public static <P extends CustomPacketPayload> void registerClientChannel(CustomPacketPayload.Type<P> type, StreamCodec<? super FriendlyByteBuf, P> codec) {
ChannelRegistry.CONFIG_S2C.registerCodec(type.id(), codec);
}

/**
* Register a server-to-client packet receiver.
* <p>
* The channel needs to be {@linkplain #registerClientChannel(ResourceLocation) registered} first.
* <p>
* Raw packet receiver is run on Netty event-loop. Read the buffer on it and run
* the operation on {@linkplain Minecraft#execute(Runnable) client thread}.
*
Expand All @@ -91,26 +121,26 @@ public static void registerServerReadyCallback(ServerConfigPacketReadyCallback c
*/
@ApiSide.ClientOnly
public static void registerClientReceiver(ResourceLocation id, ClientConfigPacketReceiver<FriendlyByteBuf> receiver) {
registerClientReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (client, handler, payload, responseSender) ->
receiver.receive(client, handler, payload.buffer(), responseSender));
ChannelRegistry.CONFIG_S2C.registerReceiver(id, (client, handler, payload, responseSender) -> receiver.receive(client, handler, ((UntypedPayload) payload).buffer(), responseSender));
}

/**
* Register a server-to-client packet receiver.
* <p>
* The channel needs to be {@linkplain #registerClientChannel(CustomPacketPayload.Type, StreamCodec) registered} first.
* <p>
* Typed packet receiver is run on the main client thread.
*
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
* @param receiver the receiver
*
* @see ClientConfigPacketReceiver#receive
* @see #disconnect
*/
@ApiSide.ClientOnly
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super FriendlyByteBuf, P> codec, ClientConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_S2C.register(type, codec, (ClientConfigPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, ClientConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_S2C.registerReceiver(type.id(), (ClientConfigPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand Down

This file was deleted.

54 changes: 42 additions & 12 deletions src/main/java/lol/bai/badpackets/api/play/PlayPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
public final class PlayPackets {

/**
* Register a client-to-server packet receiver.
* Register a client-to-server packet channel.
* <p>
* Raw packet receiver is run on Netty event-loop. Read the buffer on it and run
* the operation on {@linkplain MinecraftServer#execute(Runnable) server thread}.
*
* @param id the packet id
* @param receiver the receiver
*/
public static void registerServerReceiver(ResourceLocation id, ServerPlayPacketReceiver<FriendlyByteBuf> receiver) {
registerServerReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (server, player, handler, payload, responseSender) ->
receiver.receive(server, player, handler, payload.buffer(), responseSender));
public static void registerServerChannel(ResourceLocation id, ServerPlayPacketReceiver<FriendlyByteBuf> receiver) {
ChannelRegistry.PLAY_C2S.registerCodec(id, UntypedPayload.codec(id));
ChannelRegistry.PLAY_C2S.registerReceiver(id, (server, player, handler, payload, responseSender) -> receiver.receive(server, player, handler, ((UntypedPayload) payload).buffer(), responseSender));
}

/**
* Register a client-to-server packet receiver.
* Register a client-to-server packet channel.
* <p>
* Typed packet receiver is run on the main server thread.
*
Expand All @@ -42,8 +42,9 @@ public static void registerServerReceiver(ResourceLocation id, ServerPlayPacketR
* @param receiver the receiver
*/
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerServerReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec, ServerPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_C2S.register(type, codec, (ServerPlayPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerServerChannel(CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec, ServerPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_C2S.registerCodec(type.id(), codec);
ChannelRegistry.PLAY_C2S.registerReceiver(type.id(), (ServerPlayPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand All @@ -57,9 +58,38 @@ public static void registerServerReadyCallback(ServerPlayPacketReadyCallback cal
CallbackRegistry.SERVER_PLAY.add(callback);
}

/**
* Register a server-to-client packet channel.
* <p>
* This method needs to be called on <b>all sides</b>.
* <p>
* Register the receiver on <b>client side</b> with {@link #registerClientReceiver(ResourceLocation, ClientPlayPacketReceiver)}
*
* @param id the packet id
*/
public static void registerClientChannel(ResourceLocation id) {
ChannelRegistry.PLAY_S2C.registerCodec(id, UntypedPayload.codec(id));
}

/**
* Register a server-to-client packet channel.
* <p>
* This method needs to be called on <b>all sides</b>.
* <p>
* Register the receiver on <b>client side</b> with {@link #registerClientReceiver(CustomPacketPayload.Type, ClientPlayPacketReceiver)}
*
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
*/
public static <P extends CustomPacketPayload> void registerClientChannel(CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec) {
ChannelRegistry.PLAY_S2C.registerCodec(type.id(), codec);
}

/**
* Register a server-to-client packet receiver.
* <p>
* The channel needs to be {@linkplain #registerClientChannel(ResourceLocation) registered} first.
* <p>
* Raw packet receiver is run on Netty event-loop. Read the buffer on it and run
* the operation on {@linkplain Minecraft#execute(Runnable) client thread}.
*
Expand All @@ -68,23 +98,23 @@ public static void registerServerReadyCallback(ServerPlayPacketReadyCallback cal
*/
@ApiSide.ClientOnly
public static void registerClientReceiver(ResourceLocation id, ClientPlayPacketReceiver<FriendlyByteBuf> receiver) {
registerClientReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (client, handler, payload, responseSender) ->
receiver.receive(client, handler, payload.buffer(), responseSender));
ChannelRegistry.PLAY_S2C.registerReceiver(id, (client, handler, payload, responseSender) -> receiver.receive(client, handler, ((UntypedPayload) payload).buffer(), responseSender));
}

/**
* Register a server-to-client packet receiver.
* <p>
* The channel needs to be {@linkplain #registerClientChannel(CustomPacketPayload.Type, StreamCodec) registered} first.
* <p>
* Typed packet receiver is run on the main client thread.
*
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
* @param receiver the receiver
*/
@ApiSide.ClientOnly
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec, ClientPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_S2C.register(type, codec, (ClientPlayPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, ClientPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_S2C.registerReceiver(type.id(), (ClientPlayPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lol.bai.badpackets.impl.mixin.client;
package lol.bai.badpackets.impl.mixin;

import lol.bai.badpackets.impl.handler.AbstractPacketHandler;
import lol.bai.badpackets.impl.registry.ChannelRegistry;
Expand Down
Loading

0 comments on commit 85091b8

Please sign in to comment.