Guest User

Untitled

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