More fixes

This commit is contained in:
WayofTime 2014-11-20 08:27:53 -05:00
parent 75dcf143d5
commit 107cc50b05
4 changed files with 200 additions and 62 deletions

View file

@ -31,24 +31,76 @@ public class BaseCompressionHandler extends CompressionHandler
@Override
public ItemStack compressInventory(ItemStack[] inv)
{
int remaining = this.getRemainingNeeded(inv);
if(remaining <= 0)
{
this.drainInventory(inv);
return this.getResultStack();
}
return null;
}
public int getRemainingNeeded(ItemStack[] inv)
{
return iterateThroughInventory(inv, false);
}
public int drainInventory(ItemStack[] inv)
{
return iterateThroughInventory(inv, true);
}
public int iterateThroughInventory(ItemStack[] inv, boolean doDrain)
{
int needed = this.required.stackSize;
int kept = this.getLeftover();
int i = -1;
for(ItemStack invStack : inv)
{
i++;
if(invStack == null)
{
continue;
}
if(invStack.equals(invStack))
if(invStack.isItemEqual(this.required) && (invStack.getTagCompound() == null ? this.required.getTagCompound() == null : invStack.getTagCompound().equals(this.required.getTagCompound())))
{
int stackSize = invStack.stackSize;
int used = 0;
if(kept > 0)
{
int remainingFromStack = Math.max(stackSize - kept, 0);
used += stackSize - remainingFromStack;
}
kept -= used;
if(kept <= 0 && needed > 0)
{
int remainingFromStack = Math.max(stackSize - used - needed, 0);
needed -= (stackSize - used - remainingFromStack);
if(doDrain)
{
invStack.stackSize = remainingFromStack;
if(invStack.stackSize <= 0)
{
inv[i] = null;
}
}
}
if(needed <= 0)
{
return 0;
}
}
}
return null;
return needed;
}
public int getLeftover()