Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. package ed.enderdeath.mod.AnvilDragon;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import com.google.common.collect.Lists;
  7. import com.google.common.collect.Maps;
  8. import com.sun.istack.internal.Nullable;
  9.  
  10. import net.minecraft.block.Block;
  11. import net.minecraft.init.Blocks;
  12. import net.minecraft.init.Items;
  13. import net.minecraft.inventory.InventoryCrafting;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.item.crafting.IRecipe;
  17. import net.minecraft.world.World;
  18.  
  19. public class TutorielCraftingManager {
  20. private static final TutorielCraftingManager INSTANCE = new TutorielCraftingManager();
  21. public static TutorielCraftingManager getInstance()
  22. {
  23. return INSTANCE;
  24. }
  25.  
  26. /** La liste des recettes */
  27. private final List<IRecipe> recipes = Lists.<IRecipe>newArrayList();
  28. private Block cratingTable;
  29.  
  30. private TutorielCraftingManager()
  31. {
  32. TutorielCraftingManager.getInstance().addShapelessRecipe(new ItemStack(Blocks.anvil), Items.carrot, Items.golden_apple);
  33. TutorielCraftingManager.getInstance().addRecipe(new ItemStack(cratingTable), " C ", "X X", " C ", 'C', "slabWood", 'X', Blocks.planks); //"slabWood" est le nom des dalles de bois dans l'ore dictionnary, ceci est ajouté par Forge
  34. TutorielCraftingManager.getInstance().addRecipe(new ItemStack(Items.golden_apple), "A A", "X X", "X X", "A A", 'A', Items.carrot, 'X', Blocks.planks);
  35. }
  36. public TutorielShapedRecipes addRecipe(ItemStack result, Object... recipeComponents)
  37. {
  38. String s = "";
  39. int i = 0;
  40. int j = 0;
  41. int k = 0;
  42. if (recipeComponents[i] instanceof String[])
  43. {
  44. String[] astring = (String[])((String[])recipeComponents[i++]);
  45. for (int l = 0; l < astring.length; ++l)
  46. {
  47. String s2 = astring[l];
  48. ++k;
  49. j = s2.length();
  50. s = s + s2;
  51. }
  52. }
  53. else
  54. {
  55. while (recipeComponents[i] instanceof String)
  56. {
  57. String s1 = (String)recipeComponents[i++];
  58. ++k;
  59. j = s1.length();
  60. s = s + s1;
  61. }
  62. }
  63. Character character;
  64. Map<Character, Object> components = Maps.<Character, Object>newHashMap();
  65. Object in;
  66. for ( ; i < recipeComponents.length; i += 2)
  67. {
  68. in = recipeComponents[i + 1];
  69. Object component = null;
  70. character = (Character)recipeComponents[i];
  71. if (in instanceof Item)
  72. {
  73. component = new ItemStack((Item)recipeComponents[i + 1]);
  74. }
  75. else if (in instanceof Block)
  76. {
  77. component = new ItemStack((Block)recipeComponents[i + 1], 1, 32767);
  78. }
  79. else if (in instanceof ItemStack)
  80. {
  81. component = (ItemStack)recipeComponents[i + 1];
  82. }
  83. else if (in instanceof String)
  84. {
  85. component = (String)in;
  86. }
  87. else
  88. {
  89. throw new IllegalArgumentException("Invalid shaped recipe: unknown type " + in.getClass().getName() + "!");
  90. }
  91. components.put(character, component);
  92. }
  93. Object[] aitemstack = new Object[j * k];
  94. char key;
  95. Object component;
  96. for (int i1 = 0; i1 < j * k; ++i1)
  97. {
  98. key = s.charAt(i1);
  99. if (components.containsKey(Character.valueOf(key)))
  100. {
  101. component = components.get(Character.valueOf(key));
  102. if(component instanceof ItemStack)
  103. aitemstack[i1] = ((ItemStack)component).copy();
  104. else
  105. aitemstack[i1] = component;
  106. }
  107. else
  108. aitemstack[i1] = null;
  109. }
  110. TutorielShapedRecipes shapedrecipes = new TutorielShapedRecipes(j, k, aitemstack, result);
  111. this.recipes.add(shapedrecipes);
  112. return shapedrecipes;
  113. }
  114.  
  115.  
  116. public void addShapelessRecipe(ItemStack result, Object... recipeComponents)
  117. {
  118. List list = Lists.newArrayList();
  119. for (Object component : recipeComponents) //Pour chaque composant de la recette
  120. {
  121. if (component instanceof ItemStack)
  122. {
  123. list.add(((ItemStack)component).copy());
  124. }
  125. else if (component instanceof Item)
  126. {
  127. list.add(new ItemStack((Item)component));
  128. }
  129. else if(component instanceof Block)
  130. {
  131. list.add(new ItemStack((Block)component));
  132. }
  133. else if(component instanceof String) //Pour l'ore dictionnary
  134. {
  135. list.add(component);
  136. }
  137. else throw new IllegalArgumentException("Invalid shapeless recipe: unknown type " + component.getClass().getName() + "!");
  138. }
  139. this.recipes.add(new TutorielShapelessRecipe(result, list));
  140. }
  141. /**
  142. * Adds a shaped recipe to the games recipe list.
  143. */
  144.  
  145. /**
  146. * Adds an IRecipe to the list of crafting recipes.
  147. */
  148. public void addRecipe(IRecipe recipe)
  149. {
  150. this.recipes.add(recipe);
  151. }
  152.  
  153. /**
  154. * Retourne le résultat de la recette ou null si il n'y en a aucun
  155. */
  156. @Nullable
  157. public ItemStack findMatchingRecipe(InventoryCrafting craftMatrix, World worldIn)
  158. {
  159. for (IRecipe irecipe : this.recipes) //Pour chaque recette
  160. {
  161. if (irecipe.matches(craftMatrix, worldIn)) //Si elle correspond à la matrice actuelle
  162. {
  163. return irecipe.getCraftingResult(craftMatrix); //On donne son résultat
  164. }
  165. }
  166. return null;
  167. }
  168.  
  169. /**
  170. * Retourne les items retants après un craft
  171. */
  172.  
  173. public List<IRecipe> getRecipeList()
  174. {
  175. return this.recipes;
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement