Guest User

Untitled

a guest
Jul 10th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. package ed.enderdeath.mod.AnvilDragon;
  2.  
  3. import ed.enderdeath.mod.common.enderdeath;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.entity.player.InventoryPlayer;
  6. import net.minecraft.inventory.Container;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.inventory.InventoryCraftResult;
  9. import net.minecraft.inventory.InventoryCrafting;
  10. import net.minecraft.inventory.Slot;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.world.World;
  13.  
  14. public class ContainerDragonAnvil extends Container
  15. {
  16.  
  17.  
  18. /** Largeur du craft */
  19. public static int craftWidth = 4;
  20. /** Hauteur du craft */
  21. public static int craftHeigth = 4;
  22.  
  23. /** Inventaire contenant le craft */
  24. public InventoryCrafting craftMatrix = new InventoryCrafting(this, craftWidth, craftHeigth);
  25. /** Inventaire contenant le résultat du craft */
  26. public IInventory craftResult = new InventoryCraftResult();
  27.  
  28. public World worldObj;
  29. public int pos;
  30. public int x;
  31. public int y;
  32. public int z;
  33. public int Width;
  34.  
  35. public ContainerDragonAnvil(InventoryPlayer invPlayer, World world, int pos,int y,int z)
  36. {
  37. this.worldObj = world;
  38. this.pos = pos;
  39.  
  40. //Ajout du slot pour le résultat
  41. this.addSlotToContainer(new DragonSlotCrafting(invPlayer.player, craftMatrix, craftResult, 0, 141, 43));
  42.  
  43. int startX = 8; //Position x ou les slots de craft commencent à être dessinés
  44. int startY = 7; //Position y ou les slots de craft commencent à être dessinés
  45. //Ajout des slots de craft
  46. for (int y1 = 0; y1 < craftHeigth; ++y1)
  47. {
  48. for(int x = 0; x < craftWidth; ++x)
  49. {
  50. this.addSlotToContainer(new Slot(craftMatrix, x + y1 * craftWidth, startX + x * 18, startY + y1 * 18));
  51. }
  52. }
  53.  
  54. startX = 8; //Position x ou les slots de l'inventaire commencent à être dessinés
  55. startY = 106; //Position y ou les slots de l'inventaire commencent à être dessinés
  56. //Ajout des slots de l'inventaire du joueur
  57. for (int y1 = 0; y1 < 3; ++y1)
  58. {
  59. for(int x = 0; x < 9; ++x)
  60. {
  61. this.addSlotToContainer(new Slot(invPlayer, x + y1 * 9 + 9, startX + x * 18, startY + y1 * 18));
  62. }
  63. }
  64. startY = 164; //Position y ou les slots de la hotbar commencent à être dessinés
  65. //Ajout des slots de la hotbar
  66. for (int x = 0; x < 9; ++x)
  67. {
  68. this.addSlotToContainer(new Slot(invPlayer, x, startX + x * 18, startY));
  69. }
  70. }
  71.  
  72. /**
  73. * Appelé quand la matrice (les slots de craft) change
  74. */
  75. @Override
  76. public void onCraftMatrixChanged(IInventory iiventory)
  77. {
  78. //On met le résultat du craft dans le slot de résultat
  79. craftResult.setInventorySlotContents(0, TutorielCraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj));
  80. }
  81.  
  82. /**
  83. * Retourne true si le joueur peut interagir avec ce gui, en général on teste la distance par rapport au joueur dans cette fonction
  84. */
  85. @Override
  86. public boolean canInteractWith(EntityPlayer player)
  87. {
  88. return this.worldObj.getBlock(x, y, z) == enderdeath.AnvilDragon;
  89. }
  90.  
  91. /**
  92. * Appelé quand le container est fermé
  93. */
  94. @Override
  95. public void onContainerClosed(EntityPlayer player)
  96. {
  97. super.onContainerClosed(player);
  98. if (!this.worldObj.isRemote) //Si on est sur le serveur, on loot les items à la fermeture du gui
  99. {
  100. for (int i = 0; i < 9; ++i)
  101. {
  102. ItemStack itemstack = this.craftMatrix.getStackInSlot(i);
  103. if (itemstack != null)
  104. {
  105. player.dropPlayerItemWithRandomChoice(itemstack, false);
  106. //player.dropPlayerItemWithRandomChoice(itemstack, false); //TODO A utiliser en 1.8
  107. }
  108. }
  109. }
  110. }
  111.  
  112. /**
  113. * Cette fonction est appelée lors du shift+clic (je vous conseille de la laisser comme tel, mais je précise quel ne s'adaptera pas en fonction de la taille du craft)
  114. */
  115. @Override
  116. public ItemStack transferStackInSlot(EntityPlayer player, int slotId)
  117. {
  118. ItemStack itemstack = null;
  119. Slot slot = (Slot) this.inventorySlots.get(slotId);
  120.  
  121. if (slot != null && slot.getHasStack())
  122. {
  123. ItemStack itemstack1 = slot.getStack();
  124. itemstack = itemstack1.copy();
  125.  
  126. if (slotId == 0)
  127. {
  128. if (!this.mergeItemStack(itemstack1, 10, 46, true))
  129. {
  130. return null;
  131. }
  132. slot.onSlotChange(itemstack1, itemstack);
  133. }
  134. else if (slotId >= 10 && slotId < 37)
  135. {
  136. if (!this.mergeItemStack(itemstack1, 37, 46, false))
  137. {
  138. return null;
  139. }
  140. }
  141. else if (slotId >= 37 && slotId < 46)
  142. {
  143. if (!this.mergeItemStack(itemstack1, 10, 37, false))
  144. {
  145. return null;
  146. }
  147. }
  148. else if (!this.mergeItemStack(itemstack1, 10, 46, false))
  149. {
  150. return null;
  151. }
  152. if (itemstack1.stackSize == 0)
  153. {
  154. slot.putStack((ItemStack)null);
  155. }
  156. else
  157. {
  158. slot.onSlotChanged();
  159. }
  160. if (itemstack1.stackSize == itemstack.stackSize)
  161. {
  162. return null;
  163. }
  164.  
  165. slot.onPickupFromSlot(player, itemstack1);
  166. }
  167. return itemstack;
  168. }
  169.  
  170. /**
  171. * Appelé quand on double clic sur un slot :
  172. * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
  173. * is null for the initial slot that was double-clicked.
  174. */
  175. public boolean canMergeSlot(ItemStack stack, Slot slotIn)
  176. {
  177. return slotIn.inventory != this.craftResult && super.canDragIntoSlot(slotIn);
  178. }
  179. }
Add Comment
Please, Sign In to add comment