| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Objects;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.NonNullList;
|
| 7 | import net.minecraft.core.component.DataComponentGetter;
|
| 8 | import net.minecraft.core.component.DataComponentMap;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.tags.ItemTags;
|
| 11 | import net.minecraft.world.Container;
|
| 12 | import net.minecraft.world.ContainerHelper;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.component.ItemContainerContents;
|
| 16 | import net.minecraft.world.level.block.ChiseledBookShelfBlock;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 19 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 20 | import net.minecraft.world.level.storage.ValueInput;
|
| 21 | import net.minecraft.world.level.storage.ValueOutput;
|
| 22 | import org.slf4j.Logger;
|
| 23 |
|
| 24 | public class ChiseledBookShelfBlockEntity extends BlockEntity implements ListBackedContainer {
|
| 25 | public static final int MAX_BOOKS_IN_STORAGE = 6;
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | private static final int DEFAULT_LAST_INTERACTED_SLOT = -1;
|
| 28 | private final NonNullList<ItemStack> items = NonNullList.withSize(6, ItemStack.EMPTY);
|
| 29 | private int lastInteractedSlot = -1;
|
| 30 |
|
| 31 | public ChiseledBookShelfBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 32 | super(BlockEntityTypes.CHISELED_BOOKSHELF, worldPosition, blockState);
|
| 33 | }
|
| 34 |
|
| 35 | private void updateState(final int interactedSlot) {
|
| 36 | if (interactedSlot >= 0 && interactedSlot < 6) {
|
| 37 | this.lastInteractedSlot = interactedSlot;
|
| 38 | BlockState updatedState = this.getBlockState();
|
| 39 |
|
| 40 | for (int slot = 0; slot < ChiseledBookShelfBlock.SLOT_OCCUPIED_PROPERTIES.size(); slot++) {
|
| 41 | boolean slotIsOccupied = !this.getItem(slot).isEmpty();
|
| 42 | BooleanProperty slotProperty = ChiseledBookShelfBlock.SLOT_OCCUPIED_PROPERTIES.get(slot);
|
| 43 | updatedState = updatedState.setValue(slotProperty, slotIsOccupied);
|
| 44 | }
|
| 45 |
|
| 46 | Objects.requireNonNull(this.level).setBlockAndUpdate(this.worldPosition, updatedState);
|
| 47 | this.level.gameEvent(GameEvent.BLOCK_CHANGE, this.worldPosition, GameEvent.Context.of(updatedState));
|
| 48 | } else {
|
| 49 | LOGGER.error("Expected slot 0-5, got {}", interactedSlot);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected void loadAdditional(final ValueInput input) {
|
| 55 | super.loadAdditional(input);
|
| 56 | this.items.clear();
|
| 57 | ContainerHelper.loadAllItems(input, this.items);
|
| 58 | this.lastInteractedSlot = input.getIntOr("last_interacted_slot", -1);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void saveAdditional(final ValueOutput output) {
|
| 63 | super.saveAdditional(output);
|
| 64 | ContainerHelper.saveAllItems(output, this.items, true);
|
| 65 | output.putInt("last_interacted_slot", this.lastInteractedSlot);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public int getMaxStackSize() {
|
| 70 | return 1;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public boolean acceptsItemType(final ItemStack itemStack) {
|
| 75 | return itemStack.is(ItemTags.BOOKSHELF_BOOKS);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public ItemStack removeItem(final int slot, final int count) {
|
| 80 | ItemStack retrievedItem = Objects.requireNonNullElse(this.getItems().get(slot), ItemStack.EMPTY);
|
| 81 | this.getItems().set(slot, ItemStack.EMPTY);
|
| 82 | if (!retrievedItem.isEmpty()) {
|
| 83 | this.updateState(slot);
|
| 84 | }
|
| 85 |
|
| 86 | return retrievedItem;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 91 | if (this.acceptsItemType(itemStack)) {
|
| 92 | this.getItems().set(slot, itemStack);
|
| 93 | this.updateState(slot);
|
| 94 | } else if (itemStack.isEmpty()) {
|
| 95 | this.removeItem(slot, this.getMaxStackSize());
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean canTakeItem(final Container into, final int slot, final ItemStack itemStack) {
|
| 101 | return into.hasAnyMatching(
|
| 102 | toItem -> toItem.isEmpty()
|
| 103 | ? true
|
| 104 | : ItemStack.isSameItemSameComponents(itemStack, toItem) && toItem.getCount() + itemStack.getCount() <= into.getMaxStackSize(toItem)
|
| 105 | );
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public NonNullList<ItemStack> getItems() {
|
| 110 | return this.items;
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public boolean stillValid(final Player player) {
|
| 115 | return Container.stillValidBlockEntity(this, player);
|
| 116 | }
|
| 117 |
|
| 118 | public int getLastInteractedSlot() {
|
| 119 | return this.lastInteractedSlot;
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 124 | super.applyImplicitComponents(components);
|
| 125 | components.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).copyInto(this.items);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 130 | super.collectImplicitComponents(components);
|
| 131 | components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(this.items));
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 136 | output.discard("Items");
|
| 137 | }
|
| 138 | }
|
| 139 |
|