Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. package com.minecolonies.coremod.inventory;
  2.  
  3. import com.minecolonies.api.util.ItemStackUtils;
  4. import com.minecolonies.coremod.colony.Colony;
  5. import com.minecolonies.coremod.colony.ColonyManager;
  6. import com.minecolonies.coremod.tileentities.TileEntityRack;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.entity.player.InventoryPlayer;
  9. import net.minecraft.inventory.*;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.util.math.BlockPos;
  12. import net.minecraft.world.World;
  13. import net.minecraftforge.items.IItemHandler;
  14. import net.minecraftforge.items.SlotItemHandler;
  15. import org.jetbrains.annotations.NotNull;
  16. import org.jetbrains.annotations.Nullable;
  17.  
  18. /**
  19. * THe container class for the rack.
  20. */
  21. public class ContainerRack extends net.minecraft.inventory.Container
  22. {
  23. /**
  24. * Amount of columns in the player inventory.
  25. */
  26. private static final int INVENTORY_COLUMNS = 9;
  27.  
  28. /**
  29. * Initial x-offset of the inventory slot.
  30. */
  31. private static final int PLAYER_INVENTORY_INITIAL_X_OFFSET = 8;
  32.  
  33. /**
  34. * Initial y-offset of the inventory slot.
  35. */
  36. private static final int PLAYER_INVENTORY_INITIAL_Y_OFFSET = 84;
  37.  
  38. /**
  39. * Each offset of the inventory slots.
  40. */
  41. private static final int PLAYER_INVENTORY_OFFSET_EACH = 18;
  42.  
  43. /**
  44. * Initial y-offset of the inventory slots in the hotbar.
  45. */
  46. private static final int PLAYER_INVENTORY_HOTBAR_OFFSET = 142;
  47.  
  48. /**
  49. * Amount of rows in the player inventory.
  50. */
  51. private static final int INVENTORY_ROWS = 3;
  52.  
  53. /**
  54. * The size of the the inventory hotbar.
  55. */
  56. private static final int INVENTORY_BAR_SIZE = 8;
  57.  
  58. /**
  59. * The colony of the field.
  60. */
  61. @Nullable
  62. private final Colony colony;
  63.  
  64. /**
  65. * The fields location.
  66. */
  67. private BlockPos location;
  68.  
  69. /**
  70. * The inventory.
  71. */
  72. private IItemHandler inventory;
  73.  
  74. /**
  75. * Creates an instance of our field container, this may be serve to open the GUI.
  76. *
  77. * @param tileEntityRack the tileEntity of the field containing the inventory.
  78. * @param playerInventory the player inventory.
  79. * @param world the world.
  80. * @param location the position of the field.
  81. */
  82. public ContainerRack(@NotNull final TileEntityRack tileEntityRack, final InventoryPlayer playerInventory, @NotNull final World world, @NotNull final BlockPos location)
  83. {
  84. super();
  85. this.colony = ColonyManager.getColony(world, location);
  86. this.location = location;
  87. this.inventory = tileEntityRack.getInventory();
  88.  
  89. for (int j = 0; j < INVENTORY_ROWS; ++j)
  90. {
  91. for (int k = 0; k < INVENTORY_COLUMNS; ++k)
  92. {
  93. this.addSlotToContainer(
  94. new SlotItemHandler(inventory, k + j * INVENTORY_COLUMNS,
  95. INVENTORY_BAR_SIZE + k * PLAYER_INVENTORY_OFFSET_EACH,
  96. PLAYER_INVENTORY_OFFSET_EACH + j * PLAYER_INVENTORY_OFFSET_EACH));
  97. }
  98. }
  99.  
  100. //Ddd player inventory slots
  101. // Note: The slot numbers are within the player inventory and may be the same as the field inventory.
  102. int i;
  103. for (i = 0; i < INVENTORY_ROWS; i++)
  104. {
  105. for (int j = 0; j < INVENTORY_COLUMNS; j++)
  106. {
  107. addSlotToContainer(new Slot(
  108. playerInventory,
  109. j + i * INVENTORY_COLUMNS + INVENTORY_COLUMNS,
  110. PLAYER_INVENTORY_INITIAL_X_OFFSET + j * PLAYER_INVENTORY_OFFSET_EACH,
  111. PLAYER_INVENTORY_INITIAL_Y_OFFSET + i * PLAYER_INVENTORY_OFFSET_EACH
  112. ));
  113. }
  114. }
  115.  
  116. for (i = 0; i < INVENTORY_COLUMNS; i++)
  117. {
  118. addSlotToContainer(new Slot(
  119. playerInventory, i,
  120. PLAYER_INVENTORY_INITIAL_X_OFFSET + i * PLAYER_INVENTORY_OFFSET_EACH,
  121. PLAYER_INVENTORY_HOTBAR_OFFSET
  122. ));
  123. }
  124. }
  125.  
  126. @Override
  127. protected final Slot addSlotToContainer(final Slot slotToAdd)
  128. {
  129. return super.addSlotToContainer(slotToAdd);
  130. }
  131.  
  132. @Override
  133. public boolean canInteractWith(final EntityPlayer playerIn)
  134. {
  135. return true;
  136. }
  137.  
  138. @Override
  139. public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
  140. {
  141. Slot slot = this.inventorySlots.get(index);
  142.  
  143. if (slot == null || !slot.getHasStack())
  144. {
  145. return ItemStackUtils.EMPTY;
  146. }
  147.  
  148. ItemStack stack = slot.getStack();
  149. ItemStack stackCopy = stack.copy();
  150.  
  151. if (index < INVENTORY_ROWS * INVENTORY_COLUMNS)
  152. {
  153. if (!this.mergeItemStack(stack, INVENTORY_ROWS * INVENTORY_COLUMNS, this.inventorySlots.size(), true))
  154. {
  155. return ItemStackUtils.EMPTY;
  156. }
  157. }
  158. else if (!this.mergeItemStack(stack, 0, INVENTORY_ROWS * INVENTORY_COLUMNS, false))
  159. {
  160. return ItemStackUtils.EMPTY;
  161. }
  162.  
  163. if (ItemStackUtils.getSize(stack) == 0)
  164. {
  165. slot.putStack(ItemStackUtils.EMPTY);
  166. }
  167. else
  168. {
  169. slot.onSlotChanged();
  170. }
  171.  
  172. return stackCopy;
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement