revert inventory merging code to keep compatibility with ae2fc

This commit is contained in:
Salomão
2021-05-14 12:43:34 -03:00
parent 9d6026635d
commit 31a3afc18b
5 changed files with 236 additions and 103 deletions
@@ -1093,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;
@@ -1112,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++ )
@@ -1160,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;
@@ -1188,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
@@ -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 );
}
}