Guest User

Untitled

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