Applied-Energistics-2-tiler.../me/storage/CellInventory.java

533 lines
12 KiB
Java
Raw Normal View History

package appeng.me.storage;
import java.util.HashSet;
import java.util.Iterator;
2014-01-20 17:41:37 +01:00
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.oredict.OreDictionary;
2014-02-21 21:36:40 +01:00
import appeng.api.AEApi;
import appeng.api.config.Actionable;
2014-01-20 17:41:37 +01:00
import appeng.api.config.FuzzyMode;
import appeng.api.exceptions.AppEngException;
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.items.IStorageCell;
2014-01-20 17:41:37 +01:00
import appeng.api.networking.security.BaseActionSource;
2014-03-28 01:48:38 +01:00
import appeng.api.storage.ICellInventory;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
2014-03-28 01:48:38 +01:00
public class CellInventory implements ICellInventory
{
static final String ITEM_TYPE_TAG = "it";
static final String ITEM_COUNT_TAG = "ic";
static final String ITEM_SLOT = "#";
static final String ITEM_SLOTCOUNT = "@";
static final String ITEM_PRE_FORMATED_COUNT = "PF";
static final String ITEM_PRE_FORMATED_SLOT = "PF#";
static final String ITEM_PRE_FORMATED_NAME = "PN";
static final String ITEM_PRE_FORMATED_FUZZY = "FP";
static protected String[] ITEM_SLOT_ARR;
static protected String[] ITEM_SLOTCOUNT_ARR;
final protected NBTTagCompound tagCompound;
protected int MAX_ITEM_TYPES = 63;
protected short storedItems = 0;
protected int storedItemCount = 0;
2014-02-21 21:36:40 +01:00
protected IItemList<IAEItemStack> cellItems;
protected ItemStack i;
protected IStorageCell CellType;
protected CellInventory(NBTTagCompound data) {
tagCompound = data;
}
protected void loadCellItems()
{
if ( cellItems == null )
2014-02-21 21:36:40 +01:00
cellItems = AEApi.instance().storage().createItemList();
cellItems.resetStatus(); // clears totals and stuff.
2014-03-28 19:58:21 +01:00
int types = (int) getStoredItemTypes();
for (int x = 0; x < types; x++)
{
ItemStack t = ItemStack.loadItemStackFromNBT( tagCompound.getCompoundTag( ITEM_SLOT_ARR[x] ) );
if ( t != null )
{
t.stackSize = tagCompound.getInteger( ITEM_SLOTCOUNT_ARR[x] );
if ( t.stackSize > 0 )
{
cellItems.add( AEItemStack.create( t ) );
}
}
}
// cellItems.clean();
}
void saveChanges()
{
// cellItems.clean();
int itemCount = 0;
// add new pretty stuff...
int x = 0;
Iterator<IAEItemStack> i = cellItems.iterator();
while (i.hasNext())
{
IAEItemStack v = i.next();
itemCount += v.getStackSize();
NBTBase c = tagCompound.getTag( ITEM_SLOT_ARR[x] );
if ( c instanceof NBTTagCompound )
v.writeToNBT( (NBTTagCompound) c );
else
{
NBTTagCompound g = new NBTTagCompound();
v.writeToNBT( g );
2014-02-09 02:34:52 +01:00
tagCompound.setTag( ITEM_SLOT_ARR[x], g );
}
2014-02-09 02:34:52 +01:00
/*
* NBTBase tagSlotCount = tagCompound.getTag( ITEM_SLOTCOUNT_ARR[x] ); if ( tagSlotCount instanceof
* NBTTagInt ) ((NBTTagInt) tagSlotCount).data = (int) v.getStackSize(); else
*/
tagCompound.setInteger( ITEM_SLOTCOUNT_ARR[x], (int) v.getStackSize() );
x++;
}
2014-02-09 02:34:52 +01:00
// NBTBase tagType = tagCompound.getTag( ITEM_TYPE_TAG );
// NBTBase tagCount = tagCompound.getTag( ITEM_COUNT_TAG );
short oldStoreditems = storedItems;
2014-02-09 02:34:52 +01:00
/*
* if ( tagType instanceof NBTTagShort ) ((NBTTagShort) tagType).data = storedItems = (short) cellItems.size();
* else
*/
tagCompound.setShort( ITEM_TYPE_TAG, storedItems = (short) cellItems.size() );
2014-02-09 02:34:52 +01:00
/*
* if ( tagCount instanceof NBTTagInt ) ((NBTTagInt) tagCount).data = storedItemCount = itemCount; else
*/
tagCompound.setInteger( ITEM_COUNT_TAG, storedItemCount = itemCount );
// clean any old crusty stuff...
for (; x < oldStoreditems && x < MAX_ITEM_TYPES; x++)
{
tagCompound.removeTag( ITEM_SLOT_ARR[x] );
tagCompound.removeTag( ITEM_SLOTCOUNT_ARR[x] );
}
}
protected CellInventory(ItemStack o) throws AppEngException {
if ( ITEM_SLOT_ARR == null )
{
ITEM_SLOT_ARR = new String[MAX_ITEM_TYPES];
ITEM_SLOTCOUNT_ARR = new String[MAX_ITEM_TYPES];
for (int x = 0; x < MAX_ITEM_TYPES; x++)
{
ITEM_SLOT_ARR[x] = ITEM_SLOT + x;
ITEM_SLOTCOUNT_ARR[x] = ITEM_SLOTCOUNT + x;
}
}
if ( o == null )
{
throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" );
}
CellType = null;
i = o;
Item type = i.getItem();
if ( type instanceof IStorageCell )
{
CellType = (IStorageCell) i.getItem();
MAX_ITEM_TYPES = CellType.getTotalTypes( i );
}
if ( CellType == null )
{
throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" );
}
if ( !CellType.isStorageCell( i ) )
{
throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" );
}
if ( MAX_ITEM_TYPES > 63 )
MAX_ITEM_TYPES = 63;
if ( MAX_ITEM_TYPES < 1 )
MAX_ITEM_TYPES = 1;
tagCompound = Platform.openNbtData( o );
storedItems = tagCompound.getShort( ITEM_TYPE_TAG );
storedItemCount = tagCompound.getInteger( ITEM_COUNT_TAG );
cellItems = null;
}
2014-02-21 21:36:40 +01:00
IItemList<IAEItemStack> getCellItems()
{
if ( cellItems == null )
{
2014-02-21 21:36:40 +01:00
cellItems = AEApi.instance().storage().createItemList();
loadCellItems();
}
return cellItems;
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public int getBytesPerType()
{
return CellType.BytePerType( i );
}
2014-03-28 01:48:38 +01:00
@Override
public boolean canHoldNewItem()
{
2014-03-28 19:58:21 +01:00
long bytesFree = getFreeBytes();
return (bytesFree > getBytesPerType() || (bytesFree == getBytesPerType() && getUnusedItemCount() > 0)) && getRemainingItemTypes() > 0;
}
public static IMEInventoryHandler getCell(ItemStack o)
{
try
{
return new CellInventoryHandler( new CellInventory( o ) );
}
catch (AppEngException e)
{
return null;
}
}
private static boolean isStorageCell(ItemStack i)
{
if ( i == null )
{
return false;
}
try
{
Item type = i.getItem();
if ( type instanceof IStorageCell )
{
return !((IStorageCell) type).storableInStorageCell();
}
}
catch (Throwable err)
{
return true;
}
return false;
}
public static boolean isCell(ItemStack i)
{
if ( i == null )
{
return false;
}
Item type = i.getItem();
if ( type instanceof IStorageCell )
{
return ((IStorageCell) type).isStorageCell( i );
}
return false;
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public long getTotalBytes()
{
return CellType.getBytes( i );
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public long getFreeBytes()
{
2014-03-28 19:58:21 +01:00
return getTotalBytes() - getUsedBytes();
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public long getUsedBytes()
{
2014-03-28 19:58:21 +01:00
long bytesForItemCount = (getStoredItemCount() + getUnusedItemCount()) / 8;
return getStoredItemTypes() * getBytesPerType() + bytesForItemCount;
}
2014-03-28 01:48:38 +01:00
@Override
public long getTotalItemTypes()
{
return MAX_ITEM_TYPES;
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public long getStoredItemTypes()
{
return storedItems;
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public long getStoredItemCount()
{
return storedItemCount;
}
private void updateItemCount(long delta)
{
tagCompound.setInteger( ITEM_COUNT_TAG, storedItemCount = (int) (storedItemCount + delta) );
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public long getRemainingItemTypes()
{
2014-03-28 19:58:21 +01:00
long basedOnStorage = getFreeBytes() / getBytesPerType();
long baseOnTotal = getTotalItemTypes() - getStoredItemTypes();
return basedOnStorage > baseOnTotal ? baseOnTotal : basedOnStorage;
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public long getRemainingItemCount()
{
2014-03-28 19:58:21 +01:00
long remaining = getFreeBytes() * 8 + getUnusedItemCount();
return remaining > 0 ? remaining : 0;
}
2014-03-28 01:48:38 +01:00
@Override
2014-03-28 19:58:21 +01:00
public int getUnusedItemCount()
{
2014-03-28 19:58:21 +01:00
int div = (int) (getStoredItemCount() % 8);
if ( div == 0 )
{
return 0;
}
return 8 - div;
}
private static HashSet<Integer> blackList = new HashSet();
public static void addBasicBlackList(int itemID, int Meta)
{
blackList.add( (Meta << Platform.DEF_OFFSET) | itemID );
}
public static boolean isBlackListed(IAEItemStack input)
{
2014-02-09 02:34:52 +01:00
if ( blackList.contains( (OreDictionary.WILDCARD_VALUE << Platform.DEF_OFFSET) | Item.getIdFromItem( input.getItem() ) ) )
return true;
2014-02-09 02:34:52 +01:00
return blackList.contains( (input.getItemDamage() << Platform.DEF_OFFSET) | Item.getIdFromItem( input.getItem() ) );
}
private boolean isEmpty(IMEInventory meinv)
{
2014-02-21 21:36:40 +01:00
return meinv.getAvailableItems( AEApi.instance().storage().createItemList() ).isEmpty();
}
@Override
2014-01-20 17:41:37 +01:00
public IAEItemStack injectItems(IAEItemStack input, Actionable mode, BaseActionSource src)
{
if ( input == null )
return null;
if ( input.getStackSize() == 0 )
return null;
if ( isBlackListed( input ) || CellType.isBlackListed( i, input ) )
return input;
ItemStack sharedItemStack = input.getItemStack();
if ( CellInventory.isStorageCell( sharedItemStack ) )
{
IMEInventory meinv = getCell( sharedItemStack );
if ( meinv != null && !isEmpty( meinv ) )
return input;
}
IAEItemStack l = getCellItems().findPrecise( input );
if ( l != null )
{
2014-03-28 19:58:21 +01:00
long remainingItemSlots = getRemainingItemCount();
if ( remainingItemSlots < 0 )
return input;
if ( input.getStackSize() > remainingItemSlots )
{
IAEItemStack r = input.copy();
r.setStackSize( r.getStackSize() - remainingItemSlots );
if ( mode == Actionable.MODULATE )
{
l.setStackSize( l.getStackSize() + remainingItemSlots );
updateItemCount( remainingItemSlots );
saveChanges();
}
return r;
}
else
{
if ( mode == Actionable.MODULATE )
{
l.setStackSize( l.getStackSize() + input.getStackSize() );
updateItemCount( input.getStackSize() );
saveChanges();
}
return null;
}
}
if ( canHoldNewItem() ) // room for new type, and for at least one item!
{
2014-03-28 19:58:21 +01:00
int remainingItemCount = (int) getRemainingItemCount() - getBytesPerType() * 8;
if ( remainingItemCount > 0 )
{
if ( input.getStackSize() > remainingItemCount )
{
ItemStack toReturn = Platform.cloneItemStack( sharedItemStack );
toReturn.stackSize = sharedItemStack.stackSize - remainingItemCount;
if ( mode == Actionable.MODULATE )
{
ItemStack toWrite = Platform.cloneItemStack( sharedItemStack );
toWrite.stackSize = remainingItemCount;
cellItems.add( AEItemStack.create( toWrite ) );
updateItemCount( toWrite.stackSize );
saveChanges();
}
return AEItemStack.create( toReturn );
}
if ( mode == Actionable.MODULATE )
{
updateItemCount( input.getStackSize() );
cellItems.add( input );
saveChanges();
}
return null;
}
}
return input;
}
@Override
2014-01-20 17:41:37 +01:00
public IAEItemStack extractItems(IAEItemStack request, Actionable mode, BaseActionSource src)
{
if ( request == null )
return null;
ItemStack sharedItem = request.getItemStack();
int size = sharedItem.stackSize;
IAEItemStack Results = null;
IAEItemStack l = getCellItems().findPrecise( request );
if ( l != null )
{
Results = l.copy();
if ( l.getStackSize() <= size )
{
Results.setStackSize( l.getStackSize() );
if ( mode == Actionable.MODULATE )
{
updateItemCount( -l.getStackSize() );
l.setStackSize( 0 );
saveChanges();
}
}
else
{
Results.setStackSize( size );
if ( mode == Actionable.MODULATE )
{
l.setStackSize( l.getStackSize() - size );
updateItemCount( -size );
saveChanges();
}
}
}
return Results;
}
@Override
public IItemList getAvailableItems(IItemList out)
{
for (IAEItemStack i : getCellItems())
out.add( i );
return out;
}
@Override
public StorageChannel getChannel()
{
return StorageChannel.ITEMS;
}
2014-03-28 01:48:38 +01:00
@Override
public double getIdleDrain()
{
return CellType.getIdleDrain();
}
2014-03-28 01:48:38 +01:00
@Override
2014-01-20 17:41:37 +01:00
public FuzzyMode getFuzzyMode()
{
return CellType.getFuzzyMode( this.i );
}
2014-03-28 01:48:38 +01:00
@Override
2014-01-20 17:41:37 +01:00
public IInventory getConfigInventory()
{
return CellType.getConfigInventory( this.i );
}
2014-03-28 01:48:38 +01:00
@Override
2014-01-20 17:41:37 +01:00
public IInventory getUpgradesInventory()
{
return CellType.getUpgradesInventory( this.i );
}
2014-03-28 01:48:38 +01:00
@Override
2014-01-20 17:41:37 +01:00
public int getStatusForCell()
{
if ( canHoldNewItem() )
return 1;
2014-03-28 19:58:21 +01:00
if ( getRemainingItemCount() > 0 )
2014-01-20 17:41:37 +01:00
return 2;
return 3;
}
2014-03-28 01:48:38 +01:00
@Override
public ItemStack getItemStack()
{
return i;
}
}