|
| 1 | +package recipenator.compatibility.nei; |
| 2 | + |
| 3 | +import codechicken.nei.NEIServerUtils; |
| 4 | +import codechicken.nei.api.API; |
| 5 | +import codechicken.nei.recipe.ShapedRecipeHandler; |
| 6 | +import codechicken.nei.recipe.ShapelessRecipeHandler; |
| 7 | +import net.minecraft.item.ItemStack; |
| 8 | +import net.minecraft.item.crafting.CraftingManager; |
| 9 | +import recipenator.lualib.recipe.RecipeShaped; |
| 10 | +import recipenator.lualib.recipe.RecipeShapeless; |
| 11 | +import recipenator.utils.RecipeHelper; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class RecipeHandlers { |
| 16 | + public static void registerRecipe() { |
| 17 | + API.registerRecipeHandler(new ShapedHandler()); |
| 18 | + API.registerUsageHandler(new ShapedHandler()); |
| 19 | + API.registerRecipeHandler(new ShapelessHandler()); |
| 20 | + API.registerUsageHandler(new ShapelessHandler()); |
| 21 | + } |
| 22 | + |
| 23 | + public static class ShapelessHandler extends ShapelessRecipeHandler { |
| 24 | + @Override |
| 25 | + public void loadCraftingRecipes(String outputId, Object... results) { |
| 26 | + if (outputId.equals("crafting") && getClass() == ShapelessHandler.class) { |
| 27 | + List recipeList = CraftingManager.getInstance().getRecipeList(); |
| 28 | + for (Object recipe : recipeList) { |
| 29 | + if (!(recipe instanceof RecipeShapeless)) continue; |
| 30 | + CachedShapelessRecipe cache = toCachedRecipe((RecipeShapeless) recipe); |
| 31 | + arecipes.add(cache); |
| 32 | + } |
| 33 | + } else { |
| 34 | + super.loadCraftingRecipes(outputId, results); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void loadCraftingRecipes(ItemStack result) { |
| 40 | + List recipeList = CraftingManager.getInstance().getRecipeList(); |
| 41 | + for (Object recipe : recipeList) { |
| 42 | + if (!(recipe instanceof RecipeShapeless)) continue; |
| 43 | + |
| 44 | + RecipeShapeless recipeShapeless = (RecipeShapeless) recipe; |
| 45 | + if (!NEIServerUtils.areStacksSameTypeCrafting(recipeShapeless.getRecipeOutput(), result)) continue; |
| 46 | + |
| 47 | + CachedShapelessRecipe cache = toCachedRecipe(recipeShapeless); |
| 48 | + arecipes.add(cache); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void loadUsageRecipes(ItemStack ingredient) { |
| 54 | + List recipeList = CraftingManager.getInstance().getRecipeList(); |
| 55 | + for (Object recipe : recipeList) { |
| 56 | + if (!(recipe instanceof RecipeShapeless)) continue; |
| 57 | + |
| 58 | + CachedShapelessRecipe cache = toCachedRecipe((RecipeShapeless) recipe); |
| 59 | + if (cache.contains(cache.ingredients, ingredient)) { |
| 60 | + cache.setIngredientPermutation(cache.ingredients, ingredient); |
| 61 | + this.arecipes.add(cache); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private CachedShapelessRecipe toCachedRecipe(RecipeShapeless recipe) { |
| 67 | + return new CachedShapelessRecipe(recipe.getRecipeInputs(), recipe.getRecipeOutput()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class ShapedHandler extends ShapedRecipeHandler { |
| 72 | + @Override |
| 73 | + public void loadCraftingRecipes(String outputId, Object... results) { |
| 74 | + if (outputId.equals("crafting") && getClass() == ShapedHandler.class) { |
| 75 | + List recipeList = CraftingManager.getInstance().getRecipeList(); |
| 76 | + for (Object irecipe : recipeList) { |
| 77 | + if (!(irecipe instanceof RecipeShaped)) continue; |
| 78 | + CachedShapedRecipe recipe = toCachedRecipe((RecipeShaped) irecipe); |
| 79 | + recipe.computeVisuals(); |
| 80 | + arecipes.add(recipe); |
| 81 | + } |
| 82 | + } else { |
| 83 | + super.loadCraftingRecipes(outputId, results); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void loadCraftingRecipes(ItemStack result) { |
| 89 | + List recipeList = CraftingManager.getInstance().getRecipeList(); |
| 90 | + for (Object recipe : recipeList) { |
| 91 | + if (!(recipe instanceof RecipeShaped)) continue; |
| 92 | + |
| 93 | + RecipeShaped recipeShaped = (RecipeShaped) recipe; |
| 94 | + if (!NEIServerUtils.areStacksSameTypeCrafting(recipeShaped.getRecipeOutput(), result)) continue; |
| 95 | + |
| 96 | + CachedShapedRecipe cache = toCachedRecipe(recipeShaped); |
| 97 | + cache.computeVisuals(); |
| 98 | + arecipes.add(cache); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void loadUsageRecipes(ItemStack ingredient) { |
| 104 | + List recipeList = CraftingManager.getInstance().getRecipeList(); |
| 105 | + for (Object recipe : recipeList) { |
| 106 | + if (!(recipe instanceof RecipeShaped)) continue; |
| 107 | + |
| 108 | + CachedShapedRecipe cache = toCachedRecipe((RecipeShaped) recipe); |
| 109 | + if (!cache.contains(cache.ingredients, ingredient.getItem())) continue; |
| 110 | + |
| 111 | + cache.computeVisuals(); |
| 112 | + if (cache.contains(cache.ingredients, ingredient)) { |
| 113 | + cache.setIngredientPermutation(cache.ingredients, ingredient); |
| 114 | + this.arecipes.add(cache); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private CachedShapedRecipe toCachedRecipe(RecipeShaped recipe) { |
| 120 | + RecipeHelper.Size size = recipe.getSize(); |
| 121 | + return new CachedShapedRecipe(size.width, size.height, recipe.getRecipeInputs(), recipe.getRecipeOutput()); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments