diff --git a/codechicken/lib/inventory/InventoryCopy.java b/codechicken/lib/inventory/InventoryCopy.java index c36ba0a..8bcfdd5 100644 --- a/codechicken/lib/inventory/InventoryCopy.java +++ b/codechicken/lib/inventory/InventoryCopy.java @@ -4,6 +4,9 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; +/** + * Creates a copy of an IInventory for extended simulation + */ public class InventoryCopy implements IInventory { public boolean[] accessible; @@ -33,11 +36,11 @@ public class InventoryCopy implements IInventory int lslot = access.lastSlot(); if(lslot > accessible.length) { - boolean[] l_accessable = new boolean[lslot]; + boolean[] l_accessible = new boolean[lslot]; ItemStack[] l_items = new ItemStack[lslot]; - System.arraycopy(accessible, 0, l_accessable, 0, accessible.length); + System.arraycopy(accessible, 0, l_accessible, 0, accessible.length); System.arraycopy(items, 0, l_items, 0, items.length); - accessible = l_accessable; + accessible = l_accessible; items = l_items; } diff --git a/codechicken/lib/inventory/InventoryNBT.java b/codechicken/lib/inventory/InventoryNBT.java index e3da78a..a6e338c 100644 --- a/codechicken/lib/inventory/InventoryNBT.java +++ b/codechicken/lib/inventory/InventoryNBT.java @@ -5,6 +5,9 @@ import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +/** + * IInventory implementation which saves and loads from an NBT tag + */ public class InventoryNBT implements IInventory { protected ItemStack[] items; diff --git a/codechicken/lib/inventory/InventoryRange.java b/codechicken/lib/inventory/InventoryRange.java index 6efc0bd..f3d8c2d 100644 --- a/codechicken/lib/inventory/InventoryRange.java +++ b/codechicken/lib/inventory/InventoryRange.java @@ -4,6 +4,9 @@ import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; +/** + * Inventory wrapper for unified ISided/IInventory access + */ public class InventoryRange { public IInventory inv; diff --git a/codechicken/lib/inventory/InventorySimple.java b/codechicken/lib/inventory/InventorySimple.java index 90a24a1..12b1c8e 100644 --- a/codechicken/lib/inventory/InventorySimple.java +++ b/codechicken/lib/inventory/InventorySimple.java @@ -4,6 +4,9 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; +/** + * Simple IInventory implementation with an array of items, name and maximum stack size + */ public class InventorySimple implements IInventory { public ItemStack[] items; diff --git a/codechicken/lib/inventory/InventoryUtils.java b/codechicken/lib/inventory/InventoryUtils.java index 64bb560..921b3df 100644 --- a/codechicken/lib/inventory/InventoryUtils.java +++ b/codechicken/lib/inventory/InventoryUtils.java @@ -21,6 +21,9 @@ import net.minecraftforge.common.ForgeDirection; public class InventoryUtils { + /** + * Static default implementation for IInventory method + */ public static ItemStack decrStackSize(IInventory inv, int slot, int size) { ItemStack item = inv.getStackInSlot(slot); @@ -45,6 +48,9 @@ public class InventoryUtils return null; } + /** + * Static default implementation for IInventory method + */ public static ItemStack getStackInSlotOnClosing(IInventory inv, int slot) { ItemStack stack = inv.getStackInSlot(slot); @@ -53,9 +59,6 @@ public class InventoryUtils } /** - * - * @param base - * @param addition * @return The quantity of items from addition that can be added to base */ public static int incrStackSize(ItemStack base, ItemStack addition) @@ -67,9 +70,6 @@ public class InventoryUtils } /** - * - * @param base - * @param addition * @return The quantity of items from addition that can be added to base */ public static int incrStackSize(ItemStack base, int addition) @@ -84,11 +84,17 @@ public class InventoryUtils return 0; } + /** + * NBT item saving function + */ public static NBTTagList writeItemStacksToTag(ItemStack[] items) { return writeItemStacksToTag(items, 64); } - + + /** + * NBT item saving function with support for stack sizes > 32K + */ public static NBTTagList writeItemStacksToTag(ItemStack[] items, int maxQuantity) { NBTTagList tagList = new NBTTagList(); @@ -110,7 +116,10 @@ public class InventoryUtils } return tagList; } - + + /** + * NBT item loading function with support for stack sizes > 32K + */ public static void readItemStacksFromTag(ItemStack[] items, NBTTagList tagList) { for(int i = 0; i < tagList.tagCount(); i++) @@ -129,6 +138,9 @@ public class InventoryUtils } } + /** + * Spawns an itemstack in the world at a location + */ public static void dropItem(ItemStack stack, World world, Vector3 dropLocation) { EntityItem item = new EntityItem(world, dropLocation.x, dropLocation.y, dropLocation.z, stack); @@ -138,6 +150,9 @@ public class InventoryUtils world.spawnEntityInWorld(item); } + /** + * Copies an itemstack with a new quantity + */ public static ItemStack copyStack(ItemStack stack, int quantity) { if(stack == null) @@ -147,8 +162,11 @@ public class InventoryUtils stack.stackSize = quantity; return stack; } - - public static int getInsertableQuantity(InventoryRange inv, ItemStack stack) + + /** + * Gets the maximum quantity of an item that can be inserted into inv + */ + public static int getInsertibleQuantity(InventoryRange inv, ItemStack stack) { int quantity = 0; stack = copyStack(stack, Integer.MAX_VALUE); @@ -157,6 +175,11 @@ public class InventoryUtils return quantity; } + + public static int getInsertibleQuantity(IInventory inv, ItemStack stack) + { + return getInsertibleQuantity(new InventoryRange(inv), stack); + } public static int fitStackInSlot(InventoryRange inv, int slot, ItemStack stack) { @@ -168,11 +191,17 @@ public class InventoryUtils return Math.min(fit, stack.stackSize); } - public static boolean mergeItemStack(InventoryRange inv, ItemStack stack, boolean doMerge) + public static int fitStackInSlot(IInventory inv, int slot, ItemStack stack) { - if(doMerge && !mergeItemStack(inv, stack, false)) - return false; + return fitStackInSlot(new InventoryRange(inv), slot, stack); + } + /** + * @param simulate. If set to true, no items will actually be inserted + * @return The number of items unable to be inserted + */ + public static int insertItem(InventoryRange inv, ItemStack stack, boolean simulate) + { stack = stack.copy(); for(int pass = 0; pass < 2; pass++) { @@ -186,7 +215,7 @@ public class InventoryUtils if(base != null) { stack.stackSize-=fit; - if(doMerge) + if(!simulate) { base.stackSize+=fit; inv.inv.setInventorySlotContents(slot, base); @@ -194,27 +223,53 @@ public class InventoryUtils } else if(pass == 1) { - if(doMerge) + if(!simulate) inv.inv.setInventorySlotContents(slot, copyStack(stack, fit)); stack.stackSize-=fit; } if(stack.stackSize == 0) - return true; + return 0; } } - return false; + return stack.stackSize; + } + + public static int insertItem(IInventory inv, ItemStack stack, boolean simulate) + { + return insertItem(new InventoryRange(inv), stack, simulate); + } + + /** + * Gets the stack in slot if it can be extracted + */ + public static ItemStack getExtractableStack(InventoryRange inv, int slot) + { + ItemStack stack = inv.inv.getStackInSlot(slot); + if(stack == null || !inv.canExtractItem(slot, stack)) + return null; + + return stack; + } + + public static ItemStack getExtractableStack(IInventory inv, int slot) + { + return getExtractableStack(new InventoryRange(inv), slot); } public static boolean areStacksIdentical(ItemStack stack1, ItemStack stack2) { if(stack1 == null || stack2 == null) return stack1 == stack2; + return stack1.itemID == stack2.itemID && stack1.getItemDamage() == stack2.getItemDamage() && stack1.stackSize == stack2.stackSize && Objects.equal(stack1.getTagCompound(), stack2.getTagCompound()); } - + + /** + * Gets an IInventory from a coordinate with support for double chests + */ public static IInventory getInventory(World world, int x, int y, int z) { TileEntity tile = world.getBlockTileEntity(x, y, z); @@ -248,6 +303,9 @@ public class InventoryUtils stack1.isStackable(); } + /** + * Consumes one item from slot in inv with support for containers. + */ public static void consumeItem(IInventory inv, int slot) { ItemStack stack = inv.getStackInSlot(slot); @@ -263,21 +321,18 @@ public class InventoryUtils } } + /** + * Gets the size of the stack in a slot. Returns 0 on null stacks + */ public static int stackSize(IInventory inv, int slot) { ItemStack stack = inv.getStackInSlot(slot); return stack == null ? 0 : stack.stackSize; } - - public static ItemStack getRemovableStack(InventoryRange inv, int slot) - { - ItemStack stack = inv.inv.getStackInSlot(slot); - if(stack == null || !inv.canExtractItem(slot, stack)) - return null; - - return stack; - } + /** + * Drops all items from inv using getStackInSlotOnClosing + */ public static void dropOnClose(EntityPlayer player, IInventory inv) { for (int i = 0; i < inv.getSizeInventory(); i++) @@ -287,7 +342,10 @@ public class InventoryUtils player.dropPlayerItem(stack); } } - + + /** + * Gets the actual damage of an item without asking the Item + */ public static int actualDamage(ItemStack stack) { return Item.diamond.getDamage(stack); diff --git a/codechicken/lib/inventory/ItemKey.java b/codechicken/lib/inventory/ItemKey.java new file mode 100644 index 0000000..e52a923 --- /dev/null +++ b/codechicken/lib/inventory/ItemKey.java @@ -0,0 +1,65 @@ +package codechicken.lib.inventory; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +import com.google.common.base.Objects; +import static codechicken.lib.inventory.InventoryUtils.*; + +/** + * Comparable ItemStack with a hashCode implementation. + */ +public class ItemKey implements Comparable +{ + public ItemStack item; + private int hashcode = 0; + + public ItemKey(ItemStack k) + { + item = k; + } + + public ItemKey(int id, int damage) + { + this(new ItemStack(id, 1, damage)); + } + + public ItemKey(int id, int damage, NBTTagCompound compound) + { + this(id, damage); + item.setTagCompound(compound); + } + + @Override + public boolean equals(Object obj) + { + if(!(obj instanceof ItemKey)) + return false; + + ItemKey k = (ItemKey)obj; + return item.itemID == k.item.itemID && + actualDamage(item) == actualDamage(k.item) && + Objects.equal(item.stackTagCompound, k.item.stackTagCompound); + } + + @Override + public int hashCode() + { + return hashcode != 0 ? hashcode : (hashcode = Objects.hashCode(item.itemID, actualDamage(item), item.stackTagCompound)); + } + + public int compareInt(int a, int b) + { + return a == b ? 0 : a < b ? -1 : 1; + } + + @Override + public int compareTo(ItemKey o) + { + if(item.itemID != o.item.itemID) + return compareInt(item.itemID, o.item.itemID); + if(actualDamage(item) != actualDamage(o.item)) + return compareInt(actualDamage(item), actualDamage(o.item)); + return 0; + } +}