2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
package appeng.util.inv;
|
|
|
|
|
2014-11-19 20:44:18 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.config.FuzzyMode;
|
|
|
|
import appeng.util.InventoryAdaptor;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
2014-11-19 20:44:18 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public class AdaptorIInventory extends InventoryAdaptor
|
|
|
|
{
|
|
|
|
|
2014-09-29 09:54:34 +02:00
|
|
|
private final IInventory i;
|
|
|
|
private final boolean wrapperEnabled;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-11-19 20:44:18 +01:00
|
|
|
public AdaptorIInventory( IInventory s )
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i = s;
|
|
|
|
this.wrapperEnabled = s instanceof IInventoryWrapper;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public ItemStack removeItems( int amount, ItemStack filter, IInventoryDestination destination )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
int s = this.i.getSizeInventory();
|
2015-04-03 08:54:31 +02:00
|
|
|
ItemStack rv = null;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < s && amount > 0; x++ )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
ItemStack is = this.i.getStackInSlot( x );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is != null && this.canRemoveStackFromSlot( x, is ) && ( filter == null || Platform.isSameItemPrecise( is, filter ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
int boundAmounts = amount;
|
|
|
|
if( boundAmounts > is.stackSize )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
boundAmounts = is.stackSize;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( destination != null && !destination.canInsert( is ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
boundAmounts = 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( boundAmounts > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( rv == null )
|
|
|
|
{
|
|
|
|
rv = is.copy();
|
|
|
|
filter = rv;
|
|
|
|
rv.stackSize = boundAmounts;
|
|
|
|
amount -= boundAmounts;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv.stackSize += boundAmounts;
|
|
|
|
amount -= boundAmounts;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is.stackSize == boundAmounts )
|
2014-02-09 06:59:38 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i.setInventorySlotContents( x, null );
|
|
|
|
this.i.markDirty();
|
2014-02-09 06:59:38 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ItemStack po = is.copy();
|
2015-04-03 08:54:31 +02:00
|
|
|
po.stackSize -= boundAmounts;
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i.setInventorySlotContents( x, po );
|
|
|
|
this.i.markDirty();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
// if ( rv != null )
|
|
|
|
// i.markDirty();
|
|
|
|
|
|
|
|
return rv;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public ItemStack simulateRemove( int amount, ItemStack filter, IInventoryDestination destination )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
int s = this.i.getSizeInventory();
|
2015-04-03 08:54:31 +02:00
|
|
|
ItemStack rv = null;
|
|
|
|
|
|
|
|
for( int x = 0; x < s && amount > 0; x++ )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
ItemStack is = this.i.getStackInSlot( x );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is != null && this.canRemoveStackFromSlot( x, is ) && ( filter == null || Platform.isSameItemPrecise( is, filter ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
int boundAmount = amount;
|
2015-04-03 08:54:31 +02:00
|
|
|
if( boundAmount > is.stackSize )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
boundAmount = is.stackSize;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( destination != null && !destination.canInsert( is ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
boundAmount = 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( boundAmount > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( rv == null )
|
|
|
|
{
|
|
|
|
rv = is.copy();
|
|
|
|
rv.stackSize = boundAmount;
|
|
|
|
amount -= boundAmount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv.stackSize += boundAmount;
|
|
|
|
amount -= boundAmount;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
return rv;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public ItemStack removeSimilarItems( int amount, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
int s = this.i.getSizeInventory();
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < s; x++ )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
ItemStack is = this.i.getStackInSlot( x );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is != null && this.canRemoveStackFromSlot( x, is ) && ( filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
int newAmount = amount;
|
|
|
|
if( newAmount > is.stackSize )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
newAmount = is.stackSize;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( destination != null && !destination.canInsert( is ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
newAmount = 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
ItemStack rv = null;
|
|
|
|
if( newAmount > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
rv = is.copy();
|
|
|
|
rv.stackSize = newAmount;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is.stackSize == rv.stackSize )
|
2014-02-09 06:59:38 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i.setInventorySlotContents( x, null );
|
|
|
|
this.i.markDirty();
|
2014-02-09 06:59:38 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ItemStack po = is.copy();
|
2015-04-03 08:54:31 +02:00
|
|
|
po.stackSize -= rv.stackSize;
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i.setInventorySlotContents( x, po );
|
|
|
|
this.i.markDirty();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
if( rv != null )
|
|
|
|
{
|
|
|
|
// i.markDirty();
|
|
|
|
return rv;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
return null;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public ItemStack simulateSimilarRemove( int amount, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
int s = this.i.getSizeInventory();
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < s; x++ )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
ItemStack is = this.i.getStackInSlot( x );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
if( is != null && this.canRemoveStackFromSlot( x, is ) && ( filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-09-28 19:46:10 +02:00
|
|
|
int boundAmount = amount;
|
2015-04-03 08:54:31 +02:00
|
|
|
if( boundAmount > is.stackSize )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-28 19:46:10 +02:00
|
|
|
boundAmount = is.stackSize;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( destination != null && !destination.canInsert( is ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-28 19:46:10 +02:00
|
|
|
boundAmount = 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( boundAmount > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
ItemStack rv = is.copy();
|
|
|
|
rv.stackSize = boundAmount;
|
|
|
|
return rv;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
return null;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-21 19:08:05 +01:00
|
|
|
public ItemStack addItems( ItemStack toBeAdded )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-21 19:08:05 +01:00
|
|
|
return this.addItems( toBeAdded, true );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-21 19:08:05 +01:00
|
|
|
public ItemStack simulateAdd( ItemStack toBeSimulated )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-21 19:08:05 +01:00
|
|
|
return this.addItems( toBeSimulated, false );
|
2014-11-29 17:52:34 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public boolean containsItems()
|
|
|
|
{
|
|
|
|
int s = this.i.getSizeInventory();
|
|
|
|
for( int x = 0; x < s; x++ )
|
|
|
|
{
|
|
|
|
if( this.i.getStackInSlot( x ) != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return true;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-29 17:52:34 +01:00
|
|
|
/**
|
|
|
|
* Adds an {@link ItemStack} to the adapted {@link IInventory}.
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2014-11-29 17:52:34 +01:00
|
|
|
* It respects the inventories stack limit, which can result in not all items added and some left ones are returned.
|
|
|
|
* The ItemStack next is required for inventories, which will fail on isItemValidForSlot() for stacksizes larger
|
|
|
|
* than the limit.
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2014-11-29 17:52:34 +01:00
|
|
|
* @param itemsToAdd itemStack to add to the inventory
|
2015-04-03 08:54:31 +02:00
|
|
|
* @param modulate true to modulate, false for simulate
|
2015-01-01 21:00:39 +01:00
|
|
|
*
|
|
|
|
* @return the left itemstack, which could not be added
|
2014-11-29 17:52:34 +01:00
|
|
|
*/
|
|
|
|
private ItemStack addItems( ItemStack itemsToAdd, boolean modulate )
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( itemsToAdd == null || itemsToAdd.stackSize == 0 )
|
2014-11-29 17:52:34 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-11-29 17:52:34 +01:00
|
|
|
ItemStack left = itemsToAdd.copy();
|
|
|
|
int stackLimit = itemsToAdd.getMaxStackSize();
|
2014-12-29 15:13:47 +01:00
|
|
|
int perOperationLimit = Math.min( this.i.getInventoryStackLimit(), stackLimit );
|
|
|
|
int inventorySize = this.i.getSizeInventory();
|
2014-11-29 17:52:34 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int slot = 0; slot < inventorySize; slot++ )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-11-29 17:52:34 +01:00
|
|
|
ItemStack next = left.copy();
|
|
|
|
next.stackSize = Math.min( perOperationLimit, next.stackSize );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.i.isItemValidForSlot( slot, next ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
ItemStack is = this.i.getStackInSlot( slot );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is == null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-11-29 17:52:34 +01:00
|
|
|
left.stackSize -= next.stackSize;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( modulate )
|
2014-11-29 17:52:34 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i.setInventorySlotContents( slot, next );
|
|
|
|
this.i.markDirty();
|
2014-11-29 17:52:34 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( left.stackSize <= 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else if( Platform.isSameItemPrecise( is, left ) && is.stackSize < perOperationLimit )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-05 22:17:14 +01:00
|
|
|
int room = perOperationLimit - is.stackSize;
|
2014-11-29 17:52:34 +01:00
|
|
|
int used = Math.min( left.stackSize, room );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( modulate )
|
2014-11-29 17:52:34 +01:00
|
|
|
{
|
|
|
|
is.stackSize += used;
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i.setInventorySlotContents( slot, is );
|
|
|
|
this.i.markDirty();
|
2014-11-29 17:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
left.stackSize -= used;
|
2015-04-03 08:54:31 +02:00
|
|
|
if( left.stackSize <= 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-11-29 17:52:34 +01:00
|
|
|
return null;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
boolean canRemoveStackFromSlot( int x, ItemStack is )
|
|
|
|
{
|
|
|
|
if( this.wrapperEnabled )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return ( (IInventoryWrapper) this.i ).canRemoveItemFromSlot( x, is );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<ItemSlot> iterator()
|
|
|
|
{
|
|
|
|
return new InvIterator();
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
class InvIterator implements Iterator<ItemSlot>
|
|
|
|
{
|
|
|
|
|
|
|
|
final ItemSlot is = new ItemSlot();
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNext()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.x < AdaptorIInventory.this.i.getSizeInventory();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemSlot next()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
ItemStack iss = AdaptorIInventory.this.i.getStackInSlot( this.x );
|
2014-08-12 05:56:33 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.is.isExtractable = AdaptorIInventory.this.canRemoveStackFromSlot( this.x, iss );
|
|
|
|
this.is.setItemStack( iss );
|
2014-08-12 05:56:33 +02:00
|
|
|
|
2015-03-26 11:07:26 +01:00
|
|
|
this.is.slot = this.x;
|
|
|
|
this.x++;
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.is;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove()
|
|
|
|
{
|
|
|
|
// nothing!
|
|
|
|
}
|
2014-09-28 00:50:06 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|