Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. package com.minecolonies.coremod.inventory;
  2.  
  3. import com.minecolonies.coremod.colony.buildings.AbstractBuilding;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.entity.player.InventoryPlayer;
  6. import net.minecraft.inventory.*;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.item.crafting.CraftingManager;
  9. import net.minecraft.util.math.BlockPos;
  10. import net.minecraft.world.World;
  11.  
  12. import javax.annotation.Nullable;
  13.  
  14. public class CraftingGUIBuilding extends Container
  15. {
  16. /**
  17. * Amount of rows in the player inventory.
  18. */
  19. private static final int PLAYER_INVENTORY_ROWS = 3;
  20.  
  21. /**
  22. * Amount of columns in the player inventory.
  23. */
  24. private static final int PLAYER_INVENTORY_COLUMNS = 9;
  25.  
  26. /**
  27. * Initial x-offset of the inventory slot.
  28. */
  29. private static final int PLAYER_INVENTORY_INITIAL_X_OFFSET = 8;
  30.  
  31. /**
  32. * Initial y-offset of the inventory slot.
  33. */
  34. private static final int PLAYER_INVENTORY_INITIAL_Y_OFFSET = 84;
  35.  
  36. /**
  37. * Each offset of the inventory slots.
  38. */
  39. private static final int INVENTORY_OFFSET_EACH = 18;
  40.  
  41. /**
  42. * Initial y-offset of the inventory slots in the hotbar.
  43. */
  44. private static final int PLAYER_INVENTORY_HOTBAR_OFFSET = 142;
  45.  
  46. /**
  47. * The x position of the crafting result slot position.
  48. */
  49. private static final int X_CRAFT_RESULT = 124;
  50.  
  51. /**
  52. * The y position of the crafting result slot position.
  53. */
  54. private static final int Y_CRAFT_RESULT = 35;
  55.  
  56. /**
  57. * The x offset of the crafting slot position.
  58. */
  59. private static final int X_OFFSET_CRAFTING = 30;
  60.  
  61. /**
  62. * The y offset of the crafting slot position.
  63. */
  64. private static final int Y_OFFSET_CRAFTING = 17;
  65.  
  66. /**
  67. * The building this crafting GUI belongs to.
  68. */
  69. private final AbstractBuilding.View building;
  70.  
  71. /**
  72. * The crafting matrix inventory (2x2).
  73. */
  74. private final InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
  75.  
  76. /**
  77. * The crafting result slot.
  78. */
  79. private final IInventory craftResult = new InventoryCraftResult();
  80.  
  81. /**
  82. * The world object.
  83. */
  84. private final World worldObj;
  85.  
  86. public CraftingGUIBuilding(InventoryPlayer playerInventory, World worldIn, BlockPos posIn, final AbstractBuilding.View building)
  87. {
  88. super();
  89. this.worldObj = worldIn;
  90. this.building = building;
  91.  
  92. this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, X_CRAFT_RESULT, Y_CRAFT_RESULT));
  93.  
  94. for (int i = 0; i < 2; ++i)
  95. {
  96. for (int j = 0; j < 2; ++j)
  97. {
  98. this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, X_OFFSET_CRAFTING + j * INVENTORY_OFFSET_EACH, Y_OFFSET_CRAFTING + i * INVENTORY_OFFSET_EACH));
  99. }
  100. }
  101.  
  102. int i;
  103. for (i = 0; i < PLAYER_INVENTORY_ROWS; i++)
  104. {
  105. for (int j = 0; j < PLAYER_INVENTORY_COLUMNS; j++)
  106. {
  107. addSlotToContainer(new Slot(
  108. playerInventory,
  109. j + i * PLAYER_INVENTORY_COLUMNS + PLAYER_INVENTORY_COLUMNS,
  110. PLAYER_INVENTORY_INITIAL_X_OFFSET + j * INVENTORY_OFFSET_EACH,
  111. PLAYER_INVENTORY_INITIAL_Y_OFFSET + i * INVENTORY_OFFSET_EACH
  112. ));
  113. }
  114. }
  115.  
  116. for (i = 0; i < PLAYER_INVENTORY_COLUMNS; i++)
  117. {
  118. addSlotToContainer(new Slot(
  119. playerInventory, i,
  120. PLAYER_INVENTORY_INITIAL_X_OFFSET + i * INVENTORY_OFFSET_EACH,
  121. PLAYER_INVENTORY_HOTBAR_OFFSET
  122. ));
  123. }
  124.  
  125. this.onCraftMatrixChanged(this.craftMatrix);
  126. }
  127.  
  128. @Override
  129. protected final Slot addSlotToContainer(final Slot slotToAdd)
  130. {
  131. return super.addSlotToContainer(slotToAdd);
  132. }
  133.  
  134. /**
  135. * Callback for when the crafting matrix is changed.
  136. */
  137. @Override
  138. public void onCraftMatrixChanged(IInventory inventoryIn)
  139. {
  140. super.onCraftMatrixChanged(inventoryIn);
  141. //todo can use this to get the recipe on button click
  142. this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
  143. }
  144.  
  145. /**
  146. * Called when the container is closed.
  147. */
  148. @Override
  149. public void onContainerClosed(EntityPlayer playerIn)
  150. {
  151. super.onContainerClosed(playerIn);
  152.  
  153. if (!this.worldObj.isRemote)
  154. {
  155. for (int i = 0; i < 9; ++i)
  156. {
  157. ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);
  158.  
  159. if (itemstack != null)
  160. {
  161. playerIn.dropItem(itemstack, false);
  162. }
  163. }
  164. }
  165. }
  166.  
  167. @Override
  168. public boolean canInteractWith(EntityPlayer playerIn)
  169. {
  170. return true;
  171. }
  172.  
  173.  
  174. @Nullable
  175. @Override
  176. public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
  177. {
  178. ItemStack itemstack = null;
  179. Slot slot = this.inventorySlots.get(index);
  180.  
  181. if (slot != null && slot.getHasStack())
  182. {
  183. ItemStack itemstack1 = slot.getStack();
  184. itemstack = itemstack1.copy();
  185.  
  186. if (index == 0)
  187. {
  188. if (!this.mergeItemStack(itemstack1, 10, 46, true))
  189. {
  190. return null;
  191. }
  192.  
  193. slot.onSlotChange(itemstack1, itemstack);
  194. }
  195. else if (index >= 5 && index < 32)
  196. {
  197. if (!this.mergeItemStack(itemstack1, 32, 41, false))
  198. {
  199. return null;
  200. }
  201. }
  202. else if (index >= 32 && index < 41)
  203. {
  204. if (!this.mergeItemStack(itemstack1, 5, 32, false))
  205. {
  206. return null;
  207. }
  208. }
  209. else if (!this.mergeItemStack(itemstack1, 5, 41, false))
  210. {
  211. return null;
  212. }
  213.  
  214. if (itemstack1.stackSize == 0)
  215. {
  216. slot.putStack((ItemStack) null);
  217. }
  218. else
  219. {
  220. slot.onSlotChanged();
  221. }
  222.  
  223. if (itemstack1.stackSize == itemstack.stackSize)
  224. {
  225. return null;
  226. }
  227.  
  228. slot.onPickupFromSlot(playerIn, itemstack1);
  229. }
  230.  
  231. return itemstack;
  232. }
  233.  
  234. @Override
  235. public boolean canMergeSlot(ItemStack stack, Slot slotIn)
  236. {
  237. return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement