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;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
2015-12-24 02:07:03 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.config.FuzzyMode;
|
|
|
|
import appeng.util.InventoryAdaptor;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
import appeng.util.iterators.StackToSlotIterator;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public class AdaptorList extends InventoryAdaptor
|
|
|
|
{
|
|
|
|
|
2014-09-29 09:54:34 +02:00
|
|
|
private final List<ItemStack> i;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public AdaptorList( final List<ItemStack> s )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i = s;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-30 14:24:40 +02:00
|
|
|
public ItemStack removeItems( int amount, final ItemStack filter, final IInventoryDestination destination )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final int s = this.i.size();
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < s; x++ )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack is = this.i.get( x );
|
2016-11-04 09:27:52 +01:00
|
|
|
if( is != null && ( filter == null || Platform.itemComparisons().isSameItem( is, filter ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( amount > is.stackSize )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
amount = 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
|
|
|
amount = 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( amount > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack rv = is.copy();
|
2015-04-03 08:54:31 +02:00
|
|
|
rv.stackSize = amount;
|
|
|
|
is.stackSize -= amount;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
// remove it..
|
2015-04-03 08:54:31 +02:00
|
|
|
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
|
|
|
}
|
2013-12-27 23:59:59 +01: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 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final ItemStack is : this.i )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2016-11-04 09:27:52 +01:00
|
|
|
if( is != null && ( filter == null || Platform.itemComparisons().isSameItem( is, filter ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( amount > is.stackSize )
|
2014-09-29 09:23:02 +02:00
|
|
|
{
|
2014-12-21 19:08:05 +01:00
|
|
|
amount = is.stackSize;
|
2014-09-29 09:23:02 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( destination != null && !destination.canInsert( is ) )
|
2014-09-29 09:23:02 +02:00
|
|
|
{
|
2014-12-21 19:08:05 +01:00
|
|
|
amount = 0;
|
2014-09-29 09:23:02 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( amount > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack rv = is.copy();
|
2014-12-21 19:08:05 +01:00
|
|
|
rv.stackSize = amount;
|
2013-12-27 23:59:59 +01:00
|
|
|
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 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final int s = this.i.size();
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < s; x++ )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack is = this.i.get( x );
|
2016-11-04 09:27:52 +01:00
|
|
|
if( is != null && ( filter == null || Platform.itemComparisons().isFuzzyEqualItem( is, filter, fuzzyMode ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-05-08 23:25:19 +02:00
|
|
|
if( amount > is.stackSize )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-05-08 23:25:19 +02:00
|
|
|
amount = 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-05-08 23:25:19 +02:00
|
|
|
amount = 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-05-08 23:25:19 +02:00
|
|
|
if( amount > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack rv = is.copy();
|
2015-05-08 23:25:19 +02:00
|
|
|
rv.stackSize = amount;
|
|
|
|
is.stackSize -= amount;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
// remove it..
|
2015-04-03 08:54:31 +02:00
|
|
|
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
|
|
|
}
|
2013-12-27 23:59:59 +01: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 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final ItemStack is : this.i )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2016-11-04 09:27:52 +01:00
|
|
|
if( is != null && ( filter == null || Platform.itemComparisons().isFuzzyEqualItem( is, filter, fuzzyMode ) ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( amount > is.stackSize )
|
2014-09-29 09:23:02 +02:00
|
|
|
{
|
2014-12-21 19:08:05 +01:00
|
|
|
amount = is.stackSize;
|
2014-09-29 09:23:02 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( destination != null && !destination.canInsert( is ) )
|
2014-09-29 09:23:02 +02:00
|
|
|
{
|
2014-12-21 19:08:05 +01:00
|
|
|
amount = 0;
|
2014-09-29 09:23:02 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( amount > 0 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack rv = is.copy();
|
2014-12-21 19:08:05 +01:00
|
|
|
rv.stackSize = amount;
|
2013-12-27 23:59:59 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-30 14:24:40 +02:00
|
|
|
public ItemStack addItems( final ItemStack toBeAdded )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( toBeAdded == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( toBeAdded.stackSize == 0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack left = toBeAdded.copy();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final ItemStack is : this.i )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2016-11-04 09:27:52 +01:00
|
|
|
if( Platform.itemComparisons().isEqualItem( is, left ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
is.stackSize += left.stackSize;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.i.add( left );
|
2013-12-27 23:59:59 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-30 14:24:40 +02:00
|
|
|
public ItemStack simulateAdd( final ItemStack toBeSimulated )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean containsItems()
|
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final ItemStack is : this.i )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is != null )
|
2014-09-29 09:23:02 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return true;
|
2014-09-29 09:23:02 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<ItemSlot> iterator()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return new StackToSlotIterator( this.i.iterator() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|