Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. @Override
  2. public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
  3. ItemStack previous = null;
  4. Slot slot = (Slot) this.inventorySlots.get(fromSlot);
  5.  
  6. if (slot != null && slot.getHasStack()) {
  7. ItemStack current = slot.getStack();
  8. previous = current.copy();
  9.  
  10. if (fromSlot < 9) {
  11. if (!this.mergeItemStack(current, 9, 45, true))
  12. return null;
  13. } else {
  14. if (!this.mergeItemStack(current, 0, 9, false))
  15. return null;
  16. }
  17.  
  18. if (current.getCount() == 0)
  19. slot.putStack((ItemStack) null);
  20. else
  21. slot.onSlotChanged();
  22.  
  23. if (current.getCount() == previous.getCount())
  24. return null;
  25.  
  26.  
  27. }
  28. return previous;
  29. }
  30.  
  31. @Override
  32. protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
  33. boolean success = false;
  34. int index = startIndex;
  35.  
  36. if (useEndIndex)
  37. index = endIndex - 1;
  38.  
  39. Slot slot;
  40. ItemStack stackinslot;
  41.  
  42. if (stack.isStackable()) {
  43. while (stack.getCount() > 0 && (!useEndIndex && index < endIndex || useEndIndex && index >= startIndex)) {
  44. slot = (Slot) this.inventorySlots.get(index);
  45. stackinslot = slot.getStack();
  46.  
  47. if (stackinslot != null && stackinslot.getItem() == stack.getItem() && (!stack.getHasSubtypes() || stack.getMetadata() == stackinslot.getMetadata()) && ItemStack.areItemStackTagsEqual(stack, stackinslot)) {
  48. int l = stackinslot.getCount() + stack.getCount();
  49. int maxsize = Math.min(stack.getMaxStackSize(), slot.getItemStackLimit(stack));
  50.  
  51. if (l <= maxsize) {
  52. stack.setCount(0);
  53. stackinslot.setCount(1);
  54. slot.onSlotChanged();
  55. success = true;
  56. } else if (stackinslot.getCount() < maxsize) {
  57. stack.setCount(stack.getCount() - stack.getMaxStackSize() - stackinslot.getCount());
  58. stackinslot.setCount(stack.getMaxStackSize());
  59. slot.onSlotChanged();
  60. success = true;
  61. }
  62. }
  63.  
  64. if (useEndIndex) {
  65. --index;
  66. } else {
  67. ++index;
  68. }
  69. }
  70. }
  71.  
  72. if (stack.getCount() > 0) {
  73. if (useEndIndex) {
  74. index = endIndex - 1;
  75. } else {
  76. index = startIndex;
  77. }
  78.  
  79. while (!useEndIndex && index < endIndex || useEndIndex && index >= startIndex && stack.getCount() > 0) {
  80. slot = (Slot) this.inventorySlots.get(index);
  81. stackinslot = slot.getStack();
  82.  
  83. if (stackinslot == null && slot.isItemValid(stack)) {
  84. if (stack.getCount() < slot.getItemStackLimit(stack)) {
  85. slot.putStack(stack.copy());
  86. stack.setCount(0);
  87. success = true;
  88. break;
  89. } else {
  90. ItemStack newstack = stack.copy();
  91. newstack.setCount(slot.getItemStackLimit(stack));
  92. slot.putStack(newstack);
  93. stack.setCount(stack.getCount() - slot.getItemStackLimit(stack));
  94. success = true;
  95. }
  96. }
  97.  
  98. if (useEndIndex) {
  99. --index;
  100. } else {
  101. ++index;
  102. }
  103. }
  104. }
  105.  
  106. return success;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement