Applied-Energistics-2-tiler.../src/main/java/appeng/tile/inventory/AppEngInternalAEInventory.java

266 lines
5.3 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>.
*/
2013-12-28 22:02:33 +01:00
package appeng.tile.inventory;
import java.util.Iterator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import appeng.api.AEApi;
import appeng.api.storage.data.IAEItemStack;
import appeng.core.AELog;
2013-12-28 22:02:33 +01:00
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
2014-01-01 10:04:54 +01:00
import appeng.util.iterators.AEInvIterator;
2013-12-28 22:02:33 +01:00
import appeng.util.iterators.InvIterator;
public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack>
{
protected final IAEAppEngInventory te;
final int size;
2013-12-28 22:02:33 +01:00
int maxStack;
protected final IAEItemStack[] inv;
2013-12-28 22:02:33 +01:00
public boolean isEmpty()
{
for (int x = 0; x < getSizeInventory(); x++)
if ( getStackInSlot( x ) != null )
return false;
return true;
}
public AppEngInternalAEInventory(IAEAppEngInventory _te, int s) {
te = _te;
size = s;
maxStack = 64;
inv = new IAEItemStack[s];
}
public void setMaxStackSize(int s)
{
maxStack = s;
}
public IAEItemStack getAEStackInSlot(int var1)
{
return inv[var1];
}
@Override
public ItemStack getStackInSlot(int var1)
{
if ( inv[var1] == null )
return null;
return inv[var1].getItemStack();
}
@Override
public ItemStack decrStackSize(int slot, int qty)
{
if ( inv[slot] != null )
{
ItemStack split = getStackInSlot( slot );
ItemStack ns = null;
if ( qty >= split.stackSize )
{
ns = getStackInSlot( slot );
inv[slot] = null;
}
else
ns = split.splitStack( qty );
if ( te != null && Platform.isServer() )
{
te.onChangeInventory( this, slot, InvOperation.decreaseStackSize, ns, null );
2013-12-28 22:02:33 +01:00
}
return ns;
}
return null;
}
@Override
public ItemStack getStackInSlotOnClosing(int var1)
{
return null;
}
@Override
public void setInventorySlotContents(int slot, ItemStack newItemStack)
{
ItemStack oldStack = getStackInSlot( slot );
inv[slot] = AEApi.instance().storage().createItemStack( newItemStack );
if ( te != null && Platform.isServer() )
{
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 );
}
}
@Override
2014-02-09 02:34:52 +01:00
public void markDirty()
2013-12-28 22:02:33 +01:00
{
if ( te != null && Platform.isServer() )
{
2014-02-09 02:34:52 +01:00
te.onChangeInventory( this, -1, InvOperation.markDirty, null, null );
2013-12-28 22:02:33 +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 openInventory()
2013-12-28 22:02:33 +01:00
{
}
@Override
2014-02-09 02:34:52 +01:00
public void closeInventory()
2013-12-28 22:02:33 +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-28 22:02:33 +01:00
}
catch (Exception ignored)
2013-12-28 22:02:33 +01:00
{
}
}
}
public void readFromNBT(NBTTagCompound target)
{
for (int x = 0; x < size; x++)
{
try
{
NBTTagCompound c = target.getCompoundTag( "#" + x );
if ( c != null )
inv[x] = AEItemStack.loadItemStackFromNBT( c );
}
catch (Exception e)
2013-12-28 22:02:33 +01:00
{
AELog.error( e );
2013-12-28 22:02:33 +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-28 22:02:33 +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-28 22:02:33 +01:00
{
return "appeng-internal";
}
@Override
2014-02-09 02:34:52 +01:00
public boolean hasCustomInventoryName()
2013-12-28 22:02:33 +01:00
{
return false;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return true;
}
@Override
public Iterator<ItemStack> iterator()
{
return new InvIterator( this );
}
2014-01-01 10:04:54 +01:00
public Iterator<IAEItemStack> getNewAEIterator()
2014-01-01 10:04:54 +01:00
{
return new AEInvIterator( this );
}
2013-12-28 22:02:33 +01:00
}