Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. package com.minecolonies.coremod.inventory;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.entity.player.InventoryPlayer;
  5. import net.minecraft.init.Blocks;
  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. * The crafting matrix inventory (3x3).
  18. */
  19. public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
  20. public IInventory craftResult = new InventoryCraftResult();
  21. private final World worldObj;
  22.  
  23. /**
  24. * Position of the workbench
  25. */
  26. private final BlockPos pos;
  27.  
  28. public CraftingGUIBuilding(InventoryPlayer playerInventory, World worldIn, BlockPos posIn)
  29. {
  30. this.worldObj = worldIn;
  31. this.pos = posIn;
  32. this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));
  33.  
  34. for (int i = 0; i < 2; ++i)
  35. {
  36. for (int j = 0; j < 2; ++j)
  37. {
  38. this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 30 + j * 18, 17 + i * 18));
  39. }
  40. }
  41.  
  42. for (int l = 0; l < 3; ++l)
  43. {
  44. for (int j1 = 0; j1 < 9; ++j1)
  45. {
  46. this.addSlotToContainer(new Slot(playerInventory, j1 + l * 9 + 9, 8 + j1 * 18, 84 + l * 18));
  47. }
  48. }
  49.  
  50. for (int i1 = 0; i1 < 9; ++i1)
  51. {
  52. this.addSlotToContainer(new Slot(playerInventory, i1, 8 + i1 * 18, 142));
  53. }
  54.  
  55. this.onCraftMatrixChanged(this.craftMatrix);
  56. }
  57.  
  58. /**
  59. * Callback for when the crafting matrix is changed.
  60. */
  61. public void onCraftMatrixChanged(IInventory inventoryIn)
  62. {
  63. //todo can use this to get the recipe on button click
  64. this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
  65. }
  66.  
  67. /**
  68. * Called when the container is closed.
  69. */
  70. public void onContainerClosed(EntityPlayer playerIn)
  71. {
  72. super.onContainerClosed(playerIn);
  73.  
  74. if (!this.worldObj.isRemote)
  75. {
  76. for (int i = 0; i < 9; ++i)
  77. {
  78. ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);
  79.  
  80. if (itemstack != null)
  81. {
  82. playerIn.dropItem(itemstack, false);
  83. }
  84. }
  85. }
  86. }
  87.  
  88. public boolean canInteractWith(EntityPlayer playerIn)
  89. {
  90. return this.worldObj.getBlockState(this.pos).getBlock() != Blocks.CRAFTING_TABLE
  91. ? false
  92. : playerIn.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D;
  93. }
  94.  
  95. /**
  96. * Take a stack from the specified inventory slot.
  97. */
  98. @Nullable
  99. public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
  100. {
  101. ItemStack itemstack = null;
  102. Slot slot = (Slot) this.inventorySlots.get(index);
  103.  
  104. if (slot != null && slot.getHasStack())
  105. {
  106. ItemStack itemstack1 = slot.getStack();
  107. itemstack = itemstack1.copy();
  108.  
  109. if (index == 0)
  110. {
  111. if (!this.mergeItemStack(itemstack1, 10, 46, true))
  112. {
  113. return null;
  114. }
  115.  
  116. slot.onSlotChange(itemstack1, itemstack);
  117. }
  118. else if (index >= 5 && index < 32)
  119. {
  120. if (!this.mergeItemStack(itemstack1, 32, 41, false))
  121. {
  122. return null;
  123. }
  124. }
  125. else if (index >= 32 && index < 41)
  126. {
  127. if (!this.mergeItemStack(itemstack1, 5, 32, false))
  128. {
  129. return null;
  130. }
  131. }
  132. else if (!this.mergeItemStack(itemstack1, 5, 41, false))
  133. {
  134. return null;
  135. }
  136.  
  137. if (itemstack1.stackSize == 0)
  138. {
  139. slot.putStack((ItemStack) null);
  140. }
  141. else
  142. {
  143. slot.onSlotChanged();
  144. }
  145.  
  146. if (itemstack1.stackSize == itemstack.stackSize)
  147. {
  148. return null;
  149. }
  150.  
  151. slot.onPickupFromSlot(playerIn, itemstack1);
  152. }
  153.  
  154. return itemstack;
  155. }
  156.  
  157. /**
  158. * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
  159. * is null for the initial slot that was double-clicked.
  160. */
  161. public boolean canMergeSlot(ItemStack stack, Slot slotIn)
  162. {
  163. return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement