Use IItemHandler instead of IInventory internally (#2971)

* Use IItemHandler instead of IInventory internally
This commit is contained in:
fscan
2017-07-28 22:04:32 +02:00
committed by yueh
parent c077840988
commit 52d28fabe3
156 changed files with 2410 additions and 4604 deletions
@@ -21,24 +21,26 @@ package appeng.tile.inventory;
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.ItemHandlerHelper;
import appeng.api.AEApi;
import appeng.api.storage.data.IAEItemStack;
import appeng.core.AELog;
import appeng.util.Platform;
import appeng.util.inv.IAEAppEngInventory;
import appeng.util.inv.IInternalItemHandler;
import appeng.util.inv.InvOperation;
import appeng.util.item.AEItemStack;
import appeng.util.iterators.AEInvIterator;
import appeng.util.iterators.InvIterator;
public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack>
public class AppEngInternalAEInventory implements IInternalItemHandler, Iterable<ItemStack>
{
private final IAEAppEngInventory te;
private final IAEItemStack[] inv;
private final int size;
@@ -52,19 +54,6 @@ public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack
this.inv = new IAEItemStack[s];
}
@Override
public boolean isEmpty()
{
for( int x = 0; x < this.size; x++ )
{
if( !this.getStackInSlot( x ).isEmpty() )
{
return false;
}
}
return true;
}
public void setMaxStackSize( final int s )
{
this.maxStack = s;
@@ -132,8 +121,13 @@ public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack
}
}
protected int getStackLimit( int slot, @Nonnull ItemStack stack )
{
return Math.min( getSlotLimit( slot ), stack.getMaxStackSize() );
}
@Override
public int getSizeInventory()
public int getSlots()
{
return this.size;
}
@@ -150,42 +144,79 @@ public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack
}
@Override
public ItemStack decrStackSize( final int slot, final int qty )
public ItemStack insertItem( int slot, ItemStack stack, boolean simulate )
{
if( stack.isEmpty() )
{
return ItemStack.EMPTY;
}
ItemStack existing = getStackInSlot( slot );
int limit = getStackLimit( slot, stack );
if( !existing.isEmpty() )
{
if( !ItemHandlerHelper.canItemStacksStack( stack, existing ) )
{
return stack;
}
limit -= existing.getCount();
}
if( limit <= 0 )
{
return stack;
}
boolean reachedLimit = stack.getCount() > limit;
if( !simulate )
{
if( existing.isEmpty() )
{
this.inv[slot] = AEApi.instance().storage().createItemStack( reachedLimit ? ItemHandlerHelper.copyStackWithSize( stack, limit ) : stack );
}
else
{
existing.grow( reachedLimit ? limit : stack.getCount() );
}
fireOnChangeInventory( slot, InvOperation.INSERT, ItemStack.EMPTY, reachedLimit ? ItemHandlerHelper.copyStackWithSize( stack, limit ) : stack );
}
return reachedLimit ? ItemHandlerHelper.copyStackWithSize( stack, stack.getCount() - limit ) : ItemStack.EMPTY;
}
@Override
public ItemStack extractItem( int slot, int amount, boolean simulate )
{
if( this.inv[slot] != null )
{
final ItemStack split = this.getStackInSlot( slot );
ItemStack ns = ItemStack.EMPTY;
if( qty >= split.getCount() )
if( amount >= split.getCount() )
{
ns = this.getStackInSlot( slot );
this.inv[slot] = null;
if( !simulate )
{
this.inv[slot] = null;
fireOnChangeInventory( slot, InvOperation.EXTRACT, split, ItemStack.EMPTY );
}
return split;
}
else
{
ns = split.splitStack( qty );
if( !simulate )
{
split.grow( -amount );
fireOnChangeInventory( slot, InvOperation.EXTRACT, ItemHandlerHelper.copyStackWithSize( split, amount ), ItemStack.EMPTY );
}
return ItemHandlerHelper.copyStackWithSize( split, amount );
}
if( this.te != null && Platform.isServer() )
{
this.te.onChangeInventory( this, slot, InvOperation.decreaseStackSize, ns, ItemStack.EMPTY );
}
return ns;
}
return ItemStack.EMPTY;
}
@Override
public ItemStack removeStackFromSlot( final int var1 )
{
return ItemStack.EMPTY;
}
@Override
public void setInventorySlotContents( final int slot, final ItemStack newItemStack )
public void setStackInSlot( final int slot, final ItemStack newItemStack )
{
final ItemStack oldStack = this.getStackInSlot( slot );
this.inv[slot] = AEApi.instance().storage().createItemStack( newItemStack );
@@ -214,48 +245,22 @@ public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack
removed = added = ItemStack.EMPTY;
}
}
this.te.onChangeInventory( this, slot, InvOperation.setInventorySlotContents, removed, added );
fireOnChangeInventory( slot, InvOperation.SET, removed, added );
}
}
@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()
private void fireOnChangeInventory( int slot, InvOperation op, ItemStack removed, ItemStack inserted )
{
if( this.te != null && Platform.isServer() )
{
this.te.onChangeInventory( this, -1, InvOperation.markDirty, ItemStack.EMPTY, ItemStack.EMPTY );
this.te.onChangeInventory( this, slot, op, removed, inserted );
}
}
@Override
public boolean isUsableByPlayer( final EntityPlayer var1 )
public int getSlotLimit( int slot )
{
return true;
}
@Override
public boolean isItemValidForSlot( final int i, final ItemStack itemstack )
{
return true;
return this.maxStack > 64 ? 64 : this.maxStack;
}
@Override
@@ -270,47 +275,14 @@ public class AppEngInternalAEInventory implements IInventory, Iterable<ItemStack
}
@Override
public ITextComponent getDisplayName()
public boolean isItemValidForSlot( int slot, ItemStack stack )
{
return null;
return true;
}
@Override
public void openInventory( final EntityPlayer player )
public void markDirty( int slot )
{
}
@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 );
}
fireOnChangeInventory( slot, InvOperation.DIRTY, ItemStack.EMPTY, ItemStack.EMPTY );
}
}
@@ -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()
@@ -1,153 +0,0 @@
/*
* 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>.
*/
package appeng.tile.inventory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.text.ITextComponent;
public class AppEngNullInventory implements IInventory
{
public AppEngNullInventory()
{
}
public void writeToNBT( final NBTTagCompound target )
{
}
@Override
public int getSizeInventory()
{
return 0;
}
@Override
public ItemStack getStackInSlot( final int var1 )
{
return ItemStack.EMPTY;
}
@Override
public ItemStack decrStackSize( final int slot, final int qty )
{
return ItemStack.EMPTY;
}
@Override
public ItemStack removeStackFromSlot( final int var1 )
{
return ItemStack.EMPTY;
}
@Override
public void setInventorySlotContents( final int slot, final ItemStack newItemStack )
{
}
@Override
public String getName()
{
return "appeng-internal";
}
@Override
public boolean hasCustomName()
{
return false;
}
@Override
public int getInventoryStackLimit()
{
return 0;
}
@Override
public void markDirty()
{
}
@Override
public boolean isUsableByPlayer( final EntityPlayer var1 )
{
return false;
}
@Override
public boolean isItemValidForSlot( final int i, final ItemStack itemstack )
{
return false;
}
@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()
{
}
@Override
public boolean isEmpty()
{
return true;
}
}
@@ -1,32 +0,0 @@
/*
* 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>.
*/
package appeng.tile.inventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
public interface IAEAppEngInventory
{
void saveChanges();
void onChangeInventory( IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack );
}
@@ -1,26 +0,0 @@
/*
* 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>.
*/
package appeng.tile.inventory;
public enum InvOperation
{
decreaseStackSize, setInventorySlotContents, markDirty
}