Compare commits

...

8 Commits

Author SHA1 Message Date
Salomão 31a3afc18b revert inventory merging code to keep compatibility with ae2fc 2021-05-14 12:43:34 -03:00
Salomão 9d6026635d level emitter changes 2021-05-13 19:32:03 -03:00
Salomão 39038ac935 revert iitemrepository cache changes 2021-05-12 23:22:54 -03:00
Salomão 4f627b234c invert comparison 2021-05-12 03:26:12 -03:00
Salomão 6180c188e6 Revert "remove crafting failure penalty"
This reverts commit 0240a5d76a.
2021-05-12 02:42:34 -03:00
Salomão 7ada6d18ee small optimizations 2021-05-12 00:19:55 -03:00
Salomão f493767243 fix out of bounds crash 2021-05-09 22:00:20 -03:00
Salomão ba4cdbcb85 fix #16 2021-05-09 10:59:31 -03:00
12 changed files with 426 additions and 224 deletions
@@ -264,18 +264,19 @@ public class PartFluidStorageBus extends PartSharedStorageBus implements IMEMoni
@Override
public void postChange( final IBaseMonitor<IAEFluidStack> monitor, final Iterable<IAEFluidStack> change, final IActionSource source )
{
try
if( source == this.source || source.machine().map( machine -> machine == this ).orElse( false ) )
{
if( this.getProxy().isActive() )
try
{
this.getProxy()
.getStorage()
.postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ), change, this.source );
if( this.getProxy().isActive() )
{
this.getProxy().getStorage().postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ), change, this.source );
}
}
catch( final GridAccessException e )
{
// :(
}
}
catch( final GridAccessException e )
{
// :(
}
}
@@ -107,6 +107,8 @@ import static gregtech.api.block.machines.BlockMachine.getMetaTileEntity;
public class DualityInterface implements IGridTickable, IStorageMonitorable, IInventoryDestination, IAEAppEngInventory, IConfigManagerHost, ICraftingProvider, IUpgradeableHost
{
private int[] failedCraftTriesSlot = {0,0,0,0,0,0,0,0,0};
public static final int NUMBER_OF_STORAGE_SLOTS = 9;
public static final int NUMBER_OF_CONFIG_SLOTS = 9;
public static final int NUMBER_OF_PATTERN_SLOTS = 9;
@@ -886,8 +888,17 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
{
if( this.getInstalledUpgrades( Upgrades.CRAFTING ) > 0 && itemStack != null )
{
return this.craftingTracker.handleCrafting( x, itemStack.getStackSize(), itemStack, d, this.iHost.getTileEntity().getWorld(),
if (this.failedCraftTriesSlot[x] <= 0)
{
boolean crafted = this.craftingTracker.handleCrafting( x, itemStack.getStackSize(), itemStack, d, this.iHost.getTileEntity().getWorld(),
this.gridProxy.getGrid(), this.gridProxy.getCrafting(), this.mySource );
if (crafted) this.failedCraftTriesSlot[x] = 0;
else{
this.failedCraftTriesSlot[x] += 2;
}
}
this.failedCraftTriesSlot[x] -= 1;
}
}
catch( final GridAccessException e )
@@ -1082,13 +1093,12 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
final InventoryAdaptor ad = InventoryAdaptor.getAdaptor( te, s.getOpposite() );
if( ad != null )
{
boolean isDrawer = te.getBlockType().getRegistryName().getResourceDomain().equals( "storagedrawers" );
if( this.isBlocking() )
{
if( te.getBlockType().getRegistryName().getResourceDomain().equals( "gregtech" ) )
{
GTad = GTCEInventoryAdaptor.getAdaptor( te, s.getOpposite() );
if( invIsBlockedGTCE( GTad ) )
if( GTad != null && !invIsBlockedGTCE( GTad ) )
{
visitedFaces.remove( s );
continue;
@@ -1101,7 +1111,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
}
}
if( this.acceptsItems( ad, patternDetails, isDrawer ) )
if( this.acceptsItems( ad, table ) )
{
visitedFaces.remove( s );
for( int x = 0; x < table.getSizeInventory(); x++ )
@@ -1149,7 +1159,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
if( te.getBlockType().getRegistryName().getResourceDomain().equals( "gregtech" ) )
{
GTad = GTCEInventoryAdaptor.getAdaptor( te, s.getOpposite() );
if( !invIsBlockedGTCE( GTad ) )
if( GTad != null && !invIsBlockedGTCE( GTad ) )
{
allAreBusy = false;
break;
@@ -1177,75 +1187,23 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
return this.cm.getSetting( Settings.BLOCK ) == YesNo.YES;
}
private boolean acceptsItems( final InventoryAdaptor ad, final ICraftingPatternDetails patternDetails, boolean isDrawer )
private boolean acceptsItems( final InventoryAdaptor ad, final InventoryCrafting table )
{
List<ItemStack> patternedStacks = Arrays.stream( patternDetails.getCondensedInputs() ).map( IAEItemStack::createItemStack ).collect( Collectors.toList() );
List<ItemSlot> copiedItemSlots = new ArrayList<>();
if( patternedStacks.size() == 1 )
for( int x = 0; x < table.getSizeInventory(); x++ )
{
return ad.simulateAdd( patternedStacks.get( 0 ) ).isEmpty();
}
else
{
for ( ItemSlot itemSlot : ad )
final ItemStack is = table.getStackInSlot( x );
if( is.isEmpty() )
{
//skip storage drawers slot 0 to avoid voiding items due to broken itemhandler implementation
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.
ItemStack stackInSlot = itemSlot.getItemStack();
for ( IAEItemStack aeItemStack : patternDetails.getCondensedInputs() )
{
if( stackInSlot.isEmpty() )
{
if( itemSlot.simulateInsertItem( aeItemStack.getDefinition() ).isEmpty() )
{
copiedItemSlots.add( itemSlot.copy() );
break;
}
}
// copy partially filled slots for merging logic
else if( stackInSlot.getCount() < Math.min( itemSlot.getSlotLimit(), stackInSlot.getMaxStackSize() ) )
{
if( aeItemStack.isSameType( stackInSlot ) )
{
copiedItemSlots.add( itemSlot.copy() );
break;
}
}
}
continue;
}
// start merging in order of slots, left to right.
for ( ItemSlot copiedItemSlot : copiedItemSlots )
if( !ad.simulateAdd( is.copy() ).isEmpty() )
{
Iterator<ItemStack> patStackIterator = patternedStacks.iterator();
while ( patStackIterator.hasNext() )
{
ItemStack patStack = patStackIterator.next();
ItemStack remainder = copiedItemSlot.simulateInsertItem( patStack );
if( !remainder.isEmpty() )
{
patStack.setCount( patStack.getCount() - ( patStack.getCount() - remainder.getCount() ) );
}
else //entire stack got injected
{
patStackIterator.remove();
break;
}
}
// if we merged EVERYTHING successfully , return true.
if( patternedStacks.size() == 0 )
{
return true;
}
return false;
}
return false;
}
return true;
}
@Override
+4 -2
View File
@@ -44,6 +44,8 @@ import appeng.api.storage.IStorageChannel;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.me.storage.ItemWatcher;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
@@ -58,7 +60,7 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
@Nonnull
private final IItemList<T> cachedList;
@Nonnull
private final Map<IMEMonitorHandlerReceiver<T>, Object> listeners;
private final Object2ObjectMap<IMEMonitorHandlerReceiver<T>, Object> listeners;
private boolean sendEvent = false;
private boolean hasChanged = false;
@@ -70,7 +72,7 @@ public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
this.myGridCache = cache;
this.myChannel = chan;
this.cachedList = chan.createList();
this.listeners = new HashMap<>();
this.listeners = new Object2ObjectOpenHashMap<>();
}
@Override
@@ -70,6 +70,8 @@ import appeng.util.item.AEItemStack;
public class PartExportBus extends PartSharedItemBus implements ICraftingRequester
{
private final int[] failedCraftTriesSlot = {0,0,0,0,0,0,0,0,0};
public static final ResourceLocation MODEL_BASE = new ResourceLocation( AppEng.MOD_ID, "part/export_bus_base" );
@PartModels
@@ -140,7 +142,7 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest
if( destination != null )
{
int x;
int x = 0;
for( x = 0; x < this.availableSlots() && this.itemToSend > 0; x++ )
{
@@ -151,8 +153,16 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest
if( ais == null || this.itemToSend <= 0 ) continue;
if ( this.craftOnly() ) {
this.didSomething = this.craftingTracker.handleCrafting(slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(),
this.getProxy().getGrid(), cg, this.mySrc) || this.didSomething;
if (this.failedCraftTriesSlot[x] <= 0) {
this.didSomething = this.craftingTracker.handleCrafting(slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(),
this.getProxy().getGrid(), cg, this.mySrc) || this.didSomething;
if (this.didSomething) this.failedCraftTriesSlot[x] = 0;
else this.failedCraftTriesSlot[x] += 2;
}
this.failedCraftTriesSlot[x] -= 1;
continue;
}
final long before = this.itemToSend;
@@ -175,8 +185,15 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest
if( this.itemToSend == before && this.isCraftingEnabled() )
{
this.didSomething = this.craftingTracker.handleCrafting(slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(),
if (this.failedCraftTriesSlot[x] <= 0) {
this.didSomething = this.craftingTracker.handleCrafting(slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(),
this.getProxy().getGrid(), cg, this.mySrc) || this.didSomething;
if (this.didSomething) this.failedCraftTriesSlot[x] = 0;
else this.failedCraftTriesSlot[x] += 2;
}
this.failedCraftTriesSlot[x] -= 1;
}
}
@@ -19,6 +19,7 @@
package appeng.parts.automation;
import java.util.ArrayList;
import java.util.Random;
import appeng.me.Grid;
@@ -26,6 +27,7 @@ import appeng.me.GridNode;
import appeng.util.item.AEItemStack;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryCrafting;
@@ -126,6 +128,8 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH
static Object2LongMap<Grid> gridItemCount = new Object2LongOpenHashMap<>();
static Object2ObjectOpenHashMap<Grid, Object2LongMap<AEItemStack>> fuzzyAEItemStackCount = new Object2ObjectOpenHashMap<>();
static Object2ObjectOpenHashMap<Grid, Object2LongMap<AEItemStack>> preciseAEItemStackCount = new Object2ObjectOpenHashMap<>();
static Object2ObjectMap<Grid,Iterable<IAEItemStack>> previousChange = new Object2ObjectOpenHashMap<>();
static Object2ObjectMap<Grid,IItemList<IAEItemStack>> cachedStorageList = new Object2ObjectOpenHashMap<>();
@Reflected
public PartLevelEmitter( final ItemStack is )
@@ -324,54 +328,90 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH
private void updateReportingValue( final IMEMonitor<IAEItemStack> monitor )
{
updateReportingValue( monitor,null );
updateReportingValue( monitor, new ArrayList<>() );
}
private void updateReportingValue( final IMEMonitor<IAEItemStack> monitor, Iterable<IAEItemStack> change )
{
final IAEItemStack myStack = this.config.getAEStackInSlot( 0 );
GridNode node = (GridNode) (this.getGridNode());
GridNode node = (GridNode) ( this.getGridNode() );
if( node != null )
{
final Grid g = node.getInternalGrid();
IItemList<IAEItemStack> l;
boolean alreadyEvaluated = false;
if( previousChange.containsKey( g ) )
{
if( change.iterator().hasNext() && previousChange.get( g ).iterator().hasNext() )
{
IAEItemStack a = change.iterator().next();
IAEItemStack b = previousChange.get( g ).iterator().next();
if( a == b )
{
alreadyEvaluated = true;
}
}
else {
alreadyEvaluated = true;
}
}
if( alreadyEvaluated )
{
l = cachedStorageList.get( g );
}
else
{
l = monitor.getStorageList();
cachedStorageList.put( g, l );
if ( change.iterator().hasNext() )
{
previousChange.put( g, change );
}
else
{
previousChange.put( g, new ArrayList<>() );
}
}
if( myStack == null )
{
if( change != null )
if( gridItemCount.containsKey( g ) )
{
if( gridItemCount.containsKey( g ) )
{
change.forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() );
}
change.forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() );
}
gridItemCount.computeIfAbsent( g, ( aLong ) -> {
this.lastReportedValue = 0;
monitor.getStorageList().forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() );
l.forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() );
return ( lastReportedValue );
} );
this.lastReportedValue = gridItemCount.get( g );
}
else if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
{
final FuzzyMode fzMode = (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE );
if( change != null )
if( fuzzyAEItemStackCount.containsKey( g ) )
{
if( fuzzyAEItemStackCount.containsKey( g ) )
{
change.forEach( iaeItemStack -> {
if( iaeItemStack.sameOre( myStack ) ) lastReportedValue += iaeItemStack.getStackSize();
} );
}
change.forEach( iaeItemStack -> {
if( iaeItemStack.sameOre( myStack ) )
{
lastReportedValue += iaeItemStack.getStackSize();
}
} );
}
fuzzyAEItemStackCount.computeIfAbsent( g, ( grid -> new Object2LongOpenHashMap<>() ) );
fuzzyAEItemStackCount.get( g ).computeIfAbsent( (AEItemStack) myStack, ( aLong -> {
this.lastReportedValue = 0;
monitor.getStorageList().findFuzzy( myStack, fzMode ).forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() );
l.findFuzzy( myStack, fzMode ).forEach( iaeItemStack -> lastReportedValue += iaeItemStack.getStackSize() );
return ( lastReportedValue );
} ) );
@@ -384,19 +424,17 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH
}
else
{
if( change != null )
if( preciseAEItemStackCount.containsKey( g ) )
{
if( preciseAEItemStackCount.containsKey( g ) )
{
change.forEach( iaeItemStack -> {
if( iaeItemStack.isSameType( myStack ) ) lastReportedValue += iaeItemStack.getStackSize();
} );
}
change.forEach( iaeItemStack -> {
if( iaeItemStack.isSameType( myStack ) ) lastReportedValue += iaeItemStack.getStackSize();
} );
}
preciseAEItemStackCount.computeIfAbsent( g, ( grid -> new Object2LongOpenHashMap<>() ) );
preciseAEItemStackCount.get( g ).computeIfAbsent( (AEItemStack) myStack, ( aLong -> {
this.lastReportedValue = 0;
IAEItemStack precise = monitor.getStorageList().findPrecise( myStack );
IAEItemStack precise = l.findPrecise( myStack );
if( precise != null ) lastReportedValue = precise.getStackSize();
return ( lastReportedValue );
} ) );
@@ -23,6 +23,8 @@ import java.util.*;
import appeng.api.config.Settings;
import appeng.api.config.StorageFilter;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
@@ -49,7 +51,7 @@ import org.apache.commons.lang3.tuple.Pair;
*/
class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAEItemStack>, ITickingMonitor
{
private final Map<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<>();
private final Object2ObjectMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new Object2ObjectOpenHashMap<>();
private IActionSource mySource;
private final IItemHandler itemHandler;
private final IGridProxyable proxyable;
@@ -71,23 +73,27 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
@Override
public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src )
{
ItemStack orgInput = iox.createItemStack();
ItemStack remaining = orgInput;
int slotCount = this.itemHandler.getSlots();
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.
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 ) );
if( remaining.isEmpty() )
{
injectedList.add( Pair.of( i, countPre ) );
break;
}
else if (countPos > countPre) {
injectedList.add( Pair.of( i,countPos - countPre ) );
else if( countPos < countPre )
{
injectedList.add( Pair.of( i, countPre - countPos ) );
}
}
@@ -100,18 +106,30 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
if( type == Actionable.MODULATE )
{
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
for ( Pair<Integer, Integer> pair : injectedList )
if( this.cache.cachedAeStacks.length == 0 )
{
if( this.cache.cachedAeStacks[pair.getKey()] == null )
this.cache.update();
}
else
{
for( Pair<Integer, Integer> pair : injectedList )
{
this.cache.cachedAeStacks[pair.getKey()] = iox.copy().setStackSize( pair.getValue() );
}
else
{
this.cache.cachedAeStacks[pair.getKey()].incStackSize( pair.getValue() );
if( pair.getKey() >= this.cache.cachedAeStacks.length )
{
this.cache.update();
break;
}
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() );
}
}
}
}
return AEItemStack.fromItemStack( remaining );
@@ -120,7 +138,6 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
@Override
public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src )
{
ItemStack requestedItemStack = request.createItemStack();
int remainingSize = requestedItemStack.getCount();
@@ -161,8 +178,6 @@ 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.
@@ -181,6 +196,11 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
remainingSize -= stackSizeCurrentSlot - remainingCurrentSlot;
if (!gathered.isEmpty())
{
extractedList.add( Pair.of( i, gathered.getCount() ) );
}
// Done?
if( remainingSize <= 0 )
{
@@ -192,13 +212,22 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
{
if( mode == Actionable.MODULATE )
{
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
for ( Pair<Integer, Integer> pair : extractedList )
if( this.cache.cachedAeStacks.length == 0 )
{
this.cache.cachedAeStacks[pair.getKey()].decStackSize( pair.getValue() );
if( this.cache.cachedAeStacks[pair.getKey()].getStackSize() == 0 )
this.cache.update();
}
else
{
for( Pair<Integer, Integer> pair : extractedList )
{
this.cache.cachedAeStacks[pair.getKey()] = null;
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;
}
}
}
}
}
@@ -377,7 +406,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
if( oldAeIS != null )
{
oldAeIS.setStackSize( -oldAeIS.getStackSize() );
changes.add( oldAeIS );
changes.add( oldAeIS.copy() );
}
// Notify the network about the new stack. Note that this is null if newIS was null
@@ -1,17 +1,14 @@
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 java.util.stream.Collectors;
import appeng.api.storage.IStorageChannel;
import appeng.core.AELog;
import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.item.ItemStack;
import appeng.api.AEApi;
@@ -38,7 +35,7 @@ import javax.annotation.Nonnull;
class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAEItemStack>, ITickingMonitor
{
private final Map<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<>();
private final Object2ObjectMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new Object2ObjectOpenHashMap<>();
private IActionSource mySource;
private final IItemRepository itemRepository;
private final IGridProxyable proxyable;
@@ -71,24 +68,13 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
if( type == Actionable.MODULATE )
{
if (this.cache.cachedAeStacks.length == 0) this.cache.update();
boolean found = false;
for (IAEItemStack iaeItemStack : this.cache.cachedAeStacks)
try
{
if( iaeItemStack == null )
{
continue;
}
if( iaeItemStack.equals( iox ) )
{
found = true;
iaeItemStack.incStackSize( iox.getStackSize() );
}
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
}
if (!found)
catch( GridAccessException ex )
{
this.cache.cachedAeStacks = Arrays.copyOf( this.cache.cachedAeStacks, this.cache.cachedAeStacks.length + 1 );
this.cache.cachedAeStacks[this.cache.cachedAeStacks.length - 1] = iox.copy();
// meh
}
}
@@ -99,13 +85,12 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
@Override
public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src )
{
ItemStack requestedItemStack = request.createItemStack();
int remainingSize = requestedItemStack.getCount();
ItemStack requestedItemStack = request.getDefinition();
int remainingSize = (int) Math.min( Integer.MAX_VALUE, request.getStackSize());
final boolean simulate = ( mode == Actionable.SIMULATE );
ItemStack extracted;
extracted = this.itemRepository.extractItem( requestedItemStack, remainingSize, simulate );
ItemStack extracted = this.itemRepository.extractItem( requestedItemStack, remainingSize, simulate );
if( extracted.getCount() > remainingSize )
{
@@ -118,33 +103,19 @@ 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 )
{
for ( int i = 0; i < this.cache.cachedAeStacks.length; i++ )
try
{
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;
}
}
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
}
catch( GridAccessException ex )
{
// meh
}
}
return AEItemStack.fromItemStack( extracted );
}
return null;
}
@@ -313,7 +284,7 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
if( oldAeIS != null )
{
oldAeIS.setStackSize( -oldAeIS.getStackSize() );
changes.add( oldAeIS );
changes.add( oldAeIS.copy() );
}
// Notify the network about the new stack. Note that this is null if newIS was null
@@ -258,19 +258,19 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC
@Override
public void postChange( final IBaseMonitor<IAEItemStack> monitor, final Iterable<IAEItemStack> change, final IActionSource source )
{
try
if( source == this.mySrc || source.machine().map( machine -> machine == this ).orElse( false ) )
{
if( this.getProxy().isActive() )
try
{
this.getProxy()
.getStorage()
.postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ), change,
this.mySrc );
if( this.getProxy().isActive() )
{
this.getProxy().getStorage().postAlterationOfStoredItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ), change, this.mySrc );
}
}
catch( final GridAccessException e )
{
// :(
}
}
catch( final GridAccessException e )
{
// :(
}
}
@@ -20,10 +20,13 @@ package appeng.util;
import appeng.util.inv.*;
import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
@@ -37,12 +40,23 @@ import appeng.api.config.FuzzyMode;
*/
public abstract class InventoryAdaptor implements Iterable<ItemSlot>
{
@CapabilityInject( IItemRepository.class)
public static Capability<IItemRepository> ITEM_REPOSITORY_CAPABILITY = null;
public static InventoryAdaptor getAdaptor( final TileEntity te, final EnumFacing d )
{
if( te != null )
{
if( te.hasCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, d ) )
if( ITEM_REPOSITORY_CAPABILITY != null && te.hasCapability( ITEM_REPOSITORY_CAPABILITY, d ) )
{
IItemRepository itemRepository = te.getCapability( ITEM_REPOSITORY_CAPABILITY, d );
if (itemRepository != null){
return new AdaptorItemRepository( itemRepository );
}
}
else if( 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 )
@@ -0,0 +1,209 @@
package appeng.util.inv;
import appeng.api.config.FuzzyMode;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository;
import net.minecraft.item.ItemStack;
import java.util.Iterator;
public class AdaptorItemRepository extends InventoryAdaptor
{
protected final IItemRepository itemRepository;
public AdaptorItemRepository( IItemRepository itemRepository )
{
this.itemRepository = itemRepository;
}
@Override
public ItemStack removeItems( int amount, ItemStack filter, IInventoryDestination destination )
{
ItemStack rv = ItemStack.EMPTY;
ItemStack extracted = ItemStack.EMPTY;
if( !filter.isEmpty() )
{
extracted = this.itemRepository.extractItem( filter, amount, true );
}
else
{
for( IItemRepository.ItemRecord record : this.itemRepository.getAllItems() )
{
extracted = this.itemRepository.extractItem( record.itemPrototype, amount, true );
if( !extracted.isEmpty() )
{
break;
}
}
}
if( destination != null )
{
if( extracted.isEmpty() || !destination.canInsert( extracted ) )
{
return rv;
}
}
extracted = this.itemRepository.extractItem( filter, amount, false );
return extracted;
}
@Override
public ItemStack simulateRemove( int amount, ItemStack filter, IInventoryDestination destination )
{
ItemStack rv = ItemStack.EMPTY;
ItemStack extracted = ItemStack.EMPTY;
if( !filter.isEmpty() )
{
extracted = this.itemRepository.extractItem( filter, amount, true );
}
else
{
for( IItemRepository.ItemRecord record : this.itemRepository.getAllItems() )
{
extracted = this.itemRepository.extractItem( record.itemPrototype, amount, true );
if( !extracted.isEmpty() )
{
break;
}
}
}
if( destination != null )
{
if( extracted.isEmpty() || !destination.canInsert( extracted ) )
{
return rv;
}
}
return extracted;
}
@Override
public ItemStack removeSimilarItems( int amount, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination )
{
ItemStack rv = ItemStack.EMPTY;
ItemStack extracted = ItemStack.EMPTY;
for( IItemRepository.ItemRecord record : this.itemRepository.getAllItems() )
{
if( Platform.itemComparisons().isFuzzyEqualItem( record.itemPrototype, filter, fuzzyMode ) )
{
extracted = this.itemRepository.extractItem( record.itemPrototype, amount, true );
}
if( !extracted.isEmpty() )
{
break;
}
}
if( destination != null )
{
if( extracted.isEmpty() || !destination.canInsert( extracted ) )
{
return rv;
}
}
extracted = this.itemRepository.extractItem( extracted, amount, false );
return extracted;
}
@Override
public ItemStack simulateSimilarRemove( int amount, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination )
{
ItemStack rv = ItemStack.EMPTY;
ItemStack extracted = ItemStack.EMPTY;
for( IItemRepository.ItemRecord record : this.itemRepository.getAllItems() )
{
if( Platform.itemComparisons().isFuzzyEqualItem( record.itemPrototype, filter, fuzzyMode ) )
{
extracted = this.itemRepository.extractItem( record.itemPrototype, amount, true );
}
if( !extracted.isEmpty() )
{
break;
}
}
if( destination != null )
{
if( extracted.isEmpty() || !destination.canInsert( extracted ) )
{
return rv;
}
}
return extracted;
}
@Override
public ItemStack addItems( ItemStack toBeAdded )
{
return this.addItems( toBeAdded, false );
}
protected ItemStack addItems( final ItemStack itemsToAdd, final boolean simulate )
{
if( itemsToAdd.isEmpty() )
{
return ItemStack.EMPTY;
}
ItemStack left = itemsToAdd.copy();
left = this.itemRepository.insertItem( left, simulate );
if( left.isEmpty() )
{
return ItemStack.EMPTY;
}
return left;
}
@Override
public ItemStack simulateAdd( ItemStack toBeSimulated )
{
return this.addItems( toBeSimulated, true );
}
@Override
public boolean containsItems()
{
return !this.itemRepository.getAllItems().isEmpty();
}
@Override
public boolean hasSlots()
{
return true;
}
@Override
public Iterator<ItemSlot> iterator()
{
return null;
}
}
@@ -55,8 +55,6 @@ public class ItemHandlerIterator implements Iterator<ItemSlot>
this.itemSlot.setExtractable( !this.itemHandler.extractItem( this.slot, 1, true ).isEmpty() );
this.itemSlot.setItemStack( this.itemHandler.getStackInSlot( this.slot ) );
this.itemSlot.setSlot( this.slot );
this.itemSlot.setSlotLimit( this.itemHandler.getSlotLimit( this.slot ) );
this.itemSlot.setItemHandler( this.itemHandler );
this.slot++;
return this.itemSlot;
}
@@ -23,7 +23,6 @@ import net.minecraft.item.ItemStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AEItemStack;
import net.minecraftforge.items.IItemHandler;
public class ItemSlot
@@ -35,25 +34,6 @@ public class ItemSlot
private IAEItemStack aeItemStack;
private ItemStack itemStack;
public void setItemHandler( IItemHandler itemHandler )
{
this.itemHandler = itemHandler;
}
private IItemHandler itemHandler;
public int getSlotLimit()
{
return slotLimit;
}
public void setSlotLimit( int slotLimit )
{
this.slotLimit = slotLimit;
}
private int slotLimit;
public ItemStack getItemStack()
{
return this.itemStack
@@ -97,19 +77,4 @@ public class ItemSlot
{
this.slot = slot;
}
public ItemSlot copy()
{
ItemSlot copy = new ItemSlot();
copy.setSlot( this.slot );
copy.setSlotLimit( this.slotLimit );
copy.setItemStack( this.getItemStack() );
copy.setExtractable( this.isExtractable );
copy.setItemHandler( this.itemHandler );
return copy;
}
public ItemStack simulateInsertItem( ItemStack sourceItemStack){
return this.itemHandler.insertItem( this.slot, sourceItemStack , true );
}
}