From eb553e402056190bfff1cf703556879eb6ea3f3d Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Sat, 18 May 2024 15:30:41 +0200 Subject: [PATCH] wip: Port to Minecraft 1.20.6 --- .../mods/balm/mixin/ChatComponentMixin.java | 8 +- .../forge/network/ForgeBalmNetworking.java | 86 +++++++++---------- .../network/NeoForgeBalmNetworking.java | 2 +- 3 files changed, 44 insertions(+), 52 deletions(-) diff --git a/fabric/src/main/java/net/blay09/mods/balm/mixin/ChatComponentMixin.java b/fabric/src/main/java/net/blay09/mods/balm/mixin/ChatComponentMixin.java index a6f0397f..d1cf7d0a 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/mixin/ChatComponentMixin.java +++ b/fabric/src/main/java/net/blay09/mods/balm/mixin/ChatComponentMixin.java @@ -19,8 +19,8 @@ public class ChatComponentMixin { @Shadow private Minecraft minecraft; - @Inject(method = "render(Lnet/minecraft/client/gui/GuiGraphics;III)V", at = @At("HEAD"), cancellable = true) - public void renderPre(GuiGraphics guiGraphics, int tickCount, int x, int y, CallbackInfo callbackInfo) { + @Inject(method = "render(Lnet/minecraft/client/gui/GuiGraphics;IIIZ)V", at = @At("HEAD"), cancellable = true) + public void renderPre(GuiGraphics guiGraphics, int tickCount, int x, int y, boolean bl, CallbackInfo callbackInfo) { GuiDrawEvent.Pre event = new GuiDrawEvent.Pre(minecraft.getWindow(), guiGraphics, GuiDrawEvent.Element.CHAT); Balm.getEvents().fireEvent(event); if (event.isCanceled()) { @@ -28,8 +28,8 @@ public void renderPre(GuiGraphics guiGraphics, int tickCount, int x, int y, Call } } - @Inject(method = "render(Lnet/minecraft/client/gui/GuiGraphics;III)V", at = @At("TAIL")) - public void renderPost(GuiGraphics guiGraphics, int tickCount, int x, int y, CallbackInfo callbackInfo) { + @Inject(method = "render(Lnet/minecraft/client/gui/GuiGraphics;IIIZ)V", at = @At("TAIL")) + public void renderPost(GuiGraphics guiGraphics, int tickCount, int x, int y, boolean bl, CallbackInfo callbackInfo) { Balm.getEvents().fireEvent(new GuiDrawEvent.Post(minecraft.getWindow(), guiGraphics, GuiDrawEvent.Element.CHAT)); } } diff --git a/forge/src/main/java/net/blay09/mods/balm/forge/network/ForgeBalmNetworking.java b/forge/src/main/java/net/blay09/mods/balm/forge/network/ForgeBalmNetworking.java index fcbd1415..59d6f484 100644 --- a/forge/src/main/java/net/blay09/mods/balm/forge/network/ForgeBalmNetworking.java +++ b/forge/src/main/java/net/blay09/mods/balm/forge/network/ForgeBalmNetworking.java @@ -8,6 +8,7 @@ import net.blay09.mods.balm.api.network.ServerboundMessageRegistration; import net.minecraft.core.BlockPos; import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; @@ -31,8 +32,8 @@ public class ForgeBalmNetworking implements BalmNetworking { private static final Logger logger = LoggerFactory.getLogger(ForgeBalmNetworking.class); - private static final Map, MessageRegistration> messagesByClass = new ConcurrentHashMap<>(); - private static final Map> messagesByIdentifier = new ConcurrentHashMap<>(); + private static final Map, MessageRegistration> messagesByClass = new ConcurrentHashMap<>(); + private static final Map> messagesByIdentifier = new ConcurrentHashMap<>(); private static final Map discriminatorCounter = new ConcurrentHashMap<>(); private static CustomPayloadEvent.Context replyContext; @@ -51,7 +52,7 @@ public void allowServerOnly(String modId) { public void openGui(Player player, MenuProvider menuProvider) { if (player instanceof ServerPlayer serverPlayer) { if (menuProvider instanceof BalmMenuProvider balmMenuProvider) { - serverPlayer.openMenu(menuProvider, buf -> balmMenuProvider.writeScreenOpeningData((ServerPlayer) player, buf)); + // TODO serverPlayer.openMenu(menuProvider, buf -> balmMenuProvider.writeScreenOpeningData((ServerPlayer) player, buf)); } else { serverPlayer.openMenu(menuProvider); } @@ -59,76 +60,65 @@ public void openGui(Player player, MenuProvider menuProvider) { } @Override - public void reply(T message) { + public void reply(T message) { if (replyContext == null) { throw new IllegalStateException("No context to reply to"); } - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + final var messageRegistration = getMessageRegistrationOrThrow(message); + final var type = messageRegistration.getType(); + final var channel = NetworkChannels.get(type.id().getNamespace()); channel.reply(message, replyContext); } @Override - public void sendTo(Player player, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + public void sendTo(Player player, T message) { + final var messageRegistration = getMessageRegistrationOrThrow(message); + final var type = messageRegistration.getType(); + final var channel = NetworkChannels.get(type.id().getNamespace()); channel.send(message, PacketDistributor.PLAYER.with((ServerPlayer) player)); } @Override - public void sendToTracking(ServerLevel world, BlockPos pos, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + public void sendToTracking(ServerLevel world, BlockPos pos, T message) { + final var messageRegistration = getMessageRegistrationOrThrow(message); + final var type = messageRegistration.getType(); + final var channel = NetworkChannels.get(type.id().getNamespace()); channel.send(message, PacketDistributor.TRACKING_CHUNK.with(world.getChunkAt(pos))); } @Override - public void sendToTracking(Entity entity, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + public void sendToTracking(Entity entity, T message) { + final var messageRegistration = getMessageRegistrationOrThrow(message); + final var type = messageRegistration.getType(); + final var channel = NetworkChannels.get(type.id().getNamespace()); channel.send(message, PacketDistributor.TRACKING_ENTITY.with(entity)); } @Override - public void sendToAll(MinecraftServer server, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + public void sendToAll(MinecraftServer server, T message) { + final var messageRegistration = getMessageRegistrationOrThrow(message); + final var type = messageRegistration.getType(); + final var channel = NetworkChannels.get(type.id().getNamespace()); channel.send(message, PacketDistributor.ALL.noArg()); } @Override - public void sendToServer(T message) { + public void sendToServer(T message) { if (!Balm.getProxy().isConnectedToServer()) { logger.debug("Skipping message {} because we're not connected to a server", message); return; } - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + final var messageRegistration = getMessageRegistrationOrThrow(message); + final var type = messageRegistration.getType(); + final var channel = NetworkChannels.get(type.id().getNamespace()); channel.send(message, PacketDistributor.SERVER.noArg()); } @SuppressWarnings("unchecked") - private MessageRegistration getMessageRegistrationOrThrow(T message) { - MessageRegistration messageRegistration = (MessageRegistration) messagesByClass.get(message.getClass()); + private MessageRegistration getMessageRegistrationOrThrow(T message) { + final var messageRegistration = (MessageRegistration) messagesByClass.get(message.getClass()); if (messageRegistration == null) { throw new IllegalArgumentException("Cannot send message " + message.getClass() + " as it is not registered"); } @@ -136,8 +126,9 @@ private MessageRegistration getMessageRegistrationOrThrow(T message) { } @Override - public void registerClientboundPacket(ResourceLocation identifier, Class clazz, BiConsumer encodeFunc, Function decodeFunc, BiConsumer handler) { - ClientboundMessageRegistration messageRegistration = new ClientboundMessageRegistration<>(identifier, clazz, encodeFunc, decodeFunc, handler); + public void registerClientboundPacket(ResourceLocation identifier, Class clazz, BiConsumer encodeFunc, Function decodeFunc, BiConsumer handler) { + final var type = new CustomPacketPayload.Type(identifier); + final var messageRegistration = new ClientboundMessageRegistration<>(type, clazz, encodeFunc, decodeFunc, handler); messagesByClass.put(clazz, messageRegistration); messagesByIdentifier.put(identifier, messageRegistration); @@ -145,22 +136,23 @@ public void registerClientboundPacket(ResourceLocation identifier, Class SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); channel.messageBuilder(clazz, nextDiscriminator(identifier.getNamespace()), NetworkDirection.PLAY_TO_CLIENT) .decoder(decodeFunc) - .encoder(encodeFunc) + .encoder((payload, buffer) -> encodeFunc.accept(buffer, payload)) .consumerMainThread((packet, context) -> handler.accept(Balm.getProxy().getClientPlayer(), packet)) .add(); } @Override - public void registerServerboundPacket(ResourceLocation identifier, Class clazz, BiConsumer encodeFunc, Function decodeFunc, BiConsumer handler) { - MessageRegistration messageRegistration = new ServerboundMessageRegistration<>(identifier, clazz, encodeFunc, decodeFunc, handler); + public void registerServerboundPacket(ResourceLocation identifier, Class clazz, BiConsumer encodeFunc, Function decodeFunc, BiConsumer handler) { + final var type = new CustomPacketPayload.Type(identifier); + final var messageRegistration = new ServerboundMessageRegistration<>(type, clazz, encodeFunc, decodeFunc, handler); messagesByClass.put(clazz, messageRegistration); messagesByIdentifier.put(identifier, messageRegistration); - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + final var channel = NetworkChannels.get(identifier.getNamespace()); channel.messageBuilder(clazz, nextDiscriminator(identifier.getNamespace()), NetworkDirection.PLAY_TO_SERVER) .decoder(decodeFunc) - .encoder(encodeFunc) + .encoder((payload, buffer) -> encodeFunc.accept(buffer, payload)) .consumerMainThread((packet, context) -> { replyContext = context; handler.accept(context.getSender(), packet); diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java index 3cf2d84f..ec19df31 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java @@ -115,7 +115,7 @@ public void allowServerOnly(String modId) { public void openGui(Player player, MenuProvider menuProvider) { if (player instanceof ServerPlayer serverPlayer) { if (menuProvider instanceof BalmMenuProvider balmMenuProvider) { - serverPlayer.openMenu(menuProvider, buf -> balmMenuProvider.writeScreenOpeningData(serverPlayer, buf)); + // TODO serverPlayer.openMenu(menuProvider, buf -> balmMenuProvider.writeScreenOpeningData(serverPlayer, buf)); } else { serverPlayer.openMenu(menuProvider); }