From 5a6fcf03d231ccb64959116e3cec180d6904ae78 Mon Sep 17 00:00:00 2001 From: SirSengir Date: Sat, 8 Sep 2012 13:40:05 +0200 Subject: [PATCH] First attempt at removing StackUtil. --- .../buildcraft/core/inventory/Transactor.java | 32 +++++++ .../core/inventory/TransactorSided.java | 33 +++++++ .../core/inventory/TransactorSimple.java | 95 +++++++++++++++++++ .../core/inventory/TransactorSpecial.java | 20 ++++ .../buildcraft/factory/TileAssemblyTable.java | 1 - .../transport/PipeTransportItems.java | 10 +- 6 files changed, 185 insertions(+), 6 deletions(-) create mode 100644 common/buildcraft/core/inventory/Transactor.java create mode 100644 common/buildcraft/core/inventory/TransactorSided.java create mode 100644 common/buildcraft/core/inventory/TransactorSimple.java create mode 100644 common/buildcraft/core/inventory/TransactorSpecial.java diff --git a/common/buildcraft/core/inventory/Transactor.java b/common/buildcraft/core/inventory/Transactor.java new file mode 100644 index 00000000..bc95a7c8 --- /dev/null +++ b/common/buildcraft/core/inventory/Transactor.java @@ -0,0 +1,32 @@ +package buildcraft.core.inventory; + +import buildcraft.api.core.Orientations; +import buildcraft.api.inventory.ISpecialInventory; +import net.minecraft.src.IInventory; +import net.minecraft.src.ItemStack; +import net.minecraftforge.common.ISidedInventory; + +public abstract class Transactor { + + public ItemStack add(ItemStack stack, Orientations orientation, boolean doAdd) { + ItemStack added = stack.copy(); + added.stackSize = inject(stack, orientation, doAdd); + return added; + } + + public abstract int inject(ItemStack stack, Orientations orientation, boolean doAdd); + + public static Transactor getTransactorFor(Object object) { + + if(object instanceof ISpecialInventory) + return new TransactorSpecial((ISpecialInventory)object); + + else if(object instanceof ISidedInventory) + return new TransactorSided((ISidedInventory)object); + + else if(object instanceof IInventory) + return new TransactorSimple((IInventory)object); + + return null; + } +} diff --git a/common/buildcraft/core/inventory/TransactorSided.java b/common/buildcraft/core/inventory/TransactorSided.java new file mode 100644 index 00000000..bbf6d12c --- /dev/null +++ b/common/buildcraft/core/inventory/TransactorSided.java @@ -0,0 +1,33 @@ +package buildcraft.core.inventory; + +import buildcraft.api.core.Orientations; +import net.minecraft.src.ItemStack; +import net.minecraftforge.common.ISidedInventory; + +public class TransactorSided extends TransactorSimple { + + ISidedInventory sided; + + public TransactorSided(ISidedInventory inventory) { + super(inventory); + this.sided = inventory; + } + + @Override + protected int getPartialSlot(ItemStack stack, Orientations orientation, int skipAhead) { + + if(skipAhead < sided.getStartInventorySide(orientation.toDirection()) + || skipAhead > sided.getStartInventorySide(orientation.toDirection()) + sided.getSizeInventorySide(orientation.toDirection())) + return -1; + + return getPartialSlot(stack, skipAhead, sided.getStartInventorySide(orientation.toDirection()) + sided.getSizeInventorySide(orientation.toDirection())); + } + + @Override + protected int getEmptySlot(Orientations orientation) { + return getEmptySlot(sided.getStartInventorySide(orientation.toDirection()), + sided.getStartInventorySide(orientation.toDirection()) + sided.getSizeInventorySide(orientation.toDirection())); + } + + +} diff --git a/common/buildcraft/core/inventory/TransactorSimple.java b/common/buildcraft/core/inventory/TransactorSimple.java new file mode 100644 index 00000000..10c9f5c3 --- /dev/null +++ b/common/buildcraft/core/inventory/TransactorSimple.java @@ -0,0 +1,95 @@ +package buildcraft.core.inventory; + +import net.minecraft.src.IInventory; +import net.minecraft.src.ItemStack; +import buildcraft.api.core.Orientations; + +public class TransactorSimple extends Transactor { + + private IInventory inventory; + + public TransactorSimple(IInventory inventory) { + this.inventory = inventory; + } + + @Override + public int inject(ItemStack stack, Orientations orientation, boolean doAdd) { + + int injected = 0; + + int slot = 0; + while((slot = getPartialSlot(stack, orientation, slot)) >= 0 + && injected < stack.stackSize) + injected += addToSlot(slot, stack, injected, doAdd); + + slot = 0; + while((slot = getEmptySlot(orientation)) >= 0 + && injected < stack.stackSize) + injected += addToSlot(slot, stack, injected, doAdd); + + return injected; + } + + protected int getPartialSlot(ItemStack stack, Orientations orientation, int skipAhead) { + return getPartialSlot(stack, skipAhead, inventory.getSizeInventory()); + } + + protected int getPartialSlot(ItemStack stack, int startSlot, int endSlot) { + + for(int i = startSlot; i < endSlot; i++) { + if(inventory.getStackInSlot(i) == null) + continue; + + if(!inventory.getStackInSlot(i).isItemEqual(stack)) + continue; + + if(inventory.getStackInSlot(i).stackSize >= inventory.getStackInSlot(i).getMaxStackSize()) + continue; + + return i; + } + + return -1; + } + + protected int getEmptySlot(Orientations orientation) { + return getEmptySlot(0, inventory.getSizeInventory()); + } + + protected int getEmptySlot(int startSlot, int endSlot) { + for(int i = startSlot; i < endSlot; i++) + if(inventory.getStackInSlot(i) == null) + return i; + + return -1; + } + + protected int addToSlot(int slot, ItemStack stack, int injected, boolean doAdd) { + int remaining = stack.stackSize - injected; + + if(inventory.getStackInSlot(slot) == null) { + if(doAdd) { + inventory.setInventorySlotContents(slot, stack.copy()); + inventory.getStackInSlot(slot).stackSize = remaining; + } + return remaining; + } + + if(!inventory.getStackInSlot(slot).isItemEqual(stack)) + return 0; + + int space = inventory.getStackInSlot(slot).getMaxStackSize() - inventory.getStackInSlot(slot).stackSize; + if(space <= 0) + return 0; + + if(space >= remaining) { + if(doAdd) + inventory.getStackInSlot(slot).stackSize += remaining; + return remaining; + } else { + if(doAdd) + inventory.getStackInSlot(slot).stackSize = inventory.getStackInSlot(slot).getMaxStackSize(); + return space; + } + } +} diff --git a/common/buildcraft/core/inventory/TransactorSpecial.java b/common/buildcraft/core/inventory/TransactorSpecial.java new file mode 100644 index 00000000..526eddb0 --- /dev/null +++ b/common/buildcraft/core/inventory/TransactorSpecial.java @@ -0,0 +1,20 @@ +package buildcraft.core.inventory; + +import net.minecraft.src.ItemStack; +import buildcraft.api.core.Orientations; +import buildcraft.api.inventory.ISpecialInventory; + +public class TransactorSpecial extends Transactor { + + protected ISpecialInventory inventory; + + public TransactorSpecial(ISpecialInventory inventory) { + this.inventory = inventory; + } + + @Override + public int inject(ItemStack stack, Orientations orientation, boolean doAdd) { + return inventory.addItem(stack, doAdd, orientation); + } + +} diff --git a/common/buildcraft/factory/TileAssemblyTable.java b/common/buildcraft/factory/TileAssemblyTable.java index b6bfc48d..96280948 100644 --- a/common/buildcraft/factory/TileAssemblyTable.java +++ b/common/buildcraft/factory/TileAssemblyTable.java @@ -148,7 +148,6 @@ public class TileAssemblyTable extends TileEntity implements IMachine, IInventor } StackUtil stackUtils = new StackUtil(currentRecipe.output.copy()); - boolean added = stackUtils.addToRandomInventory(this, Orientations.Unknown); if (!added || stackUtils.items.stackSize > 0) { diff --git a/common/buildcraft/transport/PipeTransportItems.java b/common/buildcraft/transport/PipeTransportItems.java index 59c8e4dc..cd61029e 100644 --- a/common/buildcraft/transport/PipeTransportItems.java +++ b/common/buildcraft/transport/PipeTransportItems.java @@ -28,10 +28,10 @@ import buildcraft.api.transport.IPipedItem; import buildcraft.core.DefaultProps; import buildcraft.core.EntityPassiveItem; import buildcraft.core.IMachine; +import buildcraft.core.inventory.Transactor; import buildcraft.core.network.PacketIds; import buildcraft.core.network.PacketPipeTransportContent; import buildcraft.core.proxy.CoreProxy; -import buildcraft.core.utils.StackUtil; import buildcraft.core.utils.Utils; import net.minecraft.src.EntityItem; @@ -152,7 +152,7 @@ public class PipeTransportItems extends PipeTransport { return pipe.pipe.transport instanceof PipeTransportItems; } else if (entity instanceof IInventory) - if (new StackUtil(item.getItemStack()).checkAvailableSlot((IInventory) entity, false, o.reverse())) + if(Transactor.getTransactorFor(entity).add(item.getItemStack(), o.reverse(), false).stackSize > 0) return true; return false; @@ -255,13 +255,13 @@ public class PipeTransportItems extends PipeTransport { ((PipeTransportItems) pipe.pipe.transport).entityEntering(data.item, data.orientation); } else if (tile instanceof IInventory) { - StackUtil utils = new StackUtil(data.item.getItemStack()); + ItemStack added = Transactor.getTransactorFor(tile).add(data.item.getItemStack(), data.orientation.reverse(), true); if (!CoreProxy.proxy.isRemote(worldObj)) - if (utils.checkAvailableSlot((IInventory) tile, true, data.orientation.reverse()) && utils.items.stackSize == 0) + if(added.stackSize >= data.item.getItemStack().stackSize) data.item.remove(); else { - data.item.setItemStack(utils.items); + data.item.getItemStack().stackSize -= added.stackSize; EntityItem dropped = data.item.toEntityItem(data.orientation); if (dropped != null)