InventoryUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package me.km.inventory;
  2. import net.minecraft.entity.passive.EntityVillager;
  3. import net.minecraft.inventory.IInventory;
  4. import net.minecraft.inventory.InventoryBasic;
  5. import net.minecraft.item.Item;
  6. import net.minecraft.item.ItemStack;
  7. public class InventoryUtils
  8. {
  9. public static InventoryBasic getInventory(EntityVillager v)
  10. {
  11. EntityInventory real = new EntityInventory("Villager", 9, v);
  12. InventoryBasic old = v.getVillagerInventory();
  13. for(int i = 0; i < 9; i++)
  14. {
  15. real.setInventorySlotContents(i, old.getStackInSlot(i).copy());
  16. }
  17. return real;
  18. }
  19. public static void setInventory(EntityInventory inv)
  20. {
  21. if(inv.getEntity() instanceof EntityVillager)
  22. {
  23. EntityVillager v = (EntityVillager) inv.getEntity();
  24. InventoryBasic vInv = v.getVillagerInventory();
  25. for(int i = 0; i < 9; i++)
  26. {
  27. vInv.setInventorySlotContents(i, inv.getStackInSlot(i).copy());
  28. }
  29. }
  30. }
  31. public static boolean doItemStacksMatch(ItemStack first, ItemStack second)
  32. {
  33. int count = first.getCount();
  34. first.setCount(second.getCount());
  35. boolean b = ItemStack.areItemStacksEqual(first, second);
  36. first.setCount(count);
  37. return b;
  38. }
  39. public static int searchInventoryFor(IInventory inv, ItemStack searchfor, boolean useData)
  40. {
  41. int counter = 0;
  42. if(useData)
  43. {
  44. int size = inv.getSizeInventory();
  45. ItemStack stack;
  46. for(int i = 0; i < size; i++)
  47. {
  48. stack = inv.getStackInSlot(i);
  49. if(doItemStacksMatch(stack, searchfor))
  50. {
  51. counter += stack.getCount();
  52. }
  53. }
  54. }
  55. else
  56. {
  57. int size = inv.getSizeInventory();
  58. Item item = searchfor.getItem();
  59. ItemStack stack;
  60. for(int i = 0; i < size; i++)
  61. {
  62. stack = inv.getStackInSlot(i);
  63. if(stack.getItem() == item)
  64. {
  65. counter += stack.getCount();
  66. }
  67. }
  68. }
  69. return counter;
  70. }
  71. public static int removeFromInventory(IInventory inv, ItemStack searchfor)
  72. {
  73. ItemStack stack;
  74. int sub;
  75. int amount = searchfor.getCount();
  76. for(int i = 0; i < inv.getSizeInventory(); i++)
  77. {
  78. stack = inv.getStackInSlot(i);
  79. if(doItemStacksMatch(stack, searchfor))
  80. {
  81. sub = stack.getCount();
  82. if(amount > sub)
  83. {
  84. amount -= sub;
  85. inv.setInventorySlotContents(i, ItemStack.EMPTY);
  86. }
  87. else if(amount == sub)
  88. {
  89. inv.setInventorySlotContents(i, ItemStack.EMPTY);
  90. return 0;
  91. }
  92. else
  93. {
  94. stack.setCount(sub - amount);
  95. return 0;
  96. }
  97. }
  98. }
  99. return amount;
  100. }
  101. }