diff --git a/src/main/java/org/teacon/slides/block/ProjectorBlock.java b/src/main/java/org/teacon/slides/block/ProjectorBlock.java index bb40995..e192d9b 100644 --- a/src/main/java/org/teacon/slides/block/ProjectorBlock.java +++ b/src/main/java/org/teacon/slides/block/ProjectorBlock.java @@ -4,6 +4,7 @@ import net.minecraft.MethodsReturnNonnullByDefault; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.core.component.DataComponents; import net.minecraft.server.level.ServerLevel; import net.minecraft.util.ByIdMap; import net.minecraft.util.RandomSource; @@ -155,7 +156,9 @@ public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, if (level.getBlockEntity(pos) instanceof ProjectorBlockEntity projector) { var item = this.asItem().getDefaultInstance(); item.applyComponents(projector.collectComponents()); - if (!ItemStack.isSameItemSameComponents(item, this.asItem().getDefaultInstance())) { + var compareItem = this.asItem().getDefaultInstance(); + compareItem.set(DataComponents.BLOCK_STATE, item.get(DataComponents.BLOCK_STATE)); + if (!ItemStack.isSameItemSameComponents(item, compareItem)) { var itemEntity = new ItemEntity(level, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, item); itemEntity.setDefaultPickUpDelay(); level.addFreshEntity(itemEntity); diff --git a/src/main/java/org/teacon/slides/block/ProjectorBlockEntity.java b/src/main/java/org/teacon/slides/block/ProjectorBlockEntity.java index 7ea9341..4f3e905 100644 --- a/src/main/java/org/teacon/slides/block/ProjectorBlockEntity.java +++ b/src/main/java/org/teacon/slides/block/ProjectorBlockEntity.java @@ -15,12 +15,14 @@ import net.minecraft.network.Connection; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.BlockItemStateProperties; import net.minecraft.world.item.component.ItemContainerContents; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; @@ -106,6 +108,15 @@ protected void applyImplicitComponents(BlockEntity.DataComponentInput componentI this.mItemsDisplayed.setStackInSlot(i, i < count ? container.getStackInSlot(i) : ItemStack.EMPTY); this.mItemsToDisplay.setStackInSlot(i, j < count ? container.getStackInSlot(j) : ItemStack.EMPTY); } + var rotation = componentInput.getOrDefault(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY); + if (this.level instanceof ServerLevel serverLevel && serverLevel.isLoaded(this.getBlockPos())) { + var state = rotation.apply(this.getBlockState()); + // update states + if (!serverLevel.setBlock(this.getBlockPos(), state, Block.UPDATE_ALL)) { + // state is unchanged, but re-render it + serverLevel.sendBlockUpdated(this.getBlockPos(), state, state, Block.UPDATE_CLIENTS); + } + } } @Override @@ -118,6 +129,8 @@ protected void collectImplicitComponents(DataComponentMap.Builder components) { containerItems.set(j, this.mItemsToDisplay.getStackInSlot(i)); } components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(containerItems)); + var rotation = BlockItemStateProperties.EMPTY.with(ProjectorBlock.ROTATION, this.getBlockState()); + components.set(DataComponents.BLOCK_STATE, rotation); } @Override