-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix Chest Label client server desync
- Loading branch information
Showing
6 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/axanthic/icaria/common/network/packet/ChestLabelPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/axanthic/icaria/common/network/runnable/ChestLabelRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |