Compare commits

...

9 Commits

Author SHA1 Message Date
Salomão f3877aa1ec Merge branch 'iir-cache' into AE2-Omnifactory 2021-05-08 22:46:56 -03:00
Salomão 366b31b3aa import bus optimization 2021-05-08 22:11:13 -03:00
Salomão 533fb5c34b initial test 2021-05-08 19:40:58 -03:00
Salomão 62d1d904c6 update item cache directly on insert/removal
import bus fails faster and throttles down when inventory is full
2021-05-08 19:08:21 -03:00
Salomão 03fb43db16 revert blocking mode to previous implementation 2021-05-05 19:45:25 -03:00
Salomão 9576bec855 renamed method. fixed round robin while blocking mode on 2021-05-04 22:51:20 -03:00
Salomão b5da6730a1 loop optimization 2021-05-04 15:15:09 -03:00
Salomão 039bab4811 drop items waiting to be injected when interface is broken 2021-05-04 11:07:31 -03:00
Salomão 4b5bdf9893 fix NPE 2021-05-04 10:57:01 -03:00
8 changed files with 246 additions and 95 deletions
@@ -24,6 +24,7 @@ import java.util.stream.Collectors;
import javax.annotation.Nullable;
import appeng.integration.modules.gregtech.GTCEInventoryAdaptor;
import appeng.util.*;
import appeng.util.inv.*;
import com.google.common.collect.ImmutableSet;
@@ -137,6 +138,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
private final Accessor accessor = new Accessor();
private EnumSet<EnumFacing> visitedFaces = EnumSet.noneOf( EnumFacing.class );
private EnumMap<EnumFacing,List<ItemStack>> waitingToSendFacing = new EnumMap<>(EnumFacing.class);
private GTCEInventoryAdaptor GTad;
public DualityInterface( final AENetworkProxy networkProxy, final IInterfaceHost ih )
{
@@ -726,8 +728,8 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
private void pushItemsOut( final EnumFacing s )
{
if( this.waitingToSendFacing.get(s) == null || this.waitingToSendFacing.get( s ).isEmpty() )
{
if (!this.waitingToSendFacing.containsKey(s) ||
(this.waitingToSendFacing.containsKey(s) && this.waitingToSendFacing.get( s ).isEmpty())) {
return;
}
@@ -761,7 +763,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
if( this.waitingToSendFacing.get( s ).isEmpty() )
{
this.waitingToSendFacing.get( s ).clear();
this.waitingToSendFacing.remove( s );
}
}
@@ -1020,20 +1022,12 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
private boolean invIsBlocked( InventoryAdaptor inv )
{
final Iterator<ICraftingPatternDetails> i = this.craftingList.iterator();
ICraftingPatternDetails patt = i.next();
for ( ItemSlot itemSlot : inv)
{
if (itemSlot.getItemStack().isEmpty()){
continue;
}
for ( IAEItemStack iaeItemStack : patt.getCondensedInputs()){
if (iaeItemStack.isSameType( itemSlot.getItemStack() )){
return true;
}
}
}
return false;
return (inv.containsItems());
}
private static boolean invIsBlockedGTCE( GTCEInventoryAdaptor inv )
{
return ( !inv.canRemoveAllExceptCircuits() );
}
@Override
@@ -1076,12 +1070,11 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
final ICraftingMachine cm = (ICraftingMachine) te;
if( cm.acceptsPlans() )
{
visitedFaces.remove( s );
if( cm.pushPattern( patternDetails, table, s.getOpposite() ) )
{
visitedFaces.remove( s );
return true;
}
visitedFaces.remove( s );
continue;
}
}
@@ -1092,14 +1085,25 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
boolean isDrawer = te.getBlockType().getRegistryName().getResourceDomain().equals( "storagedrawers" );
if( this.isBlocking() )
{
if( invIsBlocked( ad ) )
if( te.getBlockType().getRegistryName().getResourceDomain().equals( "gregtech" ) )
{
GTad = GTCEInventoryAdaptor.getAdaptor( te, s.getOpposite() );
if( invIsBlockedGTCE( GTad ) )
{
visitedFaces.remove( s );
continue;
}
}
else if ( invIsBlocked( ad ) )
{
visitedFaces.remove( s );
continue;
}
}
if( this.acceptsItems( ad, patternDetails, isDrawer ) )
{
visitedFaces.remove( s );
for( int x = 0; x < table.getSizeInventory(); x++ )
{
final ItemStack is = table.getStackInSlot( x );
@@ -1107,9 +1111,8 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
{
addToSendListFacing( is, s );
}
pushItemsOut( s );
}
visitedFaces.remove( s );
pushItemsOut( s );
return true;
}
}
@@ -1143,7 +1146,16 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
final InventoryAdaptor ad = InventoryAdaptor.getAdaptor( te, s.getOpposite() );
if( ad != null )
{
if( !invIsBlocked( ad ) )
if( te.getBlockType().getRegistryName().getResourceDomain().equals( "gregtech" ) )
{
GTad = GTCEInventoryAdaptor.getAdaptor( te, s.getOpposite() );
if( !invIsBlockedGTCE( GTad ) )
{
allAreBusy = false;
break;
}
}
else if( !invIsBlocked( ad ) )
{
allAreBusy = false;
break;
@@ -1176,67 +1188,57 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
}
else
{
Iterator<ItemSlot> adit = ad.iterator();
while ( adit.hasNext() )
for ( ItemSlot itemSlot : ad )
{
ItemSlot is = adit.next();
//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 )
//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;
for( IAEItemStack aeItemStack : patternDetails.getCondensedInputs() )
if( stackInSlot.isEmpty() )
{
if( is.insertItem( aeItemStack.getDefinition() ).isEmpty() )
if( itemSlot.simulateInsertItem( aeItemStack.getDefinition() ).isEmpty() )
{
validInputSlotForIngredients = true;
copiedItemSlots.add( itemSlot.copy() );
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 ) )
{
copiedItemSlots.add( itemSlot.copy() );
break;
}
}
}
else if( !is.getItemStack().isEmpty() && is.getItemStack().getCount() < is.getSlotLimit() )
{
copiedItemSlots.add( is.copy() );
}
}
Iterator<ItemSlot> copiedItemSlotIterator = copiedItemSlots.iterator();
while ( copiedItemSlotIterator.hasNext() )
{
ItemSlot copiedItemSlot = copiedItemSlotIterator.next();
Iterator<ItemStack> psi = patternedStacks.iterator();
while ( psi.hasNext() )
// start merging in order of slots, left to right.
for ( ItemSlot copiedItemSlot : copiedItemSlots )
{
Iterator<ItemStack> patStackIterator = patternedStacks.iterator();
while ( patStackIterator.hasNext() )
{
ItemStack patternedStack = psi.next();
ItemStack remainder = copiedItemSlot.insertItem( patternedStack );
ItemStack patStack = patStackIterator.next();
ItemStack remainder = copiedItemSlot.simulateInsertItem( patStack );
if( !remainder.isEmpty() )
{
patternedStack.setCount( patternedStack.getCount() - ( patternedStack.getCount() - remainder.getCount() ) );
if( copiedItemSlot.getSlotLimit() == copiedItemSlot.getItemStack().getCount() )
{
copiedItemSlotIterator.remove();
break;
}
patStack.setCount( patStack.getCount() - ( patStack.getCount() - remainder.getCount() ) );
}
else
else //entire stack got injected
{
if( copiedItemSlot.getSlotLimit() == copiedItemSlot.getItemStack().getCount() )
{
copiedItemSlotIterator.remove();
}
psi.remove();
patStackIterator.remove();
break;
}
}
// if we merged EVERYTHING successfully , return true.
if( patternedStacks.size() == 0 )
{
return true;
@@ -1272,6 +1274,20 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
}
}
if( this.waitingToSendFacing != null )
{
for (List<ItemStack> itemList : waitingToSendFacing.values())
{
for (final ItemStack is : itemList)
{
if (!is.isEmpty())
{
drops.add(is);
}
}
}
}
for( final ItemStack is : this.upgrades )
{
if( !is.isEmpty() )
@@ -0,0 +1,26 @@
package appeng.integration.modules.gregtech;
import appeng.util.inv.ItemSlot;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
public abstract class GTCEInventoryAdaptor implements Iterable<ItemSlot>
{
public static GTCEInventoryAdaptor getAdaptor(final TileEntity te, final EnumFacing d)
{
if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, d))
{
// Attempt getting an IItemHandler for the given side via caps
IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, d);
if (itemHandler != null)
{
return new GTCEItemHandler(itemHandler);
}
}
return null;
}
public abstract boolean canRemoveAllExceptCircuits();
}
@@ -0,0 +1,58 @@
package appeng.integration.modules.gregtech;
import appeng.util.inv.ItemHandlerIterator;
import appeng.util.inv.ItemSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class GTCEItemHandler extends GTCEInventoryAdaptor
{
ArrayList<Integer> GTCElenses = new ArrayList<>(Arrays.asList(15085,15214,15092,15111,15113,15219,15218,15243,15244,15209,15117,15206,15216,15331,15212,15213,15154,15122,15157,15190,15247));
ArrayList<Integer> GTCEmolds = new ArrayList<>(Arrays.asList(32301,32303,32304,32305,32306,32307,32308,32309,32313,32314,32315,32317,32350,32351,32352,32353,32354,32355,32356,32358,32359,32360,32361,32363,32364,32365,32366,32367,32368,32369,32370,32371,32372,32373));
Item smallGearExtruderShape = Item.getByNameOrId("contenttweaker:smallgearextrudershape");
Item creativePortableTankMold = Item.getByNameOrId("contenttweaker:creativeportabletankmold");
protected final IItemHandler itemHandler;
public GTCEItemHandler(IItemHandler itemHandler)
{
this.itemHandler = itemHandler;
}
boolean isBlockableItem(ItemStack stack)
{
if ( stack.getItem() == Item.getByNameOrId("gregtech:meta_item_1") ) {
int metadata = stack.getItemDamage();
if ( metadata == 32766 || GTCElenses.contains(metadata) || GTCEmolds.contains(metadata)) {
return false;
}
}
if ( stack.getItem() == smallGearExtruderShape ) return false;
if ( stack.getItem() == creativePortableTankMold) return false;
return true;
}
@Override
public boolean canRemoveAllExceptCircuits()
{
int slots = this.itemHandler.getSlots();
for ( int slot = 0; slot < slots; slot++ ) {
ItemStack is = this.itemHandler.getStackInSlot(slot);
if ( is.isEmpty() || !isBlockableItem(is) ) continue;
return false;
}
return true;
}
@Override
public Iterator<ItemSlot> iterator()
{
return new ItemHandlerIterator( this.itemHandler );
}
}
@@ -218,6 +218,12 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin
private boolean importStuff( final InventoryAdaptor myAdaptor, final IAEItemStack whatToImport, final IMEMonitor<IAEItemStack> inv, final IEnergySource energy, final FuzzyMode fzMode )
{
final int toSend = this.calculateMaximumAmountToImport( myAdaptor, whatToImport, inv, fzMode );
if( toSend == 0 )
{
return true;
}
final ItemStack newItems;
if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
@@ -286,8 +292,17 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin
itemAmountNotStorable = inv.injectItems( AEItemStack.fromItemStack( simResult ), Actionable.SIMULATE, this.source );
}
if( simResult.isEmpty() )
{
return 0;
}
if( itemAmountNotStorable != null )
{
if( simResult.getCount() == itemAmountNotStorable.getStackSize() )
{
return 0;
}
return (int) Math.min( simResult.getCount() - itemAmountNotStorable.getStackSize(), toSend );
}
@@ -19,12 +19,7 @@
package appeng.parts.misc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
import appeng.api.config.Settings;
import appeng.api.config.StorageFilter;
@@ -42,11 +37,11 @@ import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.core.AELog;
import appeng.me.GridAccessException;
import appeng.me.helpers.IGridProxyable;
import appeng.me.storage.ITickingMonitor;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
import org.apache.commons.lang3.tuple.Pair;
/**
@@ -81,11 +76,19 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
int slotCount = this.itemHandler.getSlots();
boolean simulate = ( type == Actionable.SIMULATE );
List<Pair<Integer,Integer>> injectedList = new ArrayList<>();
// 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++ )
{
int countPre = remaining.getCount();
remaining = this.itemHandler.insertItem( i, remaining, simulate );
int countPos = remaining.getCount();
if (remaining.isEmpty()){
injectedList.add( Pair.of( i,countPre ) );
}
else if (countPos > countPre) {
injectedList.add( Pair.of( i,countPos - countPre ) );
}
}
// At this point, we still have some items left...
@@ -97,13 +100,17 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
if( type == Actionable.MODULATE )
{
try
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
for ( Pair<Integer, Integer> pair : injectedList )
{
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
}
catch( GridAccessException ex )
{
// meh
if( this.cache.cachedAeStacks[pair.getKey()] == null )
{
this.cache.cachedAeStacks[pair.getKey()] = iox.copy().setStackSize( pair.getValue() );
}
else
{
this.cache.cachedAeStacks[pair.getKey()].incStackSize( pair.getValue() );
}
}
}
@@ -121,8 +128,10 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
ItemStack gathered = ItemStack.EMPTY;
final boolean simulate = ( mode == Actionable.SIMULATE );
List<Pair<Integer, Integer>> extractedList = new ArrayList<>();
for( int i = 0; i < this.itemHandler.getSlots(); i++ )
for ( int i = 0; i < this.itemHandler.getSlots(); i++ )
{
ItemStack stackInInventorySlot = this.itemHandler.getStackInSlot( i );
@@ -140,6 +149,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
do
{
extracted = this.itemHandler.extractItem( i, remainingCurrentSlot, simulate );
if( !extracted.isEmpty() )
{
if( extracted.getCount() > remainingCurrentSlot )
@@ -151,6 +161,8 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
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.
// In case some stupid itemhandler (aka forge) returns an internal state we have to do a second
// expensive copy again.
@@ -165,7 +177,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
remainingCurrentSlot -= extracted.getCount();
}
}
while( !extracted.isEmpty() && remainingCurrentSlot > 0 );
while ( !extracted.isEmpty() && remainingCurrentSlot > 0 );
remainingSize -= stackSizeCurrentSlot - remainingCurrentSlot;
@@ -180,13 +192,14 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
{
if( mode == Actionable.MODULATE )
{
try
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
for ( Pair<Integer, Integer> pair : extractedList )
{
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
}
catch( GridAccessException ex )
{
// meh
this.cache.cachedAeStacks[pair.getKey()].decStackSize( pair.getValue() );
if( this.cache.cachedAeStacks[pair.getKey()].getStackSize() == 0 )
{
this.cache.cachedAeStacks[pair.getKey()] = null;
}
}
}
@@ -71,13 +71,24 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
if( type == Actionable.MODULATE )
{
try
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
boolean found = false;
for (IAEItemStack iaeItemStack : this.cache.cachedAeStacks)
{
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
if( iaeItemStack == null )
{
continue;
}
if( iaeItemStack.equals( iox ) )
{
found = true;
iaeItemStack.incStackSize( iox.getStackSize() );
}
}
catch( GridAccessException ex )
if (!found)
{
// meh
this.cache.cachedAeStacks = Arrays.copyOf( this.cache.cachedAeStacks, this.cache.cachedAeStacks.length + 1 );
this.cache.cachedAeStacks[this.cache.cachedAeStacks.length - 1] = iox.copy();
}
}
@@ -107,15 +118,27 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
if( !extracted.isEmpty() )
{
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
IAEItemStack iaeExtracted = AEItemStack.fromItemStack( extracted );
if( mode == Actionable.MODULATE )
{
try
for ( int i = 0; i < this.cache.cachedAeStacks.length; i++ )
{
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
}
catch( GridAccessException ex )
{
// meh
IAEItemStack iaeItemStack = this.cache.cachedAeStacks[i];
if( iaeExtracted.equals( iaeItemStack ) )
{
if( iaeExtracted.getStackSize() >= iaeItemStack.getStackSize() )
{
iaeExtracted.decStackSize( iaeItemStack.getStackSize() );
this.cache.cachedAeStacks[i] = null;
}
else
{
iaeItemStack.decStackSize( iaeExtracted.getStackSize() );
break;
}
}
}
}
@@ -25,7 +25,7 @@ import java.util.NoSuchElementException;
import net.minecraftforge.items.IItemHandler;
class ItemHandlerIterator implements Iterator<ItemSlot>
public class ItemHandlerIterator implements Iterator<ItemSlot>
{
private final IItemHandler itemHandler;
@@ -34,7 +34,7 @@ class ItemHandlerIterator implements Iterator<ItemSlot>
private int slot = 0;
ItemHandlerIterator( IItemHandler itemHandler )
public ItemHandlerIterator( IItemHandler itemHandler )
{
this.itemHandler = itemHandler;
}
+1 -1
View File
@@ -109,7 +109,7 @@ public class ItemSlot
return copy;
}
public ItemStack insertItem( ItemStack sourceItemStack){
public ItemStack simulateInsertItem( ItemStack sourceItemStack){
return this.itemHandler.insertItem( this.slot, sourceItemStack , true );
}
}