First attempt at removing StackUtil.

This commit is contained in:
SirSengir 2012-09-08 13:40:05 +02:00
parent 9e7e30bcfc
commit 5a6fcf03d2
6 changed files with 185 additions and 6 deletions

View file

@ -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;
}
}

View file

@ -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()));
}
}

View file

@ -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;
}
}
}

View file

@ -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);
}
}

View file

@ -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) {

View file

@ -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)