Applied-Energistics-2-tiler.../src/main/java/appeng/util/inv/AdaptorIInventory.java

369 lines
8.6 KiB
Java
Raw Normal View History

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>.
*/
package appeng.util.inv;
import java.util.Iterator;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
2015-12-24 02:07:03 +01:00
import appeng.api.config.FuzzyMode;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
public class AdaptorIInventory extends InventoryAdaptor
{
private final IInventory i;
private final boolean wrapperEnabled;
2015-09-30 14:24:40 +02:00
public AdaptorIInventory( final IInventory s )
{
2014-12-29 15:13:47 +01:00
this.i = s;
this.wrapperEnabled = s instanceof IInventoryWrapper;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack removeItems( int amount, ItemStack filter, final IInventoryDestination destination )
{
2015-09-30 14:24:40 +02:00
final int s = this.i.getSizeInventory();
ItemStack rv = null;
for( int x = 0; x < s && amount > 0; x++ )
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.i.getStackInSlot( x );
if( is != null && this.canRemoveStackFromSlot( x, is ) && ( filter == null || Platform.itemComparisons().isSameItem( is, filter ) ) )
{
int boundAmounts = amount;
2016-12-08 12:42:00 +01:00
if( boundAmounts > is.getCount() )
2015-04-29 02:30:53 +02:00
{
2016-12-08 12:42:00 +01:00
boundAmounts = is.getCount();
2015-04-29 02:30:53 +02:00
}
if( destination != null && !destination.canInsert( is ) )
2015-04-29 02:30:53 +02:00
{
boundAmounts = 0;
2015-04-29 02:30:53 +02:00
}
if( boundAmounts > 0 )
{
if( rv == null )
{
rv = is.copy();
filter = rv;
2016-12-08 12:42:00 +01:00
rv.setCount( boundAmounts );
amount -= boundAmounts;
}
else
{
2016-12-08 12:42:00 +01:00
rv.grow( boundAmounts );
amount -= boundAmounts;
}
2016-12-08 12:42:00 +01:00
if( is.getCount() == 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
}
else
{
2015-09-30 14:24:40 +02:00
final ItemStack po = is.copy();
2016-12-08 12:42:00 +01:00
po.grow( -boundAmounts );
2014-12-29 15:13:47 +01:00
this.i.setInventorySlotContents( x, po );
this.i.markDirty();
}
}
}
}
// if ( rv != null )
// i.markDirty();
return rv;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack simulateRemove( int amount, final ItemStack filter, final IInventoryDestination destination )
{
2015-09-30 14:24:40 +02:00
final int s = this.i.getSizeInventory();
ItemStack rv = null;
for( int x = 0; x < s && amount > 0; x++ )
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.i.getStackInSlot( x );
if( is != null && this.canRemoveStackFromSlot( x, is ) && ( filter == null || Platform.itemComparisons().isSameItem( is, filter ) ) )
{
int boundAmount = amount;
2016-12-08 12:42:00 +01:00
if( boundAmount > is.getCount() )
2015-04-29 02:30:53 +02:00
{
2016-12-08 12:42:00 +01:00
boundAmount = is.getCount();
2015-04-29 02:30:53 +02:00
}
if( destination != null && !destination.canInsert( is ) )
2015-04-29 02:30:53 +02:00
{
boundAmount = 0;
2015-04-29 02:30:53 +02:00
}
if( boundAmount > 0 )
{
if( rv == null )
{
rv = is.copy();
2016-12-08 12:42:00 +01:00
rv.setCount( boundAmount );
amount -= boundAmount;
}
else
{
2016-12-08 12:42:00 +01:00
rv.grow( boundAmount );
amount -= boundAmount;
}
}
}
}
return rv;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack removeSimilarItems( final int amount, final ItemStack filter, final FuzzyMode fuzzyMode, final IInventoryDestination destination )
{
2015-09-30 14:24:40 +02:00
final int s = this.i.getSizeInventory();
for( int x = 0; x < s; x++ )
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.i.getStackInSlot( x );
2016-12-08 12:42:00 +01:00
if( is != null && this.canRemoveStackFromSlot( x,
is ) && ( filter == null || Platform.itemComparisons().isFuzzyEqualItem( is, filter, fuzzyMode ) ) )
{
int newAmount = amount;
2016-12-08 12:42:00 +01:00
if( newAmount > is.getCount() )
2015-04-29 02:30:53 +02:00
{
2016-12-08 12:42:00 +01:00
newAmount = is.getCount();
2015-04-29 02:30:53 +02:00
}
if( destination != null && !destination.canInsert( is ) )
2015-04-29 02:30:53 +02:00
{
newAmount = 0;
2015-04-29 02:30:53 +02:00
}
ItemStack rv = null;
if( newAmount > 0 )
{
rv = is.copy();
2016-12-08 12:42:00 +01:00
rv.setCount( newAmount );
2016-12-08 12:42:00 +01:00
if( is.getCount() == rv.getCount() )
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
}
else
{
2015-09-30 14:24:40 +02:00
final ItemStack po = is.copy();
2016-12-08 12:42:00 +01:00
po.grow( -rv.getCount() );
2014-12-29 15:13:47 +01:00
this.i.setInventorySlotContents( x, po );
this.i.markDirty();
}
}
if( rv != null )
{
// i.markDirty();
return rv;
}
}
}
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack simulateSimilarRemove( final int amount, final ItemStack filter, final FuzzyMode fuzzyMode, final IInventoryDestination destination )
{
2015-09-30 14:24:40 +02:00
final int s = this.i.getSizeInventory();
for( int x = 0; x < s; x++ )
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.i.getStackInSlot( x );
2016-12-08 12:42:00 +01:00
if( is != null && this.canRemoveStackFromSlot( x,
is ) && ( filter == null || Platform.itemComparisons().isFuzzyEqualItem( is, filter, fuzzyMode ) ) )
{
2014-09-28 19:46:10 +02:00
int boundAmount = amount;
2016-12-08 12:42:00 +01:00
if( boundAmount > is.getCount() )
2015-04-29 02:30:53 +02:00
{
2016-12-08 12:42:00 +01:00
boundAmount = is.getCount();
2015-04-29 02:30:53 +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
}
if( boundAmount > 0 )
{
2015-09-30 14:24:40 +02:00
final ItemStack rv = is.copy();
2016-12-08 12:42:00 +01:00
rv.setCount( boundAmount );
return rv;
}
}
}
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack addItems( final ItemStack toBeAdded )
{
return this.addItems( toBeAdded, true );
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack simulateAdd( final ItemStack toBeSimulated )
{
return this.addItems( toBeSimulated, false );
}
@Override
public boolean containsItems()
{
2015-09-30 14:24:40 +02:00
final 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
{
return true;
2015-04-29 02:30:53 +02:00
}
}
return false;
}
/**
* Adds an {@link ItemStack} to the adapted {@link IInventory}.
2015-02-03 12:04:13 +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
*
* @param itemsToAdd itemStack to add to the inventory
2015-12-24 02:03:16 +01: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
*/
2015-09-30 14:24:40 +02:00
private ItemStack addItems( final ItemStack itemsToAdd, final boolean modulate )
{
2016-12-08 12:42:00 +01:00
if( itemsToAdd == null || itemsToAdd.getCount() == 0 )
{
return ItemStack.EMPTY;
}
2015-09-30 14:24:40 +02:00
final ItemStack left = itemsToAdd.copy();
final int stackLimit = itemsToAdd.getMaxStackSize();
final int perOperationLimit = Math.min( this.i.getInventoryStackLimit(), stackLimit );
final int inventorySize = this.i.getSizeInventory();
for( int slot = 0; slot < inventorySize; slot++ )
{
2015-09-30 14:24:40 +02:00
final ItemStack next = left.copy();
2016-12-08 12:42:00 +01:00
next.setCount( Math.min( perOperationLimit, next.getCount() ) );
if( this.i.isItemValidForSlot( slot, next ) )
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.i.getStackInSlot( slot );
if( is == null )
{
2016-12-08 12:42:00 +01:00
left.grow( -next.getCount() );
if( modulate )
{
2014-12-29 15:13:47 +01:00
this.i.setInventorySlotContents( slot, next );
this.i.markDirty();
}
2016-12-08 12:42:00 +01:00
if( left.getCount() <= 0 )
{
return ItemStack.EMPTY;
}
}
2016-12-08 12:42:00 +01:00
else if( Platform.itemComparisons().isSameItem( is, left ) && is.getCount() < perOperationLimit )
{
2016-12-08 12:42:00 +01:00
final int room = perOperationLimit - is.getCount();
final int used = Math.min( left.getCount(), room );
if( modulate )
{
2016-12-08 12:42:00 +01:00
is.grow( used );
2014-12-29 15:13:47 +01:00
this.i.setInventorySlotContents( slot, is );
this.i.markDirty();
}
2016-12-08 12:42:00 +01:00
left.grow( -used );
if( left.getCount() <= 0 )
{
return ItemStack.EMPTY;
}
}
}
}
return left;
}
private boolean canRemoveStackFromSlot( final int x, final ItemStack is )
{
if( this.wrapperEnabled )
2015-04-29 02:30:53 +02:00
{
return ( (IInventoryWrapper) this.i ).canRemoveItemFromSlot( x, is );
2015-04-29 02:30:53 +02:00
}
return true;
}
@Override
public Iterator<ItemSlot> iterator()
{
return new InvIterator();
}
private class InvIterator implements Iterator<ItemSlot>
{
private final ItemSlot is = new ItemSlot();
private int x = 0;
@Override
public boolean hasNext()
{
2014-12-29 15:13:47 +01:00
return this.x < AdaptorIInventory.this.i.getSizeInventory();
}
@Override
public ItemSlot next()
{
2015-09-30 14:24:40 +02:00
final ItemStack iss = AdaptorIInventory.this.i.getStackInSlot( this.x );
this.is.setExtractable( AdaptorIInventory.this.canRemoveStackFromSlot( this.x, iss ) );
2014-12-29 15:13:47 +01:00
this.is.setItemStack( iss );
this.is.setSlot( this.x );
2015-03-26 11:07:26 +01:00
this.x++;
2014-12-29 15:13:47 +01:00
return this.is;
}
@Override
public void remove()
{
// nothing!
}
2014-09-28 00:50:06 +02:00
}
}