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

223 lines
4.7 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 java.util.List;
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;
import appeng.util.iterators.StackToSlotIterator;
public class AdaptorList extends InventoryAdaptor
{
private final List<ItemStack> i;
2015-09-30 14:24:40 +02:00
public AdaptorList( final List<ItemStack> s )
{
2014-12-29 15:13:47 +01:00
this.i = s;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack removeItems( int amount, final ItemStack filter, final IInventoryDestination destination )
{
2015-09-30 14:24:40 +02:00
final int s = this.i.size();
for( int x = 0; x < s; x++ )
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.i.get( x );
if( is != null && ( filter == null || Platform.itemComparisons().isSameItem( is, filter ) ) )
{
if( amount > is.stackSize )
2015-04-29 02:30:53 +02:00
{
amount = is.stackSize;
2015-04-29 02:30:53 +02:00
}
if( destination != null && !destination.canInsert( is ) )
2015-04-29 02:30:53 +02:00
{
amount = 0;
2015-04-29 02:30:53 +02:00
}
if( amount > 0 )
{
2015-09-30 14:24:40 +02:00
final ItemStack rv = is.copy();
rv.stackSize = amount;
is.stackSize -= amount;
// remove it..
if( is.stackSize <= 0 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.i.remove( x );
2015-04-29 02:30:53 +02:00
}
return rv;
}
}
}
return null;
}
@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
for( final ItemStack is : this.i )
{
if( is != null && ( filter == null || Platform.itemComparisons().isSameItem( is, filter ) ) )
{
if( amount > is.stackSize )
{
amount = is.stackSize;
}
if( destination != null && !destination.canInsert( is ) )
{
amount = 0;
}
if( amount > 0 )
{
2015-09-30 14:24:40 +02:00
final ItemStack rv = is.copy();
rv.stackSize = amount;
return rv;
}
}
}
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack removeSimilarItems( int amount, final ItemStack filter, final FuzzyMode fuzzyMode, final IInventoryDestination destination )
{
2015-09-30 14:24:40 +02:00
final int s = this.i.size();
for( int x = 0; x < s; x++ )
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.i.get( x );
if( is != null && ( filter == null || Platform.itemComparisons().isFuzzyEqualItem( is, filter, fuzzyMode ) ) )
{
if( amount > is.stackSize )
2015-04-29 02:30:53 +02:00
{
amount = is.stackSize;
2015-04-29 02:30:53 +02:00
}
if( destination != null && !destination.canInsert( is ) )
2015-04-29 02:30:53 +02:00
{
amount = 0;
2015-04-29 02:30:53 +02:00
}
if( amount > 0 )
{
2015-09-30 14:24:40 +02:00
final ItemStack rv = is.copy();
rv.stackSize = amount;
is.stackSize -= amount;
// remove it..
if( is.stackSize <= 0 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.i.remove( x );
2015-04-29 02:30:53 +02:00
}
return rv;
}
}
}
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack simulateSimilarRemove( int amount, final ItemStack filter, final FuzzyMode fuzzyMode, final IInventoryDestination destination )
{
2015-09-30 14:24:40 +02:00
for( final ItemStack is : this.i )
{
if( is != null && ( filter == null || Platform.itemComparisons().isFuzzyEqualItem( is, filter, fuzzyMode ) ) )
{
if( amount > is.stackSize )
{
amount = is.stackSize;
}
if( destination != null && !destination.canInsert( is ) )
{
amount = 0;
}
if( amount > 0 )
{
2015-09-30 14:24:40 +02:00
final ItemStack rv = is.copy();
rv.stackSize = amount;
return rv;
}
}
}
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack addItems( final ItemStack toBeAdded )
{
if( toBeAdded == null )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
if( toBeAdded.stackSize == 0 )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final ItemStack left = toBeAdded.copy();
2015-09-30 14:24:40 +02:00
for( final ItemStack is : this.i )
{
if( Platform.itemComparisons().isEqualItem( is, left ) )
{
is.stackSize += left.stackSize;
return null;
}
}
2014-12-29 15:13:47 +01:00
this.i.add( left );
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack simulateAdd( final ItemStack toBeSimulated )
{
return null;
}
@Override
public boolean containsItems()
{
2015-09-30 14:24:40 +02:00
for( final ItemStack is : this.i )
{
if( is != null )
{
return true;
}
}
return false;
}
@Override
public Iterator<ItemSlot> iterator()
{
2014-12-29 15:13:47 +01:00
return new StackToSlotIterator( this.i.iterator() );
}
}