Merge branch 'IItemRepository' into AE2-Omnifactory
This commit is contained in:
@@ -19,19 +19,18 @@
|
||||
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;
|
||||
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.util.inv.AdaptorItemHandler;
|
||||
import appeng.util.inv.AdaptorItemHandlerPlayerInv;
|
||||
import appeng.util.inv.IInventoryDestination;
|
||||
import appeng.util.inv.ItemSlot;
|
||||
|
||||
|
||||
/**
|
||||
* Universal Facade for other inventories. Used to conveniently interact with various types of inventories. This is not
|
||||
@@ -41,15 +40,29 @@ import appeng.util.inv.ItemSlot;
|
||||
*/
|
||||
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 && te.hasCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, d ) )
|
||||
if( te != null )
|
||||
{
|
||||
// Attempt getting an IItemHandler for the given side via caps
|
||||
IItemHandler itemHandler = te.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, d );
|
||||
if( itemHandler != null )
|
||||
if( te.hasCapability( ITEM_REPOSITORY_CAPABILITY, d ) )
|
||||
{
|
||||
return new AdaptorItemHandler( itemHandler );
|
||||
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 )
|
||||
{
|
||||
return new AdaptorItemHandler( itemHandler );
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
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 = this.itemRepository.extractItem( filter, amount, true );
|
||||
|
||||
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 = this.itemRepository.extractItem( filter, amount, true );
|
||||
|
||||
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 extracted = ItemStack.EMPTY;
|
||||
|
||||
ItemStack simulated = this.itemRepository.extractItem( filter, amount, true );
|
||||
|
||||
if (simulated.isEmpty() && !filter.isEmpty()) {
|
||||
simulated = this.itemRepository.extractItem( filter, amount, true, s -> Platform.itemComparisons().isFuzzyEqualItem( s, filter, fuzzyMode ) );
|
||||
}
|
||||
|
||||
if( destination != null )
|
||||
{
|
||||
if( simulated.isEmpty() || !destination.canInsert( simulated ) )
|
||||
{
|
||||
return extracted;
|
||||
}
|
||||
}
|
||||
|
||||
extracted = this.itemRepository.extractItem( simulated, amount, false );
|
||||
return extracted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack simulateSimilarRemove( int amount, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination )
|
||||
{
|
||||
ItemStack extracted = ItemStack.EMPTY;
|
||||
|
||||
ItemStack simulated = this.itemRepository.extractItem( filter, amount, true );
|
||||
|
||||
if (simulated.isEmpty() && !filter.isEmpty()) {
|
||||
simulated = this.itemRepository.extractItem( filter, amount, true, s -> Platform.itemComparisons().isFuzzyEqualItem( s, filter, fuzzyMode ) );
|
||||
}
|
||||
|
||||
if( destination != null )
|
||||
{
|
||||
if( simulated.isEmpty() || !destination.canInsert( simulated ) )
|
||||
{
|
||||
return extracted;
|
||||
}
|
||||
}
|
||||
|
||||
return simulated;
|
||||
}
|
||||
|
||||
@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 false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<ItemSlot> iterator()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user