Skip to content

Commit

Permalink
Add internal rotation to picked projector items
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Aug 20, 2024
1 parent 4fe673a commit bb619c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/org/teacon/slides/block/ProjectorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/teacon/slides/block/ProjectorBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit bb619c6

Please sign in to comment.