Add a simulated item sink interator (#230)

This commit is contained in:
hiroscho authored and GitHub committed 2025-12-16 18:07:03 -05:00
1 parent d1e8864309
commit a7ab27b225
4 files changed
+61 -2

No files matched your search

@@ -50,6 +50,15 @@ public interface ItemSink {
return null;
}
/**
* Creates a simulated iterator for this sink. May return null if iterators are not supported. Modifying any backing
* inventories while this iterator exists (without going through the iterator) is undefined behaviour.
*/
@Nullable
default InventoryIterator simulatedSinkIterator() {
return null;
}
/**
* Sets the slots that this sink can insert into. If any given slot (or all of them) are invalid, they may be
* ignored. The sink must never crash, delete items, etc if a slot is invalid - it must be silently ignored.
@@ -8,6 +8,7 @@ import net.minecraftforge.common.util.ForgeDirection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.gtnewhorizon.gtnhlib.GTNHLib;
import com.gtnewhorizon.gtnhlib.capability.item.ItemSink;
import it.unimi.dsi.fastutil.ints.IntIterators;
@@ -103,4 +104,47 @@ public class InventoryItemSink implements ItemSink {
return Math.min(invStackLimit, existingMaxStack);
}
}
@Override
public @Nullable InventoryIterator simulatedSinkIterator() {
return new StandardInventoryIterator(inv, side, getSlots(), allowedSlots) {
@Override
protected boolean canAccess(ItemStack stack, int slot) {
return canInsert(stack, slot);
}
@Override
protected int getSlotStackLimit(int slot, ItemStack stack) {
return InventoryItemSink.this.getSlotStackLimit(slot, stack);
}
@Override
protected void setInventorySlotContents(int slot, ItemStack stack) {}
@Override
protected void markDirty() {}
@Override
protected boolean canExtract(ItemStack stack, int slot) {
return false;
}
@Override
public ItemStack extract(int amount, boolean forced) {
throw new UnsupportedOperationException();
}
@Override
public ImmutableItemStack previous() {
GTNHLib.LOG.warn("This simulated sink iterator doesn't support backward traversal");
return null;
}
@Override
public boolean rewind() {
return false;
}
};
}
}
@@ -122,9 +122,10 @@ public class StandardInventoryIterator extends AbstractInventoryIterator {
if (!ItemUtil.isStackEmpty(inSlot)) {
int toInsert = forced ? stack.getStackSize() : Math.min(maxStack - inSlot.stackSize, stack.getStackSize());
inSlot.stackSize += toInsert;
ItemStack toInsertStack = inSlot.copy();
toInsertStack.stackSize += toInsert;
setInventorySlotContents(slotIndex, inSlot);
setInventorySlotContents(slotIndex, toInsertStack);
markDirty();
@@ -47,6 +47,11 @@ public class WrappedItemIO implements ItemIO {
return sink == null ? InventoryIterator.EMPTY : sink.sinkIterator();
}
@Override
public @Nullable InventoryIterator simulatedSinkIterator() {
return sink == null ? InventoryIterator.EMPTY : sink.simulatedSinkIterator();
}
@Override
public WrappedItemIO then(ItemSink next) {
return new WrappedItemIO(source, ItemSink.chain(sink, next));