InventoryBase.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package me.km.inventory;
  2. import java.util.Arrays;
  3. import net.minecraft.entity.Entity;
  4. import net.minecraft.inventory.InventoryBasic;
  5. import net.minecraft.item.ItemStack;
  6. public class InventoryBase extends InventoryBasic
  7. {
  8. private final Entity owner;
  9. private final int rows;
  10. private final byte[] data;
  11. private final byte[] smallData;
  12. public InventoryBase(String title, byte[] data, int slots, Entity owner)
  13. {
  14. super(title, true, slots);
  15. this.owner = owner;
  16. this.rows = slots / 9 + (slots % 9 == 0 ? 0 : 1);
  17. this.data = data;
  18. this.smallData = deleteZeros(data);
  19. }
  20. private static int countZeros(String slots)
  21. {
  22. int i = 0;
  23. for(char c : slots.toCharArray())
  24. {
  25. if(c == '0')
  26. {
  27. i++;
  28. }
  29. }
  30. return i;
  31. }
  32. private static byte[] deleteZeros(byte[] bytes)
  33. {
  34. int size = 0;
  35. for(byte b : bytes)
  36. {
  37. size += (b & 3) != 0 ? 1 : 0;
  38. size += (b & 12) != 0 ? 1 : 0;
  39. size += (b & 48) != 0 ? 1 : 0;
  40. size += (b & 192) != 0 ? 1 : 0;
  41. }
  42. size = (size >> 2) + ((size & 3) != 0 ? 1 : 0);
  43. byte[] b = new byte[size];
  44. byte current;
  45. int index = 0;
  46. int pos = 0;
  47. for(int i = 0; i < bytes.length; i++)
  48. {
  49. for(int j = 0; j < 8; j += 2)
  50. {
  51. current = (byte) ((bytes[i] >> j) & 3);
  52. if(current != 0)
  53. {
  54. b[index] |= current << pos;
  55. pos += 2;
  56. if(pos == 8)
  57. {
  58. pos = 0;
  59. index++;
  60. }
  61. }
  62. }
  63. }
  64. return b;
  65. }
  66. public InventoryBase(String title, String slots, Entity owner)
  67. {
  68. super(title, true, Math.min(54, slots.length() - countZeros(slots)));
  69. this.owner = owner;
  70. int slotAmount = Math.min(54, slots.length());
  71. this.rows = slotAmount / 9 + (slotAmount % 9 == 0 ? 0 : 1);
  72. this.data = new byte[14];
  73. int id;
  74. for(int i = 0; i < slotAmount; i++)
  75. {
  76. id = Character.getNumericValue(slots.charAt(i));
  77. if(id > 3 || id < 0)
  78. {
  79. id = 0;
  80. }
  81. switch(i % 4)
  82. {
  83. case 0:
  84. data[i / 4] += id;
  85. break;
  86. case 1:
  87. data[i / 4] += id << 2;
  88. break;
  89. case 2:
  90. data[i / 4] += id << 4;
  91. break;
  92. case 3:
  93. data[i / 4] += id << 6;
  94. break;
  95. }
  96. }
  97. this.smallData = deleteZeros(data);
  98. }
  99. public InventoryBase(String title, int slots, Entity owner)
  100. {
  101. super(title, true, Math.max(9, Math.min(54, ((slots / 9) + ((slots % 9) == 0 ? 0 : 1)) * 9)));
  102. this.owner = owner;
  103. this.rows = super.getSizeInventory() / 9;
  104. this.data = new byte[14];
  105. // default is 3 to every 2 bits
  106. Arrays.fill(this.data, (byte) 0xFF);
  107. this.smallData = data;
  108. }
  109. public byte[] getData()
  110. {
  111. return data;
  112. }
  113. public Entity getOwner()
  114. {
  115. return owner;
  116. }
  117. public void setContents(Iterable<? extends ItemStack> stacks)
  118. {
  119. int counter = 0;
  120. int size = super.getSizeInventory();
  121. for(ItemStack stack : stacks)
  122. {
  123. this.setInventorySlotContents(counter, stack);
  124. counter++;
  125. if(counter >= size)
  126. {
  127. return;
  128. }
  129. }
  130. }
  131. public int getRows()
  132. {
  133. return rows;
  134. }
  135. public int getSlotStatus(int slot)
  136. {
  137. if(slot < 0 || slot >= this.getSizeInventory())
  138. {
  139. return 1;
  140. }
  141. int index = Math.max(0, Math.min(smallData.length, slot / 4));
  142. switch(slot % 4)
  143. {
  144. case 0: return smallData[index] & 3;
  145. case 1: return (smallData[index] >> 2) & 3;
  146. case 2: return (smallData[index] >> 4) & 3;
  147. case 3: return (smallData[index] >> 6) & 3;
  148. }
  149. return 0;
  150. }
  151. public int getSlotStatus(int x, int y)
  152. {
  153. int slot = y * 9 + x;
  154. int index = Math.max(0, Math.min(13, slot / 4));
  155. switch(slot % 4)
  156. {
  157. case 0: return data[index] & 3;
  158. case 1: return (data[index] >> 2) & 3;
  159. case 2: return (data[index] >> 4) & 3;
  160. case 3: return (data[index] >> 6) & 3;
  161. }
  162. return 0;
  163. }
  164. public boolean isSlotValid(int slot)
  165. {
  166. return getSlotStatus(slot) != 0;
  167. }
  168. public boolean isSlotValid(int x, int y)
  169. {
  170. return getSlotStatus(x, y) != 0;
  171. }
  172. @Override
  173. public ItemStack addItem(ItemStack itemstack)
  174. {
  175. ItemStack stack = itemstack.copy();
  176. for(int i = 0; i < this.getSizeInventory(); i++)
  177. {
  178. // ignore special fields
  179. if(getSlotStatus(i) != 1)
  180. {
  181. continue;
  182. }
  183. ItemStack slotStack = this.getStackInSlot(i);
  184. if(slotStack.isEmpty())
  185. {
  186. this.setInventorySlotContents(i, stack);
  187. this.markDirty();
  188. return ItemStack.EMPTY;
  189. }
  190. if(ItemStack.areItemsEqual(slotStack, stack))
  191. {
  192. int j = Math.min(this.getInventoryStackLimit(), slotStack.getMaxStackSize());
  193. int k = Math.min(stack.getCount(), j - slotStack.getCount());
  194. if (k > 0)
  195. {
  196. slotStack.grow(k);
  197. stack.shrink(k);
  198. if (stack.isEmpty())
  199. {
  200. this.markDirty();
  201. return ItemStack.EMPTY;
  202. }
  203. }
  204. }
  205. }
  206. if (stack.getCount() != stack.getCount())
  207. {
  208. this.markDirty();
  209. }
  210. return stack;
  211. }
  212. }