Package appeng.util.inv

Source Code of appeng.util.inv.AdaptorList

package appeng.util.inv;

import java.util.Iterator;
import java.util.List;

import net.minecraft.item.ItemStack;
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;

  public AdaptorList(List<ItemStack> s) {
    i = s;
  }

  @Override
  public ItemStack removeSimilarItems(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination dest)
  {
    int s = i.size();
    for (int x = 0; x < s; x++)
    {
      ItemStack is = i.get( x );
      if ( is != null && (filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode )) )
      {
        if ( how_many > is.stackSize )
          how_many = is.stackSize;
        if ( dest != null && !dest.canInsert( is ) )
          how_many = 0;

        if ( how_many > 0 )
        {
          ItemStack rv = is.copy();
          rv.stackSize = how_many;
          is.stackSize -= how_many;

          // remove it..
          if ( is.stackSize <= 0 )
            i.remove( x );

          return rv;
        }
      }
    }
    return null;
  }

  @Override
  public ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination dest)
  {
    for (ItemStack is : i)
    {
      if ( is != null && (filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode )) )
      {
        if ( how_many > is.stackSize )
        {
          how_many = is.stackSize;
        }
        if ( dest != null && !dest.canInsert( is ) )
        {
          how_many = 0;
        }

        if ( how_many > 0 )
        {
          ItemStack rv = is.copy();
          rv.stackSize = how_many;
          return rv;
        }
      }
    }
    return null;

  }

  @Override
  public ItemStack removeItems(int how_many, ItemStack filter, IInventoryDestination dest)
  {
    int s = i.size();
    for (int x = 0; x < s; x++)
    {
      ItemStack is = i.get( x );
      if ( is != null && (filter == null || Platform.isSameItemPrecise( is, filter )) )
      {
        if ( how_many > is.stackSize )
          how_many = is.stackSize;
        if ( dest != null && !dest.canInsert( is ) )
          how_many = 0;

        if ( how_many > 0 )
        {
          ItemStack rv = is.copy();
          rv.stackSize = how_many;
          is.stackSize -= how_many;

          // remove it..
          if ( is.stackSize <= 0 )
            i.remove( x );

          return rv;
        }
      }
    }
    return null;
  }

  @Override
  public ItemStack simulateRemove(int how_many, ItemStack filter, IInventoryDestination dest)
  {
    for (ItemStack is : i)
    {
      if ( is != null && (filter == null || Platform.isSameItemPrecise( is, filter )) )
      {
        if ( how_many > is.stackSize )
        {
          how_many = is.stackSize;
        }
        if ( dest != null && !dest.canInsert( is ) )
        {
          how_many = 0;
        }

        if ( how_many > 0 )
        {
          ItemStack rv = is.copy();
          rv.stackSize = how_many;
          return rv;
        }
      }
    }
    return null;

  }

  @Override
  public ItemStack addItems(ItemStack A)
  {
    if ( A == null )
      return null;
    if ( A.stackSize == 0 )
      return null;

    ItemStack left = A.copy();

    for (ItemStack is : i)
    {
      if ( Platform.isSameItem( is, left ) )
      {
        is.stackSize += left.stackSize;
        return null;
      }
    }

    i.add( left );
    return null;
  }

  @Override
  public ItemStack simulateAdd(ItemStack A)
  {
    return null;
  }

  @Override
  public boolean containsItems()
  {
    for (ItemStack is : i)
    {
      if ( is != null )
      {
        return true;
      }
    }
    return false;
  }

  @Override
  public Iterator<ItemSlot> iterator()
  {
    return new StackToSlotIterator( i.iterator() );
  }

}
TOP

Related Classes of appeng.util.inv.AdaptorList

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.