Fixes #348 by respecting the inventory stack limit
This commit is contained in:
parent
7e369937e0
commit
cf89873447
1 changed files with 55 additions and 97 deletions
|
@ -223,114 +223,74 @@ 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.
|
||||||
|
* The ItemStack next is required for inventories, which will fail on isItemValidForSlot() for stacksizes larger
|
||||||
|
* 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 )
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
int s = i.getSizeInventory();
|
ItemStack left = itemsToAdd.copy();
|
||||||
for ( int x = 0; x < s; x++ )
|
int stackLimit = itemsToAdd.getMaxStackSize();
|
||||||
|
int perOperationLimit = Math.min( i.getInventoryStackLimit(), stackLimit );
|
||||||
|
int inventorySize = i.getSizeInventory();
|
||||||
|
|
||||||
|
for ( int slot = 0; slot < inventorySize; slot++ )
|
||||||
{
|
{
|
||||||
if ( i.isItemValidForSlot( x, A ) )
|
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 ( modulate )
|
||||||
{
|
{
|
||||||
if ( is.stackSize < stack_limit )
|
is.stackSize += used;
|
||||||
{
|
i.setInventorySlotContents( slot, is );
|
||||||
int room = stack_limit - is.stackSize;
|
i.markDirty();
|
||||||
int used = left.stackSize;
|
}
|
||||||
if ( used > room )
|
|
||||||
used = room;
|
|
||||||
|
|
||||||
left.stackSize -= used;
|
left.stackSize -= used;
|
||||||
if ( left.stackSize <= 0 )
|
if ( left.stackSize <= 0 )
|
||||||
|
@ -340,8 +300,6 @@ public class AdaptorIInventory extends InventoryAdaptor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue