This commit is contained in:
Salomão
2021-05-09 10:59:31 -03:00
parent f3877aa1ec
commit ba4cdbcb85
2 changed files with 44 additions and 26 deletions
@@ -71,23 +71,28 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
@Override @Override
public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src ) public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src )
{ {
if( this.cache.cachedAeStacks.length == 0 ) this.cache.update();
ItemStack orgInput = iox.createItemStack(); ItemStack orgInput = iox.createItemStack();
ItemStack remaining = orgInput; ItemStack remaining = orgInput;
int slotCount = this.itemHandler.getSlots(); int slotCount = this.itemHandler.getSlots();
boolean simulate = ( type == Actionable.SIMULATE ); boolean simulate = ( type == Actionable.SIMULATE );
List<Pair<Integer,Integer>> injectedList = new ArrayList<>(); List<Pair<Integer, Integer>> injectedList = new ArrayList<>();
// This uses a brute force approach and tries to jam it in every slot the inventory exposes. // This uses a brute force approach and tries to jam it in every slot the inventory exposes.
for( int i = 0; i < slotCount && !remaining.isEmpty(); i++ ) for( int i = 0; i < slotCount && !remaining.isEmpty(); i++ )
{ {
int countPre = remaining.getCount(); int countPre = remaining.getCount();
remaining = this.itemHandler.insertItem( i, remaining, simulate ); remaining = this.itemHandler.insertItem( i, remaining, simulate );
int countPos = remaining.getCount(); int countPos = remaining.getCount();
if (remaining.isEmpty()){ if( remaining.isEmpty() )
injectedList.add( Pair.of( i,countPre ) ); {
injectedList.add( Pair.of( i, countPre ) );
break;
} }
else if (countPos > countPre) { else if( countPos > countPre )
injectedList.add( Pair.of( i,countPos - countPre ) ); {
injectedList.add( Pair.of( i, countPos - countPre ) );
} }
} }
@@ -100,8 +105,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
if( type == Actionable.MODULATE ) if( type == Actionable.MODULATE )
{ {
if (this.cache.cachedAeStacks.length == 0) this.cache.update(); for( Pair<Integer, Integer> pair : injectedList )
for ( Pair<Integer, Integer> pair : injectedList )
{ {
if( this.cache.cachedAeStacks[pair.getKey()] == null ) if( this.cache.cachedAeStacks[pair.getKey()] == null )
{ {
@@ -112,6 +116,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
this.cache.cachedAeStacks[pair.getKey()].incStackSize( pair.getValue() ); this.cache.cachedAeStacks[pair.getKey()].incStackSize( pair.getValue() );
} }
} }
} }
return AEItemStack.fromItemStack( remaining ); return AEItemStack.fromItemStack( remaining );
@@ -161,8 +166,6 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
extracted.setCount( remainingCurrentSlot ); extracted.setCount( remainingCurrentSlot );
} }
extractedList.add( Pair.of( i, extracted.getCount() ) );
// We're just gonna use the first stack we get our hands on as the template for the rest. // We're just gonna use the first stack we get our hands on as the template for the rest.
// In case some stupid itemhandler (aka forge) returns an internal state we have to do a second // In case some stupid itemhandler (aka forge) returns an internal state we have to do a second
// expensive copy again. // expensive copy again.
@@ -181,6 +184,11 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
remainingSize -= stackSizeCurrentSlot - remainingCurrentSlot; remainingSize -= stackSizeCurrentSlot - remainingCurrentSlot;
if (!gathered.isEmpty())
{
extractedList.add( Pair.of( i, gathered.getCount() ) );
}
// Done? // Done?
if( remainingSize <= 0 ) if( remainingSize <= 0 )
{ {
@@ -192,13 +200,21 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
{ {
if( mode == Actionable.MODULATE ) if( mode == Actionable.MODULATE )
{ {
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
for ( Pair<Integer, Integer> pair : extractedList ) for ( Pair<Integer, Integer> pair : extractedList )
{ {
this.cache.cachedAeStacks[pair.getKey()].decStackSize( pair.getValue() ); if (pair.getKey() >= this.cache.cachedAeStacks.length)
if( this.cache.cachedAeStacks[pair.getKey()].getStackSize() == 0 )
{ {
this.cache.cachedAeStacks[pair.getKey()] = null; this.cache.update();
break;
}
if( this.cache.cachedAeStacks[pair.getKey()] != null )
{
this.cache.cachedAeStacks[pair.getKey()].decStackSize( pair.getValue() );
if( this.cache.cachedAeStacks[pair.getKey()].getStackSize() == 0 )
{
this.cache.cachedAeStacks[pair.getKey()] = null;
}
} }
} }
} }
@@ -1,11 +1,6 @@
package appeng.parts.misc; package appeng.parts.misc;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import appeng.api.storage.IStorageChannel; import appeng.api.storage.IStorageChannel;
@@ -71,9 +66,9 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
if( type == Actionable.MODULATE ) if( type == Actionable.MODULATE )
{ {
if (this.cache.cachedAeStacks.length == 0) this.cache.update(); if( this.cache.cachedAeStacks.length == 0 ) this.cache.update();
boolean found = false; boolean found = false;
for (IAEItemStack iaeItemStack : this.cache.cachedAeStacks) for( IAEItemStack iaeItemStack : this.cache.cachedAeStacks )
{ {
if( iaeItemStack == null ) if( iaeItemStack == null )
{ {
@@ -85,11 +80,18 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
iaeItemStack.incStackSize( iox.getStackSize() ); iaeItemStack.incStackSize( iox.getStackSize() );
} }
} }
if (!found) if( !found )
{ {
this.cache.cachedAeStacks = Arrays.copyOf( this.cache.cachedAeStacks, this.cache.cachedAeStacks.length + 1 ); this.cache.cachedAeStacks = Arrays.copyOf( this.cache.cachedAeStacks, this.cache.cachedAeStacks.length + 1 );
this.cache.cachedAeStacks[this.cache.cachedAeStacks.length - 1] = iox.copy(); IAEItemStack inserted = iox.copy();
if (remaining.isEmpty())
{
this.cache.cachedAeStacks[this.cache.cachedAeStacks.length - 1] = inserted;
} else {
this.cache.cachedAeStacks[this.cache.cachedAeStacks.length - 1] = inserted.setStackSize( iox.getStackSize() - remaining.getCount() );
}
} }
} }
return AEItemStack.fromItemStack( remaining ); return AEItemStack.fromItemStack( remaining );
@@ -99,8 +101,8 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
@Override @Override
public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src ) public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src )
{ {
ItemStack requestedItemStack = request.createItemStack(); ItemStack requestedItemStack = request.getDefinition();
int remainingSize = requestedItemStack.getCount(); int remainingSize = (int) Math.min( Integer.MAX_VALUE, request.getStackSize());
final boolean simulate = ( mode == Actionable.SIMULATE ); final boolean simulate = ( mode == Actionable.SIMULATE );
@@ -126,7 +128,7 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
for ( int i = 0; i < this.cache.cachedAeStacks.length; i++ ) for ( int i = 0; i < this.cache.cachedAeStacks.length; i++ )
{ {
IAEItemStack iaeItemStack = this.cache.cachedAeStacks[i]; IAEItemStack iaeItemStack = this.cache.cachedAeStacks[i];
if( iaeExtracted.equals( iaeItemStack ) ) if( iaeExtracted.isSameType( iaeItemStack ) )
{ {
if( iaeExtracted.getStackSize() >= iaeItemStack.getStackSize() ) if( iaeExtracted.getStackSize() >= iaeItemStack.getStackSize() )
{ {