Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. package com.soravoid.sparkon.inventory.containers;
  2.  
  3. import com.soravoid.sparkon.capabilities.CapabilityAligner;
  4. import net.minecraft.entity.player.PlayerEntity;
  5. import net.minecraft.entity.player.PlayerInventory;
  6. import net.minecraft.inventory.container.Container;
  7. import net.minecraft.inventory.container.Slot;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraftforge.items.IItemHandler;
  10. import net.minecraftforge.items.SlotItemHandler;
  11.  
  12. public class AlignerContainer extends Container
  13. {
  14.  
  15. private IItemHandler handler;
  16.  
  17. private int ARMOR_START, ARMOR_END, INV_START, INV_END, HOTBAR_START, HOTBAR_END;
  18.  
  19. public AlignerContainer(int windowId, PlayerInventory inv)
  20. {
  21. this(windowId, inv, inv.player);
  22. }
  23.  
  24. public AlignerContainer(int windowId, PlayerInventory inv, PlayerEntity player)
  25. {
  26. super(SparkContainerType.ALIGNERCONTAINER, windowId);
  27. int i;
  28. IItemHandler handler = player.getCapability(CapabilityAligner.ALIGNER).orElse(null);
  29. this.addSlot(new SlotItemHandler(handler, 0, 80, 23));
  30.  
  31. this.handler = handler;
  32.  
  33. ARMOR_START = handler.getSlots();
  34. ARMOR_END = ARMOR_START+3;
  35. INV_START = ARMOR_END+1;
  36. INV_END = INV_START+26;
  37. HOTBAR_START = INV_END+1;
  38. HOTBAR_END = HOTBAR_START+8;
  39.  
  40. for (i = 0; i < 3; ++i)
  41. {
  42. for (int j = 0; j < 9; ++j)
  43. {
  44. this.addSlot(new Slot(inv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
  45. }
  46. }
  47.  
  48. for (i = 0; i < 9; ++i)
  49. {
  50. this.addSlot(new Slot(inv, i, 8 + i * 18, 142));
  51. }
  52. }
  53.  
  54. @Override
  55. public boolean canInteractWith(PlayerEntity playerIn) { return true; }
  56.  
  57. @Override
  58. public ItemStack transferStackInSlot(PlayerEntity playerIn, int index)
  59. {
  60. ItemStack itemstack = null;
  61. Slot slot = this.inventorySlots.get(index);
  62.  
  63. if (slot != null && slot.getHasStack())
  64. {
  65. ItemStack itemstack1 = slot.getStack();
  66. itemstack = itemstack1.copy();
  67.  
  68. if (index < INV_START)
  69. {
  70. if (!this.mergeItemStack(itemstack1, INV_START, 37, true))
  71. {
  72. return ItemStack.EMPTY;
  73. }
  74.  
  75. slot.onSlotChange(itemstack1, itemstack);
  76. }
  77. else
  78. {
  79. if(index == 0)
  80. {
  81. if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true))
  82. {
  83. return ItemStack.EMPTY;
  84. }
  85.  
  86. slot.onSlotChange(itemstack1, itemstack);
  87. }
  88. else if (slot.isItemValid(itemstack1))
  89. {
  90. if (!this.mergeItemStack(itemstack1, 0, handler.getSlots(), false))
  91. {
  92. return ItemStack.EMPTY;
  93. }
  94. }
  95. else if (index >= INV_START && index < HOTBAR_START)
  96. {
  97. if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_START + 1, false))
  98. {
  99. return ItemStack.EMPTY;
  100. }
  101. }
  102. else if (index >= HOTBAR_START && index < HOTBAR_END + 1)
  103. {
  104. if (!this.mergeItemStack(itemstack1, INV_START, INV_END + 1, false))
  105. {
  106. return ItemStack.EMPTY;
  107. }
  108. }
  109. }
  110.  
  111. if (itemstack1.getCount() == 0)
  112. {
  113. slot.putStack(ItemStack.EMPTY);
  114. }
  115. else
  116. {
  117. slot.onSlotChanged();
  118. }
  119.  
  120. if (itemstack1.getCount() == itemstack.getCount())
  121. {
  122. return ItemStack.EMPTY;
  123. }
  124. slot.onTake(playerIn, itemstack1);
  125. }
  126.  
  127. return itemstack;
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement