From e105e0365838e03366536c059e734664974f824f Mon Sep 17 00:00:00 2001 From: luxtracon Date: Tue, 19 Nov 2024 10:43:30 +0100 Subject: [PATCH] fix Chest Label client server desync --- src/main/java/com/axanthic/icaria/Icaria.java | 2 + .../client/screen/ChestLabelScreen.java | 5 +- .../icaria/common/item/ChestLabelItem.java | 6 ++- .../network/packet/ChestLabelPacket.java | 52 +++++++++++++++++++ .../common/network/packet/TotemPacket.java | 2 +- .../network/runnable/ChestLabelRunnable.java | 28 ++++++++++ 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/axanthic/icaria/common/network/packet/ChestLabelPacket.java create mode 100644 src/main/java/com/axanthic/icaria/common/network/runnable/ChestLabelRunnable.java diff --git a/src/main/java/com/axanthic/icaria/Icaria.java b/src/main/java/com/axanthic/icaria/Icaria.java index 19ba08bd..4055837a 100644 --- a/src/main/java/com/axanthic/icaria/Icaria.java +++ b/src/main/java/com/axanthic/icaria/Icaria.java @@ -1,6 +1,7 @@ package com.axanthic.icaria; import com.axanthic.icaria.common.config.IcariaConfig; +import com.axanthic.icaria.common.network.packet.ChestLabelPacket; import com.axanthic.icaria.common.network.packet.TotemPacket; import com.axanthic.icaria.common.registry.*; @@ -94,6 +95,7 @@ public void onFMLCommonSetup(FMLCommonSetupEvent pEvent) { public void onRegisterPayloadHandlers(RegisterPayloadHandlersEvent pEvent) { var registrar = pEvent.registrar(IcariaIdents.ID); registrar.playToClient(TotemPacket.TYPE, TotemPacket.STREAM_CODEC, TotemPacket::handler); + registrar.playToServer(ChestLabelPacket.TYPE, ChestLabelPacket.STREAM_CODEC, ChestLabelPacket::handler); } public void registerProperty(Item pItem) { diff --git a/src/main/java/com/axanthic/icaria/client/screen/ChestLabelScreen.java b/src/main/java/com/axanthic/icaria/client/screen/ChestLabelScreen.java index 8797cf91..dad3e754 100644 --- a/src/main/java/com/axanthic/icaria/client/screen/ChestLabelScreen.java +++ b/src/main/java/com/axanthic/icaria/client/screen/ChestLabelScreen.java @@ -1,5 +1,6 @@ package com.axanthic.icaria.client.screen; +import com.axanthic.icaria.common.network.packet.ChestLabelPacket; import com.axanthic.icaria.common.registry.IcariaColors; import com.axanthic.icaria.common.registry.IcariaDataComponents; import com.axanthic.icaria.common.registry.IcariaIdents; @@ -15,6 +16,8 @@ import net.minecraft.network.chat.Component; import net.minecraft.world.item.ItemStack; +import net.neoforged.neoforge.network.PacketDistributor; + import javax.annotation.ParametersAreNonnullByDefault; @MethodsReturnNonnullByDefault @@ -38,7 +41,7 @@ public void cancel() { } public void done() { - this.stack.set(IcariaDataComponents.LABEL, this.box.getValue()); + PacketDistributor.sendToServer(new ChestLabelPacket(this.stack, this.box.getValue())); this.minecraft.setScreen(null); } diff --git a/src/main/java/com/axanthic/icaria/common/item/ChestLabelItem.java b/src/main/java/com/axanthic/icaria/common/item/ChestLabelItem.java index ecc364ea..229265f5 100644 --- a/src/main/java/com/axanthic/icaria/common/item/ChestLabelItem.java +++ b/src/main/java/com/axanthic/icaria/common/item/ChestLabelItem.java @@ -92,8 +92,10 @@ public InteractionResultHolder use(Level pLevel, Player pPlayer, Inte var stack = pPlayer.getItemInHand(pUsedHand); if (pPlayer instanceof LocalPlayer localPlayer) { - Minecraft.getInstance().setScreen(new ChestLabelScreen(stack)); - localPlayer.awardStat(Stats.ITEM_USED.get(this)); + if (pUsedHand.equals(InteractionHand.MAIN_HAND)) { + Minecraft.getInstance().setScreen(new ChestLabelScreen(stack)); + localPlayer.awardStat(Stats.ITEM_USED.get(this)); + } } return InteractionResultHolder.sidedSuccess(stack, pLevel.isClientSide()); diff --git a/src/main/java/com/axanthic/icaria/common/network/packet/ChestLabelPacket.java b/src/main/java/com/axanthic/icaria/common/network/packet/ChestLabelPacket.java new file mode 100644 index 00000000..3af1a079 --- /dev/null +++ b/src/main/java/com/axanthic/icaria/common/network/packet/ChestLabelPacket.java @@ -0,0 +1,52 @@ +package com.axanthic.icaria.common.network.packet; + +import com.axanthic.icaria.common.network.runnable.ChestLabelRunnable; +import com.axanthic.icaria.common.registry.IcariaIdents; + +import net.minecraft.MethodsReturnNonnullByDefault; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; + +import net.neoforged.neoforge.network.handling.IPayloadContext; + +import javax.annotation.ParametersAreNonnullByDefault; + +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault + +public class ChestLabelPacket implements CustomPacketPayload { + public ItemStack stack; + + public String string; + + public static final StreamCodec STREAM_CODEC = CustomPacketPayload.codec(ChestLabelPacket::write, ChestLabelPacket::new); + + public static final Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(IcariaIdents.ID, "chest_label")); + + public ChestLabelPacket(ItemStack pStack, String pString) { + this.stack = pStack; + this.string = pString; + } + + public ChestLabelPacket(RegistryFriendlyByteBuf pBuffer) { + this(ItemStack.STREAM_CODEC.decode(pBuffer), ByteBufCodecs.STRING_UTF8.decode(pBuffer)); + } + + public static void handler(ChestLabelPacket pPacket, IPayloadContext pContext) { + pContext.enqueueWork(new ChestLabelRunnable(pPacket, pContext)); + } + + public void write(RegistryFriendlyByteBuf pBuffer) { + ItemStack.STREAM_CODEC.encode(pBuffer, this.stack); + ByteBufCodecs.STRING_UTF8.encode(pBuffer, this.string); + } + + @Override + public Type type() { + return ChestLabelPacket.TYPE; + } +} diff --git a/src/main/java/com/axanthic/icaria/common/network/packet/TotemPacket.java b/src/main/java/com/axanthic/icaria/common/network/packet/TotemPacket.java index 5eb35ced..f9923b66 100644 --- a/src/main/java/com/axanthic/icaria/common/network/packet/TotemPacket.java +++ b/src/main/java/com/axanthic/icaria/common/network/packet/TotemPacket.java @@ -29,8 +29,8 @@ public class TotemPacket implements CustomPacketPayload { public static final Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(IcariaIdents.ID, "totem")); public TotemPacket(ItemStack pStack, Holder pSound) { - this.sound = pSound; this.stack = pStack; + this.sound = pSound; } public TotemPacket(RegistryFriendlyByteBuf pBuffer) { diff --git a/src/main/java/com/axanthic/icaria/common/network/runnable/ChestLabelRunnable.java b/src/main/java/com/axanthic/icaria/common/network/runnable/ChestLabelRunnable.java new file mode 100644 index 00000000..f0c8721c --- /dev/null +++ b/src/main/java/com/axanthic/icaria/common/network/runnable/ChestLabelRunnable.java @@ -0,0 +1,28 @@ +package com.axanthic.icaria.common.network.runnable; + +import com.axanthic.icaria.common.network.packet.ChestLabelPacket; +import com.axanthic.icaria.common.registry.IcariaDataComponents; + +import net.minecraft.MethodsReturnNonnullByDefault; + +import net.neoforged.neoforge.network.handling.IPayloadContext; + +import javax.annotation.ParametersAreNonnullByDefault; + +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault + +public class ChestLabelRunnable implements Runnable { + public IPayloadContext context; + public ChestLabelPacket packet; + + public ChestLabelRunnable(ChestLabelPacket pPacket, IPayloadContext pContext) { + this.context = pContext; + this.packet = pPacket; + } + + @Override + public void run() { + this.context.player().getMainHandItem().set(IcariaDataComponents.LABEL, this.packet.string); + } +}