From 31a3afc18b076da7eb57d1ecc7cf8fca99b443aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Fri, 14 May 2021 12:43:34 -0300 Subject: [PATCH] revert inventory merging code to keep compatibility with ae2fc --- .../java/appeng/helpers/DualityInterface.java | 77 +------ .../java/appeng/util/InventoryAdaptor.java | 16 +- .../util/inv/AdaptorItemRepository.java | 209 ++++++++++++++++++ .../appeng/util/inv/ItemHandlerIterator.java | 2 - src/main/java/appeng/util/inv/ItemSlot.java | 35 --- 5 files changed, 236 insertions(+), 103 deletions(-) create mode 100644 src/main/java/appeng/util/inv/AdaptorItemRepository.java diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index dc91f335641..cf48a99b582 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -1093,13 +1093,12 @@ public boolean pushPattern( final ICraftingPatternDetails patternDetails, final 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; @@ -1112,7 +1111,7 @@ else if ( invIsBlocked( ad ) ) } } - if( this.acceptsItems( ad, patternDetails, isDrawer ) ) + if( this.acceptsItems( ad, table ) ) { visitedFaces.remove( s ); for( int x = 0; x < table.getSizeInventory(); x++ ) @@ -1160,7 +1159,7 @@ public boolean isBusy() if( te.getBlockType().getRegistryName().getResourceDomain().equals( "gregtech" ) ) { GTad = GTCEInventoryAdaptor.getAdaptor( te, s.getOpposite() ); - if( !invIsBlockedGTCE( GTad ) ) + if( GTad != null && !invIsBlockedGTCE( GTad ) ) { allAreBusy = false; break; @@ -1188,75 +1187,23 @@ private boolean isBlocking() 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 patternedStacks = Arrays.stream( patternDetails.getCondensedInputs() ).map( IAEItemStack::createItemStack ).collect( Collectors.toList() ); - List copiedItemSlots = new ArrayList<>(); - - if( patternedStacks.size() == 1 ) - { - return ad.simulateAdd( patternedStacks.get( 0 ) ).isEmpty(); - } - else + for( int x = 0; x < table.getSizeInventory(); x++ ) { - 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 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 diff --git a/src/main/java/appeng/util/InventoryAdaptor.java b/src/main/java/appeng/util/InventoryAdaptor.java index bdbe4295179..21295f575d2 100644 --- a/src/main/java/appeng/util/InventoryAdaptor.java +++ b/src/main/java/appeng/util/InventoryAdaptor.java @@ -20,10 +20,13 @@ 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 @@ */ 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( 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 ) diff --git a/src/main/java/appeng/util/inv/AdaptorItemRepository.java b/src/main/java/appeng/util/inv/AdaptorItemRepository.java new file mode 100644 index 00000000000..c9b71358b53 --- /dev/null +++ b/src/main/java/appeng/util/inv/AdaptorItemRepository.java @@ -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 iterator() + { + return null; + } +} + diff --git a/src/main/java/appeng/util/inv/ItemHandlerIterator.java b/src/main/java/appeng/util/inv/ItemHandlerIterator.java index 8447f63ad76..ca36a63aaa5 100644 --- a/src/main/java/appeng/util/inv/ItemHandlerIterator.java +++ b/src/main/java/appeng/util/inv/ItemHandlerIterator.java @@ -55,8 +55,6 @@ public ItemSlot next() 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 5d80149a60c..795a6336e44 100644 --- a/src/main/java/appeng/util/inv/ItemSlot.java +++ b/src/main/java/appeng/util/inv/ItemSlot.java @@ -23,7 +23,6 @@ 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 void setSlot( final int slot ) { 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 ); - } }