summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/nei/FBSShapelessRecipeHandler.java
blob: 950c1fbd3e46187056dd565ae87aa947fc40fafe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package jp.plusplus.fbs.nei;

import codechicken.nei.NEIClientUtils;
import codechicken.nei.NEIServerUtils;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.ShapedRecipeHandler;
import jp.plusplus.fbs.FBS;
import jp.plusplus.fbs.Registry;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.oredict.ShapelessOreRecipe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Createdby pluslus_Fon 2015/06/15.
 */
public class FBSShapelessRecipeHandler  extends FBSShapedRecipeHandler {
    public int[][] stackorder = new int[][]{{0, 0}, {1, 0}, {0, 1}, {1, 1}, {0, 2}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};

    public FBSShapelessRecipeHandler() {
    }

    public String getRecipeName() {
        return NEIClientUtils.translate("recipe.fbs.shapeless", new Object[0]);
    }

    public void loadCraftingRecipes(String outputId, Object... results) {
        if(outputId.equals("fbs.crafting") && this.getClass() == FBSShapelessRecipeHandler.class) {
            Iterator i$ = Registry.getCraftingRecipes().iterator();

            while(i$.hasNext()) {
                Registry.RecipePair rp=((Registry.RecipePair)i$.next());
                IRecipe irecipe = rp.recipe;
                FBSShapelessRecipeHandler.CachedShapelessRecipe recipe = null;
                if(irecipe instanceof ShapelessRecipes) {
                    recipe = this.shapelessRecipe((ShapelessRecipes)irecipe);
                } else if(irecipe instanceof ShapelessOreRecipe) {
                    recipe = this.forgeShapelessRecipe((ShapelessOreRecipe)irecipe);
                }

                if(recipe != null) {
                    this.arecipes.add(recipe);
                    manas.add(rp.mana);
                }
            }
        } else {
            super.loadCraftingRecipes(outputId, results);
        }

    }

    public void loadCraftingRecipes(ItemStack result) {
        Iterator i$ = Registry.getCraftingRecipes().iterator();

        while(i$.hasNext()) {
            Registry.RecipePair rp=((Registry.RecipePair)i$.next());
            IRecipe irecipe = rp.recipe;
            if(NEIServerUtils.areStacksSameTypeCrafting(irecipe.getRecipeOutput(), result)) {
                FBSShapelessRecipeHandler.CachedShapelessRecipe recipe = null;
                if(irecipe instanceof ShapelessRecipes) {
                    recipe = this.shapelessRecipe((ShapelessRecipes)irecipe);
                } else if(irecipe instanceof ShapelessOreRecipe) {
                    recipe = this.forgeShapelessRecipe((ShapelessOreRecipe)irecipe);
                }

                if(recipe != null) {
                    this.arecipes.add(recipe);
                    manas.add(rp.mana);
                }
            }
        }

    }

    public void loadUsageRecipes(ItemStack ingredient) {
        Iterator i$ = Registry.getCraftingRecipes().iterator();

        while(i$.hasNext()) {
            Registry.RecipePair rp=((Registry.RecipePair)i$.next());
            IRecipe irecipe = rp.recipe;
            FBSShapelessRecipeHandler.CachedShapelessRecipe recipe = null;
            if(irecipe instanceof ShapelessRecipes) {
                recipe = this.shapelessRecipe((ShapelessRecipes)irecipe);
            } else if(irecipe instanceof ShapelessOreRecipe) {
                recipe = this.forgeShapelessRecipe((ShapelessOreRecipe)irecipe);
            }

            if(recipe != null && recipe.contains(recipe.ingredients, ingredient)) {
                recipe.setIngredientPermutation(recipe.ingredients, ingredient);
                this.arecipes.add(recipe);
                manas.add(rp.mana);
            }
        }

    }

    private FBSShapelessRecipeHandler.CachedShapelessRecipe shapelessRecipe(ShapelessRecipes recipe) {
        return recipe.recipeItems == null?null:new FBSShapelessRecipeHandler.CachedShapelessRecipe(recipe.recipeItems, recipe.getRecipeOutput());
    }

    public FBSShapelessRecipeHandler.CachedShapelessRecipe forgeShapelessRecipe(ShapelessOreRecipe recipe) {
        ArrayList items = recipe.getInput();
        Iterator i$ = items.iterator();

        Object item;
        do {
            if(!i$.hasNext()) {
                return new FBSShapelessRecipeHandler.CachedShapelessRecipe(items, recipe.getRecipeOutput());
            }

            item = i$.next();
        } while(!(item instanceof List) || !((List)item).isEmpty());

        return null;
    }

    public boolean isRecipe2x2(int recipe) {
        return this.getIngredientStacks(recipe).size() <= 4;
    }

    public class CachedShapelessRecipe extends CachedRecipe {
        public ArrayList<PositionedStack> ingredients;
        public PositionedStack result;

        public CachedShapelessRecipe() {
            //super(FBSShapelessRecipeHandler.this);
            this.ingredients = new ArrayList();
        }

        public CachedShapelessRecipe(ItemStack output) {
            this();
            this.setResult(output);
        }

        public CachedShapelessRecipe(Object[] input, ItemStack output) {
            this((List) Arrays.asList(input), output);
        }

        public CachedShapelessRecipe(List input, ItemStack output) {
            this(output);
            this.setIngredients(input);
        }

        public void setIngredients(List<?> items) {
            this.ingredients.clear();

            for(int ingred = 0; ingred < items.size(); ++ingred) {
                PositionedStack stack = new PositionedStack(items.get(ingred), 25 + FBSShapelessRecipeHandler.this.stackorder[ingred][0] * 18, 6 + FBSShapelessRecipeHandler.this.stackorder[ingred][1] * 18);
                stack.setMaxSize(1);
                this.ingredients.add(stack);
            }

        }

        public void setResult(ItemStack output) {
            this.result = new PositionedStack(output, 119, 24);
        }

        public List<PositionedStack> getIngredients() {
            return this.getCycledIngredients(FBSShapelessRecipeHandler.this.cycleticks / 20, this.ingredients);
        }

        public PositionedStack getResult() {
            return this.result;
        }
    }
}