Merge pull request #1873 from HenryLoenwind/insertItemsVariant

New way to insert items into inventories
This commit is contained in:
tterrag1098 committed 2015-02-07 14:45:33 -05:00
commit 055da6f435
2 files changed
+102 -70

No files matched your search

@@ -80,7 +80,7 @@ public class TileVacuumChest extends TileEntityEio implements IEntitySelector, I
EntityItem item = (EntityItem) entity;
ItemStack stack = item.getEntityItem().copy();
int numInserted = ItemUtil.doInsertItem(this, stack);
int numInserted = ItemUtil.doInsertItem(this, stack, null);
stack.stackSize -= numInserted;
item.setEntityItemStack(stack);
if(stack.stackSize == 0) {
+101 -69
View File
@@ -47,9 +47,9 @@ public class ItemUtil {
return 0;
}
if(into instanceof ISidedInventory) {
return ItemUtil.doInsertItem((ISidedInventory) into, item, side);
return ItemUtil.doInsertItemInv((ISidedInventory) into, item, side);
} else if(into instanceof IInventory) {
return ItemUtil.doInsertItem(getInventory((IInventory) into), item);
return ItemUtil.doInsertItemInv(getInventory((IInventory) into), item, side);
} else if(into instanceof IItemDuct) {
return ItemUtil.doInsertItem((IItemDuct) into, item, side);
}
@@ -72,93 +72,77 @@ public class ItemUtil {
return startedWith - remaining.stackSize;
}
public static int doInsertItem(ISidedInventory sidedInv, ItemStack item, ForgeDirection inventorySide) {
/*
* Insert items into an IInventory or an ISidedInventory.
*/
private static int doInsertItemInv(IInventory inv, ItemStack item, ForgeDirection inventorySide) {
ISidedInventory sidedInv = inv instanceof ISidedInventory ? (ISidedInventory) inv : null;
ISlotIterator slots;
if(inventorySide == null) {
inventorySide = ForgeDirection.UNKNOWN;
}
int[] slots = sidedInv.getAccessibleSlotsFromSide(inventorySide.ordinal());
if(slots == null) {
return 0;
if (sidedInv != null) {
if(inventorySide == null) {
inventorySide = ForgeDirection.UNKNOWN;
}
slots = new sidedSlotter(sidedInv.getAccessibleSlotsFromSide(inventorySide.ordinal()));
} else {
slots = new invSlotter(inv.getSizeInventory());
}
int numInserted = 0;
int numToInsert = item.stackSize;
for (int i = 0; i < slots.length && numToInsert > 0; i++) {
int slot = slots[i];
if(sidedInv.canInsertItem(slot, item, inventorySide.ordinal())) {
ItemStack contents = sidedInv.getStackInSlot(slot);
ItemStack toInsert = item.copy();
toInsert.stackSize = Math.min(toInsert.stackSize, sidedInv.getInventoryStackLimit());
toInsert.stackSize = Math.min(toInsert.stackSize, toInsert.getMaxStackSize()); // some inventories like using itemstacks with invalid stack sizes
toInsert.stackSize = Math.min(toInsert.stackSize, numToInsert);
int inserted = 0;
if(contents == null) {
inserted = toInsert.stackSize;
} else {
if(contents.isItemEqual(item) && ItemStack.areItemStackTagsEqual(contents, item)) {
int space = sidedInv.getInventoryStackLimit() - contents.stackSize;
space = Math.min(space, contents.getMaxStackSize() - contents.stackSize);
inserted += Math.min(space, toInsert.stackSize);
toInsert.stackSize = contents.stackSize + inserted;
} else {
toInsert.stackSize = 0;
}
}
if(inserted > 0) {
numInserted += inserted;
numToInsert -= inserted;
sidedInv.setInventorySlotContents(slot, toInsert);
// PASS1: Try to add to an existing stack
while (numToInsert > 0 && slots.hasNext()) {
int slot = slots.nextSlot();
if(sidedInv == null || sidedInv.canInsertItem(slot, item, inventorySide.ordinal())) {
ItemStack contents = inv.getStackInSlot(slot);
if(contents != null && areStackMergable(contents, item)) {
int freeSpace = Math.min(inv.getInventoryStackLimit(), contents.getMaxStackSize()) - contents.stackSize; // some inventories like using itemstacks with invalid stack sizes
if (freeSpace > 0) {
int noToInsert = Math.min(numToInsert, freeSpace);
ItemStack toInsert = item.copy();
toInsert.stackSize = contents.stackSize + noToInsert;
// isItemValidForSlot() may check the stacksize, so give it the number the stack would have in the end.
// If it does something funny, like "only even numbers", we are screwed.
if(sidedInv != null || inv.isItemValidForSlot(slot, toInsert)) {
numInserted += noToInsert;
numToInsert -= noToInsert;
inv.setInventorySlotContents(slot, toInsert);
}
}
}
}
}
if(numInserted > 0) {
sidedInv.markDirty();
}
return numInserted;
}
public static int doInsertItem(IInventory inv, ItemStack item) {
int numInserted = 0;
int numToInsert = item.stackSize;
for (int slot = 0; slot < inv.getSizeInventory() && numToInsert > 0; slot++) {
ItemStack contents = inv.getStackInSlot(slot);
if(!isStackFull(contents)) {
ItemStack toInsert = item.copy();
toInsert.stackSize = Math.min(toInsert.stackSize, inv.getInventoryStackLimit());
toInsert.stackSize = Math.min(toInsert.stackSize, toInsert.getMaxStackSize()); // some inventories like using itemstacks with invalid stack sizes
toInsert.stackSize = Math.min(toInsert.stackSize, numToInsert);
int inserted = 0;
slots.reset();
// PASS2: Try to insert into an empty slot
while (numToInsert > 0 && slots.hasNext()) {
int slot = slots.nextSlot();
if(sidedInv == null || sidedInv.canInsertItem(slot, item, inventorySide.ordinal())) {
ItemStack contents = inv.getStackInSlot(slot);
if(contents == null) {
inserted = toInsert.stackSize;
} else {
if(contents.isItemEqual(item) && ItemStack.areItemStackTagsEqual(contents, item)) {
int space = inv.getInventoryStackLimit() - contents.stackSize;
space = Math.min(space, contents.getMaxStackSize() - contents.stackSize);
inserted += Math.min(space, toInsert.stackSize);
toInsert.stackSize = contents.stackSize + inserted;
} else {
toInsert.stackSize = 0;
ItemStack toInsert = item.copy();
toInsert.stackSize = min(numToInsert, inv.getInventoryStackLimit(), toInsert.getMaxStackSize()); // some inventories like using itemstacks with invalid stack sizes
if(sidedInv != null || inv.isItemValidForSlot(slot, toInsert)) {
numInserted += toInsert.stackSize;
numToInsert -= toInsert.stackSize;
inv.setInventorySlotContents(slot, toInsert);
}
}
if(!inv.isItemValidForSlot(slot, toInsert)) {
inserted = 0;
}
if(inserted > 0) {
numInserted += inserted;
numToInsert -= inserted;
inv.setInventorySlotContents(slot, toInsert);
}
}
}
if(numInserted > 0) {
inv.markDirty();
}
return numInserted;
}
private static int min(int i1, int i2, int i3) {
return i1 < i2 ? (i1 < i3 ? i1 : i3) : (i2 < i3 ? i2 : i3);
}
public static boolean isStackFull(ItemStack contents) {
if(contents == null) {
return false;
@@ -221,4 +205,52 @@ public class ItemUtil {
return ItemStack.areItemStackTagsEqual(s1, s2);
}
private interface ISlotIterator {
int nextSlot();
boolean hasNext();
void reset();
}
private static class invSlotter implements ISlotIterator {
final private int size;
private int current;
invSlotter(int size) {
this.size = size;
this.current = 0;
}
@Override
public int nextSlot() {
return current++;
}
@Override
public void reset() {
current = 0;
}
@Override
public boolean hasNext() {
return current < size;
}
}
private static class sidedSlotter implements ISlotIterator {
final private int[] slots;
private int current;
sidedSlotter(int[] slots) {
this.slots = slots;
this.current = 0;
}
@Override
public int nextSlot() {
return slots[current++];
}
@Override
public void reset() {
current = 0;
}
@Override
public boolean hasNext() {
return slots != null && current < slots.length;
}
}
}