From cae20d72fc5a2089d12c8e93f540027f5d6f1ad3 Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Sat, 28 Dec 2013 15:02:33 -0600 Subject: [PATCH] New Inventory Helper stuff. --- tile/inventory/AppEngInternalAEInventory.java | 240 ++++++++++++++++++ util/inv/WrapperInvSlot.java | 100 ++++++++ util/iterators/InvIterator.java | 39 +++ 3 files changed, 379 insertions(+) create mode 100644 tile/inventory/AppEngInternalAEInventory.java create mode 100644 util/inv/WrapperInvSlot.java create mode 100644 util/iterators/InvIterator.java diff --git a/tile/inventory/AppEngInternalAEInventory.java b/tile/inventory/AppEngInternalAEInventory.java new file mode 100644 index 00000000..f3c231e8 --- /dev/null +++ b/tile/inventory/AppEngInternalAEInventory.java @@ -0,0 +1,240 @@ +package appeng.tile.inventory; + +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; +import appeng.api.AEApi; +import appeng.api.storage.data.IAEItemStack; +import appeng.util.Platform; +import appeng.util.item.AEItemStack; +import appeng.util.iterators.InvIterator; + +public class AppEngInternalAEInventory implements IInventory, Iterable +{ + + protected IAEAppEngInventory te; + int size; + int maxStack; + + protected IAEItemStack inv[]; + + public boolean isEmpty() + { + for (int x = 0; x < getSizeInventory(); x++) + if ( getStackInSlot( x ) != null ) + return false; + return true; + } + + public AppEngInternalAEInventory(IAEAppEngInventory _te, int s) { + te = _te; + size = s; + maxStack = 64; + inv = new IAEItemStack[s]; + } + + public void setMaxStackSize(int s) + { + maxStack = s; + } + + public IAEItemStack getAEStackInSlot(int var1) + { + return inv[var1]; + } + + @Override + public ItemStack getStackInSlot(int var1) + { + if ( inv[var1] == null ) + return null; + + return inv[var1].getItemStack(); + } + + @Override + public ItemStack decrStackSize(int slot, int qty) + { + if ( inv[slot] != null ) + { + ItemStack split = getStackInSlot( slot ); + ItemStack ns = null; + + if ( qty >= split.stackSize ) + { + ns = getStackInSlot( slot ); + inv[slot] = null; + } + else + ns = split.splitStack( qty ); + + if ( te != null && Platform.isServer() ) + { + te.onChangeInventory( this, slot, InvOperation.decrStackSize, ns, null ); + } + + return ns; + } + + return null; + } + + @Override + public ItemStack getStackInSlotOnClosing(int var1) + { + return null; + } + + @Override + public void setInventorySlotContents(int slot, ItemStack newItemStack) + { + ItemStack oldStack = getStackInSlot( slot ); + inv[slot] = AEApi.instance().storage().createItemStack( newItemStack ); + + if ( te != null && Platform.isServer() ) + { + ItemStack removed = oldStack; + ItemStack added = newItemStack; + + if ( oldStack != null && newItemStack != null && Platform.isSameItem( oldStack, newItemStack ) ) + { + if ( oldStack.stackSize > newItemStack.stackSize ) + { + removed = removed.copy(); + removed.stackSize -= newItemStack.stackSize; + added = null; + } + else if ( oldStack.stackSize < newItemStack.stackSize ) + { + added = added.copy(); + added.stackSize -= oldStack.stackSize; + removed = null; + } + else + { + removed = added = null; + } + } + + te.onChangeInventory( this, slot, InvOperation.setInventorySlotContents, removed, added ); + } + } + + @Override + public void onInventoryChanged() + { + if ( te != null && Platform.isServer() ) + { + te.onChangeInventory( this, -1, InvOperation.onInventoryChanged, null, null ); + } + } + + @Override + public int getInventoryStackLimit() + { + return maxStack > 64 ? 64 : maxStack; + } + + @Override + public boolean isUseableByPlayer(EntityPlayer var1) + { + return true; + } + + @Override + public void openChest() + { + } + + @Override + public void closeChest() + { + } + + public void writeToNBT(NBTTagCompound target) + { + for (int x = 0; x < size; x++) + { + try + { + NBTTagCompound c = new NBTTagCompound(); + + if ( inv[x] != null ) + { + inv[x].writeToNBT( c ); + } + + target.setCompoundTag( "#" + x, c ); + } + catch (Exception err) + { + } + } + } + + public void readFromNBT(NBTTagCompound target) + { + for (int x = 0; x < size; x++) + { + try + { + NBTTagCompound c = target.getCompoundTag( "#" + x ); + + if ( c != null ) + inv[x] = AEItemStack.loadItemStackFromNBT( c ); + + } + catch (Exception err) + { + err.printStackTrace(); + } + } + } + + public void writeToNBT(NBTTagCompound data, String name) + { + NBTTagCompound c = new NBTTagCompound(); + writeToNBT( c ); + data.setCompoundTag( name, c ); + } + + public void readFromNBT(NBTTagCompound data, String name) + { + NBTTagCompound c = data.getCompoundTag( name ); + if ( c != null ) + readFromNBT( c ); + } + + @Override + public int getSizeInventory() + { + return size; + } + + @Override + public String getInvName() + { + return "appeng-internal"; + } + + @Override + public boolean isInvNameLocalized() + { + return false; + } + + @Override + public boolean isItemValidForSlot(int i, ItemStack itemstack) + { + return true; + } + + @Override + public Iterator iterator() + { + return new InvIterator( this ); + } +} diff --git a/util/inv/WrapperInvSlot.java b/util/inv/WrapperInvSlot.java new file mode 100644 index 00000000..16b6ac34 --- /dev/null +++ b/util/inv/WrapperInvSlot.java @@ -0,0 +1,100 @@ +package appeng.util.inv; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +public class WrapperInvSlot implements IInventory +{ + + private final IInventory inv; + private int slot = 0; + + public WrapperInvSlot(IInventory inv) { + this.inv = inv; + } + + public void setSlot(int slot) + { + this.slot = slot; + } + + @Override + public int getSizeInventory() + { + return 1; + } + + @Override + public ItemStack getStackInSlot(int i) + { + return inv.getStackInSlot( slot ); + } + + @Override + public ItemStack decrStackSize(int i, int num) + { + return inv.decrStackSize( slot, num ); + } + + @Override + public ItemStack getStackInSlotOnClosing(int i) + { + return inv.getStackInSlotOnClosing( slot ); + } + + @Override + public void setInventorySlotContents(int i, ItemStack itemstack) + { + inv.setInventorySlotContents( slot, itemstack ); + } + + @Override + public String getInvName() + { + return inv.getInvName(); + } + + @Override + public boolean isInvNameLocalized() + { + return inv.isInvNameLocalized(); + } + + @Override + public int getInventoryStackLimit() + { + return inv.getInventoryStackLimit(); + } + + @Override + public void onInventoryChanged() + { + inv.onInventoryChanged(); + } + + @Override + public boolean isUseableByPlayer(EntityPlayer entityplayer) + { + return inv.isUseableByPlayer( entityplayer ); + } + + @Override + public void openChest() + { + inv.openChest(); + } + + @Override + public void closeChest() + { + inv.closeChest(); + } + + @Override + public boolean isItemValidForSlot(int i, ItemStack itemstack) + { + return inv.isItemValidForSlot( slot, itemstack ); + } + +} diff --git a/util/iterators/InvIterator.java b/util/iterators/InvIterator.java new file mode 100644 index 00000000..bbe7eb90 --- /dev/null +++ b/util/iterators/InvIterator.java @@ -0,0 +1,39 @@ +package appeng.util.iterators; + +import java.util.Iterator; + +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +public class InvIterator implements Iterator +{ + + final IInventory inv; + final int size; + + int x = 0; + + public InvIterator(IInventory i) { + inv = i; + size = inv.getSizeInventory(); + } + + @Override + public boolean hasNext() + { + return x < size; + } + + @Override + public ItemStack next() + { + return inv.getStackInSlot( x++ ); + } + + @Override + public void remove() + { + throw new RuntimeException( "no..." ); + } + +}