Skip to content

Commit

Permalink
use generic receiver and ready callback interface
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Apr 13, 2024
1 parent 9a5514e commit 655fa14
Show file tree
Hide file tree
Showing 28 changed files with 198 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FabricBadPacketTest implements ModInitializer {

@Override
public void onInitialize() {
BadPacketTest.server();
BadPacketTest.common();

// TODO
// ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/lol/bai/badpackets/api/PacketReadyCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package lol.bai.badpackets.api;

@FunctionalInterface
public interface PacketReadyCallback<C> {

void onReady(C context);

}
8 changes: 8 additions & 0 deletions src/main/java/lol/bai/badpackets/api/PacketReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package lol.bai.badpackets.api;

@FunctionalInterface
public interface PacketReceiver<C, P> {

void receive(C context, P payload);

}
22 changes: 10 additions & 12 deletions src/main/java/lol/bai/badpackets/api/PacketSender.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package lol.bai.badpackets.api;

import lol.bai.badpackets.api.play.ClientPlayPacketReadyCallback;
import lol.bai.badpackets.api.play.ClientPlayContext;
import lol.bai.badpackets.api.play.PlayPackets;
import lol.bai.badpackets.api.play.ServerPlayPacketReadyCallback;
import lol.bai.badpackets.api.play.ServerPlayPacketReceiver;
import lol.bai.badpackets.api.play.ServerPlayContext;
import lol.bai.badpackets.impl.handler.ClientPlayPacketHandler;
import lol.bai.badpackets.impl.handler.ServerPlayPacketHandler;
import lol.bai.badpackets.impl.marker.ApiSide;
Expand All @@ -25,11 +24,11 @@ public interface PacketSender {
* <p>
* <b>Only available when on game.</b>
*
* @see PlayPackets#registerServerChannel(ResourceLocation, ServerPlayPacketReceiver)
* @see PlayPackets#registerServerChannel(CustomPacketPayload.Type, StreamCodec, ServerPlayPacketReceiver)
* @see PlayPackets#registerServerChannel(ResourceLocation)
* @see PlayPackets#registerServerChannel(CustomPacketPayload.Type, StreamCodec)
*/
@ApiSide.ClientOnly
static PacketSender c2s() {
static ClientPlayContext c2s() {
return ClientPlayPacketHandler.get();
}

Expand All @@ -41,8 +40,7 @@ static PacketSender c2s() {
* @see PlayPackets#registerClientChannel(ResourceLocation)
* @see PlayPackets#registerClientChannel(CustomPacketPayload.Type, StreamCodec)
*/
@ApiSide.ServerOnly
static PacketSender s2c(ServerPlayer player) {
static ServerPlayContext s2c(ServerPlayer player) {
return ServerPlayPacketHandler.get(player);
}

Expand All @@ -51,8 +49,8 @@ static PacketSender s2c(ServerPlayer player) {
* <p>
* <b>Note:</b> Only works for Bad Packets channels.
*
* @see PlayPackets#registerClientReadyCallback(ClientPlayPacketReadyCallback)
* @see PlayPackets#registerServerReadyCallback(ServerPlayPacketReadyCallback)
* @see PlayPackets#registerClientReadyCallback(PacketReadyCallback)
* @see PlayPackets#registerServerReadyCallback(PacketReadyCallback)
*/
boolean canSend(ResourceLocation id);

Expand All @@ -61,8 +59,8 @@ static PacketSender s2c(ServerPlayer player) {
* <p>
* <b>Note:</b> Only works for Bad Packets channels.
*
* @see PlayPackets#registerClientReadyCallback(ClientPlayPacketReadyCallback)
* @see PlayPackets#registerServerReadyCallback(ServerPlayPacketReadyCallback)
* @see PlayPackets#registerClientReadyCallback(PacketReadyCallback)
* @see PlayPackets#registerServerReadyCallback(PacketReadyCallback)
*/
default boolean canSend(CustomPacketPayload.Type<?> type) {
return canSend(type.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@ApiSide.ClientOnly
@ApiStatus.NonExtendable
public interface ClientConfigConnectionContext extends PacketSender {
public interface ClientConfigContext extends PacketSender {

Minecraft client();

Expand Down

This file was deleted.

This file was deleted.

75 changes: 47 additions & 28 deletions src/main/java/lol/bai/badpackets/api/config/ConfigPackets.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lol.bai.badpackets.api.config;

import lol.bai.badpackets.api.PacketReadyCallback;
import lol.bai.badpackets.api.PacketReceiver;
import lol.bai.badpackets.api.PacketSender;
import lol.bai.badpackets.impl.handler.ServerConfigPacketHandler;
import lol.bai.badpackets.impl.marker.ApiSide;
Expand Down Expand Up @@ -33,37 +35,61 @@ public static void registerTask(ResourceLocation id, ConfigTaskExecutor executor
ServerConfigPacketHandler.registerTask(id, executor);
}

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

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

/**
* Register a client-to-server packet receiver.
* <p>
* The channel needs to be {@linkplain #registerServerChannel(ResourceLocation) registered} first.
* <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}.
* the operation on {@linkplain MinecraftServer#execute(Runnable) client thread}.
*
* @param id the packet id
* @param receiver the receiver
*
* @see ServerConfigPacketReceiver#receive
*/
public static void registerServerChannel(ResourceLocation id, ServerConfigPacketReceiver<FriendlyByteBuf> receiver) {
ChannelRegistry.CONFIG_C2S.registerCodec(id, UntypedPayload.codec(id));
public static void registerServerReceiver(ResourceLocation id, PacketReceiver<ServerConfigContext, FriendlyByteBuf> receiver) {
ChannelRegistry.CONFIG_C2S.registerReceiver(id, (context, payload) -> receiver.receive(context, ((UntypedPayload) payload).buffer()));
}

/**
* Register a client-to-server packet receiver.
* <p>
* The channel needs to be {@linkplain #registerServerChannel(CustomPacketPayload.Type, StreamCodec) registered} first.
* <p>
* Typed packet receiver is run on the main server thread.
*
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
* @param receiver the receiver
*
* @see ServerConfigPacketReceiver#receive
*/
@SuppressWarnings("unchecked")
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);
public static <P extends CustomPacketPayload> void registerServerReceiver(CustomPacketPayload.Type<P> type, PacketReceiver<ServerConfigContext, P> receiver) {
ChannelRegistry.CONFIG_C2S.registerReceiver(type.id(), (PacketReceiver<ServerConfigContext, CustomPacketPayload>) receiver);
}

/**
Expand All @@ -73,17 +99,16 @@ public static <P extends CustomPacketPayload> void registerServerChannel(CustomP
* <p>
* Not a general-purpose player join callback, use platform specific API for that.
*/
@ApiSide.ServerOnly
public static void registerServerReadyCallback(ServerConfigPacketReadyCallback callback) {
public static void registerServerReadyCallback(PacketReadyCallback<ServerConfigContext> callback) {
CallbackRegistry.SERVER_READY_CONFIG.add(callback);
}

/**
* Register a server-to-client packet receiver.
* 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, ClientConfigPacketReceiver)}
* Register the receiver on <b>client side</b> with {@link #registerClientReceiver(ResourceLocation, PacketReceiver)}
*
* @param id the packet id
*/
Expand All @@ -92,11 +117,11 @@ public static void registerClientChannel(ResourceLocation id) {
}

/**
* Register a server-to-client packet receiver.
* 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, ClientConfigPacketReceiver)}
* Register the receiver on <b>client side</b> with {@link #registerClientReceiver(CustomPacketPayload.Type, PacketReceiver)}
*
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
Expand All @@ -115,12 +140,9 @@ public static <P extends CustomPacketPayload> void registerClientChannel(CustomP
*
* @param id the packet id
* @param receiver the receiver
*
* @see ClientConfigPacketReceiver#receive
* @see #disconnect
*/
@ApiSide.ClientOnly
public static void registerClientReceiver(ResourceLocation id, ClientConfigPacketReceiver<FriendlyByteBuf> receiver) {
public static void registerClientReceiver(ResourceLocation id, PacketReceiver<ClientConfigContext, FriendlyByteBuf> receiver) {
ChannelRegistry.CONFIG_S2C.registerReceiver(id, (context, payload) -> receiver.receive(context, ((UntypedPayload) payload).buffer()));
}

Expand All @@ -133,14 +155,11 @@ public static void registerClientReceiver(ResourceLocation id, ClientConfigPacke
*
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param receiver the receiver
*
* @see ClientConfigPacketReceiver#receive
* @see #disconnect
*/
@ApiSide.ClientOnly
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, ClientConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_S2C.registerReceiver(type.id(), (ClientConfigPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, PacketReceiver<ClientConfigContext, P> receiver) {
ChannelRegistry.CONFIG_S2C.registerReceiver(type.id(), (PacketReceiver<ClientConfigContext, CustomPacketPayload>) receiver);
}

/**
Expand All @@ -153,15 +172,15 @@ public static <P extends CustomPacketPayload> void registerClientReceiver(Custom
* @see #disconnect
*/
@ApiSide.ClientOnly
public static void registerClientReadyCallback(ClientConfigPacketReadyCallback callback) {
public static void registerClientReadyCallback(PacketReadyCallback<ClientConfigContext> callback) {
CallbackRegistry.CLIENT_READY_CONFIG.add(callback);
}

/**
* Helper method to disconnect client-to-server connection.
*
* @param handler the handler instance
* @param reason the disconnection reason
* @param reason the disconnection reason
*/
@ApiSide.ClientOnly
public static void disconnect(ClientConfigurationPacketListenerImpl handler, Component reason) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lol.bai.badpackets.api.config;

import lol.bai.badpackets.api.PacketSender;
import net.minecraft.resources.ResourceLocation;

@FunctionalInterface
Expand All @@ -9,13 +8,13 @@ public interface ConfigTaskExecutor {
/**
* Return whether the task is run or not.
* <p>
* Make use of {@link PacketSender#canSend(ResourceLocation)} to check whether the client can process the task,
* Make use of {@link ServerConfigContext#canSend(ResourceLocation)} to check whether the client can process the task,
* and return {@code false} otherwise.
* <p>
* Once the client received the task packet, it should send a response packet to the server.
* After the server received the response packet, it should call {@link ServerConfigConnectionContext#finishTask(ResourceLocation)}
* After the server received the response packet, it should call {@link ServerConfigContext#finishTask(ResourceLocation)}
* to allow the client to join the server.
*/
boolean runTask(ServerConfigConnectionContext context);
boolean runTask(ServerConfigContext context);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.NonExtendable
public interface ServerConfigConnectionContext extends PacketSender {
public interface ServerConfigContext extends PacketSender {

MinecraftServer server();

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@ApiSide.ClientOnly
@ApiStatus.NonExtendable
public interface ClientPlayConnectionContext extends PacketSender {
public interface ClientPlayContext extends PacketSender {

Minecraft client();

Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 655fa14

Please sign in to comment.