Skip to content

Commit

Permalink
fix Chest Label client server desync
Browse files Browse the repository at this point in the history
  • Loading branch information
luxtracon committed Nov 19, 2024
1 parent e139cbf commit e105e03
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/axanthic/icaria/Icaria.java
Original file line number Diff line number Diff line change
@@ -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.*;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public InteractionResultHolder<ItemStack> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RegistryFriendlyByteBuf, ChestLabelPacket> STREAM_CODEC = CustomPacketPayload.codec(ChestLabelPacket::write, ChestLabelPacket::new);

public static final Type<ChestLabelPacket> 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<? extends CustomPacketPayload> type() {
return ChestLabelPacket.TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class TotemPacket implements CustomPacketPayload {
public static final Type<TotemPacket> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(IcariaIdents.ID, "totem"));

public TotemPacket(ItemStack pStack, Holder<SoundEvent> pSound) {
this.sound = pSound;
this.stack = pStack;
this.sound = pSound;
}

public TotemPacket(RegistryFriendlyByteBuf pBuffer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e105e03

Please sign in to comment.