Skip to content

Commit

Permalink
revert inventory merging code to keep compatibility with ae2fc
Browse files Browse the repository at this point in the history
  • Loading branch information
PrototypeTrousers committed May 14, 2021
1 parent 9d60266 commit 31a3afc
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 103 deletions.
77 changes: 12 additions & 65 deletions src/main/java/appeng/helpers/DualityInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++ )
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ItemStack> patternedStacks = Arrays.stream( patternDetails.getCondensedInputs() ).map( IAEItemStack::createItemStack ).collect( Collectors.toList() );
List<ItemSlot> 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<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
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/appeng/util/InventoryAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,12 +40,23 @@
*/
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 )
Expand Down
209 changes: 209 additions & 0 deletions src/main/java/appeng/util/inv/AdaptorItemRepository.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

2 changes: 0 additions & 2 deletions src/main/java/appeng/util/inv/ItemHandlerIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 31a3afc

Please sign in to comment.