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

316 lines
6.1 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;
2013-12-28 22:02:33 +01:00
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;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.IChatComponent;
2015-12-24 02:07:03 +01:00
2013-12-28 22:02:33 +01:00
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;
2013-12-28 22:02:33 +01:00
public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack>
{
private final IAEAppEngInventory te;
private final IAEItemStack[] inv;
private final int size;
private int maxStack;
2013-12-28 22:02:33 +01:00
2015-09-30 14:24:40 +02:00
public AppEngInternalAEInventory( final IAEAppEngInventory te, final int s )
2013-12-28 22:02:33 +01:00
{
this.te = te;
2014-12-29 15:13:47 +01:00
this.size = s;
this.maxStack = 64;
this.inv = new IAEItemStack[s];
2013-12-28 22:02:33 +01:00
}
public boolean isEmpty()
{
for( int x = 0; x < this.size; x++ )
2015-04-29 02:30:53 +02:00
{
if( this.getStackInSlot( x ) != null )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
}
return true;
}
2015-09-30 14:24:40 +02:00
public void setMaxStackSize( final int s )
2013-12-28 22:02:33 +01:00
{
2014-12-29 15:13:47 +01:00
this.maxStack = s;
2013-12-28 22:02:33 +01:00
}
2015-09-30 14:24:40 +02:00
public IAEItemStack getAEStackInSlot( final int var1 )
2013-12-28 22:02:33 +01:00
{
2014-12-29 15:13:47 +01:00
return this.inv[var1];
2013-12-28 22:02:33 +01:00
}
2015-09-30 14:24:40 +02:00
public void writeToNBT( final NBTTagCompound data, final String name )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = new NBTTagCompound();
this.writeToNBT( c );
data.setTag( name, c );
}
private void writeToNBT( final NBTTagCompound target )
{
for( int x = 0; x < this.size; x++ )
{
try
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = new NBTTagCompound();
if( this.inv[x] != null )
{
this.inv[x].writeToNBT( c );
}
target.setTag( "#" + x, c );
}
2015-09-30 14:24:40 +02:00
catch( final Exception ignored )
{
}
}
}
2015-09-30 14:24:40 +02:00
public void readFromNBT( final NBTTagCompound data, final String name )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = data.getCompoundTag( name );
if( c != null )
2015-04-29 02:30:53 +02:00
{
this.readFromNBT( c );
2015-04-29 02:30:53 +02:00
}
}
private void readFromNBT( final NBTTagCompound target )
{
for( int x = 0; x < this.size; x++ )
{
try
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = target.getCompoundTag( "#" + x );
if( c != null )
2015-04-29 02:30:53 +02:00
{
this.inv[x] = AEItemStack.loadItemStackFromNBT( c );
2015-04-29 02:30:53 +02:00
}
}
2015-09-30 14:24:40 +02:00
catch( final Exception e )
{
AELog.debug( e );
}
}
}
2013-12-28 22:02:33 +01:00
@Override
public int getSizeInventory()
2013-12-28 22:02:33 +01:00
{
return this.size;
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack getStackInSlot( final int var1 )
{
if( this.inv[var1] == null )
2015-04-29 02:30:53 +02:00
{
2013-12-28 22:02:33 +01:00
return null;
2015-04-29 02:30:53 +02:00
}
2013-12-28 22:02:33 +01:00
2014-12-29 15:13:47 +01:00
return this.inv[var1].getItemStack();
2013-12-28 22:02:33 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack decrStackSize( final int slot, final int qty )
2013-12-28 22:02:33 +01:00
{
if( this.inv[slot] != null )
2013-12-28 22:02:33 +01:00
{
2015-09-30 14:24:40 +02:00
final ItemStack split = this.getStackInSlot( slot );
2013-12-28 22:02:33 +01:00
ItemStack ns = null;
if( qty >= split.stackSize )
2013-12-28 22:02:33 +01:00
{
2014-12-29 15:13:47 +01:00
ns = this.getStackInSlot( slot );
this.inv[slot] = null;
2013-12-28 22:02:33 +01:00
}
else
2015-04-29 02:30:53 +02:00
{
2013-12-28 22:02:33 +01:00
ns = split.splitStack( qty );
2015-04-29 02:30:53 +02:00
}
2013-12-28 22:02:33 +01:00
if( this.te != null && Platform.isServer() )
2013-12-28 22:02:33 +01:00
{
2014-12-29 15:13:47 +01:00
this.te.onChangeInventory( this, slot, InvOperation.decreaseStackSize, ns, null );
2013-12-28 22:02:33 +01:00
}
return ns;
}
return null;
}
@Override
public ItemStack removeStackFromSlot( final int var1 )
2013-12-28 22:02:33 +01:00
{
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public void setInventorySlotContents( final int slot, final ItemStack newItemStack )
2013-12-28 22:02:33 +01:00
{
2015-09-30 14:24:40 +02:00
final ItemStack oldStack = this.getStackInSlot( slot );
2014-12-29 15:13:47 +01:00
this.inv[slot] = AEApi.instance().storage().createItemStack( newItemStack );
2013-12-28 22:02:33 +01:00
if( this.te != null && Platform.isServer() )
2013-12-28 22:02:33 +01:00
{
ItemStack removed = oldStack;
ItemStack added = newItemStack;
if( oldStack != null && newItemStack != null && Platform.isSameItem( oldStack, newItemStack ) )
2013-12-28 22:02:33 +01:00
{
if( oldStack.stackSize > newItemStack.stackSize )
2013-12-28 22:02:33 +01:00
{
removed = removed.copy();
removed.stackSize -= newItemStack.stackSize;
added = null;
}
else if( oldStack.stackSize < newItemStack.stackSize )
2013-12-28 22:02:33 +01:00
{
added = added.copy();
added.stackSize -= oldStack.stackSize;
removed = null;
}
else
{
removed = added = null;
}
}
2014-12-29 15:13:47 +01:00
this.te.onChangeInventory( this, slot, InvOperation.setInventorySlotContents, removed, added );
2013-12-28 22:02:33 +01:00
}
}
@Override
public String getName()
2013-12-28 22:02:33 +01:00
{
return "appeng-internal";
2013-12-28 22:02:33 +01:00
}
@Override
2015-06-16 02:44:59 +02:00
public boolean hasCustomName()
2013-12-28 22:02:33 +01:00
{
return false;
2013-12-28 22:02:33 +01:00
}
@Override
public int getInventoryStackLimit()
2013-12-28 22:02:33 +01:00
{
return this.maxStack > 64 ? 64 : this.maxStack;
2013-12-28 22:02:33 +01:00
}
@Override
public void markDirty()
2013-12-28 22:02:33 +01:00
{
if( this.te != null && Platform.isServer() )
2013-12-28 22:02:33 +01:00
{
this.te.onChangeInventory( this, -1, InvOperation.markDirty, null, null );
2013-12-28 22:02:33 +01:00
}
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isUseableByPlayer( final EntityPlayer var1 )
2013-12-28 22:02:33 +01:00
{
return true;
2013-12-28 22:02:33 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isItemValidForSlot( final int i, final ItemStack itemstack )
2013-12-28 22:02:33 +01:00
{
2015-06-16 02:44:59 +02:00
return true;
2013-12-28 22:02:33 +01:00
}
@Override
2015-06-16 02:44:59 +02:00
public Iterator<ItemStack> iterator()
{
return new InvIterator( this );
}
public Iterator<IAEItemStack> getNewAEIterator()
2013-12-28 22:02:33 +01:00
{
2015-06-16 02:44:59 +02:00
return new AEInvIterator( this );
2013-12-28 22:02:33 +01:00
}
@Override
2015-06-16 02:44:59 +02:00
public IChatComponent getDisplayName()
2013-12-28 22:02:33 +01:00
{
2015-06-16 02:44:59 +02:00
return null;
2013-12-28 22:02:33 +01:00
}
@Override
public void openInventory( final EntityPlayer player )
2013-12-28 22:02:33 +01:00
{
2015-12-24 02:03:16 +01:00
2013-12-28 22:02:33 +01:00
}
2014-01-01 10:04:54 +01:00
2015-06-16 02:44:59 +02:00
@Override
public void closeInventory( final EntityPlayer player )
2014-01-01 10:04:54 +01:00
{
2015-12-24 02:03:16 +01:00
2015-06-16 02:44:59 +02:00
}
@Override
public int getField( final int id )
2015-06-16 02:44:59 +02:00
{
return 0;
}
@Override
public void setField( final int id, final int value )
2015-06-16 02:44:59 +02:00
{
2015-12-24 02:03:16 +01:00
2015-06-16 02:44:59 +02:00
}
@Override
public int getFieldCount()
{
return 0;
}
@Override
public void clear()
{
for( int x = 0; x < this.size; x++ )
{
2015-09-30 14:26:54 +02:00
this.setInventorySlotContents( x, null );
2015-06-16 02:44:59 +02:00
}
2014-01-01 10:04:54 +01:00
}
2013-12-28 22:02:33 +01:00
}