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;
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
|
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;
|
2017-04-12 16:10:13 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class AppEngInternalInventory implements IInventory, Iterable<ItemStack>
|
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final int size;
|
|
|
|
private final ItemStack[] inv;
|
|
|
|
private boolean enableClientEvents = false;
|
|
|
|
private IAEAppEngInventory te;
|
|
|
|
private int maxStack;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public AppEngInternalInventory( final IAEAppEngInventory inventory, final int size )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
this.setTileEntity( inventory );
|
2015-01-03 02:53:14 +01:00
|
|
|
this.size = size;
|
|
|
|
this.maxStack = 64;
|
|
|
|
this.inv = new ItemStack[size];
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-09-28 11:47:17 +02:00
|
|
|
public IMEInventory getMEInventory()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
return new MEIInventoryWrapper( this, null );
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEmpty()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < this.size; x++ )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +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;
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
@Override
|
|
|
|
public int getSizeInventory()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
return this.size;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public ItemStack getStackInSlot( final 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
|
2015-09-25 23:10:56 +02:00
|
|
|
public ItemStack decrStackSize( final int slot, final int qty )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.inv[slot] != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final ItemStack split = this.getStackInSlot( slot );
|
2014-09-24 02:26:27 +02:00
|
|
|
ItemStack ns = null;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
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
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
if( this.getTileEntity() != null && this.eventsEnabled() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
this.getTileEntity().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;
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
protected boolean eventsEnabled()
|
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
return Platform.isServer() || this.isEnableClientEvents();
|
2015-01-03 02:53:14 +01:00
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public ItemStack getStackInSlotOnClosing( final int var1 )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void setInventorySlotContents( final int slot, final ItemStack newItemStack )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final ItemStack oldStack = this.inv[slot];
|
2014-12-29 15:13:47 +01:00
|
|
|
this.inv[slot] = newItemStack;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
if( this.getTileEntity() != null && this.eventsEnabled() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
ItemStack removed = oldStack;
|
|
|
|
ItemStack added = newItemStack;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( oldStack != null && newItemStack != null && Platform.isSameItem( oldStack, newItemStack ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( oldStack.stackSize > newItemStack.stackSize )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
removed = removed.copy();
|
|
|
|
removed.stackSize -= newItemStack.stackSize;
|
|
|
|
added = null;
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
this.getTileEntity().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-01-03 02:53:14 +01:00
|
|
|
public String getInventoryName()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
return "appeng-internal";
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasCustomInventoryName()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01: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
|
2015-01-03 02:53:14 +01:00
|
|
|
public void markDirty()
|
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
if( this.getTileEntity() != null && this.eventsEnabled() )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
this.getTileEntity().onChangeInventory( this, -1, InvOperation.markDirty, null, null );
|
2015-01-03 02:53:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean isUseableByPlayer( final EntityPlayer var1 )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
@Override
|
|
|
|
public void openInventory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
@Override
|
|
|
|
public void closeInventory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean isItemValidForSlot( final int i, final ItemStack itemstack )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
return true;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void setMaxStackSize( final int s )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
this.maxStack = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for guis...
|
2015-09-25 23:10:56 +02:00
|
|
|
public void markDirty( final int slotIndex )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
if( this.getTileEntity() != null && this.eventsEnabled() )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
this.getTileEntity().onChangeInventory( this, slotIndex, InvOperation.markDirty, null, null );
|
2015-01-03 02:53:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void writeToNBT( final NBTTagCompound data, final String name )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final NBTTagCompound c = new NBTTagCompound();
|
2015-01-03 02:53:14 +01:00
|
|
|
this.writeToNBT( c );
|
|
|
|
data.setTag( name, c );
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private void writeToNBT( final NBTTagCompound target )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < this.size; x++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final NBTTagCompound c = new NBTTagCompound();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
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 );
|
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final Exception ignored )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void readFromNBT( final NBTTagCompound data, final String name )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final NBTTagCompound c = data.getCompoundTag( name );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( c != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
this.readFromNBT( c );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-01-03 02:53:14 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void readFromNBT( final NBTTagCompound target )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < this.size; x++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final NBTTagCompound c = target.getCompoundTag( "#" + x );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
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
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final Exception e )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-11-22 15:54:29 +01:00
|
|
|
AELog.debug( e );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<ItemStack> iterator()
|
|
|
|
{
|
|
|
|
return new InvIterator( this );
|
|
|
|
}
|
2015-10-08 15:42:42 +02:00
|
|
|
|
|
|
|
private boolean isEnableClientEvents()
|
|
|
|
{
|
|
|
|
return this.enableClientEvents;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEnableClientEvents( final boolean enableClientEvents )
|
|
|
|
{
|
|
|
|
this.enableClientEvents = enableClientEvents;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IAEAppEngInventory getTileEntity()
|
|
|
|
{
|
|
|
|
return this.te;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTileEntity( final IAEAppEngInventory te )
|
|
|
|
{
|
|
|
|
this.te = te;
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|