loop optimization

This commit is contained in:
Salomão
2021-05-04 11:14:55 -03:00
parent 039bab4811
commit b5da6730a1
@@ -1175,67 +1175,57 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
} }
else else
{ {
Iterator<ItemSlot> adit = ad.iterator(); for ( ItemSlot itemSlot : ad )
while ( adit.hasNext() )
{ {
ItemSlot is = adit.next();
//skip storage drawers slot 0 to avoid voiding items due to broken itemhandler implementation //skip storage drawers slot 0 to avoid voiding items due to broken itemhandler implementation
if( isDrawer && is.getSlot() == 0 ) is = adit.next(); if( isDrawer && itemSlot.getSlot() == 0 ){
continue;
}
//some inventory may expose their special slots ( for upgrades, etc ) //some inventory may expose their special slots ( for upgrades, etc )
//if its empty AND we cant fit any of the items in the recipes, skip it. //if its empty AND we cant fit any of the items in the recipes, skip it.
if( is.getItemStack().isEmpty() ) ItemStack stackInSlot = itemSlot.getItemStack();
for ( IAEItemStack aeItemStack : patternDetails.getCondensedInputs() )
{ {
boolean validInputSlotForIngredients = false; if( stackInSlot.isEmpty() )
for( IAEItemStack aeItemStack : patternDetails.getCondensedInputs() )
{ {
if( is.insertItem( aeItemStack.getDefinition() ).isEmpty() ) if( itemSlot.insertItem( aeItemStack.getDefinition() ).isEmpty() )
{ {
validInputSlotForIngredients = true; copiedItemSlots.add( itemSlot.copy() );
break; break;
} }
} }
if( validInputSlotForIngredients ) // copy partially filled slots for merging logic
else if( stackInSlot.getCount() < Math.min( itemSlot.getSlotLimit(), stackInSlot.getMaxStackSize() ) )
{ {
copiedItemSlots.add( is.copy() ); if( aeItemStack.isSameType( stackInSlot ) )
}
}
else if( !is.getItemStack().isEmpty() && is.getItemStack().getCount() < is.getSlotLimit() )
{ {
copiedItemSlots.add( is.copy() ); copiedItemSlots.add( itemSlot.copy() );
break;
}
}
} }
} }
Iterator<ItemSlot> copiedItemSlotIterator = copiedItemSlots.iterator();
while ( copiedItemSlotIterator.hasNext() )
{
ItemSlot copiedItemSlot = copiedItemSlotIterator.next();
Iterator<ItemStack> psi = patternedStacks.iterator(); // start merging in order of slots, left to right.
while ( psi.hasNext() ) for ( ItemSlot copiedItemSlot : copiedItemSlots )
{ {
ItemStack patternedStack = psi.next(); Iterator<ItemStack> patStackIterator = patternedStacks.iterator();
ItemStack remainder = copiedItemSlot.insertItem( patternedStack ); while ( patStackIterator.hasNext() )
{
ItemStack patStack = patStackIterator.next();
ItemStack remainder = copiedItemSlot.insertItem( patStack );
if( !remainder.isEmpty() ) if( !remainder.isEmpty() )
{ {
patternedStack.setCount( patternedStack.getCount() - ( patternedStack.getCount() - remainder.getCount() ) ); patStack.setCount( patStack.getCount() - ( patStack.getCount() - remainder.getCount() ) );
if( copiedItemSlot.getSlotLimit() == copiedItemSlot.getItemStack().getCount() ) }
else //entire stack got injected
{ {
copiedItemSlotIterator.remove(); patStackIterator.remove();
break; break;
} }
} }
else // if we merged EVERYTHING successfully , return true.
{
if( copiedItemSlot.getSlotLimit() == copiedItemSlot.getItemStack().getCount() )
{
copiedItemSlotIterator.remove();
}
psi.remove();
break;
}
}
if( patternedStacks.size() == 0 ) if( patternedStacks.size() == 0 )
{ {
return true; return true;