diff --git a/gradle.properties b/gradle.properties index 360e934..5ec0281 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,12 +1,12 @@ org.gradle.jvmargs = -Xmx1G -minecraft = 1.20.4 +minecraft = 24w03a archiveBaseName = badpackets group = lol.bai majorVersion = 0 -fabricLoader = 0.15.3 +fabricLoader = 0.15.6 fabricApi = 0.93.1+1.20.4 forge = 49.0.19 neo = 20.4.96-beta diff --git a/platform/fabric/build.gradle.kts b/platform/fabric/build.gradle.kts index 90fef9b..f698e94 100644 --- a/platform/fabric/build.gradle.kts +++ b/platform/fabric/build.gradle.kts @@ -9,7 +9,7 @@ dependencies { mappings(loom.officialMojangMappings()) modImplementation("net.fabricmc:fabric-loader:${rootProp["fabricLoader"]}") - modImplementation("net.fabricmc.fabric-api:fabric-api:${rootProp["fabricApi"]}") +// modImplementation("net.fabricmc.fabric-api:fabric-api:${rootProp["fabricApi"]}") } loom { diff --git a/platform/fabric/src/main/java/lol/bai/badpackets/impl/fabric/FabricProxy.java b/platform/fabric/src/main/java/lol/bai/badpackets/impl/fabric/FabricProxy.java index 5d017d7..cfa34a4 100644 --- a/platform/fabric/src/main/java/lol/bai/badpackets/impl/fabric/FabricProxy.java +++ b/platform/fabric/src/main/java/lol/bai/badpackets/impl/fabric/FabricProxy.java @@ -1,45 +1,39 @@ package lol.bai.badpackets.impl.fabric; -import lol.bai.badpackets.impl.Constants; import lol.bai.badpackets.impl.platform.PlatformProxy; -import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.protocol.Packet; public class FabricProxy extends PlatformProxy { private static final boolean HAS_FABRIC_API = FabricLoader.getInstance().isModLoaded("fabric-networking-api-v1"); - @Override - public Packet createVanillaRegisterConfigS2CPacket(FriendlyByteBuf buf) { - return HAS_FABRIC_API - ? ServerConfigurationNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf) - : super.createVanillaRegisterConfigS2CPacket(buf); - } - - @Override - public Packet createVanillaRegisterConfigC2SPacket(FriendlyByteBuf buf) { - return HAS_FABRIC_API - ? ClientConfigurationNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf) - : super.createVanillaRegisterConfigC2SPacket(buf); - } - - @Override - public Packet createVanillaRegisterPlayS2CPacket(FriendlyByteBuf buf) { - return HAS_FABRIC_API - ? ServerPlayNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf) - : super.createVanillaRegisterPlayS2CPacket(buf); - } - - @Override - public Packet createVanillaRegisterPlayC2SPacket(FriendlyByteBuf buf) { - return HAS_FABRIC_API - ? ClientPlayNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf) - : super.createVanillaRegisterPlayC2SPacket(buf); - } + // TODO + // @Override + // public Packet createVanillaRegisterConfigS2CPacket(FriendlyByteBuf buf) { + // return HAS_FABRIC_API + // ? ServerConfigurationNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf) + // : super.createVanillaRegisterConfigS2CPacket(buf); + // } + + // @Override + // public Packet createVanillaRegisterConfigC2SPacket(FriendlyByteBuf buf) { + // return HAS_FABRIC_API + // ? ClientConfigurationNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf) + // : super.createVanillaRegisterConfigC2SPacket(buf); + // } + + // @Override + // public Packet createVanillaRegisterPlayS2CPacket(FriendlyByteBuf buf) { + // return HAS_FABRIC_API + // ? ServerPlayNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf) + // : super.createVanillaRegisterPlayS2CPacket(buf); + // } + + // @Override + // public Packet createVanillaRegisterPlayC2SPacket(FriendlyByteBuf buf) { + // return HAS_FABRIC_API + // ? ClientPlayNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf) + // : super.createVanillaRegisterPlayC2SPacket(buf); + // } } diff --git a/platform/fabric/src/main/resources/fabric.mod.json b/platform/fabric/src/main/resources/fabric.mod.json index 194ca46..70965b1 100644 --- a/platform/fabric/src/main/resources/fabric.mod.json +++ b/platform/fabric/src/main/resources/fabric.mod.json @@ -10,7 +10,7 @@ "license" : "Apache-2.0", "mixins" : ["badpackets.mixins.json"], "depends" : { - "minecraft" : ">=1.20.4-", + "minecraft" : ">=1.20.5-", "fabricloader": ">=0.14.22" }, "custom" : { diff --git a/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTest.java b/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTest.java index 8b71406..fcfe820 100644 --- a/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTest.java +++ b/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTest.java @@ -3,9 +3,6 @@ import lol.bai.badpackets.impl.Constants; import lol.bai.badpackets.test.BadPacketTest; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; -import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; public class FabricBadPacketTest implements ModInitializer { @@ -16,11 +13,12 @@ public class FabricBadPacketTest implements ModInitializer { public void onInitialize() { BadPacketTest.server(); - ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { - if (!ServerConfigurationNetworking.canSend(handler, FABRIC_TEST)) { - handler.disconnect(Component.literal("fabric test channel not found")); - } - }); + // TODO + // ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { + // if (!ServerConfigurationNetworking.canSend(handler, FABRIC_TEST)) { + // handler.disconnect(Component.literal("fabric test channel not found")); + // } + // }); } } diff --git a/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTestClient.java b/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTestClient.java index 8a7475d..ab86050 100644 --- a/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTestClient.java +++ b/platform/fabric/src/testmod/java/lol/bai/badpackets/test/fabric/FabricBadPacketTestClient.java @@ -2,7 +2,6 @@ import lol.bai.badpackets.test.BadPacketTest; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; public class FabricBadPacketTestClient implements ClientModInitializer { @@ -10,7 +9,8 @@ public class FabricBadPacketTestClient implements ClientModInitializer { public void onInitializeClient() { BadPacketTest.client(); - ClientConfigurationNetworking.registerGlobalReceiver(FabricBadPacketTest.FABRIC_TEST, (client, handler, buf, responseSender) -> {}); + // TODO + // ClientConfigurationNetworking.registerGlobalReceiver(FabricBadPacketTest.FABRIC_TEST, (client, handler, buf, responseSender) -> {}); } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 033b748..444ced1 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -26,5 +26,5 @@ fun platform(name: String) { platform("mojmap") platform("fabric") -platform("forge") -platform("neo") +//platform("forge") +//platform("neo") diff --git a/src/main/java/lol/bai/badpackets/api/PacketSender.java b/src/main/java/lol/bai/badpackets/api/PacketSender.java index 8f48be3..ab31660 100644 --- a/src/main/java/lol/bai/badpackets/api/PacketSender.java +++ b/src/main/java/lol/bai/badpackets/api/PacketSender.java @@ -11,6 +11,7 @@ import lol.bai.badpackets.impl.payload.UntypedPayload; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.PacketSendListener; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; @@ -26,7 +27,7 @@ public interface PacketSender { * Only available when on game. * * @see PlayPackets#registerServerReceiver(ResourceLocation, ServerPlayPacketReceiver) - * @see PlayPackets#registerServerReceiver(ResourceLocation, FriendlyByteBuf.Reader, ServerPlayPacketReceiver) + * @see PlayPackets#registerServerReceiver(CustomPacketPayload.Type, StreamCodec, ServerPlayPacketReceiver) */ @ApiSide.ClientOnly static PacketSender c2s() { @@ -39,7 +40,7 @@ static PacketSender c2s() { * @param player the player that we want to send packets to. * * @see PlayPackets#registerClientReceiver(ResourceLocation, ClientPlayPacketReceiver) - * @see PlayPackets#registerClientReceiver(ResourceLocation, FriendlyByteBuf.Reader, ClientPlayPacketReceiver) + * @see PlayPackets#registerClientReceiver(CustomPacketPayload.Type, StreamCodec, ClientPlayPacketReceiver) */ @ApiSide.ServerOnly static PacketSender s2c(ServerPlayer player) { @@ -56,6 +57,18 @@ static PacketSender s2c(ServerPlayer player) { */ boolean canSend(ResourceLocation id); + /** + * Returns whether the target can receive a packet with the specified id. + *

+ * Note: Only works for Bad Packets channels. + * + * @see PlayPackets#registerClientReadyCallback(ClientPlayPacketReadyCallback) + * @see PlayPackets#registerServerReadyCallback(ServerPlayPacketReadyCallback) + */ + default boolean canSend(CustomPacketPayload.Type type) { + return canSend(type.id()); + } + /** * Send a packet to the target. */ @@ -83,7 +96,7 @@ default void send(ResourceLocation id, FriendlyByteBuf buf) { * @param callback a callback in which will be called after the packet sent to the target. */ default void send(ResourceLocation id, FriendlyByteBuf buf, @Nullable PacketSendListener callback) { - send(new UntypedPayload(id, buf), callback); + send(new UntypedPayload(UntypedPayload.type(id), buf), callback); } } diff --git a/src/main/java/lol/bai/badpackets/api/config/ConfigPackets.java b/src/main/java/lol/bai/badpackets/api/config/ConfigPackets.java index 466d6c0..d1a6b35 100644 --- a/src/main/java/lol/bai/badpackets/api/config/ConfigPackets.java +++ b/src/main/java/lol/bai/badpackets/api/config/ConfigPackets.java @@ -11,6 +11,7 @@ import net.minecraft.client.multiplayer.ClientConfigurationPacketListenerImpl; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; @@ -44,7 +45,7 @@ public static void registerTask(ResourceLocation id, ConfigTaskExecutor executor * @see ServerConfigPacketReceiver#receive */ public static void registerServerReceiver(ResourceLocation id, ServerConfigPacketReceiver receiver) { - registerServerReceiver(id, UntypedPayload.reader(id), (server, handler, payload, responseSender, taskFinisher) -> + registerServerReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (server, handler, payload, responseSender, taskFinisher) -> receiver.receive(server, handler, payload.buffer(), responseSender, taskFinisher)); } @@ -53,15 +54,15 @@ public static void registerServerReceiver(ResourceLocation id, ServerConfigPacke *

* Typed packet receiver is run on the main server thread. * - * @param id the {@linkplain CustomPacketPayload#id() packet id} - * @param reader the payload reader + * @param type the {@linkplain CustomPacketPayload#type() packet type} + * @param codec the payload codec * @param receiver the receiver * * @see ServerConfigPacketReceiver#receive */ @SuppressWarnings("unchecked") - public static

void registerServerReceiver(ResourceLocation id, FriendlyByteBuf.Reader

reader, ServerConfigPacketReceiver

receiver) { - ChannelRegistry.CONFIG_C2S.register(id, reader, (ServerConfigPacketReceiver) receiver); + public static

void registerServerReceiver(CustomPacketPayload.Type

type, StreamCodec codec, ServerConfigPacketReceiver

receiver) { + ChannelRegistry.CONFIG_C2S.register(type, codec, (ServerConfigPacketReceiver) receiver); } /** @@ -90,7 +91,7 @@ public static void registerServerReadyCallback(ServerConfigPacketReadyCallback c */ @ApiSide.ClientOnly public static void registerClientReceiver(ResourceLocation id, ClientConfigPacketReceiver receiver) { - registerClientReceiver(id, UntypedPayload.reader(id), (client, handler, payload, responseSender) -> + registerClientReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (client, handler, payload, responseSender) -> receiver.receive(client, handler, payload.buffer(), responseSender)); } @@ -99,8 +100,8 @@ public static void registerClientReceiver(ResourceLocation id, ClientConfigPacke *

* Typed packet receiver is run on the main client thread. * - * @param id the {@linkplain CustomPacketPayload#id() packet id} - * @param reader the payload reader + * @param type the {@linkplain CustomPacketPayload#type() packet type} + * @param codec the payload codec * @param receiver the receiver * * @see ClientConfigPacketReceiver#receive @@ -108,8 +109,8 @@ public static void registerClientReceiver(ResourceLocation id, ClientConfigPacke */ @ApiSide.ClientOnly @SuppressWarnings("unchecked") - public static

void registerClientReceiver(ResourceLocation id, FriendlyByteBuf.Reader

reader, ClientConfigPacketReceiver

receiver) { - ChannelRegistry.CONFIG_S2C.register(id, reader, (ClientConfigPacketReceiver) receiver); + public static

void registerClientReceiver(CustomPacketPayload.Type

type, StreamCodec codec, ClientConfigPacketReceiver

receiver) { + ChannelRegistry.CONFIG_S2C.register(type, codec, (ClientConfigPacketReceiver) receiver); } /** diff --git a/src/main/java/lol/bai/badpackets/api/play/PlayPackets.java b/src/main/java/lol/bai/badpackets/api/play/PlayPackets.java index be26e83..59c56d6 100644 --- a/src/main/java/lol/bai/badpackets/api/play/PlayPackets.java +++ b/src/main/java/lol/bai/badpackets/api/play/PlayPackets.java @@ -7,6 +7,8 @@ import lol.bai.badpackets.impl.registry.ChannelRegistry; import net.minecraft.client.Minecraft; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; @@ -26,7 +28,7 @@ public final class PlayPackets { * @param receiver the receiver */ public static void registerServerReceiver(ResourceLocation id, ServerPlayPacketReceiver receiver) { - registerServerReceiver(id, UntypedPayload.reader(id), (server, player, handler, payload, responseSender) -> + registerServerReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (server, player, handler, payload, responseSender) -> receiver.receive(server, player, handler, payload.buffer(), responseSender)); } @@ -35,13 +37,13 @@ public static void registerServerReceiver(ResourceLocation id, ServerPlayPacketR *

* Typed packet receiver is run on the main server thread. * - * @param id the {@linkplain CustomPacketPayload#id() packet id} - * @param reader the payload reader + * @param type the {@linkplain CustomPacketPayload#type() packet type} + * @param codec the payload codec * @param receiver the receiver */ @SuppressWarnings("unchecked") - public static

void registerServerReceiver(ResourceLocation id, FriendlyByteBuf.Reader

reader, ServerPlayPacketReceiver

receiver) { - ChannelRegistry.PLAY_C2S.register(id, reader, (ServerPlayPacketReceiver) receiver); + public static

void registerServerReceiver(CustomPacketPayload.Type

type, StreamCodec codec, ServerPlayPacketReceiver

receiver) { + ChannelRegistry.PLAY_C2S.register(type, codec, (ServerPlayPacketReceiver) receiver); } /** @@ -66,7 +68,7 @@ public static void registerServerReadyCallback(ServerPlayPacketReadyCallback cal */ @ApiSide.ClientOnly public static void registerClientReceiver(ResourceLocation id, ClientPlayPacketReceiver receiver) { - registerClientReceiver(id, UntypedPayload.reader(id), (client, handler, payload, responseSender) -> + registerClientReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (client, handler, payload, responseSender) -> receiver.receive(client, handler, payload.buffer(), responseSender)); } @@ -75,14 +77,14 @@ public static void registerClientReceiver(ResourceLocation id, ClientPlayPacketR *

* Typed packet receiver is run on the main client thread. * - * @param id the {@linkplain CustomPacketPayload#id() packet id} - * @param reader the payload reader + * @param type the {@linkplain CustomPacketPayload#type() packet type} + * @param codec the payload codec * @param receiver the receiver */ @ApiSide.ClientOnly @SuppressWarnings("unchecked") - public static

void registerClientReceiver(ResourceLocation id, FriendlyByteBuf.Reader

reader, ClientPlayPacketReceiver

receiver) { - ChannelRegistry.PLAY_S2C.register(id, reader, (ClientPlayPacketReceiver) receiver); + public static

void registerClientReceiver(CustomPacketPayload.Type

type, StreamCodec codec, ClientPlayPacketReceiver

receiver) { + ChannelRegistry.PLAY_S2C.register(type, codec, (ClientPlayPacketReceiver) receiver); } /** diff --git a/src/main/java/lol/bai/badpackets/impl/handler/AbstractPacketHandler.java b/src/main/java/lol/bai/badpackets/impl/handler/AbstractPacketHandler.java index a15f794..46413b5 100644 --- a/src/main/java/lol/bai/badpackets/impl/handler/AbstractPacketHandler.java +++ b/src/main/java/lol/bai/badpackets/impl/handler/AbstractPacketHandler.java @@ -26,9 +26,9 @@ import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; -public abstract class AbstractPacketHandler implements PacketSender { +public abstract class AbstractPacketHandler implements PacketSender { - protected final ChannelRegistry registry; + protected final ChannelRegistry registry; protected final Logger logger; private final Function> packetFactory; @@ -39,7 +39,7 @@ public abstract class AbstractPacketHandler implements PacketSender { private boolean initialized = false; - protected AbstractPacketHandler(String desc, ChannelRegistry registry, Function> packetFactory, BlockableEventLoop eventLoop, Connection connection) { + protected AbstractPacketHandler(String desc, ChannelRegistry registry, Function> packetFactory, BlockableEventLoop eventLoop, Connection connection) { this.logger = LogManager.getLogger(desc); this.registry = registry; this.packetFactory = packetFactory; @@ -69,12 +69,8 @@ private void receiveChannelSyncPacket(FriendlyByteBuf buf) { } } - public static void addChannelSyncReader(Map> map) { - map.put(Constants.CHANNEL_SYNC, UntypedPayload.reader(Constants.CHANNEL_SYNC)); - } - public boolean receive(CustomPacketPayload payload) { - ResourceLocation id = payload.id(); + ResourceLocation id = payload.type().id(); if (id.equals(Constants.CHANNEL_SYNC)) { UntypedPayload untyped = (UntypedPayload) payload; @@ -84,7 +80,7 @@ public boolean receive(CustomPacketPayload payload) { if (registry.has(id)) { try { - T receiver = registry.get(id); + R receiver = registry.get(id); if (payload instanceof UntypedPayload || eventLoop.isSameThread()) { receiveUnsafe(receiver, payload); @@ -103,7 +99,7 @@ public boolean receive(CustomPacketPayload payload) { protected abstract void onInitialChannelSyncPacketReceived(); - protected abstract void receiveUnsafe(T receiver, CustomPacketPayload payload); + protected abstract void receiveUnsafe(R receiver, CustomPacketPayload payload); public void sendInitialChannelSyncPacket() { if (!initialized) { diff --git a/src/main/java/lol/bai/badpackets/impl/mixin/AccessServerboundCustomPayloadPacket.java b/src/main/java/lol/bai/badpackets/impl/mixin/AccessServerboundCustomPayloadPacket.java deleted file mode 100644 index cdc0426..0000000 --- a/src/main/java/lol/bai/badpackets/impl/mixin/AccessServerboundCustomPayloadPacket.java +++ /dev/null @@ -1,27 +0,0 @@ -package lol.bai.badpackets.impl.mixin; - -import java.util.Map; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; -import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.ResourceLocation; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; -import org.spongepowered.asm.mixin.gen.Accessor; - -@Mixin(ServerboundCustomPayloadPacket.class) -public interface AccessServerboundCustomPayloadPacket { - - @Accessor("KNOWN_TYPES") - static Map> badpackets_getPacketReaders() { - throw new AssertionError(); - } - - @Mutable - @Accessor("KNOWN_TYPES") - static void badpackets_setPacketReaders(Map> val) { - throw new AssertionError(); - } - -} diff --git a/src/main/java/lol/bai/badpackets/impl/mixin/MixinServerboundCustomPayloadPacket.java b/src/main/java/lol/bai/badpackets/impl/mixin/MixinServerboundCustomPayloadPacket.java index 980a3ff..58fc464 100644 --- a/src/main/java/lol/bai/badpackets/impl/mixin/MixinServerboundCustomPayloadPacket.java +++ b/src/main/java/lol/bai/badpackets/impl/mixin/MixinServerboundCustomPayloadPacket.java @@ -1,39 +1,27 @@ package lol.bai.badpackets.impl.mixin; -import java.util.HashMap; -import java.util.Map; - import lol.bai.badpackets.impl.handler.AbstractPacketHandler; +import lol.bai.badpackets.impl.registry.ChannelRegistry; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.ServerCommonPacketListener; import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ServerboundCustomPayloadPacket.class) public abstract class MixinServerboundCustomPayloadPacket { - @Shadow - @Final - @Mutable - private static Map> KNOWN_TYPES; - @Shadow public abstract CustomPacketPayload payload(); - @Inject(method = "", at = @At("TAIL")) - private static void badpackets_makeReaderMapMutable(CallbackInfo ci) { - KNOWN_TYPES = new HashMap<>(KNOWN_TYPES); - AbstractPacketHandler.addChannelSyncReader(KNOWN_TYPES); - } - @Inject(method = "handle(Lnet/minecraft/network/protocol/common/ServerCommonPacketListener;)V", at = @At("HEAD"), cancellable = true) private void badpackets_handle(ServerCommonPacketListener listener, CallbackInfo ci) { if (listener instanceof AbstractPacketHandler.Holder holder && holder.badpackets_receive(payload())) { @@ -41,4 +29,22 @@ private void badpackets_handle(ServerCommonPacketListener listener, CallbackInfo } } + @Inject(method = "lambda$static$0", at = @At("HEAD"), cancellable = true, require = 0) + private static void badpackets_getCodec_mojmap(ResourceLocation id, CallbackInfoReturnable> cir) { + badpackets_getCodec(id, cir); + } + + @SuppressWarnings({"UnresolvedMixinReference", "MixinAnnotationTarget"}) + @Inject(method = "method_56475", at = @At("HEAD"), cancellable = true, require = 0) + private static void badpackets_getCodec_intermediary(ResourceLocation id, CallbackInfoReturnable> cir) { + badpackets_getCodec(id, cir); + } + + @Unique + private static void badpackets_getCodec(ResourceLocation id, CallbackInfoReturnable> cir) { + var codec = ChannelRegistry.PLAY_C2S.getCodec(id); + if (codec == null) codec = ChannelRegistry.CONFIG_C2S.getCodec(id); + if (codec != null) cir.setReturnValue(codec); + } + } diff --git a/src/main/java/lol/bai/badpackets/impl/mixin/client/AccessClientboundCustomPayloadPacket.java b/src/main/java/lol/bai/badpackets/impl/mixin/client/AccessClientboundCustomPayloadPacket.java deleted file mode 100644 index cb86bd0..0000000 --- a/src/main/java/lol/bai/badpackets/impl/mixin/client/AccessClientboundCustomPayloadPacket.java +++ /dev/null @@ -1,27 +0,0 @@ -package lol.bai.badpackets.impl.mixin.client; - -import java.util.Map; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; -import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.ResourceLocation; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; -import org.spongepowered.asm.mixin.gen.Accessor; - -@Mixin(ClientboundCustomPayloadPacket.class) -public interface AccessClientboundCustomPayloadPacket { - - @Accessor("KNOWN_TYPES") - static Map> badpackets_getPacketReaders() { - throw new AssertionError(); - } - - @Mutable - @Accessor("KNOWN_TYPES") - static void badpackets_setPacketReaders(Map> val) { - throw new AssertionError(); - } - -} diff --git a/src/main/java/lol/bai/badpackets/impl/mixin/client/MixinClientboundCustomPayloadPacket.java b/src/main/java/lol/bai/badpackets/impl/mixin/client/MixinClientboundCustomPayloadPacket.java index 747432a..65eb001 100644 --- a/src/main/java/lol/bai/badpackets/impl/mixin/client/MixinClientboundCustomPayloadPacket.java +++ b/src/main/java/lol/bai/badpackets/impl/mixin/client/MixinClientboundCustomPayloadPacket.java @@ -1,39 +1,27 @@ package lol.bai.badpackets.impl.mixin.client; -import java.util.HashMap; -import java.util.Map; - import lol.bai.badpackets.impl.handler.AbstractPacketHandler; +import lol.bai.badpackets.impl.registry.ChannelRegistry; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.ClientCommonPacketListener; import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ClientboundCustomPayloadPacket.class) public abstract class MixinClientboundCustomPayloadPacket { - @Shadow - @Final - @Mutable - private static Map> KNOWN_TYPES; - @Shadow public abstract CustomPacketPayload payload(); - @Inject(method = "", at = @At("TAIL")) - private static void badpackets_makeReaderMapMutable(CallbackInfo ci) { - KNOWN_TYPES = new HashMap<>(KNOWN_TYPES); - AbstractPacketHandler.addChannelSyncReader(KNOWN_TYPES); - } - @Inject(method = "handle(Lnet/minecraft/network/protocol/common/ClientCommonPacketListener;)V", at = @At("HEAD"), cancellable = true) private void badpackets_handle(ClientCommonPacketListener listener, CallbackInfo ci) { if (listener instanceof AbstractPacketHandler.Holder holder && holder.badpackets_receive(payload())) { @@ -41,4 +29,38 @@ private void badpackets_handle(ClientCommonPacketListener listener, CallbackInfo } } + @Unique + private static void badpackets_getPlayCodec(ResourceLocation id, CallbackInfoReturnable> cir) { + var codec = ChannelRegistry.PLAY_S2C.getCodec(id); + if (codec != null) cir.setReturnValue(codec); + } + + @Unique + private static void badpackets_getConfigCodec(ResourceLocation id, CallbackInfoReturnable> cir) { + var codec = ChannelRegistry.CONFIG_S2C.getCodec(id); + if (codec != null) cir.setReturnValue(codec); + } + + @Inject(method = "lambda$static$0", at = @At("HEAD"), cancellable = true, require = 0) + private static void badpackets_getPlayCodec_mojmap(ResourceLocation id, CallbackInfoReturnable> cir) { + badpackets_getPlayCodec(id, cir); + } + + @Inject(method = "lambda$static$1", at = @At("HEAD"), cancellable = true, require = 0) + private static void badpackets_getConfigCodec_mojmap(ResourceLocation id, CallbackInfoReturnable> cir) { + badpackets_getConfigCodec(id, cir); + } + + @SuppressWarnings({"UnresolvedMixinReference", "MixinAnnotationTarget"}) + @Inject(method = "method_56461", at = @At("HEAD"), cancellable = true, require = 0) + private static void badpackets_getPlayCodec_intermediary(ResourceLocation id, CallbackInfoReturnable> cir) { + badpackets_getPlayCodec(id, cir); + } + + @SuppressWarnings({"UnresolvedMixinReference", "MixinAnnotationTarget"}) + @Inject(method = "method_56460", at = @At("HEAD"), cancellable = true, require = 0) + private static void badpackets_getConfigCodec_intermediary(ResourceLocation id, CallbackInfoReturnable> cir) { + badpackets_getConfigCodec(id, cir); + } + } diff --git a/src/main/java/lol/bai/badpackets/impl/payload/UntypedPayload.java b/src/main/java/lol/bai/badpackets/impl/payload/UntypedPayload.java index 02a27a9..9d82bfd 100644 --- a/src/main/java/lol/bai/badpackets/impl/payload/UntypedPayload.java +++ b/src/main/java/lol/bai/badpackets/impl/payload/UntypedPayload.java @@ -1,32 +1,45 @@ package lol.bai.badpackets.impl.payload; +import java.util.HashMap; +import java.util.Map; + import io.netty.buffer.Unpooled; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; public record UntypedPayload( - ResourceLocation channelId, + Type type, FriendlyByteBuf buffer ) implements CustomPacketPayload { - public static FriendlyByteBuf.Reader reader(ResourceLocation channelId) { - return buf -> { + private static final Map> TYPES = new HashMap<>(); + + public static Type type(ResourceLocation id) { + return TYPES.computeIfAbsent(id, Type::new); + } + + public UntypedPayload(ResourceLocation id, FriendlyByteBuf buf) { + this(type(id), buf); + } + + public static StreamCodec codec(ResourceLocation id) { + return CustomPacketPayload.codec(UntypedPayload::write, buf -> { FriendlyByteBuf copy = new FriendlyByteBuf(Unpooled.buffer()); copy.writeBytes(buf); - return new UntypedPayload(channelId, copy); - }; + return new UntypedPayload(id, copy); + }); } - @Override public void write(@NotNull FriendlyByteBuf buf) { buf.writeBytes(buffer.copy()); } @Override - public @NotNull ResourceLocation id() { - return channelId; + public @NotNull Type type() { + return type; } @Override diff --git a/src/main/java/lol/bai/badpackets/impl/platform/PlatformProxy.java b/src/main/java/lol/bai/badpackets/impl/platform/PlatformProxy.java index f84fb01..7ee3e30 100644 --- a/src/main/java/lol/bai/badpackets/impl/platform/PlatformProxy.java +++ b/src/main/java/lol/bai/badpackets/impl/platform/PlatformProxy.java @@ -3,11 +3,11 @@ import java.util.ServiceLoader; import lol.bai.badpackets.impl.Constants; -import lol.bai.badpackets.impl.payload.UntypedPayload; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.DiscardedPayload; public class PlatformProxy { @@ -18,19 +18,19 @@ public boolean canSendVanillaRegisterPackets() { } public Packet createVanillaRegisterConfigC2SPacket(FriendlyByteBuf buf) { - return new ServerboundCustomPayloadPacket(new UntypedPayload(Constants.MC_REGISTER_CHANNEL, buf)); + return new ServerboundCustomPayloadPacket(new DiscardedPayload(Constants.MC_REGISTER_CHANNEL)); } public Packet createVanillaRegisterConfigS2CPacket(FriendlyByteBuf buf) { - return new ClientboundCustomPayloadPacket(new UntypedPayload(Constants.MC_REGISTER_CHANNEL, buf)); + return new ClientboundCustomPayloadPacket(new DiscardedPayload(Constants.MC_REGISTER_CHANNEL)); } public Packet createVanillaRegisterPlayC2SPacket(FriendlyByteBuf buf) { - return new ServerboundCustomPayloadPacket(new UntypedPayload(Constants.MC_REGISTER_CHANNEL, buf)); + return new ServerboundCustomPayloadPacket(new DiscardedPayload(Constants.MC_REGISTER_CHANNEL)); } public Packet createVanillaRegisterPlayS2CPacket(FriendlyByteBuf buf) { - return new ClientboundCustomPayloadPacket(new UntypedPayload(Constants.MC_REGISTER_CHANNEL, buf)); + return new ClientboundCustomPayloadPacket(new DiscardedPayload(Constants.MC_REGISTER_CHANNEL)); } } diff --git a/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java b/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java index b9c3748..45749af 100644 --- a/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java +++ b/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java @@ -6,69 +6,60 @@ import java.util.Set; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.function.Consumer; -import java.util.function.Supplier; -import com.google.common.base.Suppliers; import lol.bai.badpackets.api.config.ClientConfigPacketReceiver; import lol.bai.badpackets.api.config.ServerConfigPacketReceiver; import lol.bai.badpackets.api.play.ClientPlayPacketReceiver; import lol.bai.badpackets.api.play.ServerPlayPacketReceiver; import lol.bai.badpackets.impl.Constants; import lol.bai.badpackets.impl.handler.AbstractPacketHandler; -import lol.bai.badpackets.impl.mixin.AccessServerboundCustomPayloadPacket; -import lol.bai.badpackets.impl.mixin.client.AccessClientboundCustomPayloadPacket; +import lol.bai.badpackets.impl.payload.UntypedPayload; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; -public class ChannelRegistry { +public class ChannelRegistry { private static final Set RESERVED_CHANNELS = Set.of( Constants.CHANNEL_SYNC, Constants.MC_REGISTER_CHANNEL, Constants.MC_UNREGISTER_CHANNEL); - private static final Supplier C2S_READERS = Suppliers.memoize(() -> new ReaderMapHolder(AccessServerboundCustomPayloadPacket::badpackets_getPacketReaders, AccessServerboundCustomPayloadPacket::badpackets_setPacketReaders)); - private static final Supplier S2C_READERS = Suppliers.memoize(() -> new ReaderMapHolder(AccessClientboundCustomPayloadPacket::badpackets_getPacketReaders, AccessClientboundCustomPayloadPacket::badpackets_setPacketReaders)); + public static final ChannelRegistry> CONFIG_S2C = new ChannelRegistry<>(RESERVED_CHANNELS); + public static final ChannelRegistry> CONFIG_C2S = new ChannelRegistry<>(RESERVED_CHANNELS); - public static final ChannelRegistry> CONFIG_S2C = new ChannelRegistry<>(RESERVED_CHANNELS, S2C_READERS); - public static final ChannelRegistry> CONFIG_C2S = new ChannelRegistry<>(RESERVED_CHANNELS, C2S_READERS); + public static final ChannelRegistry> PLAY_S2C = new ChannelRegistry<>(RESERVED_CHANNELS); + public static final ChannelRegistry> PLAY_C2S = new ChannelRegistry<>(RESERVED_CHANNELS); - public static final ChannelRegistry> PLAY_S2C = new ChannelRegistry<>(RESERVED_CHANNELS, S2C_READERS); - public static final ChannelRegistry> PLAY_C2S = new ChannelRegistry<>(RESERVED_CHANNELS, C2S_READERS); - - private final Map channels = new HashMap<>(); private final Set reservedChannels; - private final Set> handlers = new HashSet<>(); - private final Supplier readersHolder; + + private final Map> codecs = new HashMap<>(); + private final Map channels = new HashMap<>(); + private final Set> handlers = new HashSet<>(); private final ReentrantReadWriteLock locks = new ReentrantReadWriteLock(); - private ChannelRegistry(Set reservedChannels, Supplier readersHolder) { + private ChannelRegistry(Set reservedChannels) { this.reservedChannels = reservedChannels; - this.readersHolder = readersHolder; + + codecs.put(Constants.CHANNEL_SYNC, UntypedPayload.codec(Constants.CHANNEL_SYNC)); } - public void register(ResourceLocation id, FriendlyByteBuf.Reader reader, T receiver) { + public

void register(CustomPacketPayload.Type

type, StreamCodec codec, R receiver) { Lock lock = locks.writeLock(); lock.lock(); try { - if (reservedChannels.contains(id)) { - throw new IllegalArgumentException("Reserved channel id " + id); - } - - Map> readers = readersHolder.get().getter.get(); - if (!(readers instanceof HashMap)) { - readers = new HashMap<>(readers); - readersHolder.get().setter.accept(readers); + if (reservedChannels.contains(type.id())) { + throw new IllegalArgumentException("Reserved channel id " + type.id()); } - readers.put(id, reader); - channels.put(id, receiver); - for (AbstractPacketHandler handler : handlers) { - handler.onRegister(id); + codecs.put(type.id(), codec); + channels.put(type.id(), receiver); + for (AbstractPacketHandler handler : handlers) { + handler.onRegister(type.id()); } } finally { lock.unlock(); @@ -86,7 +77,7 @@ public boolean has(ResourceLocation id) { } } - public T get(ResourceLocation id) { + public R get(ResourceLocation id) { Lock lock = locks.readLock(); lock.lock(); @@ -97,6 +88,18 @@ public T get(ResourceLocation id) { } } + @SuppressWarnings("unchecked") + public StreamCodec getCodec(ResourceLocation id) { + Lock lock = locks.readLock(); + lock.lock(); + + try { + return (StreamCodec) codecs.get(id); + } finally { + lock.unlock(); + } + } + public Set getChannels() { Lock lock = locks.readLock(); lock.lock(); @@ -108,7 +111,7 @@ public Set getChannels() { } } - public void addHandler(AbstractPacketHandler handler) { + public void addHandler(AbstractPacketHandler handler) { Lock lock = locks.writeLock(); lock.lock(); @@ -119,7 +122,7 @@ public void addHandler(AbstractPacketHandler handler) { } } - public void removeHandler(AbstractPacketHandler handler) { + public void removeHandler(AbstractPacketHandler handler) { Lock lock = locks.writeLock(); lock.lock(); @@ -130,11 +133,4 @@ public void removeHandler(AbstractPacketHandler handler) { } } - private record ReaderMapHolder( - Supplier>> getter, - Consumer>> setter - ) { - - } - } diff --git a/src/main/resources/badpackets.mixins.json b/src/main/resources/badpackets.mixins.json index 2d2ad3b..ffc8b5a 100644 --- a/src/main/resources/badpackets.mixins.json +++ b/src/main/resources/badpackets.mixins.json @@ -5,7 +5,6 @@ "compatibilityLevel": "JAVA_16", "refmap" : "badpackets.refmap.json", "mixins" : [ - "AccessServerboundCustomPayloadPacket", "MixinPlayerList", "MixinServerboundCustomPayloadPacket", "MixinServerCommonPacketListenerImpl", @@ -13,7 +12,6 @@ "MixinServerGamePacketListenerImpl" ], "client" : [ - "client.AccessClientboundCustomPayloadPacket", "client.AccessClientCommonPacketListenerImpl", "client.MixinClientboundCustomPayloadPacket", "client.MixinClientCommonPacketListenerImpl", diff --git a/src/testmod/java/lol/bai/badpackets/test/BadPacketTest.java b/src/testmod/java/lol/bai/badpackets/test/BadPacketTest.java index 69ef13a..91c17cd 100644 --- a/src/testmod/java/lol/bai/badpackets/test/BadPacketTest.java +++ b/src/testmod/java/lol/bai/badpackets/test/BadPacketTest.java @@ -27,7 +27,7 @@ public static void server() { // TASK -------------------------------------------------------------------------------------------------------- ConfigPackets.registerTask(CONFIG_TASK, (handler, sender, server) -> { - if (sender.canSend(TestTaskPayload.ID)) { + if (sender.canSend(TestTaskPayload.TYPE)) { sender.send(new TestTaskPayload(TestTaskPayload.Stage.QUESTION_1)); return true; } @@ -35,7 +35,7 @@ public static void server() { return false; }); - ConfigPackets.registerServerReceiver(TestTaskPayload.ID, TestTaskPayload::new, (server, handler, payload, responseSender, taskFinisher) -> { + ConfigPackets.registerServerReceiver(TestTaskPayload.TYPE, TestTaskPayload.CODEC, (server, handler, payload, responseSender, taskFinisher) -> { LOGGER.info("[config task] client -> server " + payload.stage().name()); switch (payload.stage()) { @@ -51,12 +51,12 @@ public static void server() { ConfigPackets.registerServerReceiver(CONFIG_C2S, (server, handler, buf, responseSender, taskFinisher) -> LOGGER.info(buf.readUtf())); - ConfigPackets.registerServerReceiver(TestConfigPayload.ID, TestConfigPayload::new, (server, handler, payload, responseSender, taskFinisher) -> + ConfigPackets.registerServerReceiver(TestConfigPayload.TYPE, TestConfigPayload.CODEC, (server, handler, payload, responseSender, taskFinisher) -> LOGGER.info(payload.msg())); ConfigPackets.registerServerReadyCallback((handler, sender, server) -> { Validate.isTrue(sender.canSend(BadPacketTest.CONFIG_S2C)); - Validate.isTrue(sender.canSend(TestConfigPayload.ID)); + Validate.isTrue(sender.canSend(TestConfigPayload.TYPE)); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); buf.writeUtf("[config untyped] server -> client"); @@ -70,12 +70,12 @@ public static void server() { PlayPackets.registerServerReceiver(PLAY_C2S, (server, player, handler, buf, responseSender) -> LOGGER.info(buf.readUtf())); - PlayPackets.registerServerReceiver(TestPlayPayload.ID, TestPlayPayload::new, (server, player, handler, payload, responseSender) -> + PlayPackets.registerServerReceiver(TestPlayPayload.TYPE, TestPlayPayload.CODEC, (server, player, handler, payload, responseSender) -> LOGGER.info(payload.msg())); PlayPackets.registerServerReadyCallback((handler, sender, server) -> { Validate.isTrue(sender.canSend(BadPacketTest.PLAY_S2C)); - Validate.isTrue(sender.canSend(TestPlayPayload.ID)); + Validate.isTrue(sender.canSend(TestPlayPayload.TYPE)); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); buf.writeUtf("[play untyped] server -> client"); @@ -88,7 +88,7 @@ public static void server() { public static void client() { // TASK -------------------------------------------------------------------------------------------------------- - ConfigPackets.registerClientReceiver(TestTaskPayload.ID, TestTaskPayload::new, (client, handler, payload, responseSender) -> { + ConfigPackets.registerClientReceiver(TestTaskPayload.TYPE, TestTaskPayload.CODEC, (client, handler, payload, responseSender) -> { LOGGER.info("[config task] server -> client " + payload.stage().name()); switch (payload.stage()) { @@ -104,12 +104,12 @@ public static void client() { ConfigPackets.registerClientReceiver(CONFIG_S2C, (client, handler, buf, responseSender) -> LOGGER.info(buf.readUtf())); - ConfigPackets.registerClientReceiver(TestConfigPayload.ID, TestConfigPayload::new, (client, handler, payload, responseSender) -> + ConfigPackets.registerClientReceiver(TestConfigPayload.TYPE, TestConfigPayload.CODEC, (client, handler, payload, responseSender) -> LOGGER.info(payload.msg())); ConfigPackets.registerClientReadyCallback((handler, sender, client) -> { Validate.isTrue(sender.canSend(BadPacketTest.CONFIG_C2S)); - Validate.isTrue(sender.canSend(TestConfigPayload.ID)); + Validate.isTrue(sender.canSend(TestConfigPayload.TYPE)); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); buf.writeUtf("[config untyped] client -> server"); @@ -123,12 +123,12 @@ public static void client() { PlayPackets.registerClientReceiver(PLAY_S2C, (client, handler, buf, responseSender) -> LOGGER.info(buf.readUtf())); - PlayPackets.registerClientReceiver(TestPlayPayload.ID, TestPlayPayload::new, (client, handler, payload, responseSender) -> + PlayPackets.registerClientReceiver(TestPlayPayload.TYPE, TestPlayPayload.CODEC, (client, handler, payload, responseSender) -> LOGGER.info(payload.msg())); PlayPackets.registerClientReadyCallback((handler, sender, client) -> { Validate.isTrue(sender.canSend(BadPacketTest.PLAY_C2S)); - Validate.isTrue(sender.canSend(TestPlayPayload.ID)); + Validate.isTrue(sender.canSend(TestPlayPayload.TYPE)); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); buf.writeUtf("[play untyped] client -> server"); diff --git a/src/testmod/java/lol/bai/badpackets/test/TestConfigPayload.java b/src/testmod/java/lol/bai/badpackets/test/TestConfigPayload.java index 94f8d1b..c3c8fc7 100644 --- a/src/testmod/java/lol/bai/badpackets/test/TestConfigPayload.java +++ b/src/testmod/java/lol/bai/badpackets/test/TestConfigPayload.java @@ -2,28 +2,28 @@ import lol.bai.badpackets.impl.Constants; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; public record TestConfigPayload( String msg ) implements CustomPacketPayload { - public static final ResourceLocation ID = Constants.id("test/config/payload"); + public static final Type TYPE = new Type<>(Constants.id("test/config/payload")); + public static final StreamCodec CODEC = CustomPacketPayload.codec(TestConfigPayload::write, TestConfigPayload::new); public TestConfigPayload(FriendlyByteBuf buf) { this(buf.readUtf()); } - @Override public void write(@NotNull FriendlyByteBuf buf) { buf.writeUtf(msg); } @Override - public @NotNull ResourceLocation id() { - return ID; + public @NotNull Type type() { + return TYPE; } } diff --git a/src/testmod/java/lol/bai/badpackets/test/TestPlayPayload.java b/src/testmod/java/lol/bai/badpackets/test/TestPlayPayload.java index 622c911..e3528d3 100644 --- a/src/testmod/java/lol/bai/badpackets/test/TestPlayPayload.java +++ b/src/testmod/java/lol/bai/badpackets/test/TestPlayPayload.java @@ -2,28 +2,28 @@ import lol.bai.badpackets.impl.Constants; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; public record TestPlayPayload( String msg ) implements CustomPacketPayload { - public static final ResourceLocation ID = Constants.id("test/play/payload"); + public static final Type TYPE = new Type<>(Constants.id("test/play/payload")); + public static final StreamCodec CODEC = CustomPacketPayload.codec(TestPlayPayload::write, TestPlayPayload::new); public TestPlayPayload(FriendlyByteBuf buf) { this(buf.readUtf()); } - @Override public void write(@NotNull FriendlyByteBuf buf) { buf.writeUtf(msg); } @Override - public @NotNull ResourceLocation id() { - return ID; + public @NotNull Type type() { + return TYPE; } } diff --git a/src/testmod/java/lol/bai/badpackets/test/TestTaskPayload.java b/src/testmod/java/lol/bai/badpackets/test/TestTaskPayload.java index 912d8be..4828967 100644 --- a/src/testmod/java/lol/bai/badpackets/test/TestTaskPayload.java +++ b/src/testmod/java/lol/bai/badpackets/test/TestTaskPayload.java @@ -2,15 +2,16 @@ import lol.bai.badpackets.impl.Constants; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; public record TestTaskPayload( Stage stage ) implements CustomPacketPayload { - public static final ResourceLocation ID = Constants.id("test/config/task/payload"); + public static final Type TYPE = new Type<>(Constants.id("test/config/task/payload")); + public static final StreamCodec CODEC = CustomPacketPayload.codec(TestTaskPayload::write, TestTaskPayload::new); public enum Stage { QUESTION_1, @@ -25,14 +26,13 @@ public TestTaskPayload(FriendlyByteBuf buf) { this(buf.readEnum(Stage.class)); } - @Override public void write(@NotNull FriendlyByteBuf buf) { buf.writeEnum(stage); } @Override - public @NotNull ResourceLocation id() { - return ID; + public Type type() { + return TYPE; } }