feat: sync with blockentity update packet

This commit is contained in:
SushiCannibale 2025-08-04 15:15:09 +02:00
parent 9188fcaef1
commit ebc9f61f9b
2 changed files with 32 additions and 1 deletions

View file

@ -116,6 +116,7 @@ public class FloralWorkbench extends BaseEntityBlock
Containers.dropItemStack(level, pos.getX(),
pos.getY() + 1.0f, pos.getZ(), crown);
blockentity.setChanged();
level.sendBlockUpdated(pos, state, state, 3);
return InteractionResult.SUCCESS_SERVER;
}
else if (stack.is(CnFRegistries.Items.FLOWER_CROWN.get()) &&
@ -124,6 +125,7 @@ public class FloralWorkbench extends BaseEntityBlock
handler.insertItem(0, stack.copy(), false);
stack.consume(1, player);
blockentity.setChanged();
level.sendBlockUpdated(pos, state, state, 3);
return InteractionResult.CONSUME;
} else {
return InteractionResult.FAIL;
@ -131,7 +133,7 @@ public class FloralWorkbench extends BaseEntityBlock
}
else
{
return hasCrown ^ emptyHand ? InteractionResult.SUCCESS :
return !hasCrown ^ emptyHand ? InteractionResult.SUCCESS :
InteractionResult.FAIL;
}
}

View file

@ -4,11 +4,16 @@ import fr.sushi.charmsnfabrics.common.CnFRegistries;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;
public class FloralWorkbenchBlockEntity extends BlockEntity
{
@ -39,6 +44,30 @@ public class FloralWorkbenchBlockEntity extends BlockEntity
.ifPresent(tag -> itemHandler.deserializeNBT(levelRegistry, tag));
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries)
{
CompoundTag tag = super.getUpdateTag(registries);
tag.put("Inventory", itemHandler.serializeNBT(registries));
return tag;
}
@Override
public void handleUpdateTag(CompoundTag nbt,
HolderLookup.Provider lookupProvider)
{
super.handleUpdateTag(nbt, lookupProvider);
nbt.getCompound("Inventory")
.ifPresent(tag -> itemHandler.deserializeNBT(lookupProvider, tag));
}
@Override
public @Nullable Packet<ClientGamePacketListener> getUpdatePacket()
{
return ClientboundBlockEntityDataPacket.create(this);
}
public RandomSource getRandomSource()
{
return this.randomSource;