2013-12-27 23:59:59 +01:00
|
|
|
package appeng.tile.inventory;
|
|
|
|
|
2013-12-28 22:04:21 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import appeng.api.storage.IMEInventory;
|
2014-02-07 21:37:22 +01:00
|
|
|
import appeng.core.AELog;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.storage.MEIInventoryWrapper;
|
|
|
|
import appeng.util.Platform;
|
2013-12-28 22:04:21 +01:00
|
|
|
import appeng.util.iterators.InvIterator;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2013-12-28 22:04:21 +01:00
|
|
|
public class AppEngInternalInventory implements IInventory, Iterable<ItemStack>
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
protected IAEAppEngInventory te;
|
2013-12-30 06:58:08 +01:00
|
|
|
protected int size;
|
|
|
|
protected int maxStack;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public boolean enableClientEvents = false;
|
2013-12-27 23:59:59 +01:00
|
|
|
protected ItemStack inv[];
|
|
|
|
|
|
|
|
public IMEInventory getIMEI()
|
|
|
|
{
|
|
|
|
return new MEIInventoryWrapper( this, null );
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEmpty()
|
|
|
|
{
|
|
|
|
for (int x = 0; x < getSizeInventory(); x++)
|
|
|
|
if ( getStackInSlot( x ) != null )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AppEngInternalInventory(IAEAppEngInventory _te, int s) {
|
|
|
|
te = _te;
|
|
|
|
size = s;
|
|
|
|
maxStack = 64;
|
|
|
|
inv = new ItemStack[s];
|
|
|
|
}
|
|
|
|
|
2013-12-30 06:58:08 +01:00
|
|
|
protected boolean eventsEnabled()
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
return Platform.isServer() || enableClientEvents;
|
2013-12-30 06:58:08 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public void setMaxStackSize(int s)
|
|
|
|
{
|
|
|
|
maxStack = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int var1)
|
|
|
|
{
|
|
|
|
return inv[var1];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int slot, int qty)
|
|
|
|
{
|
|
|
|
if ( inv[slot] != null )
|
|
|
|
{
|
|
|
|
ItemStack split = getStackInSlot( slot );
|
|
|
|
ItemStack ns = null;
|
|
|
|
|
|
|
|
if ( qty >= split.stackSize )
|
|
|
|
{
|
|
|
|
ns = inv[slot];
|
|
|
|
inv[slot] = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ns = split.splitStack( qty );
|
|
|
|
|
2013-12-30 06:58:08 +01:00
|
|
|
if ( te != null && eventsEnabled() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
te.onChangeInventory( this, slot, InvOperation.decrStackSize, ns, null );
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
markDirty();
|
2013-12-27 23:59:59 +01:00
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlotOnClosing(int var1)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int slot, ItemStack newItemStack)
|
|
|
|
{
|
|
|
|
ItemStack oldStack = inv[slot];
|
|
|
|
inv[slot] = newItemStack;
|
|
|
|
|
2013-12-30 06:58:08 +01:00
|
|
|
if ( te != null && eventsEnabled() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
ItemStack removed = oldStack;
|
|
|
|
ItemStack added = newItemStack;
|
|
|
|
|
|
|
|
if ( oldStack != null && newItemStack != null && Platform.isSameItem( oldStack, newItemStack ) )
|
|
|
|
{
|
|
|
|
if ( oldStack.stackSize > newItemStack.stackSize )
|
|
|
|
{
|
|
|
|
removed = removed.copy();
|
|
|
|
removed.stackSize -= newItemStack.stackSize;
|
|
|
|
added = null;
|
|
|
|
}
|
|
|
|
else if ( oldStack.stackSize < newItemStack.stackSize )
|
|
|
|
{
|
|
|
|
added = added.copy();
|
|
|
|
added.stackSize -= oldStack.stackSize;
|
|
|
|
removed = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removed = added = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
te.onChangeInventory( this, slot, InvOperation.setInventorySlotContents, removed, added );
|
2014-01-31 01:50:11 +01:00
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
markDirty();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void markDirty()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2013-12-30 06:58:08 +01:00
|
|
|
if ( te != null && eventsEnabled() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
te.onChangeInventory( this, -1, InvOperation.markDirty, null, null );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 02:23:12 +01:00
|
|
|
// for guis...
|
2014-02-09 02:34:52 +01:00
|
|
|
public void markDirty(int slotIndex)
|
2014-01-31 02:23:12 +01:00
|
|
|
{
|
|
|
|
if ( te != null && eventsEnabled() )
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
te.onChangeInventory( this, slotIndex, InvOperation.markDirty, null, null );
|
2014-01-31 02:23:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
return maxStack > 64 ? 64 : maxStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUseableByPlayer(EntityPlayer var1)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void closeInventory()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void openInventory()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeToNBT(NBTTagCompound target)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < size; x++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
NBTTagCompound c = new NBTTagCompound();
|
|
|
|
|
|
|
|
if ( inv[x] != null )
|
|
|
|
{
|
|
|
|
inv[x].writeToNBT( c );
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
target.setTag( "#" + x, c );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
catch (Exception err)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound target)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < size; x++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
NBTTagCompound c = target.getCompoundTag( "#" + x );
|
|
|
|
|
|
|
|
if ( c != null )
|
|
|
|
inv[x] = ItemStack.loadItemStackFromNBT( c );
|
|
|
|
|
|
|
|
}
|
2014-02-07 21:37:22 +01:00
|
|
|
catch (Exception e)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-07 21:37:22 +01:00
|
|
|
AELog.error( e );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeToNBT(NBTTagCompound data, String name)
|
|
|
|
{
|
|
|
|
NBTTagCompound c = new NBTTagCompound();
|
|
|
|
writeToNBT( c );
|
2014-02-09 02:34:52 +01:00
|
|
|
data.setTag( name, c );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound data, String name)
|
|
|
|
{
|
|
|
|
NBTTagCompound c = data.getCompoundTag( name );
|
|
|
|
if ( c != null )
|
|
|
|
readFromNBT( c );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory()
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public String getInventoryName()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
return "appeng-internal";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public boolean hasCustomInventoryName()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-28 22:04:21 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<ItemStack> iterator()
|
|
|
|
{
|
|
|
|
return new InvIterator( this );
|
|
|
|
}
|
2014-01-31 02:23:12 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|