From fc154d85685d88ca7d499ed98b9017b422a2034a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Mon, 3 May 2021 21:00:35 -0300 Subject: [PATCH 1/6] remove slotless capability implementation from inventory adaptor affects interface,import and export busses --- .../appeng/parts/misc/ItemHandlerAdapter.java | 23 +- .../java/appeng/util/InventoryAdaptor.java | 16 +- .../util/inv/AdaptorItemRepository.java | 208 ------------------ 3 files changed, 6 insertions(+), 241 deletions(-) delete mode 100644 src/main/java/appeng/util/inv/AdaptorItemRepository.java diff --git a/src/main/java/appeng/parts/misc/ItemHandlerAdapter.java b/src/main/java/appeng/parts/misc/ItemHandlerAdapter.java index 201e1582e..52236c957 100644 --- a/src/main/java/appeng/parts/misc/ItemHandlerAdapter.java +++ b/src/main/java/appeng/parts/misc/ItemHandlerAdapter.java @@ -142,14 +142,6 @@ class ItemHandlerAdapter implements IMEInventory, IBaseMonitor remainingCurrentSlot ) { // Something broke. It should never return more than we requested... @@ -159,17 +151,12 @@ class ItemHandlerAdapter implements IMEInventory, IBaseMonitor extracted.getMaxStackSize()) { - extracted.setCount(remainingCurrentSlot); - } - + // 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. if( gathered.isEmpty() ) { - gathered = extracted; + gathered = extracted.copy(); } else { @@ -178,7 +165,7 @@ class ItemHandlerAdapter implements IMEInventory, IBaseMonitor 0 ); + while( !extracted.isEmpty() && remainingCurrentSlot > 0 ); remainingSize -= stackSizeCurrentSlot - remainingCurrentSlot; diff --git a/src/main/java/appeng/util/InventoryAdaptor.java b/src/main/java/appeng/util/InventoryAdaptor.java index 21295f575..bdbe42951 100644 --- a/src/main/java/appeng/util/InventoryAdaptor.java +++ b/src/main/java/appeng/util/InventoryAdaptor.java @@ -20,13 +20,10 @@ 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; @@ -40,23 +37,12 @@ import appeng.api.config.FuzzyMode; */ public abstract class InventoryAdaptor implements Iterable { - @CapabilityInject( IItemRepository.class) - public static Capability ITEM_REPOSITORY_CAPABILITY = null; - public static InventoryAdaptor getAdaptor( final TileEntity te, final EnumFacing d ) { if( te != null ) { - if( ITEM_REPOSITORY_CAPABILITY != null && te.hasCapability( ITEM_REPOSITORY_CAPABILITY, d ) ) + if( te.hasCapability( CapabilityItemHandler.ITEM_HANDLER_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 ) diff --git a/src/main/java/appeng/util/inv/AdaptorItemRepository.java b/src/main/java/appeng/util/inv/AdaptorItemRepository.java deleted file mode 100644 index d54e3771d..000000000 --- a/src/main/java/appeng/util/inv/AdaptorItemRepository.java +++ /dev/null @@ -1,208 +0,0 @@ -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 iterator() - { - return null; - } -} From 62f842d4efa99edb737725fa5851309b877af290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Mon, 3 May 2021 21:03:23 -0300 Subject: [PATCH 2/6] visit crafting mediums in round-robin --- .../implementations/CraftingCPUCluster.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java index bf42c35de..d4be8c266 100644 --- a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java +++ b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java @@ -19,13 +19,9 @@ package appeng.me.cluster.implementations; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; +import java.util.stream.Collectors; import com.google.common.collect.ImmutableList; @@ -34,8 +30,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.world.World; -import net.minecraft.world.WorldServer; -import net.minecraftforge.fml.common.FMLCommonHandler; import appeng.api.AEApi; import appeng.api.config.Actionable; @@ -93,6 +87,7 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU private final List storage = new ArrayList<>(); private final List status = new ArrayList<>(); private final HashMap, Object> listeners = new HashMap<>(); + private final Map> visitedMediums = new HashMap<>(); private ICraftingLink myLastLink; private String myName = ""; private boolean isDestroyed = false; @@ -646,14 +641,22 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU { InventoryCrafting ic = null; - for( final ICraftingMedium m : cc.getMediums( e.getKey() ) ) + if (!visitedMediums.containsKey( details ) || visitedMediums.get( details ).isEmpty()) { + visitedMediums.put( details, new ArrayDeque<>( cc.getMediums( details ).stream().filter( Objects::nonNull ).collect( Collectors.toList()) ) ); + } + + while (!visitedMediums.get( details ).isEmpty()) + { + + ICraftingMedium m = visitedMediums.get( details ).poll(); + if( e.getValue().value <= 0 ) { continue; } - if( !m.isBusy() ) + if( m != null && !m.isBusy() ) { if( ic == null ) { From b541025738d2348c30e712845c776e0d9bd7f33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Mon, 3 May 2021 21:02:37 -0300 Subject: [PATCH 3/6] implement patterned input-based blocking mode remove GTCE-specific handler and adaptor --- .../java/appeng/helpers/DualityInterface.java | 59 +++++++------------ .../gregtech/GTCEInventoryAdaptor.java | 26 -------- .../modules/gregtech/GTCEItemHandler.java | 57 ------------------ 3 files changed, 20 insertions(+), 122 deletions(-) delete mode 100644 src/main/java/appeng/integration/modules/gregtech/GTCEInventoryAdaptor.java delete mode 100644 src/main/java/appeng/integration/modules/gregtech/GTCEItemHandler.java diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index 9f7174fe2..a4a49acf3 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -148,7 +148,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn private IMEInventory destination; private int isWorking = -1; private final Accessor accessor = new Accessor(); - private GTCEInventoryAdaptor GTad; + private EnumSet visitedFaces = EnumSet.noneOf( EnumFacing.class ); public DualityInterface( final AENetworkProxy networkProxy, final IInterfaceHost ih ) { @@ -913,18 +913,22 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn }; } - private static boolean invIsBlocked( InventoryAdaptor inv ) + private boolean invIsBlocked( InventoryAdaptor inv ) { - if( AEConfig.instance().isFeatureEnabled( AEFeature.INSANE_BLOCKING_MODE ) ) + final Iterator i = this.craftingList.iterator(); + ICraftingPatternDetails patt = i.next(); + for ( ItemSlot itemSlot : inv) { - return !inv.simulateRemove( 1, ItemStack.EMPTY, null ).isEmpty(); + if (itemSlot.getItemStack().isEmpty()){ + continue; + } + for ( IAEItemStack iaeItemStack : patt.getCondensedInputs()){ + if (iaeItemStack.isSameType( itemSlot.getItemStack() )){ + return true; + } + } } - else return inv.containsItems(); - } - - private static boolean invIsBlockedGTCE( GTCEInventoryAdaptor inv ) - { - return ( !inv.canRemoveAllExceptCircuits() ); + return false; } @Override @@ -975,22 +979,12 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn { if( this.isBlocking() ) { - if( te.getBlockType().getRegistryName().getResourceDomain().equals( "gregtech" ) ) + if( invIsBlocked( ad ) ) { - GTad = GTCEInventoryAdaptor.getAdaptor( te, s.getOpposite() ); - if( invIsBlockedGTCE( GTad ) ) - { - continue; - } - } - else - { - if( invIsBlocked( ad ) ) - { - continue; - } + continue; } } + } if( this.acceptsItems( ad, table ) ) { @@ -1037,23 +1031,10 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn final InventoryAdaptor ad = InventoryAdaptor.getAdaptor( te, s.getOpposite() ); if( ad != null ) { - if( te.getBlockType().getRegistryName().getResourceDomain().equals( "gregtech" ) ) + if( !invIsBlocked( ad ) ) { - GTad = GTCEInventoryAdaptor.getAdaptor( te, s.getOpposite() ); - if( !invIsBlockedGTCE( GTad ) ) - { - allAreBusy = false; - break; - } - } - else - { - if( !invIsBlocked( ad ) ) - { - allAreBusy = false; - break; - } - + allAreBusy = false; + break; } } } diff --git a/src/main/java/appeng/integration/modules/gregtech/GTCEInventoryAdaptor.java b/src/main/java/appeng/integration/modules/gregtech/GTCEInventoryAdaptor.java deleted file mode 100644 index 9be68e2fb..000000000 --- a/src/main/java/appeng/integration/modules/gregtech/GTCEInventoryAdaptor.java +++ /dev/null @@ -1,26 +0,0 @@ -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 -{ - 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(); -} diff --git a/src/main/java/appeng/integration/modules/gregtech/GTCEItemHandler.java b/src/main/java/appeng/integration/modules/gregtech/GTCEItemHandler.java deleted file mode 100644 index b31028bfc..000000000 --- a/src/main/java/appeng/integration/modules/gregtech/GTCEItemHandler.java +++ /dev/null @@ -1,57 +0,0 @@ -package appeng.integration.modules.gregtech; - -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 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 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 iterator() - { - return null; - } -} From 0c0aafba2f298d56e6eb64569fe5333fe8dd19c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Mon, 3 May 2021 21:07:51 -0300 Subject: [PATCH 4/6] make ItemSlots copy-able --- .../appeng/util/inv/ItemHandlerIterator.java | 2 ++ src/main/java/appeng/util/inv/ItemSlot.java | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/appeng/util/inv/ItemHandlerIterator.java b/src/main/java/appeng/util/inv/ItemHandlerIterator.java index 6994cbbb4..7b4f6acf1 100644 --- a/src/main/java/appeng/util/inv/ItemHandlerIterator.java +++ b/src/main/java/appeng/util/inv/ItemHandlerIterator.java @@ -55,6 +55,8 @@ class ItemHandlerIterator implements Iterator 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; } diff --git a/src/main/java/appeng/util/inv/ItemSlot.java b/src/main/java/appeng/util/inv/ItemSlot.java index 795a6336e..560cfe7d3 100644 --- a/src/main/java/appeng/util/inv/ItemSlot.java +++ b/src/main/java/appeng/util/inv/ItemSlot.java @@ -23,6 +23,7 @@ import net.minecraft.item.ItemStack; import appeng.api.storage.data.IAEItemStack; import appeng.util.item.AEItemStack; +import net.minecraftforge.items.IItemHandler; public class ItemSlot @@ -34,6 +35,25 @@ 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 @@ -77,4 +97,19 @@ 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 insertItem( ItemStack sourceItemStack){ + return this.itemHandler.insertItem( this.slot, sourceItemStack , true ); + } } From 0240a5d76a5b74096a24160e1232300bc5452f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Mon, 3 May 2021 21:09:03 -0300 Subject: [PATCH 5/6] remove crafting failure penalty --- .../java/appeng/helpers/DualityInterface.java | 13 +--------- .../parts/automation/PartExportBus.java | 25 +++---------------- 2 files changed, 5 insertions(+), 33 deletions(-) diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index a4a49acf3..368e0ca0c 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -117,8 +117,6 @@ 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; @@ -770,17 +768,8 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn { if( this.getInstalledUpgrades( Upgrades.CRAFTING ) > 0 && itemStack != null ) { - if (this.failedCraftTriesSlot[x] <= 0) - { - boolean crafted = this.craftingTracker.handleCrafting( x, itemStack.getStackSize(), itemStack, d, this.iHost.getTileEntity().getWorld(), + return 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 ) diff --git a/src/main/java/appeng/parts/automation/PartExportBus.java b/src/main/java/appeng/parts/automation/PartExportBus.java index 85bd90b73..755051441 100644 --- a/src/main/java/appeng/parts/automation/PartExportBus.java +++ b/src/main/java/appeng/parts/automation/PartExportBus.java @@ -70,8 +70,6 @@ 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 @@ -142,7 +140,7 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest if( destination != null ) { - int x = 0; + int x; for( x = 0; x < this.availableSlots() && this.itemToSend > 0; x++ ) { @@ -153,16 +151,8 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest if( ais == null || this.itemToSend <= 0 ) continue; if ( this.craftOnly() ) { - 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; + this.didSomething = this.craftingTracker.handleCrafting(slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(), + this.getProxy().getGrid(), cg, this.mySrc) || this.didSomething; } final long before = this.itemToSend; @@ -185,15 +175,8 @@ public class PartExportBus extends PartSharedItemBus implements ICraftingRequest if( this.itemToSend == before && this.isCraftingEnabled() ) { - if (this.failedCraftTriesSlot[x] <= 0) { - - this.didSomething = this.craftingTracker.handleCrafting(slotToExport, this.itemToSend, ais, destination, this.getTile().getWorld(), + 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; } } From af91c04ea1bbcb0f9696440776a6ae9102647b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Mon, 3 May 2021 21:10:39 -0300 Subject: [PATCH 6/6] re - implement blocking mode --- .../java/appeng/core/features/AEFeature.java | 1 - .../java/appeng/helpers/DualityInterface.java | 291 ++++++++++++++---- 2 files changed, 238 insertions(+), 54 deletions(-) diff --git a/src/main/java/appeng/core/features/AEFeature.java b/src/main/java/appeng/core/features/AEFeature.java index edfe86917..f7e1e707c 100644 --- a/src/main/java/appeng/core/features/AEFeature.java +++ b/src/main/java/appeng/core/features/AEFeature.java @@ -164,7 +164,6 @@ public enum AEFeature PATTERNS( "Patterns", Constants.CATEGORY_CRAFTING_FEATURES ), CRAFTING_CPU( "CraftingCPU", Constants.CATEGORY_CRAFTING_FEATURES ), CRAFTING_MANAGER_FALLBACK( "CraftingManagerFallback", Constants.CATEGORY_CRAFTING_FEATURES, "Use CraftingManager to find an alternative recipe, after a pattern rejected an ingredient. Should be enabled to avoid issues, but can have a minor performance impact." ), - INSANE_BLOCKING_MODE( "InsaneBlockingMode", Constants.CATEGORY_CRAFTING_FEATURES, "Use the default AE2 blocking mode that doesn't work on any machines" ), BASIC_CARDS( "BasicCards", Constants.CATEGORY_UPGRADES ), ADVANCED_CARDS( "AdvancedCards", Constants.CATEGORY_UPGRADES ), diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index 368e0ca0c..39486b5df 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -19,18 +19,13 @@ package appeng.helpers; -import java.util.ArrayList; -import java.util.Collection; -import java.util.EnumSet; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Optional; +import java.util.*; +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; import gregtech.api.block.machines.BlockMachine; @@ -93,8 +88,6 @@ import appeng.api.util.AEPartLocation; import appeng.api.util.DimensionalCoord; import appeng.api.util.IConfigManager; import appeng.capabilities.Capabilities; -import appeng.core.AEConfig; -import appeng.core.features.AEFeature; import appeng.core.settings.TickRates; import appeng.me.GridAccessException; import appeng.me.helpers.AENetworkProxy; @@ -106,10 +99,6 @@ import appeng.parts.automation.StackUpgradeInventory; import appeng.parts.automation.UpgradeInventory; import appeng.tile.inventory.AppEngInternalAEInventory; import appeng.tile.inventory.AppEngInternalInventory; -import appeng.util.inv.AdaptorItemHandler; -import appeng.util.inv.IAEAppEngInventory; -import appeng.util.inv.IInventoryDestination; -import appeng.util.inv.InvOperation; import appeng.util.item.AEItemStack; import static gregtech.api.block.machines.BlockMachine.getMetaTileEntity; @@ -147,6 +136,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn private int isWorking = -1; private final Accessor accessor = new Accessor(); private EnumSet visitedFaces = EnumSet.noneOf( EnumFacing.class ); + private EnumMap> waitingToSendFacing = new EnumMap<>(EnumFacing.class); public DualityInterface( final AENetworkProxy networkProxy, final IInterfaceHost ih ) { @@ -240,6 +230,27 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn } } data.setTag( "waitingToSend", waitingToSend ); + + NBTTagCompound sidedWaitList = new NBTTagCompound(); + + if (this.waitingToSendFacing != null) + { + for( EnumFacing s : this.iHost.getTargets() ) + { + NBTTagList waitingListSided = new NBTTagList(); + if (this.waitingToSendFacing.containsKey( s )) + { + for( final ItemStack is : this.waitingToSendFacing.get( s ) ) + { + final NBTTagCompound item = new NBTTagCompound(); + is.writeToNBT( item ); + waitingListSided.appendTag( item ); + } + sidedWaitList.setTag( s.name(), waitingListSided ); + } + } + } + data.setTag( "sidedWaitList", sidedWaitList ); } public void readFromNBT( final NBTTagCompound data ) @@ -259,6 +270,23 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn } } + this.waitingToSendFacing = null; + final NBTTagCompound waitingListSided = data.getCompoundTag("sidedWaitList"); + + for (EnumFacing s : EnumFacing.values()) + if (waitingListSided.hasKey( s.name() )) { + NBTTagList w = waitingListSided.getTagList( s.name(), 10 ); + for( int x = 0; x < w.tagCount(); x++ ) + { + final NBTTagCompound c = w.getCompoundTagAt( x ); + if( c != null ) + { + final ItemStack is = new ItemStack( c ); + this.addToSendListFacing( is , EnumFacing.getFront( s.getIndex() ) ); + } + } + } + this.craftingTracker.readFromNBT( data ); this.upgrades.readFromNBT( data, "upgrades" ); this.config.readFromNBT( data, "config" ); @@ -294,6 +322,30 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn } } + private void addToSendListFacing( final ItemStack is, EnumFacing f ) + { + if( is.isEmpty() ) + { + return; + } + if (this.waitingToSendFacing == null){ + this.waitingToSendFacing = new EnumMap<>(EnumFacing.class); + } + + this.waitingToSendFacing.computeIfAbsent( f, k -> new ArrayList<>() ); + + this.waitingToSendFacing.get( f ).add( is ); + + try + { + this.gridProxy.getTick().wakeDevice( this.gridProxy.getNode() ); + } + catch( final GridAccessException e ) + { + // :P + } + } + private void readConfig() { this.hasConfig = false; @@ -391,24 +443,22 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn } } - private boolean hasWorkToDo() - { - if( this.hasItemsToSend() ) - { + private boolean hasWorkToDo() { + + if (hasItemsToSend()){ return true; } - else - { - for( final IAEItemStack requiredWork : this.requireWork ) - { - if( requiredWork != null ) - { - return true; - } - } - return false; + if(hasItemsToSendFacing()){ + return true; } + + for (final IAEItemStack requiredWork : this.requireWork) { + if (requiredWork != null) { + return true; + } + } + return false; } private void updatePlan( final int slot ) @@ -513,6 +563,21 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn return this.waitingToSend != null && !this.waitingToSend.isEmpty(); } + private boolean hasItemsToSendFacing() + { + if (waitingToSendFacing != null) + { + for( EnumFacing enumFacing : waitingToSendFacing.keySet() ) + { + if( !waitingToSendFacing.get( enumFacing ).isEmpty() ) + { + return true; + } + } + } + return false; + } + @Override public boolean canInsert( final ItemStack stack ) { @@ -584,11 +649,21 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn return TickRateModulation.SLEEP; } + //Previous version might have items saved in this list + //recover them if( this.hasItemsToSend() ) { this.pushItemsOut( this.iHost.getTargets() ); } + if (hasItemsToSendFacing()) + { + for( EnumFacing enumFacing : waitingToSendFacing.keySet() ) + { + this.pushItemsOut( enumFacing ); + } + } + final boolean couldDoWork = this.updateStorage(); return this.hasWorkToDo() ? ( couldDoWork ? TickRateModulation.URGENT : TickRateModulation.SLOWER ) : TickRateModulation.SLEEP; } @@ -649,6 +724,47 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn } } + private void pushItemsOut( final EnumFacing s ) + { + if( this.waitingToSendFacing.get(s) == null || this.waitingToSendFacing.get( s ).isEmpty() ) + { + return; + } + + final TileEntity tile = this.iHost.getTileEntity(); + final World w = tile.getWorld(); + + final TileEntity te = w.getTileEntity( tile.getPos().offset( s ) ); + if( te == null ) + { + return; + } + + final Iterator i = this.waitingToSendFacing.get( s ).iterator(); + while ( i.hasNext() ) + { + ItemStack whatToSend = i.next(); + final InventoryAdaptor ad = InventoryAdaptor.getAdaptor( te, s.getOpposite() ); + if( ad != null ) + { + final ItemStack result = ad.addItems( whatToSend ); + if( !result.isEmpty() ) + { + whatToSend.setCount( whatToSend.getCount() - ( whatToSend.getCount() - result.getCount() ) ); + } + else + { + i.remove(); + } + } + } + + if( this.waitingToSendFacing.get( s ).isEmpty() ) + { + this.waitingToSendFacing.get( s ).clear(); + } + } + private boolean updateStorage() { boolean didSomething = false; @@ -923,7 +1039,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn @Override public boolean pushPattern( final ICraftingPatternDetails patternDetails, final InventoryCrafting table ) { - if( this.hasItemsToSend() || !this.gridProxy.isActive() || !this.craftingList.contains( patternDetails ) ) + if( this.hasItemsToSend() || this.hasItemsToSendFacing() || !this.gridProxy.isActive() || !this.craftingList.contains( patternDetails ) ) { return false; } @@ -931,8 +1047,12 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn final TileEntity tile = this.iHost.getTileEntity(); final World w = tile.getWorld(); - final EnumSet possibleDirections = this.iHost.getTargets(); - for( final EnumFacing s : possibleDirections ) + if( this.visitedFaces.isEmpty() ) + { + this.visitedFaces = this.iHost.getTargets(); + } + + for( final EnumFacing s : visitedFaces ) { final TileEntity te = w.getTileEntity( tile.getPos().offset( s ) ); if( te instanceof IInterfaceHost ) @@ -941,6 +1061,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn { if( ( (IInterfaceHost) te ).getInterfaceDuality().sameGrid( this.gridProxy.getGrid() ) ) { + visitedFaces.remove( s ); continue; } } @@ -957,8 +1078,10 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn { if( cm.pushPattern( patternDetails, table, s.getOpposite() ) ) { + visitedFaces.remove( s ); return true; } + visitedFaces.remove( s ); continue; } } @@ -966,6 +1089,7 @@ 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( invIsBlocked( ad ) ) @@ -973,38 +1097,37 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn continue; } } - } - if( this.acceptsItems( ad, table ) ) + if( this.acceptsItems( ad, patternDetails, isDrawer ) ) { for( int x = 0; x < table.getSizeInventory(); x++ ) { final ItemStack is = table.getStackInSlot( x ); if( !is.isEmpty() ) { - final ItemStack added = ad.addItems( is ); - this.addToSendList( added ); + addToSendListFacing( is, s ); } + pushItemsOut( s ); } - this.pushItemsOut( possibleDirections ); + visitedFaces.remove( s ); return true; } } + visitedFaces.remove( s ); } - return false; } @Override public boolean isBusy() { - if( this.hasItemsToSend() ) + boolean busy = false; + + if( this.hasItemsToSend() || hasItemsToSendFacing() ) { return true; } - boolean busy = false; - if( this.isBlocking() ) { final EnumSet possibleDirections = this.iHost.getTargets(); @@ -1042,23 +1165,85 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn return this.cm.getSetting( Settings.BLOCK ) == YesNo.YES; } - private boolean acceptsItems( final InventoryAdaptor ad, final InventoryCrafting table ) + private boolean acceptsItems( final InventoryAdaptor ad, final ICraftingPatternDetails patternDetails, boolean isDrawer ) { - for( int x = 0; x < table.getSizeInventory(); x++ ) + List patternedStacks = Arrays.stream( patternDetails.getCondensedInputs() ).map( IAEItemStack::createItemStack ).collect( Collectors.toList() ); + List copiedItemSlots = new ArrayList<>(); + + if( patternedStacks.size() == 1 ) { - final ItemStack is = table.getStackInSlot( x ); - if( is.isEmpty() ) - { - continue; - } - - if( !ad.simulateAdd( is.copy() ).isEmpty() ) - { - return false; - } + return ad.simulateAdd( patternedStacks.get( 0 ) ).isEmpty(); } + else + { + Iterator adit = ad.iterator(); + while ( adit.hasNext() ) + { + 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(); - return true; + //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() ) + { + boolean validInputSlotForIngredients = false; + for( IAEItemStack aeItemStack : patternDetails.getCondensedInputs() ) + { + if( is.insertItem( aeItemStack.getDefinition() ).isEmpty() ) + { + validInputSlotForIngredients = true; + break; + } + } + if( validInputSlotForIngredients ) + { + copiedItemSlots.add( is.copy() ); + } + } + else if( !is.getItemStack().isEmpty() && is.getItemStack().getCount() < is.getSlotLimit() ) + { + copiedItemSlots.add( is.copy() ); + } + } + Iterator copiedItemSlotIterator = copiedItemSlots.iterator(); + while ( copiedItemSlotIterator.hasNext() ) + { + ItemSlot copiedItemSlot = copiedItemSlotIterator.next(); + + Iterator psi = patternedStacks.iterator(); + while ( psi.hasNext() ) + { + ItemStack patternedStack = psi.next(); + ItemStack remainder = copiedItemSlot.insertItem( patternedStack ); + + if( !remainder.isEmpty() ) + { + patternedStack.setCount( patternedStack.getCount() - ( patternedStack.getCount() - remainder.getCount() ) ); + if( copiedItemSlot.getSlotLimit() == copiedItemSlot.getItemStack().getCount() ) + { + copiedItemSlotIterator.remove(); + break; + } + } + else + { + if( copiedItemSlot.getSlotLimit() == copiedItemSlot.getItemStack().getCount() ) + { + copiedItemSlotIterator.remove(); + } + psi.remove(); + break; + } + } + + if( patternedStacks.size() == 0 ) + { + return true; + } + } + return false; + } } @Override