Advertisement
leo01418

Untitled

Jul 5th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. package ed.enderdeath.mod.AnvilDragon;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.google.common.collect.Lists;
  7.  
  8. import net.minecraft.inventory.InventoryCrafting;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.item.crafting.IRecipe;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.world.IBlockAccess;
  13. import net.minecraft.world.World;
  14. import net.minecraftforge.common.ForgeHooks;
  15. import net.minecraftforge.oredict.OreDictionary;
  16.  
  17. public class TutorielShapedRecipes implements IRecipe
  18.  
  19. { /** How many horizontal slots this recipe is wide. */
  20. public final int recipeWidth;
  21. /** How many vertical slots this recipe uses. */
  22. public final int recipeHeight;
  23. /** Is a array of ItemStack that composes the recipe. */
  24. public final Object[] recipeItems;
  25. /** Is the ItemStack that you get when craft the recipe. */
  26. private final ItemStack recipeOutput;
  27. private boolean copyIngredientNBT;
  28.  
  29. public TutorielShapedRecipes(int width, int height, Object[] items, ItemStack output)
  30. {
  31. this.recipeWidth = width;
  32. this.recipeHeight = height;
  33. this.recipeItems = items;
  34. this.recipeOutput = output;
  35. }
  36.  
  37. public ItemStack getRecipeOutput()
  38. {
  39. return this.recipeOutput;
  40. }
  41.  
  42. public ItemStack[] getRemainingItems(InventoryCrafting inv)
  43. {
  44. ItemStack[] aitemstack = new ItemStack[inv.getSizeInventory()];
  45. for (int i = 0; i < aitemstack.length; ++i)
  46. {
  47. ItemStack itemstack = inv.getStackInSlot(i);
  48. aitemstack[i] = recipeOutput.getItem().getContainerItem(recipeOutput);
  49. }
  50. return aitemstack;
  51. }
  52.  
  53. /**
  54. * Used to check if a recipe matches current crafting inventory
  55. * Retourne true si la recette correspond à la matrice donnée (le craft que le joueur a fait)
  56. */
  57. public boolean matches(InventoryCrafting inv, World worldIn)
  58. {
  59. for (int i = 0; i <= this.recipeWidth - this.recipeWidth; ++i)
  60. {
  61. for (int j = 0; j <= ((IBlockAccess) inv).getHeight() - this.recipeHeight; ++j)
  62. {
  63. if (this.checkMatch(inv, i, j, true))
  64. {
  65. return true;
  66. }
  67. if (this.checkMatch(inv, i, j, false))
  68. {
  69. return true;
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75.  
  76. /**
  77. * Checks if the region of a crafting inventory is match for the recipe.
  78. * Compare deux parties du craft
  79. */
  80. private boolean checkMatch(InventoryCrafting inv, int regionX, int regionY, boolean mirror)
  81. {
  82. for (int x = 0 ; x < this.recipeWidth ;++x)
  83. {
  84. for (int y = 0 ; y < this.recipeHeight ; ++y)
  85. {
  86. int x1 = x - regionX;
  87. int y1 = y - regionY;
  88. Object patternStack = null;
  89. if (x1 >= 0 && y1 >= 0 && x1 < this.recipeWidth && y1 < this.recipeHeight)
  90. {
  91. if (mirror)
  92. patternStack = this.recipeItems[this.recipeWidth - x1 - 1 + y1 * this.recipeWidth];
  93. else
  94. patternStack = this.recipeItems[x1 + y1 * this.recipeWidth];
  95. if(patternStack instanceof String)
  96. {
  97. List<ItemStack> stacks = OreDictionary.getOres((String) patternStack);
  98. boolean matches = false;
  99. for(ItemStack stack : stacks)
  100. {
  101. if(areItemStacksEquals(stack, inv.getStackInRowAndColumn(x, y))) //If the pattern's stack doesn't match with the stack in the inv crafting
  102. {
  103. matches = true;
  104. }
  105. }
  106. if(!matches)
  107. return false;
  108. }
  109. else if(!areItemStacksEquals((ItemStack) patternStack, inv.getStackInRowAndColumn(x, y)))
  110. {
  111. return false;
  112. }
  113. }
  114. }
  115. }
  116. return true;
  117. }
  118.  
  119.  
  120. /**
  121. * Compare les deux stacks
  122. */
  123. public static boolean areItemStacksEquals(ItemStack stack1, ItemStack stack2)
  124. {
  125. if(stack1 == null || stack2 == null) return stack1 == stack2;
  126. return stack1.getItem() == stack2.getItem() && (stack1.getItemDamage() == OreDictionary.WILDCARD_VALUE || stack1.getItemDamage() == stack2.getItemDamage());
  127. }
  128.  
  129.  
  130. /**
  131. * Returns an Item that is the result of this recipe
  132. */
  133. public ItemStack getCraftingResult(InventoryCrafting inv)
  134. {
  135. ItemStack itemstack = this.getRecipeOutput().copy();
  136. if (this.copyIngredientNBT)
  137. {
  138. for (int i = 0; i < inv.getSizeInventory(); ++i)
  139. {
  140. ItemStack itemstack1 = inv.getStackInSlot(i);
  141. if (itemstack1 != null && itemstack1.hasTagCompound())
  142. {
  143. itemstack.setTagCompound((NBTTagCompound)itemstack1.getTagCompound().copy());
  144. }
  145. }
  146. }
  147. return itemstack;
  148. }
  149.  
  150. /**
  151. * Returns the size of the recipe area
  152. */
  153. public int getRecipeSize()
  154. {
  155. return this.recipeWidth * this.recipeHeight;
  156. }
  157.  
  158. /**
  159. * Set this crafting recipe to copy the NBT tag compound of the last ItemStack that has one in the crafting table.
  160. */
  161. public TutorielShapedRecipes setCopyIngredientNBT()
  162. {
  163. this.copyIngredientNBT = true;
  164. return this;
  165. }
  166.  
  167.  
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement