package me.km.inventory; import net.minecraft.entity.passive.EntityVillager; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryBasic; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; public class InventoryUtils { public static InventoryBasic getInventory(EntityVillager v) { EntityInventory real = new EntityInventory("Villager", 9, v); InventoryBasic old = v.getVillagerInventory(); for(int i = 0; i < 9; i++) { real.setInventorySlotContents(i, old.getStackInSlot(i).copy()); } return real; } public static void setInventory(EntityInventory inv) { if(inv.getEntity() instanceof EntityVillager) { EntityVillager v = (EntityVillager) inv.getEntity(); InventoryBasic vInv = v.getVillagerInventory(); for(int i = 0; i < 9; i++) { vInv.setInventorySlotContents(i, inv.getStackInSlot(i).copy()); } } } public static boolean doItemStacksMatch(ItemStack first, ItemStack second) { int count = first.getCount(); first.setCount(second.getCount()); boolean b = ItemStack.areItemStacksEqual(first, second); first.setCount(count); return b; } public static int searchInventoryFor(IInventory inv, ItemStack searchfor, boolean useData) { int counter = 0; if(useData) { int size = inv.getSizeInventory(); ItemStack stack; for(int i = 0; i < size; i++) { stack = inv.getStackInSlot(i); if(doItemStacksMatch(stack, searchfor)) { counter += stack.getCount(); } } } else { int size = inv.getSizeInventory(); Item item = searchfor.getItem(); ItemStack stack; for(int i = 0; i < size; i++) { stack = inv.getStackInSlot(i); if(stack.getItem() == item) { counter += stack.getCount(); } } } return counter; } public static int removeFromInventory(IInventory inv, ItemStack searchfor) { ItemStack stack; int sub; int amount = searchfor.getCount(); for(int i = 0; i < inv.getSizeInventory(); i++) { stack = inv.getStackInSlot(i); if(doItemStacksMatch(stack, searchfor)) { sub = stack.getCount(); if(amount > sub) { amount -= sub; inv.setInventorySlotContents(i, ItemStack.EMPTY); } else if(amount == sub) { inv.setInventorySlotContents(i, ItemStack.EMPTY); return 0; } else { stack.setCount(sub - amount); return 0; } } } return amount; } }