Posted July 2, 201411 yr Can someone just look over and error check my inventory utils class (feel free to use it yourself)? Thanks for any feed back! package Technomage3.Core.Utils; import java.util.Iterator; import Technomage3.Core.Utils.InventoryUtils.Inventory; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; public class InventoryUtils { public static class Inventory implements Iterable<ItemStack> { private IInventory inv; public Inventory(IInventory i){ inv = i; } @Override public Iterator<ItemStack> iterator() { return new InventoryIterator(); } public InventoryIterator inventoryIterator() { return new InventoryIterator(); } public boolean containsStack(ItemStack stack){ for(ItemStack s : this){ if(ItemStack.areItemStacksEqual(stack, s)) return true; } return false; } public boolean containsAll(Iterable<ItemStack> inv){ for(ItemStack s : inv){ if(!this.contains(s)) return false; } return true; } public boolean removeStack(ItemStack s){ if(!contains(s)) return false; InventoryIterator itr = this.inventoryIterator(); while(itr.hasNext()){ if(itr.get().isItemEqual(s)){ itr.remove(); return true; } itr.next(); } return true; } /** * * @param stack2 the stack to try to remove * @return the amount that was actually removed */ public ItemStack removeByItem(ItemStack stack2){ ItemStack toTake = stack2.copy(); for(ItemStack s : this){ int b = toTake.stackSize; toTake = takeStack(toTake, s); } return new ItemStack(toTake.itemID, toTake.getItemDamage(), stack2.stackSize - toTake.stackSize); } public boolean canRemoveAll(ItemStack stack2) { ItemStack stack = stack2.copy(); for(ItemStack s : this){ stack = takeStackNoMod(stack, s); if(stack.stackSize <= 0 || stack == null) break; } return stack.stackSize <= 0 || stack == null; } /** * removes either the entire stack or nothing from the inventory * @param stack * @return whether the stack was added */ public boolean removeIffCan(ItemStack stack){ if(canRemoveAll(stack)){ removeByItem(stack); return true; } else return false; } public boolean canAdd(ItemStack stack2) { ItemStack stack = stack2.copy(); InventoryIterator itr = this.inventoryIterator(); while(itr.hasNext()){ ItemStack i = itr.get(); if(i == null){ return true; } else stack = mergeStacksNoMod(stack, i); if(stack == null || stack.stackSize == 0) return true; itr.next(); } return stack == null || stack.stackSize == 0; } public ItemStack add(ItemStack stack){ InventoryIterator itr = this.inventoryIterator(); while(itr.hasNext()){ ItemStack i = itr.get(); if(i == null){ itr.set(stack); return null; } else stack = mergeStacks(stack, i); itr.set(i); if(stack == null || stack.stackSize == 0) return stack; itr.next(); } return stack; } /** * adds either the entire stack or nothing into the inventory * @param stack * @return whether the stack was added */ public boolean addIffCan(ItemStack stack){ if(canAdd(stack)){ add(stack); return true; } else return false; } public boolean contains(ItemStack stack){ ItemStack s = stack.copy(); for(ItemStack i : this){ if(areStacksCompatible(i, s)){ s.stackSize -= i.stackSize; } } return s.stackSize <= 0; } /** * * @param s the stack to search for * @return the amount of room for s's items in the inventory */ public int getRoom(ItemStack s){ int room = 0; for(ItemStack i : this){ if(i == null){ room += 64; } else if(areStacksCompatible(s, i)){ room += i.getMaxStackSize() - i.stackSize; } } return room; } /** * * @param s the stack to search for * @return the amount of s's items in the inventory */ public int getAmount(ItemStack s){ int have = 0; for(ItemStack i : this){ if(i != null && areStacksCompatible(s, i)){ have += i.stackSize; } } return have; } public class InventoryIterator implements Iterator<ItemStack> { private int pos; @Override public boolean hasNext() { return pos < inv.getSizeInventory() - 1; } @Override public ItemStack next() { return inv.getStackInSlot(++pos); } public ItemStack previous() { return inv.getStackInSlot(--pos); } @Override public void remove() { inv.setInventorySlotContents(pos, null); } public void set(ItemStack stack){ inv.setInventorySlotContents(pos, stack); } public ItemStack get(){ return inv.getStackInSlot(pos); } public int getSlot(){ return pos; } } } public static Inventory getInventory(IInventory i){ return new Inventory(i); } /** * * Merges s2 into s1, and returns the extra of s2 * * @param s1 * @param s2 * @return the extra of s2 */ public static ItemStack mergeStacks(ItemStack s1, ItemStack s2) { if(areStacksCompatible(s1, s2)) return s2; int room = Math.min(s1.getMaxStackSize() - s1.stackSize, s2.stackSize); if(room <= 0) return s2; s1.stackSize += room; s2.stackSize -= room; if(s2.stackSize == 0) return null; else return s2; } /** * * Simulates mergeing s2 into s1, and returns the extra of s2 * * @param s1 * @param s2 * @return the extra of s2 */ private static ItemStack mergeStacksNoMod(ItemStack s1, ItemStack s2) { if(areStacksCompatible(s1, s2)) return s2; int room = Math.min(s1.getMaxStackSize() - s1.stackSize, s2.stackSize); if(room <= 0) return s2; s2.stackSize -= room; if(s2.stackSize == 0) return null; else return s2; } /** * * @param toTake the goal amount to take * @param from the stack to take from * @return what still has to be taken (the remainder of toTake) */ public static ItemStack takeStack(ItemStack toTake, ItemStack from){ if(!areStacksCompatible(toTake, from)) return toTake; int take = Math.min(from.stackSize, toTake.getMaxStackSize() - toTake.stackSize); toTake.stackSize -= take; from.stackSize -= take; return toTake; } /** * * @param toTake the goal amount to take * @param from the stack to take from * @return what still has to be taken (the remainder of toTake) */ private static ItemStack takeStackNoMod(ItemStack toTake, ItemStack from){ if(!areStacksCompatible(toTake, from)) return toTake; int take = Math.min(from.stackSize, toTake.getMaxStackSize() - toTake.stackSize); toTake.stackSize -= take; return toTake; } public static boolean areStacksCompatible(ItemStack s1, ItemStack s2){ return s1.itemID == s2.itemID && s1.getItemDamage() == s2.getItemDamage(); } /** * * transfers as much as possible of goal from pullFrom into pushTo * * @param pushTo the inventory to push items to * @param stack the stack to try to transfer * @param pullFrom the inventory to take items from */ public static void transferStack(Inventory pushTo, ItemStack goal, Inventory pullFrom) { int toTransfer = Math.min(goal.stackSize, Math.min(pushTo.getRoom(goal), pullFrom.getAmount(goal))); pushTo.add(pullFrom.removeByItem(new ItemStack(goal.itemID, goal.getItemDamage(), toTransfer))); } }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.