| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.NonNullList;
|
| 5 | import net.minecraft.network.chat.Component;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.world.ContainerHelper;
|
| 8 | import net.minecraft.world.entity.player.Inventory;
|
| 9 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 10 | import net.minecraft.world.inventory.DispenserMenu;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.storage.ValueInput;
|
| 14 | import net.minecraft.world.level.storage.ValueOutput;
|
| 15 |
|
| 16 | public class DispenserBlockEntity extends RandomizableContainerBlockEntity {
|
| 17 | public static final int CONTAINER_SIZE = 9;
|
| 18 | private static final Component DEFAULT_NAME = Component.translatable("container.dispenser");
|
| 19 | private NonNullList<ItemStack> items = NonNullList.withSize(9, ItemStack.EMPTY);
|
| 20 |
|
| 21 | protected DispenserBlockEntity(final BlockEntityType<?> type, final BlockPos worldPosition, final BlockState blockState) {
|
| 22 | super(type, worldPosition, blockState);
|
| 23 | }
|
| 24 |
|
| 25 | public DispenserBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 26 | this(BlockEntityTypes.DISPENSER, worldPosition, blockState);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public int getContainerSize() {
|
| 31 | return 9;
|
| 32 | }
|
| 33 |
|
| 34 | public int getRandomSlot(final RandomSource random) {
|
| 35 | this.unpackLootTable(null);
|
| 36 | int replaceSlot = -1;
|
| 37 | int replaceOdds = 1;
|
| 38 |
|
| 39 | for (int i = 0; i < this.items.size(); i++) {
|
| 40 | if (!this.items.get(i).isEmpty() && random.nextInt(replaceOdds++) == 0) {
|
| 41 | replaceSlot = i;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | return replaceSlot;
|
| 46 | }
|
| 47 |
|
| 48 | public ItemStack insertItem(final ItemStack itemStack) {
|
| 49 | int maxStackSize = this.getMaxStackSize(itemStack);
|
| 50 |
|
| 51 | for (int i = 0; i < this.items.size(); i++) {
|
| 52 | ItemStack targetStack = this.items.get(i);
|
| 53 | if (targetStack.isEmpty() || ItemStack.isSameItemSameComponents(itemStack, targetStack)) {
|
| 54 | int transferCount = Math.min(itemStack.getCount(), maxStackSize - targetStack.getCount());
|
| 55 | if (transferCount > 0) {
|
| 56 | if (targetStack.isEmpty()) {
|
| 57 | this.setItem(i, itemStack.split(transferCount));
|
| 58 | } else {
|
| 59 | itemStack.shrink(transferCount);
|
| 60 | targetStack.grow(transferCount);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | if (itemStack.isEmpty()) {
|
| 65 | break;
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | return itemStack;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected Component getDefaultName() {
|
| 75 | return DEFAULT_NAME;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected void loadAdditional(final ValueInput input) {
|
| 80 | super.loadAdditional(input);
|
| 81 | this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
| 82 | if (!this.tryLoadLootTable(input)) {
|
| 83 | ContainerHelper.loadAllItems(input, this.items);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected void saveAdditional(final ValueOutput output) {
|
| 89 | super.saveAdditional(output);
|
| 90 | if (!this.trySaveLootTable(output)) {
|
| 91 | ContainerHelper.saveAllItems(output, this.items);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected NonNullList<ItemStack> getItems() {
|
| 97 | return this.items;
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | protected void setItems(final NonNullList<ItemStack> items) {
|
| 102 | this.items = items;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | protected AbstractContainerMenu createMenu(final int containerId, final Inventory inventory) {
|
| 107 | return new DispenserMenu(containerId, inventory, this);
|
| 108 | }
|
| 109 | }
|
| 110 |
|