From a126112a2d0158c1c53189bf640177c512cb3509 Mon Sep 17 00:00:00 2001 From: fscan Date: Sat, 11 Nov 2017 19:59:11 +0100 Subject: [PATCH] Rework ImportBus (#3206) Fixes #3201 --- .../parts/automation/PartImportBus.java | 84 +++++++++---------- 1 file changed, 38 insertions(+), 46 deletions(-) diff --git a/src/main/java/appeng/parts/automation/PartImportBus.java b/src/main/java/appeng/parts/automation/PartImportBus.java index f68823575..d6663edab 100644 --- a/src/main/java/appeng/parts/automation/PartImportBus.java +++ b/src/main/java/appeng/parts/automation/PartImportBus.java @@ -29,7 +29,6 @@ import net.minecraft.util.math.Vec3d; import appeng.api.AEApi; import appeng.api.config.Actionable; import appeng.api.config.FuzzyMode; -import appeng.api.config.PowerMultiplier; import appeng.api.config.RedstoneMode; import appeng.api.config.Settings; import appeng.api.config.Upgrades; @@ -41,7 +40,6 @@ import appeng.api.networking.ticking.TickRateModulation; import appeng.api.networking.ticking.TickingRequest; import appeng.api.parts.IPartCollisionHelper; import appeng.api.parts.IPartModel; -import appeng.api.storage.IMEInventory; import appeng.api.storage.IMEMonitor; import appeng.api.storage.channels.IItemStorageChannel; import appeng.api.storage.data.IAEItemStack; @@ -72,9 +70,7 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin public static final IPartModel MODELS_HAS_CHANNEL = new PartModel( MODEL_BASE, new ResourceLocation( AppEng.MOD_ID, "part/import_bus_has_channel" ) ); private final IActionSource source; - private IMEInventory destination = null; - private IAEItemStack lastItemChecked = null; - private int itemToSend; // used in tickingRequest + private int itemsToSend; // used in tickingRequest private boolean worked; // used in tickingRequest @Reflected @@ -95,14 +91,24 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin return false; } - final IAEItemStack out = this.destination.injectItems( - this.lastItemChecked = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createStack( stack ), Actionable.SIMULATE, - this.source ); - if( out == null ) + try { - return true; + final IMEMonitor inv = this.getProxy().getStorage().getInventory( + AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); + + final IAEItemStack out = inv.injectItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createStack( stack ), + Actionable.SIMULATE, + this.source ); + if( out == null ) + { + return true; + } + return out.getStackSize() != stack.getCount(); + } + catch( GridAccessException ex ) + { + return false; } - return out.getStackSize() != stack.getCount(); } @Override @@ -165,9 +171,7 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin { try { - this.itemToSend = this.calculateItemsToSend(); - this.itemToSend = Math.min( this.itemToSend, - (int) ( 0.01 + this.getProxy().getEnergy().extractAEPower( this.itemToSend, Actionable.SIMULATE, PowerMultiplier.CONFIG ) ) ); + this.itemsToSend = this.calculateItemsToSend(); final IMEMonitor inv = this.getProxy().getStorage().getInventory( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); @@ -177,10 +181,10 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin for( int x = 0; x < this.availableSlots(); x++ ) { final IAEItemStack ais = this.getConfig().getAEStackInSlot( x ); - if( ais != null && this.itemToSend > 0 ) + if( ais != null && itemsToSend > 0 ) { Configured = true; - while( this.itemToSend > 0 ) + while( itemsToSend > 0 ) { if( this.importStuff( myAdaptor, ais, inv, energy, fzMode ) ) { @@ -192,7 +196,7 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin if( !Configured ) { - while( this.itemToSend > 0 ) + while( itemsToSend > 0 ) { if( this.importStuff( myAdaptor, null, inv, energy, fzMode ) ) { @@ -221,38 +225,32 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 ) { - newItems = myAdaptor.removeSimilarItems( toSend, whatToImport == null ? ItemStack.EMPTY : whatToImport.getDefinition(), fzMode, - this.configDestination( inv ) ); + newItems = myAdaptor.removeSimilarItems( toSend, whatToImport == null ? ItemStack.EMPTY : whatToImport.getDefinition(), fzMode, this ); } else { - newItems = myAdaptor.removeItems( toSend, whatToImport == null ? ItemStack.EMPTY : whatToImport.getDefinition(), this.configDestination( inv ) ); + newItems = myAdaptor.removeItems( toSend, whatToImport == null ? ItemStack.EMPTY : whatToImport.getDefinition(), this ); } if( !newItems.isEmpty() ) { - newItems.setCount( (int) ( Math.min( newItems.getCount(), - energy.extractAEPower( newItems.getCount(), Actionable.SIMULATE, PowerMultiplier.CONFIG ) ) + 0.01 ) ); - this.itemToSend -= newItems.getCount(); - - if( this.lastItemChecked == null || !this.lastItemChecked.isSameType( newItems ) ) - { - this.lastItemChecked = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createStack( newItems ); - } - else - { - this.lastItemChecked.setStackSize( newItems.getCount() ); - } - - final IAEItemStack failed = Platform.poweredInsert( energy, this.destination, this.lastItemChecked, this.source ); + final IAEItemStack aeStack = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createStack( newItems ); + final IAEItemStack failed = Platform.poweredInsert( energy, inv, aeStack, this.source ); if( failed != null ) { - myAdaptor.addItems( failed.createItemStack() ); + // try unpowered insert, better be a bit lenient then void items + final IAEItemStack spill = inv.injectItems( failed, Actionable.MODULATE, this.source ); + if( spill != null ) + { + // last resort try to put it back .. lets hope it's a chest type of thing + myAdaptor.addItems( spill.createItemStack() ); + } return true; } else { + this.itemsToSend -= newItems.getCount(); this.worked = true; } } @@ -266,7 +264,7 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin private int calculateMaximumAmountToImport( final InventoryAdaptor myAdaptor, final IAEItemStack whatToImport, final IMEMonitor inv, final FuzzyMode fzMode ) { - final int toSend = Math.min( this.itemToSend, 64 ); + final int toSend = Math.min( this.itemsToSend, 64 ); final ItemStack itemStackToImport; if( whatToImport == null ) @@ -282,13 +280,13 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin final ItemStack simResult; if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 ) { - simResult = myAdaptor.simulateSimilarRemove( toSend, itemStackToImport, fzMode, this.configDestination( inv ) ); - itemAmountNotStorable = this.destination.injectItems( AEItemStack.fromItemStack( simResult ), Actionable.SIMULATE, this.source ); + simResult = myAdaptor.simulateSimilarRemove( toSend, itemStackToImport, fzMode, this ); + itemAmountNotStorable = inv.injectItems( AEItemStack.fromItemStack( simResult ), Actionable.SIMULATE, this.source ); } else { - simResult = myAdaptor.simulateRemove( toSend, itemStackToImport, this.configDestination( inv ) ); - itemAmountNotStorable = this.destination.injectItems( AEItemStack.fromItemStack( simResult ), Actionable.SIMULATE, this.source ); + simResult = myAdaptor.simulateRemove( toSend, itemStackToImport, this ); + itemAmountNotStorable = inv.injectItems( AEItemStack.fromItemStack( simResult ), Actionable.SIMULATE, this.source ); } if( itemAmountNotStorable != null ) @@ -299,12 +297,6 @@ public class PartImportBus extends PartSharedItemBus implements IInventoryDestin return toSend; } - private IInventoryDestination configDestination( final IMEMonitor itemInventory ) - { - this.destination = itemInventory; - return this; - } - @Override protected boolean isSleeping() {