diff --git a/src/main/java/mekanism/common/base/EnergyAcceptorWrapper.java b/src/main/java/mekanism/common/base/EnergyAcceptorWrapper.java index 859116807..465dc32ec 100644 --- a/src/main/java/mekanism/common/base/EnergyAcceptorWrapper.java +++ b/src/main/java/mekanism/common/base/EnergyAcceptorWrapper.java @@ -29,7 +29,7 @@ public abstract class EnergyAcceptorWrapper implements IStrictEnergyAcceptor EnergyAcceptorWrapper wrapper = null; - if(CapabilityUtils.hasCapability(tileEntity, Capabilities.ENERGY_ACCEPTOR_CAPABILITY, null)) + if(CapabilityUtils.hasCapability(tileEntity, Capabilities.ENERGY_ACCEPTOR_CAPABILITY, side)) { wrapper = new MekanismAcceptor(CapabilityUtils.getCapability(tileEntity, Capabilities.ENERGY_ACCEPTOR_CAPABILITY, side)); } diff --git a/src/main/java/mekanism/common/base/ILogisticalTransporter.java b/src/main/java/mekanism/common/base/ILogisticalTransporter.java index 68511b35f..62399380c 100644 --- a/src/main/java/mekanism/common/base/ILogisticalTransporter.java +++ b/src/main/java/mekanism/common/base/ILogisticalTransporter.java @@ -7,12 +7,11 @@ import mekanism.api.transmitters.IGridTransmitter; import mekanism.common.content.transporter.TransporterStack; import mekanism.common.tile.TileEntityLogisticalSorter; import mekanism.common.transmitters.grid.InventoryNetwork; -import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; -public interface ILogisticalTransporter extends IGridTransmitter, IBlockableConnection +public interface ILogisticalTransporter extends IGridTransmitter, IBlockableConnection { public ItemStack insert(Coord4D original, ItemStack itemStack, EnumColor color, boolean doEmit, int min); diff --git a/src/main/java/mekanism/common/capabilities/DefaultLogisticalTransporter.java b/src/main/java/mekanism/common/capabilities/DefaultLogisticalTransporter.java index c3aea3df7..2a76a710d 100644 --- a/src/main/java/mekanism/common/capabilities/DefaultLogisticalTransporter.java +++ b/src/main/java/mekanism/common/capabilities/DefaultLogisticalTransporter.java @@ -10,7 +10,6 @@ import mekanism.common.capabilities.DefaultStorageHelper.NullStorage; import mekanism.common.content.transporter.TransporterStack; import mekanism.common.tile.TileEntityLogisticalSorter; import mekanism.common.transmitters.grid.InventoryNetwork; -import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; @@ -161,7 +160,7 @@ public class DefaultLogisticalTransporter implements ILogisticalTransporter } @Override - public IInventory getAcceptor(EnumFacing side) + public TileEntity getAcceptor(EnumFacing side) { return null; } diff --git a/src/main/java/mekanism/common/content/transporter/InvStack.java b/src/main/java/mekanism/common/content/transporter/InvStack.java index e5e836e33..3e040baa1 100644 --- a/src/main/java/mekanism/common/content/transporter/InvStack.java +++ b/src/main/java/mekanism/common/content/transporter/InvStack.java @@ -2,27 +2,34 @@ package mekanism.common.content.transporter; import java.util.ArrayList; +import mekanism.common.util.InventoryUtils; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.items.IItemHandler; public final class InvStack { - public IInventory inventory; + public TileEntity tileEntity; public ArrayList itemStacks; public ArrayList slotIDs; + public EnumFacing side; - public InvStack(IInventory inv) + public InvStack(TileEntity inv, EnumFacing facing) { - inventory = inv; + tileEntity = inv; itemStacks = new ArrayList(); slotIDs = new ArrayList(); + side = facing; } - public InvStack(IInventory inv, int id, ItemStack stack) + public InvStack(TileEntity inv, int id, ItemStack stack, EnumFacing facing) { - inventory = inv; + tileEntity = inv; itemStacks = new ArrayList(); slotIDs = new ArrayList(); + side = facing; appendStack(id, stack); } @@ -55,25 +62,46 @@ public final class InvStack public void use(int amount) { - for(int i = 0; i < slotIDs.size(); i++) + if(InventoryUtils.isItemHandler(tileEntity, side)) { - ItemStack stack = itemStacks.get(i); - - if(inventory.getStackInSlot(slotIDs.get(i)).getCount() == stack.getCount() && stack.getCount() <= amount) + IItemHandler handler = InventoryUtils.getItemHandler(tileEntity, side); + + for(int i = 0; i < slotIDs.size(); i++) { - inventory.setInventorySlotContents(slotIDs.get(i), ItemStack.EMPTY); + ItemStack stack = itemStacks.get(i); + handler.extractItem(slotIDs.get(i), stack.getCount(), false); amount -= stack.getCount(); + + if(amount == 0) + { + return; + } } - else { - ItemStack ret = stack.copy(); - ret.setCount(inventory.getStackInSlot(slotIDs.get(i)).getCount() - Math.min(stack.getCount(), amount)); - inventory.setInventorySlotContents(slotIDs.get(i), ret); - amount -= ret.getCount(); - } - - if(amount == 0) + } + else if(tileEntity instanceof IInventory) + { + IInventory inventory = InventoryUtils.checkChestInv((IInventory)tileEntity); + + for(int i = 0; i < slotIDs.size(); i++) { - return; + ItemStack stack = itemStacks.get(i); + + if(inventory.getStackInSlot(slotIDs.get(i)).getCount() == stack.getCount() && stack.getCount() <= amount) + { + inventory.setInventorySlotContents(slotIDs.get(i), ItemStack.EMPTY); + amount -= stack.getCount(); + } + else { + ItemStack ret = stack.copy(); + ret.setCount(inventory.getStackInSlot(slotIDs.get(i)).getCount() - stack.getCount()); + inventory.setInventorySlotContents(slotIDs.get(i), ret); + amount -= stack.getCount(); + } + + if(amount == 0) + { + return; + } } } } diff --git a/src/main/java/mekanism/common/content/transporter/StackSearcher.java b/src/main/java/mekanism/common/content/transporter/StackSearcher.java index df5ce3a42..d79961020 100644 --- a/src/main/java/mekanism/common/content/transporter/StackSearcher.java +++ b/src/main/java/mekanism/common/content/transporter/StackSearcher.java @@ -5,81 +5,110 @@ import mekanism.common.util.StackUtils; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraftforge.items.IItemHandler; public class StackSearcher { - public int i; + public int i = -1; public int[] slots; - public IInventory theInventory; + public TileEntity tileEntity; public EnumFacing side; - public StackSearcher(IInventory inventory, EnumFacing direction) + public StackSearcher(TileEntity tile, EnumFacing direction) { - theInventory = InventoryUtils.checkChestInv(inventory); + tileEntity = tile; side = direction; - if(!(theInventory instanceof ISidedInventory)) + if(InventoryUtils.isItemHandler(tile, direction.getOpposite())) { - i = inventory.getSizeInventory(); - } - else { - slots = ((ISidedInventory)theInventory).getSlotsForFace(side.getOpposite()); + i = InventoryUtils.getItemHandler(tile, direction.getOpposite()).getSlots(); + } + else if(tile instanceof ISidedInventory) + { + slots = ((ISidedInventory)tile).getSlotsForFace(side.getOpposite()); if(slots != null) { i = slots.length; } + } + else if(tile instanceof IInventory) + { + i = ((IInventory)tile).getSizeInventory(); } } public InvStack takeTopStack(Finder id) { - if(!(theInventory instanceof ISidedInventory)) + if(InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) { + IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite()); + for(i = i - 1; i >= 0; i--) { - if(!theInventory.getStackInSlot(i).isEmpty() && id.modifies(theInventory.getStackInSlot(i))) + ItemStack stack = inventory.extractItem(i, 64, true); + + if(!stack.isEmpty() && id.modifies(stack)) { - ItemStack toSend = theInventory.getStackInSlot(i).copy(); - return new InvStack(theInventory, i, toSend); + return new InvStack(tileEntity, i, stack, side.getOpposite()); } } } - else { + else if(tileEntity instanceof ISidedInventory) + { + ISidedInventory inventory = (ISidedInventory)tileEntity; + if(slots != null && slots.length != 0) { for(i = i - 1; i >= 0; i--) { int slotID = slots[i]; - if(!theInventory.getStackInSlot(slotID).isEmpty() && id.modifies(theInventory.getStackInSlot(slotID))) + if(!inventory.getStackInSlot(slotID).isEmpty() && id.modifies(inventory.getStackInSlot(slotID))) { - ItemStack toSend = theInventory.getStackInSlot(slotID); + ItemStack toSend = inventory.getStackInSlot(slotID); - if(((ISidedInventory)theInventory).canExtractItem(slotID, toSend, side.getOpposite())) + if(((ISidedInventory)inventory).canExtractItem(slotID, toSend, side.getOpposite())) { - return new InvStack(theInventory, slotID, toSend); + return new InvStack(tileEntity, slotID, toSend, side.getOpposite()); } } } } } + else if(tileEntity instanceof IInventory) + { + IInventory inventory = InventoryUtils.checkChestInv((IInventory)tileEntity); + + for(i = i - 1; i >= 0; i--) + { + if(!inventory.getStackInSlot(i).isEmpty() && id.modifies(inventory.getStackInSlot(i))) + { + ItemStack toSend = inventory.getStackInSlot(i).copy(); + return new InvStack(tileEntity, i, toSend, side.getOpposite()); + } + } + } return null; } public InvStack takeDefinedItem(ItemStack type, int min, int max) { - InvStack ret = new InvStack(theInventory); + InvStack ret = new InvStack(tileEntity, side.getOpposite()); - if(!(theInventory instanceof ISidedInventory)) + if(InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) { + IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite()); + for(i = i - 1; i >= 0; i--) { - if(!theInventory.getStackInSlot(i).isEmpty() && StackUtils.equalsWildcard(theInventory.getStackInSlot(i), type)) + ItemStack stack = inventory.extractItem(i, max, true); + + if(!stack.isEmpty() && StackUtils.equalsWildcard(stack, type)) { - ItemStack stack = theInventory.getStackInSlot(i); int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0; if(current+stack.getCount() <= max) @@ -99,8 +128,9 @@ public class StackSearcher } } } - else { - ISidedInventory sidedInventory = (ISidedInventory)theInventory; + else if(tileEntity instanceof ISidedInventory) + { + ISidedInventory sidedInventory = (ISidedInventory)tileEntity; int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); if(slots != null && slots.length != 0) @@ -109,7 +139,7 @@ public class StackSearcher { int slotID = slots[i]; - if(!sidedInventory.getStackInSlot(slotID).isEmpty() && StackUtils.equalsWildcard(theInventory.getStackInSlot(slotID), type)) + if(!sidedInventory.getStackInSlot(slotID).isEmpty() && StackUtils.equalsWildcard(sidedInventory.getStackInSlot(slotID), type)) { ItemStack stack = sidedInventory.getStackInSlot(slotID); int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0; @@ -141,6 +171,34 @@ public class StackSearcher } } } + else if(tileEntity instanceof IInventory) + { + IInventory inventory = InventoryUtils.checkChestInv((IInventory)tileEntity); + + for(i = i - 1; i >= 0; i--) + { + if(!inventory.getStackInSlot(i).isEmpty() && StackUtils.equalsWildcard(inventory.getStackInSlot(i), type)) + { + ItemStack stack = inventory.getStackInSlot(i); + int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0; + + if(current+stack.getCount() <= max) + { + ret.appendStack(i, stack.copy()); + } + else { + ItemStack copy = stack.copy(); + copy.setCount(max-current); + ret.appendStack(i, copy); + } + + if(!ret.getStack().isEmpty() && ret.getStack().getCount() == max) + { + return ret; + } + } + } + } if(ret != null && !ret.getStack().isEmpty() && ret.getStack().getCount() >= min) { diff --git a/src/main/java/mekanism/common/content/transporter/TransporterManager.java b/src/main/java/mekanism/common/content/transporter/TransporterManager.java index 2bcd66532..a3727c387 100644 --- a/src/main/java/mekanism/common/content/transporter/TransporterManager.java +++ b/src/main/java/mekanism/common/content/transporter/TransporterManager.java @@ -91,8 +91,6 @@ public class TransporterManager public static InventoryCopy copyInvFromSide(IInventory inv, EnumFacing side) { - inv = InventoryUtils.checkChestInv(inv); - NonNullList ret = NonNullList.withSize(inv.getSizeInventory(), ItemStack.EMPTY); if(!(inv instanceof ISidedInventory)) @@ -130,13 +128,13 @@ public class TransporterManager return new InventoryCopy(ret); } - public static void testInsert(IInventory inv, InventoryCopy copy, EnumFacing side, TransporterStack stack) + public static void testInsert(TileEntity tile, InventoryCopy copy, EnumFacing side, TransporterStack stack) { ItemStack toInsert = stack.itemStack.copy(); - if(stack.pathType != Path.HOME && inv instanceof ISideConfiguration) + if(stack.pathType != Path.HOME && tile instanceof ISideConfiguration) { - ISideConfiguration config = (ISideConfiguration)inv; + ISideConfiguration config = (ISideConfiguration)tile; EnumFacing tileSide = config.getOrientation(); EnumColor configColor = config.getEjector().getInputColor(MekanismUtils.getBaseOrientation(side, tileSide).getOpposite()); @@ -146,13 +144,107 @@ public class TransporterManager } } - if(Loader.isModLoaded("MinefactoryReloaded") && inv instanceof IDeepStorageUnit && !(inv instanceof TileEntityBin)) + if(Loader.isModLoaded("MinefactoryReloaded") && tile instanceof IDeepStorageUnit && !(tile instanceof TileEntityBin)) { return; } - if(!(inv instanceof ISidedInventory)) + if(tile instanceof ISidedInventory) { + ISidedInventory sidedInventory = (ISidedInventory)tile; + int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); + + if(slots != null && slots.length != 0) + { + if(stack.pathType != Path.HOME && sidedInventory instanceof TileEntityBin && side.getOpposite() == EnumFacing.DOWN) + { + slots = sidedInventory.getSlotsForFace(EnumFacing.UP); + } + + if(tile instanceof TileEntityBin) + { + int slot = slots[0]; + + if(!sidedInventory.isItemValidForSlot(slot, toInsert) || !sidedInventory.canInsertItem(slot, toInsert, side.getOpposite())) + { + return; + } + + int amountRemaining = ((TileEntityBin)sidedInventory).getMaxStoredCount()-copy.binAmount; + copy.binAmount += Math.min(amountRemaining, toInsert.getCount()); + + return; + } + else { + for(int get = 0; get <= slots.length - 1; get++) + { + int slotID = slots[get]; + + if(stack.pathType != Path.HOME) + { + if(!sidedInventory.isItemValidForSlot(slotID, toInsert) || !sidedInventory.canInsertItem(slotID, toInsert, side.getOpposite())) + { + continue; + } + } + + ItemStack inSlot = copy.inventory.get(slotID); + + if(inSlot.isEmpty()) + { + if(toInsert.getCount() <= sidedInventory.getInventoryStackLimit()) + { + copy.inventory.set(slotID, toInsert); + return; + } + else { + int rejects = toInsert.getCount() - sidedInventory.getInventoryStackLimit(); + + ItemStack toSet = toInsert.copy(); + toSet.setCount(sidedInventory.getInventoryStackLimit()); + + ItemStack remains = toInsert.copy(); + remains.setCount(rejects); + + copy.inventory.set(slotID, toSet); + + toInsert = remains; + } + } + else if(InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit())) + { + int max = Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit()); + + if(inSlot.getCount() + toInsert.getCount() <= max) + { + ItemStack toSet = toInsert.copy(); + toSet.grow(inSlot.getCount()); + + copy.inventory.set(slotID, toSet); + return; + } + else { + int rejects = (inSlot.getCount() + toInsert.getCount()) - max; + + ItemStack toSet = toInsert.copy(); + toSet.setCount(max); + + ItemStack remains = toInsert.copy(); + remains.setCount(rejects); + + copy.inventory.set(slotID, toSet); + + toInsert = remains; + } + } + } + } + } + } + else if(tile instanceof IInventory) + { + IInventory inv = InventoryUtils.checkChestInv((IInventory)tile); + for(int i = 0; i <= inv.getSizeInventory() - 1; i++) { if(stack.pathType != Path.HOME) @@ -214,93 +306,67 @@ public class TransporterManager } } } - else { - ISidedInventory sidedInventory = (ISidedInventory)inv; - int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); - - if(slots != null && slots.length != 0) + else if(InventoryUtils.isItemHandler(tile, side.getOpposite())) + { + IItemHandler inv = InventoryUtils.getItemHandler(tile, side.getOpposite()); + + for(int i = 0; i <= inv.getSlots() - 1; i++) { - if(stack.pathType != Path.HOME && sidedInventory instanceof TileEntityBin && side.getOpposite() == EnumFacing.DOWN) + if(stack.pathType != Path.HOME) { - slots = sidedInventory.getSlotsForFace(EnumFacing.UP); + if(inv.insertItem(i, toInsert, true).isEmpty()) + { + continue; + } } - if(inv instanceof TileEntityBin) + ItemStack inSlot = copy.inventory.get(i); + + if(inSlot.isEmpty()) { - int slot = slots[0]; - - if(!sidedInventory.isItemValidForSlot(slot, toInsert) || !sidedInventory.canInsertItem(slot, toInsert, side.getOpposite())) + if(toInsert.getCount() <= inv.getSlotLimit(i)) { + copy.inventory.set(i, toInsert); return; } - - int amountRemaining = ((TileEntityBin)inv).getMaxStoredCount()-copy.binAmount; - copy.binAmount += Math.min(amountRemaining, toInsert.getCount()); - - return; + else { + int rejects = toInsert.getCount() - inv.getSlotLimit(i); + + ItemStack toSet = toInsert.copy(); + toSet.setCount(inv.getSlotLimit(i)); + + ItemStack remains = toInsert.copy(); + remains.setCount(rejects); + + copy.inventory.set(i, toSet); + + toInsert = remains; + } } - else { - for(int get = 0; get <= slots.length - 1; get++) + else if(InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), inv.getSlotLimit(i))) + { + int max = Math.min(inSlot.getMaxStackSize(), inv.getSlotLimit(i)); + + if(inSlot.getCount() + toInsert.getCount() <= max) { - int slotID = slots[get]; - - if(stack.pathType != Path.HOME) - { - if(!sidedInventory.isItemValidForSlot(slotID, toInsert) || !sidedInventory.canInsertItem(slotID, toInsert, side.getOpposite())) - { - continue; - } - } - - ItemStack inSlot = copy.inventory.get(slotID); - - if(inSlot.isEmpty()) - { - if(toInsert.getCount() <= inv.getInventoryStackLimit()) - { - copy.inventory.set(slotID, toInsert); - return; - } - else { - int rejects = toInsert.getCount() - inv.getInventoryStackLimit(); - - ItemStack toSet = toInsert.copy(); - toSet.setCount(inv.getInventoryStackLimit()); + ItemStack toSet = toInsert.copy(); + toSet.grow(inSlot.getCount()); - ItemStack remains = toInsert.copy(); - remains.setCount(rejects); + copy.inventory.set(i, toSet); + return; + } + else { + int rejects = (inSlot.getCount() + toInsert.getCount()) - max; - copy.inventory.set(slotID, toSet); + ItemStack toSet = toInsert.copy(); + toSet.setCount(max); - toInsert = remains; - } - } - else if(InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), inv.getInventoryStackLimit())) - { - int max = Math.min(inSlot.getMaxStackSize(), inv.getInventoryStackLimit()); - - if(inSlot.getCount() + toInsert.getCount() <= max) - { - ItemStack toSet = toInsert.copy(); - toSet.grow(inSlot.getCount()); - - copy.inventory.set(slotID, toSet); - return; - } - else { - int rejects = (inSlot.getCount() + toInsert.getCount()) - max; - - ItemStack toSet = toInsert.copy(); - toSet.setCount(max); - - ItemStack remains = toInsert.copy(); - remains.setCount(rejects); - - copy.inventory.set(slotID, toSet); - - toInsert = remains; - } - } + ItemStack remains = toInsert.copy(); + remains.setCount(rejects); + + copy.inventory.set(i, toSet); + + toInsert = remains; } } } @@ -327,11 +393,6 @@ public class TransporterManager */ public static ItemStack getPredictedInsert(TileEntity tileEntity, EnumColor color, ItemStack itemStack, EnumFacing side) { - if(!(tileEntity instanceof IInventory)) - { - return itemStack; - } - if(tileEntity instanceof ISideConfiguration) { ISideConfiguration config = (ISideConfiguration)tileEntity; @@ -344,8 +405,16 @@ public class TransporterManager } } - IInventory inventory = (IInventory)tileEntity; - InventoryCopy copy = copyInvFromSide(inventory, side); + InventoryCopy copy = null; + + if(tileEntity instanceof IInventory) + { + copy = copyInvFromSide(InventoryUtils.checkChestInv((IInventory)tileEntity), side); + } + else if(InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) + { + copy = copyInv(InventoryUtils.getItemHandler(tileEntity, side.getOpposite())); + } if(copy == null) { @@ -356,15 +425,93 @@ public class TransporterManager for(TransporterStack tStack : insertQueue) { - testInsert(inventory, copy, side, tStack); + testInsert(tileEntity, copy, side, tStack); } ItemStack toInsert = itemStack.copy(); - if(!(inventory instanceof ISidedInventory)) + if(tileEntity instanceof ISidedInventory) { - inventory = InventoryUtils.checkChestInv(inventory); + ISidedInventory sidedInventory = (ISidedInventory)tileEntity; + int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); + if(slots != null && slots.length != 0) + { + if(tileEntity instanceof TileEntityBin) + { + int slot = slots[0]; + + if(!sidedInventory.isItemValidForSlot(slot, toInsert) || !sidedInventory.canInsertItem(slot, toInsert, side.getOpposite())) + { + return toInsert; + } + + int amountRemaining = ((TileEntityBin)tileEntity).getMaxStoredCount()-copy.binAmount; + + if(toInsert.getCount() <= amountRemaining) + { + return ItemStack.EMPTY; + } + else { + return StackUtils.size(toInsert, toInsert.getCount()-amountRemaining); + } + } + else { + for(int get = 0; get <= slots.length - 1; get++) + { + int slotID = slots[get]; + + if(!sidedInventory.isItemValidForSlot(slotID, toInsert) || !sidedInventory.canInsertItem(slotID, toInsert, side.getOpposite())) + { + continue; + } + + ItemStack inSlot = copy.inventory.get(slotID); + + if(toInsert.isEmpty()) + { + return ItemStack.EMPTY; + } + else if(inSlot.isEmpty()) + { + if(toInsert.getCount() <= sidedInventory.getInventoryStackLimit()) + { + return ItemStack.EMPTY; + } + else { + int rejects = toInsert.getCount() - sidedInventory.getInventoryStackLimit(); + + if(rejects < toInsert.getCount()) + { + toInsert = StackUtils.size(toInsert, rejects); + } + } + } + else if(InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit())) + { + int max = Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit()); + + if(inSlot.getCount() + toInsert.getCount() <= max) + { + return ItemStack.EMPTY; + } + else { + int rejects = (inSlot.getCount() + toInsert.getCount()) - max; + + if(rejects < toInsert.getCount()) + { + toInsert = StackUtils.size(toInsert, rejects); + } + } + } + } + } + } + } + else if(tileEntity instanceof IInventory) + { + IInventory inventory = InventoryUtils.checkChestInv((IInventory)tileEntity); + for(int i = 0; i <= inventory.getSizeInventory() - 1; i++) { if(!inventory.isItemValidForSlot(i, toInsert)) @@ -412,78 +559,52 @@ public class TransporterManager } } } - else { - ISidedInventory sidedInventory = (ISidedInventory)inventory; - int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); - - if(slots != null && slots.length != 0) + else if(InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) + { + IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite()); + + for(int i = 0; i <= inventory.getSlots() - 1; i++) { - if(inventory instanceof TileEntityBin) + if(inventory.insertItem(i, toInsert, true).isEmpty()) { - int slot = slots[0]; - - if(!sidedInventory.isItemValidForSlot(slot, toInsert) || !sidedInventory.canInsertItem(slot, toInsert, side.getOpposite())) - { - return toInsert; - } - - int amountRemaining = ((TileEntityBin)inventory).getMaxStoredCount()-copy.binAmount; - - if(toInsert.getCount() <= amountRemaining) + continue; + } + + ItemStack inSlot = copy.inventory.get(i); + + if(toInsert.isEmpty()) + { + return ItemStack.EMPTY; + } + else if(inSlot.isEmpty()) + { + if(toInsert.getCount() <= inventory.getSlotLimit(i)) { return ItemStack.EMPTY; } else { - return StackUtils.size(toInsert, toInsert.getCount()-amountRemaining); + int rejects = toInsert.getCount() - inventory.getSlotLimit(i); + + if(rejects < toInsert.getCount()) + { + toInsert = StackUtils.size(toInsert, rejects); + } } } - else { - for(int get = 0; get <= slots.length - 1; get++) + else if(InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), inventory.getSlotLimit(i))) + { + int max = Math.min(inSlot.getMaxStackSize(), inventory.getSlotLimit(i)); + + if(inSlot.getCount() + toInsert.getCount() <= max) { - int slotID = slots[get]; - - if(!sidedInventory.isItemValidForSlot(slotID, toInsert) || !sidedInventory.canInsertItem(slotID, toInsert, side.getOpposite())) + return ItemStack.EMPTY; + } + else { + int rejects = (inSlot.getCount() + toInsert.getCount()) - max; + + if(rejects < toInsert.getCount()) { - continue; - } - - ItemStack inSlot = copy.inventory.get(slotID); - - if(toInsert.isEmpty()) - { - return ItemStack.EMPTY; - } - else if(inSlot.isEmpty()) - { - if(toInsert.getCount() <= inventory.getInventoryStackLimit()) - { - return ItemStack.EMPTY; - } - else { - int rejects = toInsert.getCount() - inventory.getInventoryStackLimit(); - - if(rejects < toInsert.getCount()) - { - toInsert = StackUtils.size(toInsert, rejects); - } - } - } - else if(InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), inventory.getInventoryStackLimit())) - { - int max = Math.min(inSlot.getMaxStackSize(), inventory.getInventoryStackLimit()); - - if(inSlot.getCount() + toInsert.getCount() <= max) - { - return ItemStack.EMPTY; - } - else { - int rejects = (inSlot.getCount() + toInsert.getCount()) - max; - - if(rejects < toInsert.getCount()) - { - toInsert = StackUtils.size(toInsert, rejects); - } - } + toInsert = StackUtils.size(toInsert, rejects); } } } diff --git a/src/main/java/mekanism/common/tile/TileEntityBin.java b/src/main/java/mekanism/common/tile/TileEntityBin.java index c53b7a1e8..7e6fde918 100644 --- a/src/main/java/mekanism/common/tile/TileEntityBin.java +++ b/src/main/java/mekanism/common/tile/TileEntityBin.java @@ -270,9 +270,8 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento setInventorySlotContents(0, rejects); } } - else if(tile instanceof IInventory) - { - setInventorySlotContents(0, InventoryUtils.putStackInInventory((IInventory)tile, bottomStack, EnumFacing.DOWN, false)); + else { + setInventorySlotContents(0, InventoryUtils.putStackInInventory(tile, bottomStack, EnumFacing.DOWN, false)); } delayTicks = 10; diff --git a/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java b/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java index 8c32f3e5d..8f4ca3b14 100644 --- a/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java +++ b/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java @@ -304,24 +304,28 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I setActive(false); } } + + ItemStack topEject = getTopEject(false, ItemStack.EMPTY); - if(doEject && delayTicks == 0 && !getTopEject(false, ItemStack.EMPTY).isEmpty() && getEjectInv() != null && getEjectTile() != null) + if(doEject && delayTicks == 0 && !topEject.isEmpty() && getEjectInv() != null && getEjectTile() != null) { - if(getEjectInv() instanceof IInventory) + if(CapabilityUtils.hasCapability(getEjectInv(), Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, facing.getOpposite())) { - ItemStack remains = InventoryUtils.putStackInInventory((IInventory)getEjectInv(), getTopEject(false, ItemStack.EMPTY), facing.getOpposite(), false); + ItemStack rejected = TransporterUtils.insert(getEjectTile(), CapabilityUtils.getCapability(getEjectInv(), Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, facing.getOpposite()), topEject, null, true, 0); - getTopEject(true, remains); - } - else if(CapabilityUtils.hasCapability(getEjectInv(), Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, facing.getOpposite())) - { - ItemStack rejected = TransporterUtils.insert(getEjectTile(), CapabilityUtils.getCapability(getEjectInv(), Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, facing.getOpposite()), getTopEject(false, ItemStack.EMPTY), null, true, 0); - - if(TransporterManager.didEmit(getTopEject(false, ItemStack.EMPTY), rejected)) + if(TransporterManager.didEmit(topEject, rejected)) { getTopEject(true, rejected); } } + else { + ItemStack remains = InventoryUtils.putStackInInventory(getEjectInv(), topEject, facing.getOpposite(), false); + + if(TransporterManager.didEmit(topEject, remains)) + { + getTopEject(true, remains); + } + } delayTicks = 10; } @@ -432,9 +436,9 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I } } - if(doPull && getPullInv() instanceof IInventory) + if(doPull && getPullInv() != null) { - InvStack stack = InventoryUtils.takeDefinedItem((IInventory)getPullInv(), EnumFacing.UP, filter.replaceStack.copy(), 1, 1); + InvStack stack = InventoryUtils.takeDefinedItem(getPullInv(), EnumFacing.UP, filter.replaceStack.copy(), 1, 1); if(stack != null) { diff --git a/src/main/java/mekanism/common/tile/TileEntityLogisticalSorter.java b/src/main/java/mekanism/common/tile/TileEntityLogisticalSorter.java index e3759324e..b9dbbf9ab 100644 --- a/src/main/java/mekanism/common/tile/TileEntityLogisticalSorter.java +++ b/src/main/java/mekanism/common/tile/TileEntityLogisticalSorter.java @@ -99,72 +99,67 @@ public class TileEntityLogisticalSorter extends TileEntityElectricBlock implemen TileEntity back = Coord4D.get(this).offset(facing.getOpposite()).getTileEntity(world); TileEntity front = Coord4D.get(this).offset(facing).getTileEntity(world); - if(back instanceof IInventory && (front != null && CapabilityUtils.hasCapability(front, Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, facing.getOpposite()) || front instanceof IInventory)) + boolean sentItems = false; + int min = 0; + + outer: + for(TransporterFilter filter : filters) { - IInventory inventory = InventoryUtils.checkChestInv((IInventory)back); - - boolean sentItems = false; - int min = 0; - - outer: - for(TransporterFilter filter : filters) + inner: + for(StackSearcher search = new StackSearcher(back, facing.getOpposite()); search.i >= 0;) { - inner: - for(StackSearcher search = new StackSearcher(inventory, facing.getOpposite()); search.i >= 0;) + InvStack invStack = filter.getStackFromInventory(search); + + if(invStack == null || invStack.getStack().isEmpty()) { - InvStack invStack = filter.getStackFromInventory(search); - - if(invStack == null || invStack.getStack().isEmpty()) - { - break inner; - } - - if(filter.canFilter(invStack.getStack())) - { - if(filter instanceof TItemStackFilter) - { - TItemStackFilter itemFilter = (TItemStackFilter)filter; - - if(itemFilter.sizeMode) - { - min = itemFilter.min; - } - } - - ItemStack used = emitItemToTransporter(front, invStack, filter.color, min); - - if(used != null) - { - invStack.use(used.getCount()); - inventory.markDirty(); - setActive(true); - sentItems = true; - - break outer; - } - } + break inner; } - } - if(!sentItems && autoEject) - { - InvStack invStack = InventoryUtils.takeTopStack(inventory, facing.getOpposite(), new FirstFinder()); - - if(invStack != null && !invStack.getStack().isEmpty()) + if(filter.canFilter(invStack.getStack())) { - ItemStack used = emitItemToTransporter(front, invStack, color, 0); - - if(!used.isEmpty()) + if(filter instanceof TItemStackFilter) + { + TItemStackFilter itemFilter = (TItemStackFilter)filter; + + if(itemFilter.sizeMode) + { + min = itemFilter.min; + } + } + + ItemStack used = emitItemToTransporter(front, invStack, filter.color, min); + + if(used != null) { invStack.use(used.getCount()); - inventory.markDirty(); + back.markDirty(); setActive(true); + sentItems = true; + + break outer; } } } - - delayTicks = 10; } + + if(!sentItems && autoEject) + { + InvStack invStack = InventoryUtils.takeTopStack(back, facing.getOpposite(), new FirstFinder()); + + if(invStack != null && !invStack.getStack().isEmpty()) + { + ItemStack used = emitItemToTransporter(front, invStack, color, 0); + + if(!used.isEmpty()) + { + invStack.use(used.getCount()); + back.markDirty(); + setActive(true); + } + } + } + + delayTicks = 10; } if(playersUsing.size() > 0) @@ -206,9 +201,8 @@ public class TileEntityLogisticalSorter extends TileEntityElectricBlock implemen } } } - else if(front instanceof IInventory) - { - ItemStack rejects = InventoryUtils.putStackInInventory((IInventory)front, inInventory.getStack(), facing, false); + else { + ItemStack rejects = InventoryUtils.putStackInInventory(front, inInventory.getStack(), facing, false); if(TransporterManager.didEmit(inInventory.getStack(), rejects)) { @@ -470,30 +464,19 @@ public class TileEntityLogisticalSorter extends TileEntityElectricBlock implemen public boolean canSendHome(ItemStack stack) { TileEntity back = Coord4D.get(this).offset(facing.getOpposite()).getTileEntity(world); - - if(back instanceof IInventory) - { - return InventoryUtils.canInsert(back, null, stack, facing.getOpposite(), true); - } - - return false; + return InventoryUtils.canInsert(back, null, stack, facing.getOpposite(), true); } public boolean hasInventory() { - return Coord4D.get(this).offset(facing.getOpposite()).getTileEntity(world) instanceof IInventory; + TileEntity tile = Coord4D.get(this).offset(facing.getOpposite()).getTileEntity(world); + return TransporterUtils.isValidAcceptorOnSide(tile, facing.getOpposite()); } public ItemStack sendHome(ItemStack stack) { TileEntity back = Coord4D.get(this).offset(facing.getOpposite()).getTileEntity(world); - - if(back instanceof IInventory) - { - return InventoryUtils.putStackInInventory((IInventory)back, stack, facing.getOpposite(), true); - } - - return stack; + return InventoryUtils.putStackInInventory(back, stack, facing.getOpposite(), true); } @Override diff --git a/src/main/java/mekanism/common/tile/component/TileComponentEjector.java b/src/main/java/mekanism/common/tile/component/TileComponentEjector.java index cb938eaf3..d8bdb1b8b 100644 --- a/src/main/java/mekanism/common/tile/component/TileComponentEjector.java +++ b/src/main/java/mekanism/common/tile/component/TileComponentEjector.java @@ -193,11 +193,7 @@ public class TileComponentEjector implements ITileComponent TileEntity tile = Coord4D.get(tileEntity).offset(side).getTileEntity(tileEntity.getWorld()); ItemStack prev = stack.copy(); - if(tile instanceof IInventory && !(CapabilityUtils.hasCapability(tile, Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, side.getOpposite()))) - { - stack = InventoryUtils.putStackInInventory((IInventory)tile, stack, side, false); - } - else if(CapabilityUtils.hasCapability(tile, Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, side.getOpposite())) + if(CapabilityUtils.hasCapability(tile, Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, side.getOpposite())) { ItemStack rejects = TransporterUtils.insert(tileEntity, CapabilityUtils.getCapability(tile, Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, side.getOpposite()), stack, outputColor, true, 0); @@ -206,6 +202,9 @@ public class TileComponentEjector implements ITileComponent stack = rejects; } } + else { + stack = InventoryUtils.putStackInInventory(tile, stack, side, false); + } if(stack.isEmpty() || prev.getCount() != stack.getCount()) { diff --git a/src/main/java/mekanism/common/tile/transmitter/TileEntityLogisticalTransporter.java b/src/main/java/mekanism/common/tile/transmitter/TileEntityLogisticalTransporter.java index 3f3f2ff96..9d9282969 100644 --- a/src/main/java/mekanism/common/tile/transmitter/TileEntityLogisticalTransporter.java +++ b/src/main/java/mekanism/common/tile/transmitter/TileEntityLogisticalTransporter.java @@ -44,7 +44,7 @@ import net.minecraftforge.common.property.IExtendedBlockState; import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.fml.common.FMLCommonHandler; -public class TileEntityLogisticalTransporter extends TileEntityTransmitter +public class TileEntityLogisticalTransporter extends TileEntityTransmitter { public Tier.TransporterTier tier = Tier.TransporterTier.BASIC; @@ -91,9 +91,9 @@ public class TileEntityLogisticalTransporter extends TileEntityTransmitter implements ILogisticalTransporter +public class TransporterImpl extends TransmitterImpl implements ILogisticalTransporter { public HashList transit = new HashList<>(); @@ -109,27 +109,24 @@ public class TransporterImpl extends TransmitterImpl +public class InventoryNetwork extends DynamicNetwork { public InventoryNetwork() {} @@ -110,15 +110,15 @@ public class InventoryNetwork extends DynamicNetwork transmitter) {} + public void absorbBuffer(IGridTransmitter transmitter) {} @Override public void clampBuffer() {} @Override - public Set> getAcceptors(Object data) + public Set> getAcceptors(Object data) { - Set> toReturn = new HashSet<>(); + Set> toReturn = new HashSet<>(); if(FMLCommonHandler.instance().getEffectiveSide().isClient()) { diff --git a/src/main/java/mekanism/common/util/InventoryUtils.java b/src/main/java/mekanism/common/util/InventoryUtils.java index bf7960e68..d62e4f7d1 100644 --- a/src/main/java/mekanism/common/util/InventoryUtils.java +++ b/src/main/java/mekanism/common/util/InventoryUtils.java @@ -4,6 +4,7 @@ import mekanism.api.EnumColor; import mekanism.common.base.ISideConfiguration; import mekanism.common.content.transporter.Finder; import mekanism.common.content.transporter.InvStack; +import mekanism.common.content.transporter.TransporterManager; import mekanism.common.tile.TileEntityBin; import mekanism.common.tile.TileEntityLogisticalSorter; import net.minecraft.inventory.IInventory; @@ -14,6 +15,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityChest; import net.minecraft.util.EnumFacing; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; public final class InventoryUtils { @@ -30,7 +33,7 @@ public final class InventoryUtils return ret; } - + public static IInventory checkChestInv(IInventory inv) { if(inv instanceof TileEntityChest) @@ -64,19 +67,102 @@ public final class InventoryUtils return inv; } - public static ItemStack putStackInInventory(IInventory inventory, ItemStack itemStack, EnumFacing side, boolean force) + public static ItemStack putStackInInventory(TileEntity tile, ItemStack itemStack, EnumFacing side, boolean force) { - inventory = checkChestInv(inventory); - - if(force && inventory instanceof TileEntityLogisticalSorter) + if(force && tile instanceof TileEntityLogisticalSorter) { - return ((TileEntityLogisticalSorter)inventory).sendHome(itemStack.copy()); + return ((TileEntityLogisticalSorter)tile).sendHome(itemStack.copy()); } ItemStack toInsert = itemStack.copy(); - if(!(inventory instanceof ISidedInventory)) + //prioritize other implementations first to allow item forcing + if(tile instanceof ISidedInventory) { + ISidedInventory sidedInventory = (ISidedInventory)tile; + int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); + + if(slots != null && slots.length != 0) + { + if(force && sidedInventory instanceof TileEntityBin && side == EnumFacing.UP) + { + slots = sidedInventory.getSlotsForFace(EnumFacing.UP); + } + + for(int get = 0; get <= slots.length - 1; get++) + { + int slotID = slots[get]; + + if(!force) + { + if(!sidedInventory.isItemValidForSlot(slotID, toInsert) || !sidedInventory.canInsertItem(slotID, toInsert, side.getOpposite())) + { + continue; + } + } + + ItemStack inSlot = sidedInventory.getStackInSlot(slotID); + + if(inSlot.isEmpty()) + { + if(toInsert.getCount() <= sidedInventory.getInventoryStackLimit()) + { + sidedInventory.setInventorySlotContents(slotID, toInsert); + sidedInventory.markDirty(); + + return ItemStack.EMPTY; + } + else { + int rejects = toInsert.getCount() - sidedInventory.getInventoryStackLimit(); + + ItemStack toSet = toInsert.copy(); + toSet.setCount(sidedInventory.getInventoryStackLimit()); + + ItemStack remains = toInsert.copy(); + remains.setCount(rejects); + + sidedInventory.setInventorySlotContents(slotID, toSet); + sidedInventory.markDirty(); + + toInsert = remains; + } + } + else if(areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit())) + { + int max = Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit()); + + if(inSlot.getCount() + toInsert.getCount() <= max) + { + ItemStack toSet = toInsert.copy(); + toSet.grow(inSlot.getCount()); + + sidedInventory.setInventorySlotContents(slotID, toSet); + sidedInventory.markDirty(); + + return ItemStack.EMPTY; + } + else { + int rejects = (inSlot.getCount() + toInsert.getCount()) - max; + + ItemStack toSet = toInsert.copy(); + toSet.setCount(max); + + ItemStack remains = toInsert.copy(); + remains.setCount(rejects); + + sidedInventory.setInventorySlotContents(slotID, toSet); + sidedInventory.markDirty(); + + toInsert = remains; + } + } + } + } + } + else if(tile instanceof IInventory) + { + IInventory inventory = checkChestInv((IInventory)tile); + for(int i = 0; i <= inventory.getSizeInventory() - 1; i++) { if(!force) @@ -144,84 +230,17 @@ public final class InventoryUtils } } } - else { - ISidedInventory sidedInventory = (ISidedInventory)inventory; - int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); - - if(slots != null && slots.length != 0) + else if(isItemHandler(tile, side.getOpposite())) + { + IItemHandler inventory = getItemHandler(tile, side.getOpposite()); + + for(int i = 0; i < inventory.getSlots(); i++) { - if(force && sidedInventory instanceof TileEntityBin && side == EnumFacing.UP) + toInsert = inventory.insertItem(i, toInsert, false); + + if(toInsert.isEmpty()) { - slots = sidedInventory.getSlotsForFace(EnumFacing.UP); - } - - for(int get = 0; get <= slots.length - 1; get++) - { - int slotID = slots[get]; - - if(!force) - { - if(!sidedInventory.isItemValidForSlot(slotID, toInsert) || !sidedInventory.canInsertItem(slotID, toInsert, side.getOpposite())) - { - continue; - } - } - - ItemStack inSlot = inventory.getStackInSlot(slotID); - - if(inSlot.isEmpty()) - { - if(toInsert.getCount() <= inventory.getInventoryStackLimit()) - { - inventory.setInventorySlotContents(slotID, toInsert); - inventory.markDirty(); - - return ItemStack.EMPTY; - } - else { - int rejects = toInsert.getCount() - inventory.getInventoryStackLimit(); - - ItemStack toSet = toInsert.copy(); - toSet.setCount(inventory.getInventoryStackLimit()); - - ItemStack remains = toInsert.copy(); - remains.setCount(rejects); - - inventory.setInventorySlotContents(slotID, toSet); - inventory.markDirty(); - - toInsert = remains; - } - } - else if(areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), inventory.getInventoryStackLimit())) - { - int max = Math.min(inSlot.getMaxStackSize(), inventory.getInventoryStackLimit()); - - if(inSlot.getCount() + toInsert.getCount() <= max) - { - ItemStack toSet = toInsert.copy(); - toSet.grow(inSlot.getCount()); - - inventory.setInventorySlotContents(slotID, toSet); - inventory.markDirty(); - - return ItemStack.EMPTY; - } - else { - int rejects = (inSlot.getCount() + toInsert.getCount()) - max; - - ItemStack toSet = toInsert.copy(); - toSet.setCount(max); - - ItemStack remains = toInsert.copy(); - remains.setCount(rejects); - - inventory.setInventorySlotContents(slotID, toSet); - inventory.markDirty(); - - toInsert = remains; - } - } + return ItemStack.EMPTY; } } } @@ -239,25 +258,25 @@ public final class InventoryUtils return inSlot.isItemEqual(toInsert) && ItemStack.areItemStackTagsEqual(inSlot, toInsert); } - public static InvStack takeTopItem(IInventory inventory, EnumFacing side, int amount) + public static InvStack takeTopItem(TileEntity tile, EnumFacing side, int amount) { - inventory = checkChestInv(inventory); - - if(!(inventory instanceof ISidedInventory)) - { - for(int i = inventory.getSizeInventory() - 1; i >= 0; i--) + if(isItemHandler(tile, side.getOpposite())) + { + IItemHandler inventory = getItemHandler(tile, side.getOpposite()); + + for(int i = inventory.getSlots() - 1; i >= 0; i--) { - if(!inventory.getStackInSlot(i).isEmpty() && inventory.getStackInSlot(i).getCount() > 0) + ItemStack stack = inventory.extractItem(i, amount, true); + + if(!stack.isEmpty()) { - ItemStack toSend = inventory.getStackInSlot(i).copy(); - toSend.setCount(Math.min(amount, toSend.getCount())); - - return new InvStack(inventory, i, toSend); + return new InvStack(tile, i, stack, side.getOpposite()); } } - } - else { - ISidedInventory sidedInventory = (ISidedInventory)inventory; + } + if(tile instanceof ISidedInventory) + { + ISidedInventory sidedInventory = (ISidedInventory)tile; int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); if(slots != null) @@ -273,29 +292,45 @@ public final class InventoryUtils if(sidedInventory.canExtractItem(slotID, toSend, side.getOpposite())) { - return new InvStack(inventory, slotID, toSend); + return new InvStack(tile, slotID, toSend, side.getOpposite()); } } } } } + else if(tile instanceof IInventory) + { + IInventory inventory = checkChestInv((IInventory)tile); + + for(int i = inventory.getSizeInventory() - 1; i >= 0; i--) + { + if(!inventory.getStackInSlot(i).isEmpty() && inventory.getStackInSlot(i).getCount() > 0) + { + ItemStack toSend = inventory.getStackInSlot(i).copy(); + toSend.setCount(Math.min(amount, toSend.getCount())); + + return new InvStack(tile, i, toSend, side.getOpposite()); + } + } + } return null; } - public static InvStack takeDefinedItem(IInventory inventory, EnumFacing side, ItemStack type, int min, int max) + public static InvStack takeDefinedItem(TileEntity tile, EnumFacing side, ItemStack type, int min, int max) { - inventory = checkChestInv(inventory); + InvStack ret = new InvStack(tile, side.getOpposite()); - InvStack ret = new InvStack(inventory); - - if(!(inventory instanceof ISidedInventory)) + if(isItemHandler(tile, side.getOpposite())) { - for(int i = inventory.getSizeInventory() - 1; i >= 0; i--) + IItemHandler inventory = getItemHandler(tile, side.getOpposite()); + + for(int i = inventory.getSlots() - 1; i >= 0; i--) { - if(!inventory.getStackInSlot(i).isEmpty() && StackUtils.equalsWildcard(inventory.getStackInSlot(i), type)) + ItemStack stack = inventory.extractItem(i, max, true); + + if(!stack.isEmpty() && StackUtils.equalsWildcard(stack, type)) { - ItemStack stack = inventory.getStackInSlot(i); int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0; if(current+stack.getCount() <= max) @@ -315,8 +350,9 @@ public final class InventoryUtils } } } - else { - ISidedInventory sidedInventory = (ISidedInventory)inventory; + else if(tile instanceof ISidedInventory) + { + ISidedInventory sidedInventory = (ISidedInventory)tile; int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); if(slots != null && slots.length != 0) @@ -325,7 +361,7 @@ public final class InventoryUtils { int slotID = slots[get]; - if(!sidedInventory.getStackInSlot(slotID).isEmpty() && StackUtils.equalsWildcard(inventory.getStackInSlot(slotID), type)) + if(!sidedInventory.getStackInSlot(slotID).isEmpty() && StackUtils.equalsWildcard(sidedInventory.getStackInSlot(slotID), type)) { ItemStack stack = sidedInventory.getStackInSlot(slotID); int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0; @@ -357,6 +393,34 @@ public final class InventoryUtils } } } + else if(tile instanceof IInventory) + { + IInventory inventory = checkChestInv((IInventory)tile); + + for(int i = inventory.getSizeInventory() - 1; i >= 0; i--) + { + if(!inventory.getStackInSlot(i).isEmpty() && StackUtils.equalsWildcard(inventory.getStackInSlot(i), type)) + { + ItemStack stack = inventory.getStackInSlot(i); + int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0; + + if(current+stack.getCount() <= max) + { + ret.appendStack(i, stack.copy()); + } + else { + ItemStack copy = stack.copy(); + copy.setCount(max-current); + ret.appendStack(i, copy); + } + + if(!ret.getStack().isEmpty() && ret.getStack().getCount() == max) + { + return ret; + } + } + } + } if(ret != null && !ret.getStack().isEmpty() && ret.getStack().getCount() >= min) { @@ -366,23 +430,25 @@ public final class InventoryUtils return null; } - public static InvStack takeTopStack(IInventory inventory, EnumFacing side, Finder id) + public static InvStack takeTopStack(TileEntity tile, EnumFacing side, Finder id) { - inventory = checkChestInv(inventory); - - if(!(inventory instanceof ISidedInventory)) + if(isItemHandler(tile, side.getOpposite())) { - for(int i = inventory.getSizeInventory() - 1; i >= 0; i--) + IItemHandler inventory = getItemHandler(tile, side.getOpposite()); + + for(int i = inventory.getSlots() - 1; i >= 0; i--) { - if(!inventory.getStackInSlot(i).isEmpty() && id.modifies(inventory.getStackInSlot(i))) + ItemStack stack = inventory.extractItem(i, 64, true); + + if(!stack.isEmpty() && id.modifies(stack)) { - ItemStack toSend = inventory.getStackInSlot(i).copy(); - return new InvStack(inventory, i, toSend); + return new InvStack(tile, i, stack, side.getOpposite()); } } } - else { - ISidedInventory sidedInventory = (ISidedInventory)inventory; + else if(tile instanceof ISidedInventory) + { + ISidedInventory sidedInventory = (ISidedInventory)tile; int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); if(slots != null && slots.length != 0) @@ -397,23 +463,31 @@ public final class InventoryUtils if(sidedInventory.canExtractItem(slotID, toSend, side.getOpposite())) { - return new InvStack(inventory, slotID, toSend); + return new InvStack(tile, slotID, toSend, side.getOpposite()); } } } } } + else if(tile instanceof IInventory) + { + IInventory inventory = checkChestInv((IInventory)tile); + + for(int i = inventory.getSizeInventory() - 1; i >= 0; i--) + { + if(!inventory.getStackInSlot(i).isEmpty() && id.modifies(inventory.getStackInSlot(i))) + { + ItemStack toSend = inventory.getStackInSlot(i).copy(); + return new InvStack(tile, i, toSend, side.getOpposite()); + } + } + } return null; } public static boolean canInsert(TileEntity tileEntity, EnumColor color, ItemStack itemStack, EnumFacing side, boolean force) { - if(!(tileEntity instanceof IInventory)) - { - return false; - } - if(force && tileEntity instanceof TileEntityLogisticalSorter) { return ((TileEntityLogisticalSorter)tileEntity).canSendHome(itemStack); @@ -431,10 +505,85 @@ public final class InventoryUtils } } - IInventory inventory = checkChestInv((IInventory)tileEntity); - - if(!(inventory instanceof ISidedInventory)) + if(isItemHandler(tileEntity, side.getOpposite())) { + IItemHandler inventory = getItemHandler(tileEntity, side.getOpposite()); + + for(int i = 0; i < inventory.getSlots(); i++) + { + ItemStack rejects = inventory.insertItem(i, itemStack, true); + + if(TransporterManager.didEmit(itemStack, rejects)) + { + return true; + } + } + } + else if(tileEntity instanceof ISidedInventory) + { + ISidedInventory sidedInventory = (ISidedInventory)tileEntity; + int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); + + if(slots != null && slots.length != 0) + { + if(force && sidedInventory instanceof TileEntityBin && side == EnumFacing.UP) + { + slots = sidedInventory.getSlotsForFace(EnumFacing.UP); + } + + for(int get = 0; get <= slots.length - 1; get++) + { + int slotID = slots[get]; + + if(!force) + { + if(!sidedInventory.isItemValidForSlot(slotID, itemStack) || !sidedInventory.canInsertItem(slotID, itemStack, side.getOpposite())) + { + continue; + } + } + + ItemStack inSlot = sidedInventory.getStackInSlot(slotID); + + if(inSlot.isEmpty()) + { + if(itemStack.getCount() <= sidedInventory.getInventoryStackLimit()) + { + return true; + } + else { + int rejects = itemStack.getCount() - sidedInventory.getInventoryStackLimit(); + + if(rejects < itemStack.getCount()) + { + return true; + } + } + } + else if(areItemsStackable(itemStack, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit())) + { + int max = Math.min(inSlot.getMaxStackSize(), sidedInventory.getInventoryStackLimit()); + + if(inSlot.getCount() + itemStack.getCount() <= max) + { + return true; + } + else { + int rejects = (inSlot.getCount() + itemStack.getCount()) - max; + + if(rejects < itemStack.getCount()) + { + return true; + } + } + } + } + } + } + else if(tileEntity instanceof IInventory) + { + IInventory inventory = checkChestInv((IInventory)tileEntity); + for(int i = 0; i <= inventory.getSizeInventory() - 1; i++) { if(!force) @@ -481,66 +630,6 @@ public final class InventoryUtils } } } - else { - ISidedInventory sidedInventory = (ISidedInventory)inventory; - int[] slots = sidedInventory.getSlotsForFace(side.getOpposite()); - - if(slots != null && slots.length != 0) - { - if(force && sidedInventory instanceof TileEntityBin && side == EnumFacing.UP) - { - slots = sidedInventory.getSlotsForFace(EnumFacing.UP); - } - - for(int get = 0; get <= slots.length - 1; get++) - { - int slotID = slots[get]; - - if(!force) - { - if(!sidedInventory.isItemValidForSlot(slotID, itemStack) || !sidedInventory.canInsertItem(slotID, itemStack, side.getOpposite())) - { - continue; - } - } - - ItemStack inSlot = inventory.getStackInSlot(slotID); - - if(inSlot.isEmpty()) - { - if(itemStack.getCount() <= inventory.getInventoryStackLimit()) - { - return true; - } - else { - int rejects = itemStack.getCount() - inventory.getInventoryStackLimit(); - - if(rejects < itemStack.getCount()) - { - return true; - } - } - } - else if(areItemsStackable(itemStack, inSlot) && inSlot.getCount() < Math.min(inSlot.getMaxStackSize(), inventory.getInventoryStackLimit())) - { - int max = Math.min(inSlot.getMaxStackSize(), inventory.getInventoryStackLimit()); - - if(inSlot.getCount() + itemStack.getCount() <= max) - { - return true; - } - else { - int rejects = (inSlot.getCount() + itemStack.getCount()) - max; - - if(rejects < itemStack.getCount()) - { - return true; - } - } - } - } - } - } return false; } @@ -550,6 +639,16 @@ public final class InventoryUtils ItemStack ret = new ItemStack(nbtTags); return ret; } + + public static boolean isItemHandler(TileEntity tile, EnumFacing side) + { + return CapabilityUtils.hasCapability(tile, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side); + } + + public static IItemHandler getItemHandler(TileEntity tile, EnumFacing side) + { + return CapabilityUtils.getCapability(tile, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side); + } /*TODO From CCLib -- go back to that version when we're using dependencies again*/ public static boolean canStack(ItemStack stack1, ItemStack stack2) { diff --git a/src/main/java/mekanism/common/util/TransporterUtils.java b/src/main/java/mekanism/common/util/TransporterUtils.java index 86632eab5..c4a44960f 100644 --- a/src/main/java/mekanism/common/util/TransporterUtils.java +++ b/src/main/java/mekanism/common/util/TransporterUtils.java @@ -28,18 +28,25 @@ public final class TransporterUtils return false; } - IInventory inventory = (IInventory)tile; - - if(inventory.getSizeInventory() > 0) + if(InventoryUtils.isItemHandler(tile, side.getOpposite())) { - if(!(inventory instanceof ISidedInventory)) + return true; + } + else if(tile instanceof IInventory) + { + IInventory inventory = (IInventory)tile; + + if(inventory.getSizeInventory() > 0) { - return true; + if(!(inventory instanceof ISidedInventory)) + { + return true; + } + + int[] slots = ((ISidedInventory)inventory).getSlotsForFace(side.getOpposite()); + + return (slots != null && slots.length > 0); } - - int[] slots = ((ISidedInventory)inventory).getSlotsForFace(side.getOpposite()); - - return (slots != null && slots.length > 0); } return false;