Fixes #348 by respecting the inventory stack limit

This commit is contained in:
yueh
2014-11-29 17:52:34 +01:00
parent 7e369937e0
commit cf89873447
@@ -223,121 +223,79 @@ public class AdaptorIInventory extends InventoryAdaptor
} }
@Override @Override
public ItemStack addItems( ItemStack A ) public ItemStack addItems( ItemStack itemsToAdd )
{ {
if ( A == null ) return addItems( itemsToAdd, true );
return null;
if ( A.stackSize == 0 )
return null;
ItemStack left = A.copy();
int stack_limit = A.getMaxStackSize();
if ( stack_limit > i.getInventoryStackLimit() )
stack_limit = i.getInventoryStackLimit();
int s = i.getSizeInventory();
for ( int pass = 0; pass < 2; pass++ )
{
for ( int x = 0; x < s; x++ )
{
if ( i.isItemValidForSlot( x, A ) )
{
ItemStack is = i.getStackInSlot( x );
if ( is == null && pass != 0 )
{
ItemStack thisSlot = left.copy();
if ( thisSlot.stackSize > stack_limit )
thisSlot.stackSize = stack_limit;
left.stackSize -= thisSlot.stackSize;
i.setInventorySlotContents( x, thisSlot );
i.markDirty();
if ( left.stackSize <= 0 )
{
return null;
}
}
else if ( is != null )
{
if ( Platform.isSameItemPrecise( is, left ) )
{
if ( is.stackSize < stack_limit )
{
int room = stack_limit - is.stackSize;
int used = left.stackSize;
if ( used > room )
used = room;
is.stackSize += used;
i.setInventorySlotContents( x, is );
i.markDirty();
left.stackSize -= used;
if ( left.stackSize <= 0 )
{
return null;
}
}
}
}
}
}
}
// if ( left.stackSize != A.stackSize )
// i.markDirty();
return left;
} }
@Override @Override
public ItemStack simulateAdd( ItemStack A ) public ItemStack simulateAdd( ItemStack itemsToAdd )
{ {
if ( A == null ) return addItems( itemsToAdd, false );
return A; }
ItemStack left = A.copy();
int stack_limit = A.getMaxStackSize(); /**
if ( stack_limit > i.getInventoryStackLimit() ) * Adds an {@link ItemStack} to the adapted {@link IInventory}.
stack_limit = i.getInventoryStackLimit(); *
* It respects the inventories stack limit, which can result in not all items added and some left ones are returned.
int s = i.getSizeInventory(); * The ItemStack next is required for inventories, which will fail on isItemValidForSlot() for stacksizes larger
for ( int x = 0; x < s; x++ ) * than the limit.
*
* @param itemsToAdd itemStack to add to the inventory
* @param modulate true to modulate, false for simulate
* @return
*/
private ItemStack addItems( ItemStack itemsToAdd, boolean modulate )
{
if ( itemsToAdd == null || itemsToAdd.stackSize == 0 )
{ {
if ( i.isItemValidForSlot( x, A ) ) return null;
}
ItemStack left = itemsToAdd.copy();
int stackLimit = itemsToAdd.getMaxStackSize();
int perOperationLimit = Math.min( i.getInventoryStackLimit(), stackLimit );
int inventorySize = i.getSizeInventory();
for ( int slot = 0; slot < inventorySize; slot++ )
{
ItemStack next = left.copy();
next.stackSize = Math.min( perOperationLimit, next.stackSize );
if ( i.isItemValidForSlot( slot, next ) )
{ {
ItemStack is = i.getStackInSlot( x ); ItemStack is = i.getStackInSlot( slot );
if ( is == null ) if ( is == null )
{ {
ItemStack thisSlot = left.copy(); left.stackSize -= next.stackSize;
if ( thisSlot.stackSize > stack_limit )
thisSlot.stackSize = stack_limit; if ( modulate )
left.stackSize -= thisSlot.stackSize; {
i.setInventorySlotContents( slot, next );
i.markDirty();
}
if ( left.stackSize <= 0 ) if ( left.stackSize <= 0 )
{ {
return null; return null;
} }
} }
else else if ( Platform.isSameItemPrecise( is, left ) && is.stackSize < stackLimit )
{ {
if ( Platform.isSameItemPrecise( is, left ) ) int room = stackLimit - is.stackSize;
{ int used = Math.min( left.stackSize, room );
if ( is.stackSize < stack_limit )
{
int room = stack_limit - is.stackSize;
int used = left.stackSize;
if ( used > room )
used = room;
left.stackSize -= used; if ( modulate )
if ( left.stackSize <= 0 ) {
{ is.stackSize += used;
return null; i.setInventorySlotContents( slot, is );
} i.markDirty();
} }
left.stackSize -= used;
if ( left.stackSize <= 0 )
{
return null;
} }
} }
} }