123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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;
- }
- }
|