| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.HolderLookup;
|
| 7 | import net.minecraft.core.NonNullList;
|
| 8 | import net.minecraft.core.component.DataComponentGetter;
|
| 9 | import net.minecraft.core.component.DataComponentMap;
|
| 10 | import net.minecraft.core.component.DataComponents;
|
| 11 | import net.minecraft.nbt.CompoundTag;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 13 | import net.minecraft.util.ProblemReporter;
|
| 14 | import net.minecraft.world.Container;
|
| 15 | import net.minecraft.world.ContainerHelper;
|
| 16 | import net.minecraft.world.entity.ItemOwner;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.item.component.ItemContainerContents;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.block.ShelfBlock;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 24 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 25 | import net.minecraft.world.level.storage.ValueInput;
|
| 26 | import net.minecraft.world.level.storage.ValueOutput;
|
| 27 | import net.minecraft.world.phys.Vec3;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 | import org.slf4j.Logger;
|
| 30 |
|
| 31 | public class ShelfBlockEntity extends BlockEntity implements ListBackedContainer, ItemOwner {
|
| 32 | public static final int MAX_ITEMS = 3;
|
| 33 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 34 | private static final String ALIGN_ITEMS_TO_BOTTOM_TAG = "align_items_to_bottom";
|
| 35 | private final NonNullList<ItemStack> items = NonNullList.withSize(3, ItemStack.EMPTY);
|
| 36 | private boolean alignItemsToBottom;
|
| 37 |
|
| 38 | public ShelfBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 39 | super(BlockEntityTypes.SHELF, worldPosition, blockState);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected void loadAdditional(final ValueInput input) {
|
| 44 | super.loadAdditional(input);
|
| 45 | this.items.clear();
|
| 46 | ContainerHelper.loadAllItems(input, this.items);
|
| 47 | this.alignItemsToBottom = input.getBooleanOr("align_items_to_bottom", false);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void saveAdditional(final ValueOutput output) {
|
| 52 | super.saveAdditional(output);
|
| 53 | ContainerHelper.saveAllItems(output, this.items, true);
|
| 54 | output.putBoolean("align_items_to_bottom", this.alignItemsToBottom);
|
| 55 | }
|
| 56 |
|
| 57 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 58 | return ClientboundBlockEntityDataPacket.create(this);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 63 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this.problemPath(), LOGGER)) {
|
| 64 | TagValueOutput output = TagValueOutput.createWithContext(reporter, registries);
|
| 65 | ContainerHelper.saveAllItems(output, this.items, true);
|
| 66 | output.putBoolean("align_items_to_bottom", this.alignItemsToBottom);
|
| 67 | return output.buildResult();
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public NonNullList<ItemStack> getItems() {
|
| 73 | return this.items;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public boolean stillValid(final Player player) {
|
| 78 | return Container.stillValidBlockEntity(this, player);
|
| 79 | }
|
| 80 |
|
| 81 | public ItemStack swapItemNoUpdate(final int slot, final ItemStack heldItemStack) {
|
| 82 | ItemStack retrievedItem = this.removeItemNoUpdate(slot);
|
| 83 | this.setItemNoUpdate(slot, heldItemStack);
|
| 84 | return retrievedItem;
|
| 85 | }
|
| 86 |
|
| 87 | public void setChanged(final Holder.@Nullable Reference<GameEvent> event) {
|
| 88 | super.setChanged();
|
| 89 | if (this.level != null) {
|
| 90 | if (event != null) {
|
| 91 | this.level.gameEvent(event, this.worldPosition, GameEvent.Context.of(this.getBlockState()));
|
| 92 | }
|
| 93 |
|
| 94 | this.getLevel().sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void setChanged() {
|
| 100 | this.setChanged(GameEvent.BLOCK_ACTIVATE);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 105 | super.applyImplicitComponents(components);
|
| 106 | components.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).copyInto(this.items);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 111 | super.collectImplicitComponents(components);
|
| 112 | components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(this.items));
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 117 | output.discard("Items");
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public Level level() {
|
| 122 | return this.level;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public Vec3 position() {
|
| 127 | return Vec3.atCenterOf(this.getBlockPos());
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public float getVisualRotationYInDegrees() {
|
| 132 | return this.getBlockState().getValue(ShelfBlock.FACING).getOpposite().toYRot();
|
| 133 | }
|
| 134 |
|
| 135 | public boolean getAlignItemsToBottom() {
|
| 136 | return this.alignItemsToBottom;
|
| 137 | }
|
| 138 | }
|
| 139 |
|