| 1 | package net.minecraft.world.item.slot;
|
| 2 |
|
| 3 | import java.util.function.Predicate;
|
| 4 | import net.minecraft.world.item.ItemStack;
|
| 5 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 6 | import org.jspecify.annotations.Nullable;
|
| 7 |
|
| 8 | public sealed interface SlotSelector permits SlotSelector.FilteringSelector, SlotSelector.TrackingSelector {
|
| 9 | SlotSelector ANY_SLOT = new SlotSelector.FilteringSelector(null);
|
| 10 | SlotSelector EMPTY_SLOTS = selecting(ItemStack::isEmpty);
|
| 11 | SlotSelector NON_EMPTY_SLOTS = selecting(s -> !s.isEmpty());
|
| 12 |
|
| 13 | boolean trySelectSlot(ItemStack itemInSlot);
|
| 14 |
|
| 15 | SlotSelector filter(Predicate<? super ItemStack> predicate);
|
| 16 |
|
| 17 | SlotSelector limit(int limit);
|
| 18 |
|
| 19 | static SlotSelector selecting(final Predicate<? super ItemStack> predicate) {
|
| 20 | return new SlotSelector.FilteringSelector(predicate);
|
| 21 | }
|
| 22 |
|
| 23 | static SlotSelector tracking(final SlotSelector slotSelector, final MutableInt selectedCount) {
|
| 24 | return new SlotSelector.TrackingSelector(slotSelector, Integer.MAX_VALUE, selectedCount);
|
| 25 | }
|
| 26 |
|
| 27 | static SlotSelector tracking(final MutableInt selectedCount) {
|
| 28 | return tracking(ANY_SLOT, selectedCount);
|
| 29 | }
|
| 30 |
|
| 31 | record FilteringSelector(@Nullable Predicate<? super ItemStack> filter) implements SlotSelector {
|
| 32 | @Override
|
| 33 | public boolean trySelectSlot(final ItemStack itemInSlot) {
|
| 34 | return this.filter == null || this.filter.test(itemInSlot);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public SlotSelector filter(final Predicate<? super ItemStack> predicate) {
|
| 39 | return this.filter == null
|
| 40 | ? new SlotSelector.FilteringSelector(predicate)
|
| 41 | : new SlotSelector.FilteringSelector(t -> this.filter.test(t) && predicate.test(t));
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public SlotSelector limit(final int limit) {
|
| 46 | return new SlotSelector.TrackingSelector(this, limit, new MutableInt());
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | record TrackingSelector(SlotSelector slotSelector, int limit, MutableInt selectedCount) implements SlotSelector {
|
| 51 | @Override
|
| 52 | public boolean trySelectSlot(final ItemStack itemInSlot) {
|
| 53 | if (this.selectedCount.intValue() < this.limit && this.slotSelector.trySelectSlot(itemInSlot)) {
|
| 54 | this.selectedCount.increment();
|
| 55 | return true;
|
| 56 | } else {
|
| 57 | return false;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public SlotSelector filter(final Predicate<? super ItemStack> predicate) {
|
| 63 | return new SlotSelector.TrackingSelector(this.slotSelector.filter(predicate), this.limit, this.selectedCount);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public SlotSelector limit(final int limit) {
|
| 68 | return limit < this.limit ? new SlotSelector.TrackingSelector(this.slotSelector, limit, this.selectedCount) : this;
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 |
|