Use IItemHandler instead of IInventory internally (#2971)
* Use IItemHandler instead of IInventory internally
This commit is contained in:
@@ -19,88 +19,115 @@
|
||||
package appeng.tile.inventory;
|
||||
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
|
||||
import appeng.core.AELog;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.iterators.InvIterator;
|
||||
import appeng.util.inv.IAEAppEngInventory;
|
||||
import appeng.util.inv.IInternalItemHandler;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
|
||||
|
||||
public class AppEngInternalInventory implements IInventory, Iterable<ItemStack>
|
||||
public class AppEngInternalInventory extends ItemStackHandler implements IInternalItemHandler, Iterable<ItemStack>
|
||||
{
|
||||
private final int size;
|
||||
private final ItemStack[] inv;
|
||||
private boolean enableClientEvents = false;
|
||||
private IAEAppEngInventory te;
|
||||
private int maxStack;
|
||||
private InvOperation currentOp;
|
||||
private ItemStack previousStack = ItemStack.EMPTY;
|
||||
private IAEItemFilter filter;
|
||||
|
||||
public AppEngInternalInventory( final IAEAppEngInventory inventory, final int size, final int maxStack, IAEItemFilter filter )
|
||||
{
|
||||
super( size );
|
||||
this.setTileEntity( inventory );
|
||||
this.setFilter( filter );
|
||||
this.maxStack = maxStack;
|
||||
}
|
||||
|
||||
public AppEngInternalInventory( final IAEAppEngInventory inventory, final int size, final int maxStack )
|
||||
{
|
||||
this( inventory, size, maxStack, null );
|
||||
}
|
||||
|
||||
public AppEngInternalInventory( final IAEAppEngInventory inventory, final int size )
|
||||
{
|
||||
this.setTileEntity( inventory );
|
||||
this.size = size;
|
||||
this.maxStack = 64;
|
||||
this.inv = new ItemStack[size];
|
||||
this( inventory, size, 64 );
|
||||
}
|
||||
|
||||
public void setFilter( IAEItemFilter filter )
|
||||
{
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
public int getSlotLimit( int slot )
|
||||
{
|
||||
for( int x = 0; x < this.size; x++ )
|
||||
return maxStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot( int slot, @Nonnull ItemStack stack )
|
||||
{
|
||||
currentOp = InvOperation.SET;
|
||||
previousStack = getStackInSlot( slot );
|
||||
super.setStackInSlot( slot, stack );
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack insertItem( int slot, @Nonnull ItemStack stack, boolean simulate )
|
||||
{
|
||||
if( filter != null && !filter.allowInsert( this, slot, stack ) )
|
||||
{
|
||||
if( !this.getStackInSlot( x ).isEmpty() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSizeInventory()
|
||||
{
|
||||
return this.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot( final int var1 )
|
||||
{
|
||||
return this.inv[var1] == null ? ItemStack.EMPTY : this.inv[var1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize( final int slot, final int qty )
|
||||
{
|
||||
if( this.inv[slot] != null )
|
||||
{
|
||||
final ItemStack split = this.getStackInSlot( slot );
|
||||
ItemStack ns = ItemStack.EMPTY;
|
||||
|
||||
if( qty >= split.getCount() )
|
||||
{
|
||||
ns = this.inv[slot];
|
||||
this.inv[slot] = ItemStack.EMPTY;
|
||||
}
|
||||
else
|
||||
{
|
||||
ns = split.splitStack( qty );
|
||||
}
|
||||
|
||||
if( this.getTileEntity() != null && this.eventsEnabled() )
|
||||
{
|
||||
this.getTileEntity().onChangeInventory( this, slot, InvOperation.decreaseStackSize, ns, ItemStack.EMPTY );
|
||||
}
|
||||
|
||||
this.markDirty();
|
||||
return ns;
|
||||
return stack;
|
||||
}
|
||||
|
||||
return ItemStack.EMPTY;
|
||||
currentOp = InvOperation.INSERT;
|
||||
previousStack = getStackInSlot( slot ).copy();
|
||||
return super.insertItem( slot, stack, simulate );
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack extractItem( int slot, int amount, boolean simulate )
|
||||
{
|
||||
if( filter != null && !filter.allowExtract( this, slot, amount ) )
|
||||
{
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
currentOp = InvOperation.EXTRACT;
|
||||
previousStack = getStackInSlot( slot );
|
||||
return super.extractItem( slot, amount, simulate );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onContentsChanged( int slot )
|
||||
{
|
||||
if( this.getTileEntity() != null && this.eventsEnabled() )
|
||||
{
|
||||
ItemStack added = getStackInSlot( slot ).copy();
|
||||
ItemStack removed = previousStack.copy();
|
||||
|
||||
if( currentOp == InvOperation.INSERT )
|
||||
{
|
||||
added.grow( -removed.getCount() );
|
||||
}
|
||||
else if( currentOp == InvOperation.EXTRACT )
|
||||
{
|
||||
removed.grow( -added.getCount() );
|
||||
}
|
||||
this.getTileEntity().onChangeInventory( this, slot, currentOp, removed, added );
|
||||
}
|
||||
super.onContentsChanged( slot );
|
||||
}
|
||||
|
||||
protected boolean eventsEnabled()
|
||||
@@ -108,138 +135,36 @@ public class AppEngInternalInventory implements IInventory, Iterable<ItemStack>
|
||||
return Platform.isServer() || this.isEnableClientEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeStackFromSlot( final int var1 )
|
||||
{
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
private ItemStack getOldStack( int slot )
|
||||
{
|
||||
if( this.inv[slot] == null )
|
||||
{
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
return this.inv[slot];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents( final int slot, final ItemStack newItemStack )
|
||||
{
|
||||
final ItemStack oldStack = this.getOldStack( slot );
|
||||
this.inv[slot] = newItemStack;
|
||||
|
||||
if( this.getTileEntity() != null && this.eventsEnabled() )
|
||||
{
|
||||
ItemStack removed = oldStack;
|
||||
ItemStack added = newItemStack;
|
||||
|
||||
if( !oldStack.isEmpty() && !newItemStack.isEmpty() && Platform.itemComparisons().isEqualItem( oldStack, newItemStack ) )
|
||||
{
|
||||
if( oldStack.getCount() > newItemStack.getCount() )
|
||||
{
|
||||
removed = removed.copy();
|
||||
removed.grow( -newItemStack.getCount() );
|
||||
added = ItemStack.EMPTY;
|
||||
}
|
||||
else if( oldStack.getCount() < newItemStack.getCount() )
|
||||
{
|
||||
added = added.copy();
|
||||
added.grow( -oldStack.getCount() );
|
||||
removed = ItemStack.EMPTY;
|
||||
}
|
||||
else
|
||||
{
|
||||
removed = added = ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
this.getTileEntity().onChangeInventory( this, slot, InvOperation.setInventorySlotContents, removed, added );
|
||||
|
||||
this.markDirty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return "appeng-internal";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomName()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInventoryStackLimit()
|
||||
{
|
||||
return this.maxStack > 64 ? 64 : this.maxStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty()
|
||||
{
|
||||
if( this.getTileEntity() != null && this.eventsEnabled() )
|
||||
{
|
||||
this.getTileEntity().onChangeInventory( this, -1, InvOperation.markDirty, ItemStack.EMPTY, ItemStack.EMPTY );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUsableByPlayer( final EntityPlayer var1 )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot( final int i, final ItemStack itemstack )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setMaxStackSize( final int s )
|
||||
{
|
||||
this.maxStack = s;
|
||||
}
|
||||
|
||||
// for guis...
|
||||
public void markDirty( final int slotIndex )
|
||||
public void markDirty( final int slot )
|
||||
{
|
||||
if( this.getTileEntity() != null && this.eventsEnabled() )
|
||||
{
|
||||
this.getTileEntity().onChangeInventory( this, slotIndex, InvOperation.markDirty, ItemStack.EMPTY, ItemStack.EMPTY );
|
||||
this.getTileEntity().onChangeInventory( this, slot, InvOperation.DIRTY, ItemStack.EMPTY, ItemStack.EMPTY );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot( int slot, ItemStack stack )
|
||||
{
|
||||
if( this.maxStack == 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if( this.filter != null )
|
||||
{
|
||||
return filter.allowInsert( this, slot, stack );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void writeToNBT( final NBTTagCompound data, final String name )
|
||||
{
|
||||
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
|
||||
{
|
||||
final NBTTagCompound c = new NBTTagCompound();
|
||||
|
||||
if( this.inv[x] != null )
|
||||
{
|
||||
this.inv[x].writeToNBT( c );
|
||||
}
|
||||
|
||||
target.setTag( "#" + x, c );
|
||||
}
|
||||
catch( final Exception ignored )
|
||||
{
|
||||
}
|
||||
}
|
||||
data.setTag( name, serializeNBT() );
|
||||
}
|
||||
|
||||
public void readFromNBT( final NBTTagCompound data, final String name )
|
||||
@@ -247,78 +172,19 @@ public class AppEngInternalInventory implements IInventory, Iterable<ItemStack>
|
||||
final NBTTagCompound c = data.getCompoundTag( name );
|
||||
if( c != null )
|
||||
{
|
||||
this.readFromNBT( c );
|
||||
readFromNBT( c );
|
||||
}
|
||||
}
|
||||
|
||||
public void readFromNBT( final NBTTagCompound target )
|
||||
public void readFromNBT( final NBTTagCompound data )
|
||||
{
|
||||
for( int x = 0; x < this.size; x++ )
|
||||
{
|
||||
try
|
||||
{
|
||||
final NBTTagCompound c = target.getCompoundTag( "#" + x );
|
||||
|
||||
if( c != null )
|
||||
{
|
||||
this.inv[x] = new ItemStack( c );
|
||||
}
|
||||
}
|
||||
catch( final Exception e )
|
||||
{
|
||||
AELog.debug( e );
|
||||
}
|
||||
}
|
||||
deserializeNBT( data );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<ItemStack> iterator()
|
||||
{
|
||||
return new InvIterator( this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITextComponent getDisplayName()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openInventory( final EntityPlayer player )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeInventory( final EntityPlayer player )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getField( final int id )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setField( final int id, final int value )
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFieldCount()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
for( int x = 0; x < this.size; x++ )
|
||||
{
|
||||
this.setInventorySlotContents( x, ItemStack.EMPTY );
|
||||
}
|
||||
return Collections.unmodifiableList( super.stacks ).iterator();
|
||||
}
|
||||
|
||||
private boolean isEnableClientEvents()
|
||||
|
||||
Reference in New Issue
Block a user