PageRenderTime 48ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/common/buildcraft/lib/client/guide/parts/recipe/GuideCraftingRecipes.java

https://github.com/2xsaiko/BuildCraft
Java | 102 lines | 82 code | 14 blank | 6 comment | 24 complexity | 87c6a5052b8d577554744c4cff231671 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /*
  2. * Copyright (c) 2017 SpaceToad and the BuildCraft team
  3. * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
  4. * distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/
  5. */
  6. package buildcraft.lib.client.guide.parts.recipe;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import javax.annotation.Nonnull;
  10. import javax.annotation.Nullable;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.item.crafting.IRecipe;
  13. import net.minecraft.item.crafting.Ingredient;
  14. import net.minecraft.util.NonNullList;
  15. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  16. import net.minecraftforge.oredict.OreDictionary;
  17. import buildcraft.lib.client.guide.parts.GuidePartFactory;
  18. import buildcraft.lib.misc.StackUtil;
  19. import buildcraft.lib.recipe.ChangingItemStack;
  20. import buildcraft.lib.recipe.IRecipeViewable;
  21. public enum GuideCraftingRecipes implements IStackRecipes {
  22. INSTANCE;
  23. @Override
  24. public List<GuidePartFactory> getUsages(@Nonnull ItemStack target) {
  25. List<GuidePartFactory> list = new ArrayList<>();
  26. for (IRecipe recipe : ForgeRegistries.RECIPES) {
  27. if (checkRecipeUses(recipe, target)) {
  28. GuidePartFactory factory = GuideCraftingFactory.getFactory(recipe);
  29. if (factory != null) {
  30. list.add(factory);
  31. }
  32. }
  33. }
  34. return list;
  35. }
  36. private static boolean checkRecipeUses(IRecipe recipe, @Nonnull ItemStack target) {
  37. NonNullList<Ingredient> ingrediants = recipe.getIngredients();
  38. if (ingrediants.isEmpty()) {
  39. if (recipe instanceof IRecipeViewable) {
  40. // TODO!
  41. }
  42. }
  43. for (Ingredient ing : ingrediants) {
  44. if (ing.test(target)) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. private static boolean matches(@Nonnull ItemStack target, @Nullable Object in) {
  51. if (in instanceof ItemStack) {
  52. return StackUtil.doesEitherStackMatch((ItemStack) in, target);
  53. } else if (in instanceof List) {
  54. for (Object obj : (List<?>) in) {
  55. if (obj instanceof ItemStack) {
  56. if (StackUtil.doesEitherStackMatch((ItemStack) obj, target)) {
  57. return true;
  58. }
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64. @Override
  65. public List<GuidePartFactory> getRecipes(@Nonnull ItemStack target) {
  66. List<GuidePartFactory> list = new ArrayList<>();
  67. for (IRecipe recipe : ForgeRegistries.RECIPES) {
  68. if (recipe instanceof IRecipeViewable) {
  69. ChangingItemStack changing = ((IRecipeViewable) recipe).getRecipeOutputs();
  70. if (changing.matches(target)) {
  71. GuidePartFactory factory = GuideCraftingFactory.getFactory(recipe);
  72. if (factory != null) {
  73. list.add(factory);
  74. }
  75. }
  76. } else {
  77. ItemStack out = StackUtil.asNonNull(recipe.getRecipeOutput());
  78. if (OreDictionary.itemMatches(target, out, false) || OreDictionary.itemMatches(out, target, false)) {
  79. GuidePartFactory factory = GuideCraftingFactory.getFactory(recipe);
  80. if (factory != null) {
  81. list.add(factory);
  82. }
  83. }
  84. }
  85. }
  86. return list;
  87. }
  88. }