| 1 | package net.minecraft.world;
|
| 2 |
|
| 3 | import java.util.Iterator;
|
| 4 | import java.util.List;
|
| 5 | import java.util.NoSuchElementException;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.function.Predicate;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.world.entity.ContainerUser;
|
| 10 | import net.minecraft.world.entity.SlotAccess;
|
| 11 | import net.minecraft.world.entity.SlotProvider;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.item.Item;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public interface Container extends Clearable, Iterable<ItemStack>, SlotProvider {
|
| 20 | float DEFAULT_DISTANCE_BUFFER = 4.0F;
|
| 21 |
|
| 22 | int getContainerSize();
|
| 23 |
|
| 24 | boolean isEmpty();
|
| 25 |
|
| 26 | ItemStack getItem(int slot);
|
| 27 |
|
| 28 | ItemStack removeItem(int slot, int count);
|
| 29 |
|
| 30 | ItemStack removeItemNoUpdate(int slot);
|
| 31 |
|
| 32 | void setItem(int slot, ItemStack itemStack);
|
| 33 |
|
| 34 | default int getMaxStackSize() {
|
| 35 | return 99;
|
| 36 | }
|
| 37 |
|
| 38 | default int getMaxStackSize(final ItemStack itemStack) {
|
| 39 | return Math.min(this.getMaxStackSize(), itemStack.getMaxStackSize());
|
| 40 | }
|
| 41 |
|
| 42 | void setChanged();
|
| 43 |
|
| 44 | boolean stillValid(Player player);
|
| 45 |
|
| 46 | default void startOpen(final ContainerUser containerUser) {
|
| 47 | }
|
| 48 |
|
| 49 | default void stopOpen(final ContainerUser containerUser) {
|
| 50 | }
|
| 51 |
|
| 52 | default List<ContainerUser> getEntitiesWithContainerOpen() {
|
| 53 | return List.of();
|
| 54 | }
|
| 55 |
|
| 56 | default boolean canPlaceItem(final int slot, final ItemStack itemStack) {
|
| 57 | return true;
|
| 58 | }
|
| 59 |
|
| 60 | default boolean canTakeItem(final Container into, final int slot, final ItemStack itemStack) {
|
| 61 | return true;
|
| 62 | }
|
| 63 |
|
| 64 | default int countItem(final Item item) {
|
| 65 | int count = 0;
|
| 66 |
|
| 67 | for (ItemStack slotItem : this) {
|
| 68 | if (slotItem.getItem().equals(item)) {
|
| 69 | count += slotItem.getCount();
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return count;
|
| 74 | }
|
| 75 |
|
| 76 | default boolean hasAnyOf(final Set<Item> item) {
|
| 77 | return this.hasAnyMatching(stack -> !stack.isEmpty() && item.contains(stack.getItem()));
|
| 78 | }
|
| 79 |
|
| 80 | default boolean hasAnyMatching(final Predicate<ItemStack> predicate) {
|
| 81 | for (ItemStack slotItem : this) {
|
| 82 | if (predicate.test(slotItem)) {
|
| 83 | return true;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | return false;
|
| 88 | }
|
| 89 |
|
| 90 | static boolean stillValidBlockEntity(final BlockEntity blockEntity, final Player player) {
|
| 91 | return stillValidBlockEntity(blockEntity, player, 4.0F);
|
| 92 | }
|
| 93 |
|
| 94 | static boolean stillValidBlockEntity(final BlockEntity blockEntity, final Player player, final float distanceBuffer) {
|
| 95 | Level level = blockEntity.getLevel();
|
| 96 | BlockPos worldPosition = blockEntity.getBlockPos();
|
| 97 | if (level == null) {
|
| 98 | return false;
|
| 99 | } else {
|
| 100 | return level.getBlockEntity(worldPosition) != blockEntity ? false : player.isWithinBlockInteractionRange(worldPosition, distanceBuffer);
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | default @Nullable SlotAccess getSlot(final int slot) {
|
| 106 | return slot >= 0 && slot < this.getContainerSize() ? new SlotAccess() {
|
| 107 | @Override
|
| 108 | public ItemStack get() {
|
| 109 | return Container.this.getItem(slot);
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public boolean set(final ItemStack itemStack) {
|
| 114 | Container.this.setItem(slot, itemStack);
|
| 115 | return true;
|
| 116 | }
|
| 117 | } : null;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | default Iterator<ItemStack> iterator() {
|
| 122 | return new Container.ContainerIterator(this);
|
| 123 | }
|
| 124 |
|
| 125 | class ContainerIterator implements Iterator<ItemStack> {
|
| 126 | private final Container container;
|
| 127 | private int index;
|
| 128 | private final int size;
|
| 129 |
|
| 130 | public ContainerIterator(final Container container) {
|
| 131 | this.container = container;
|
| 132 | this.size = container.getContainerSize();
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public boolean hasNext() {
|
| 137 | return this.index < this.size;
|
| 138 | }
|
| 139 |
|
| 140 | public ItemStack next() {
|
| 141 | if (!this.hasNext()) {
|
| 142 | throw new NoSuchElementException();
|
| 143 | } else {
|
| 144 | return this.container.getItem(this.index++);
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 |
|