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

324 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>.
*/
2014-09-24 02:26:27 +02:00
package appeng.tile.inventory;
2014-09-24 02:26:27 +02: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;
2014-09-24 02:26:27 +02:00
import appeng.api.storage.IMEInventory;
import appeng.core.AELog;
import appeng.me.storage.MEIInventoryWrapper;
import appeng.util.Platform;
import appeng.util.iterators.InvIterator;
2014-09-24 02:26:27 +02:00
public class AppEngInternalInventory implements IInventory, Iterable<ItemStack>
{
protected final int size;
protected final ItemStack[] inv;
public boolean enableClientEvents = false;
protected IAEAppEngInventory te;
2014-09-24 02:26:27 +02:00
protected int maxStack;
public AppEngInternalInventory( IAEAppEngInventory inventory, int size )
{
this.te = inventory;
this.size = size;
this.maxStack = 64;
this.inv = new ItemStack[size];
}
2014-09-24 02:26:27 +02:00
public IMEInventory getMEInventory()
2014-09-24 02:26:27 +02:00
{
return new MEIInventoryWrapper( this, null );
}
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
{
2014-09-24 02:26:27 +02:00
return false;
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
return true;
}
@Override
public int getSizeInventory()
2014-09-24 02:26:27 +02:00
{
return this.size;
2014-09-24 02:26:27 +02:00
}
@Override
public ItemStack getStackInSlot( int var1 )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.inv[var1];
2014-09-24 02:26:27 +02:00
}
@Override
public ItemStack decrStackSize( int slot, int qty )
2014-09-24 02:26:27 +02:00
{
if( this.inv[slot] != null )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
ItemStack split = this.getStackInSlot( slot );
2014-09-24 02:26:27 +02:00
ItemStack ns = null;
if( qty >= split.stackSize )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
ns = this.inv[slot];
this.inv[slot] = null;
2014-09-24 02:26:27 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
ns = split.splitStack( qty );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( this.te != null && this.eventsEnabled() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.te.onChangeInventory( this, slot, InvOperation.decreaseStackSize, ns, null );
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
this.markDirty();
2014-09-24 02:26:27 +02:00
return ns;
}
return null;
}
protected boolean eventsEnabled()
{
return Platform.isServer() || this.enableClientEvents;
}
2014-09-24 02:26:27 +02:00
@Override
public ItemStack getStackInSlotOnClosing( int var1 )
2014-09-24 02:26:27 +02:00
{
return null;
}
@Override
public void setInventorySlotContents( int slot, ItemStack newItemStack )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
ItemStack oldStack = this.inv[slot];
this.inv[slot] = newItemStack;
2014-09-24 02:26:27 +02:00
if( this.te != null && this.eventsEnabled() )
2014-09-24 02:26:27 +02:00
{
ItemStack removed = oldStack;
ItemStack added = newItemStack;
if( oldStack != null && newItemStack != null && Platform.isSameItem( oldStack, newItemStack ) )
2014-09-24 02:26:27 +02:00
{
if( oldStack.stackSize > newItemStack.stackSize )
2014-09-24 02:26:27 +02:00
{
removed = removed.copy();
removed.stackSize -= newItemStack.stackSize;
added = null;
}
else if( oldStack.stackSize < newItemStack.stackSize )
2014-09-24 02:26:27 +02: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 );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.markDirty();
2014-09-24 02:26:27 +02:00
}
}
@Override
2015-09-29 15:47:55 +02:00
public String getCommandSenderName()
2014-09-24 02:26:27 +02:00
{
return "appeng-internal";
2014-09-24 02:26:27 +02:00
}
@Override
2015-06-16 02:44:59 +02:00
public boolean hasCustomName()
2014-09-24 02:26:27 +02:00
{
return false;
2014-09-24 02:26:27 +02:00
}
@Override
public int getInventoryStackLimit()
{
2014-12-29 15:13:47 +01:00
return this.maxStack > 64 ? 64 : this.maxStack;
2014-09-24 02:26:27 +02:00
}
@Override
public void markDirty()
{
if( this.te != null && this.eventsEnabled() )
{
this.te.onChangeInventory( this, -1, InvOperation.markDirty, null, null );
}
}
@Override
public boolean isUseableByPlayer( EntityPlayer var1 )
2014-09-24 02:26:27 +02:00
{
return true;
}
@Override
public boolean isItemValidForSlot( int i, ItemStack itemstack )
2014-09-24 02:26:27 +02:00
{
return true;
2014-09-24 02:26:27 +02:00
}
public void setMaxStackSize( int s )
2014-09-24 02:26:27 +02:00
{
this.maxStack = s;
}
// for guis...
public void markDirty( int slotIndex )
{
if( this.te != null && this.eventsEnabled() )
{
this.te.onChangeInventory( this, slotIndex, InvOperation.markDirty, null, null );
}
}
public void writeToNBT( NBTTagCompound data, String name )
{
NBTTagCompound c = new NBTTagCompound();
this.writeToNBT( c );
data.setTag( name, c );
}
public void writeToNBT( NBTTagCompound target )
{
for( int x = 0; x < this.size; x++ )
2014-09-24 02:26:27 +02:00
{
try
{
NBTTagCompound c = new NBTTagCompound();
if( this.inv[x] != null )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.inv[x].writeToNBT( c );
2014-09-24 02:26:27 +02:00
}
target.setTag( "#" + x, c );
}
catch( Exception ignored )
2014-09-24 02:26:27 +02:00
{
}
}
}
public void readFromNBT( NBTTagCompound data, String name )
2014-09-24 02:26:27 +02:00
{
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
}
}
public void readFromNBT( NBTTagCompound target )
{
for( int x = 0; x < this.size; x++ )
2014-09-24 02:26:27 +02:00
{
try
{
NBTTagCompound c = target.getCompoundTag( "#" + x );
if( c != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.inv[x] = ItemStack.loadItemStackFromNBT( c );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
catch( Exception e )
2014-09-24 02:26:27 +02:00
{
AELog.error( e );
}
}
}
@Override
public Iterator<ItemStack> iterator()
{
return new InvIterator( this );
}
2015-06-16 02:44:59 +02:00
@Override
public IChatComponent getDisplayName()
{
return null;
}
@Override
public void openInventory(
EntityPlayer player )
{
}
@Override
public void closeInventory(
EntityPlayer player )
{
}
@Override
public int getField(
int id )
{
return 0;
}
@Override
public void setField(
int id,
int value )
{
}
@Override
public int getFieldCount()
{
return 0;
}
@Override
public void clear()
{
for( int x = 0; x < this.size; x++ )
{
setInventorySlotContents( x,null );
}
}
2014-09-24 02:26:27 +02:00
}