Advertisement
Guest User

Untitled

a guest
Jun 12th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. package me.aPanda.PandaTheEnchanter;
  2.  
  3. import org.bukkit.inventory.Inventory;
  4. import org.bukkit.inventory.ItemStack;
  5.  
  6. public class InventoryHelper
  7. {
  8. public static int amtItem(Inventory inventory, int itemid)
  9. {
  10. int ret = 0;
  11. if (inventory != null) {
  12. ItemStack[] items = inventory.getContents();
  13. for (int slot = 0; slot < items.length; slot++) {
  14. if (items[slot] != null) {
  15. int id = items[slot].getTypeId();
  16. int amt = items[slot].getAmount();
  17. if (id == itemid) {
  18. ret += amt;
  19. }
  20. }
  21. }
  22. }
  23. return ret;
  24. }
  25.  
  26. public static void setItem(Inventory inventory, int itemid, int amt) {
  27. if (inventory != null) {
  28. ItemStack[] items = inventory.getContents();
  29. for (int slot = 0; slot < items.length; slot++)
  30. if (items[slot] != null) {
  31. int id = items[slot].getTypeId();
  32. if (id == itemid) {
  33. items[slot].setAmount(amt);
  34. return;
  35. }
  36. }
  37. }
  38. }
  39.  
  40. public static void removeItem(Inventory inventory, int itemid, int ret)
  41. {
  42. int start = ret;
  43. if (inventory != null) {
  44. ItemStack[] items = inventory.getContents();
  45. for (int slot = 0; slot < items.length; slot++)
  46. if (items[slot] != null) {
  47. int id = items[slot].getTypeId();
  48. int amt = items[slot].getAmount();
  49. if (id == itemid) {
  50. if (ret > 0) {
  51. if (amt >= ret) {
  52. amt -= ret;
  53. ret = 0;
  54. } else {
  55. ret = start - amt;
  56. amt = 0;
  57. }
  58. if (amt > 0)
  59. inventory.setItem(slot, new ItemStack(id, amt));
  60. else {
  61. inventory.setItem(slot, null);
  62. }
  63. }
  64. if (ret <= 0)
  65. return;
  66. }
  67. }
  68. }
  69. }
  70.  
  71. public static void removeItem(Inventory inventory, int slot1)
  72. {
  73. if (inventory != null) {
  74. ItemStack[] items = inventory.getContents();
  75. items[slot1].setAmount(0);
  76. }
  77. }
  78.  
  79. public static boolean isEmpty(Inventory inventory) {
  80. if (inventory != null) {
  81. ItemStack[] items = inventory.getContents();
  82. for (int slot = 0; slot < items.length; slot++) {
  83. if ((items[slot] != null) &&
  84. (items[slot].getTypeId() > 0)) {
  85. return false;
  86. }
  87. }
  88. }
  89.  
  90. return true;
  91. }
  92.  
  93. public static boolean clearInventory(Inventory inventory) {
  94. if (inventory != null) {
  95. ItemStack[] items = inventory.getContents();
  96. for (int slot = 0; slot < items.length; slot++) {
  97. if ((items[slot] == null) ||
  98. (items[slot].getTypeId() <= 0)) continue;
  99. items[slot] = null;
  100. }
  101.  
  102. }
  103.  
  104. return true;
  105. }
  106.  
  107. public static int getItemPosition(Inventory inventory, ItemStack itm) {
  108. if (inventory != null) {
  109. ItemStack[] items = inventory.getContents();
  110. for (int slot = 0; slot < items.length; slot++) {
  111. if ((items[slot] != null) &&
  112. (items[slot].equals(itm))) {
  113. return slot;
  114. }
  115. }
  116. }
  117.  
  118. return -1;
  119. }
  120.  
  121. public static int getFirstFreeSlot(Inventory inventory) {
  122. if (inventory != null) {
  123. ItemStack[] items = inventory.getContents();
  124. for (int slot = 0; slot < items.length; slot++) {
  125. if (items[slot] != null) {
  126. if (items[slot].getTypeId() == 0)
  127. return slot;
  128. }
  129. else {
  130. return slot;
  131. }
  132. }
  133. }
  134. return -1;
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement