Skip to content

Commit 750703f

Browse files
committed
support foresty
1 parent d977fc7 commit 750703f

File tree

5 files changed

+177
-4
lines changed

5 files changed

+177
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.glodblock.github.nei.object;
2+
3+
public interface IRecipeExtractorLegacy extends IRecipeExtractor {
4+
5+
String getClassName();
6+
7+
}

src/main/java/com/glodblock/github/nei/recipes/DefaultExtractorLoader.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.glodblock.github.nei.recipes;
22

3-
import com.glodblock.github.nei.recipes.extractor.EnderIORecipeExtractor;
4-
import com.glodblock.github.nei.recipes.extractor.GregTech5RecipeExtractor;
5-
import com.glodblock.github.nei.recipes.extractor.GregTech6RecipeExtractor;
6-
import com.glodblock.github.nei.recipes.extractor.VanillaRecipeExtractor;
3+
import com.glodblock.github.nei.recipes.extractor.*;
74
import com.glodblock.github.util.ModAndClassUtil;
5+
import forestry.factory.recipes.nei.*;
86
import gregapi.recipes.Recipe;
97
import gregtech.api.util.GT_Recipe;
108

@@ -38,6 +36,17 @@ public void run() {
3836
FluidRecipe.addRecipeMap("EnderIOVat", new EnderIORecipeExtractor());
3937
}
4038

39+
if (ModAndClassUtil.FTR) {
40+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerBottler()));
41+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerCarpenter()));
42+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerCentrifuge()));
43+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerFabricator()));
44+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerFermenter()));
45+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerMoistener()));
46+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerSqueezer()));
47+
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerStill()));
48+
}
49+
4150
}
4251

4352
}

src/main/java/com/glodblock/github/nei/recipes/FluidRecipe.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import codechicken.nei.recipe.IRecipeHandler;
55
import codechicken.nei.recipe.TemplateRecipeHandler;
66
import com.glodblock.github.nei.object.IRecipeExtractor;
7+
import com.glodblock.github.nei.object.IRecipeExtractorLegacy;
78
import com.glodblock.github.nei.object.OrderStack;
89

910
import java.util.ArrayList;
@@ -14,14 +15,19 @@
1415
public final class FluidRecipe {
1516

1617
private static final HashMap<String, IRecipeExtractor> IdentifierMap = new HashMap<>();
18+
private static final HashMap<String, IRecipeExtractor> IdentifierMapLegacy = new HashMap<>();
1719

1820
public static void addRecipeMap(String recipeIdentifier, IRecipeExtractor extractor) {
1921
IdentifierMap.put(recipeIdentifier, extractor);
22+
if (recipeIdentifier == null && extractor instanceof IRecipeExtractorLegacy) {
23+
IdentifierMapLegacy.put(((IRecipeExtractorLegacy) extractor).getClassName(), extractor);
24+
}
2025
}
2126

2227
public static List<OrderStack<?>> getPackageInputs(IRecipeHandler recipe, int index) {
2328
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
2429
if (tRecipe == null || !IdentifierMap.containsKey(tRecipe.getOverlayIdentifier())) return new ArrayList<>();
30+
if (tRecipe.getOverlayIdentifier() == null) return getPackageInputsLegacy(recipe, index);
2531
IRecipeExtractor extractor = IdentifierMap.get(tRecipe.getOverlayIdentifier());
2632
if (extractor == null) return new ArrayList<>();
2733
List<PositionedStack> tmp = new ArrayList<>(tRecipe.getIngredientStacks(index));
@@ -31,6 +37,7 @@ public static List<OrderStack<?>> getPackageInputs(IRecipeHandler recipe, int in
3137
public static List<OrderStack<?>> getPackageOutputs(IRecipeHandler recipe, int index, boolean useOther) {
3238
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
3339
if (tRecipe == null || !IdentifierMap.containsKey(tRecipe.getOverlayIdentifier())) return new ArrayList<>();
40+
if (tRecipe.getOverlayIdentifier() == null) return getPackageOutputsLegacy(recipe, index, useOther);
3441
IRecipeExtractor extractor = IdentifierMap.get(tRecipe.getOverlayIdentifier());
3542
if (extractor == null) return new ArrayList<>();
3643
List<PositionedStack> tmp = new ArrayList<>(Collections.singleton(tRecipe.getResultStack(index)));
@@ -42,4 +49,22 @@ public static List<String> getSupportRecipes() {
4249
return new ArrayList<>(IdentifierMap.keySet());
4350
}
4451

52+
public static List<OrderStack<?>> getPackageInputsLegacy(IRecipeHandler recipe, int index) {
53+
if (recipe == null || !IdentifierMapLegacy.containsKey(recipe.getHandlerId())) return new ArrayList<>();
54+
IRecipeExtractor extractor = IdentifierMapLegacy.get(recipe.getHandlerId());
55+
if (extractor == null) return new ArrayList<>();
56+
List<PositionedStack> tmp = new ArrayList<>(recipe.getIngredientStacks(index));
57+
return extractor.getInputIngredients(tmp, recipe, index);
58+
}
59+
60+
public static List<OrderStack<?>> getPackageOutputsLegacy(IRecipeHandler recipe, int index, boolean useOther) {
61+
if (recipe == null || !IdentifierMapLegacy.containsKey(recipe.getHandlerId())) return new ArrayList<>();
62+
IRecipeExtractor extractor = IdentifierMapLegacy.get(recipe.getHandlerId());
63+
if (extractor == null) return new ArrayList<>();
64+
List<PositionedStack> tmp = new ArrayList<>(Collections.singleton(recipe.getResultStack(index)));
65+
if (useOther) tmp.addAll(recipe.getOtherStacks(index));
66+
return extractor.getOutputIngredients(tmp, recipe, index);
67+
}
68+
69+
4570
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.glodblock.github.nei.recipes.extractor;
2+
3+
import codechicken.nei.PositionedStack;
4+
import codechicken.nei.recipe.IRecipeHandler;
5+
import codechicken.nei.recipe.TemplateRecipeHandler;
6+
import com.glodblock.github.nei.object.IRecipeExtractorLegacy;
7+
import com.glodblock.github.nei.object.OrderStack;
8+
import crazypants.enderio.nei.VatRecipeHandler;
9+
import forestry.core.recipes.nei.PositionedFluidTank;
10+
import forestry.core.recipes.nei.RecipeHandlerBase;
11+
import forestry.factory.recipes.nei.NEIHandlerFabricator;
12+
import forestry.factory.recipes.nei.NEIHandlerSqueezer;
13+
import net.minecraft.item.ItemStack;
14+
import net.minecraftforge.fluids.FluidStack;
15+
16+
import java.util.ArrayList;
17+
import java.util.LinkedList;
18+
import java.util.List;
19+
import java.util.Objects;
20+
import java.util.stream.Collectors;
21+
22+
public class ForestryRecipeExtractor implements IRecipeExtractorLegacy {
23+
24+
private final IRecipeHandler handler;
25+
26+
public ForestryRecipeExtractor(IRecipeHandler recipes) {
27+
handler = recipes;
28+
}
29+
30+
@Override
31+
public String getClassName() {
32+
return handler.getHandlerId();
33+
}
34+
35+
@Override
36+
public List<OrderStack<?>> getInputIngredients(List<PositionedStack> rawInputs) {
37+
List<OrderStack<?>> tmp = new LinkedList<>();
38+
for (int i = 0; i < rawInputs.size(); i ++) {
39+
if (rawInputs.get(i) == null) continue;
40+
OrderStack<?> stack = OrderStack.pack(rawInputs.get(i), i);
41+
if (stack != null) tmp.add(stack);
42+
}
43+
return tmp;
44+
}
45+
46+
@Override
47+
public List<OrderStack<?>> getOutputIngredients(List<PositionedStack> rawOutputs) {
48+
List<OrderStack<?>> tmp = new LinkedList<>();
49+
for (int i = 0; i < rawOutputs.size(); i ++) {
50+
if (rawOutputs.get(i) == null) continue;
51+
OrderStack<?> stack = OrderStack.pack(rawOutputs.get(i), i);
52+
if (stack != null) tmp.add(stack);
53+
}
54+
return tmp;
55+
}
56+
57+
@Override
58+
public List<OrderStack<?>> getInputIngredients(List<PositionedStack> rawInputs, IRecipeHandler recipe, int index) {
59+
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
60+
List<OrderStack<?>> tmp = new ArrayList<>();
61+
List<PositionedStack> compressed = compress(rawInputs);
62+
if (tRecipe.arecipes.get(index) instanceof RecipeHandlerBase.CachedBaseRecipe) {
63+
tmp = getInputIngredients(compressed);
64+
List<PositionedFluidTank> tanks = ((RecipeHandlerBase.CachedBaseRecipe) tRecipe.arecipes.get(index)).getFluidTanks();
65+
if (tanks.size() > 0 && !(handler instanceof NEIHandlerSqueezer)) {
66+
FluidStack fluid = tanks.get(0).tank.getFluid();
67+
if (fluid != null) {
68+
tmp.add(new OrderStack<>(fluid, compressed.size()));
69+
}
70+
}
71+
}
72+
return tmp;
73+
}
74+
75+
@Override
76+
public List<OrderStack<?>> getOutputIngredients(List<PositionedStack> rawOutputs, IRecipeHandler recipe, int index) {
77+
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
78+
removeGlass(rawOutputs);
79+
List<OrderStack<?>> tmp = new ArrayList<>();
80+
List<PositionedStack> compressed = compress(rawOutputs);
81+
if (tRecipe.arecipes.get(index) instanceof RecipeHandlerBase.CachedBaseRecipe) {
82+
tmp = getOutputIngredients(compressed);
83+
List<PositionedFluidTank> tanks = ((RecipeHandlerBase.CachedBaseRecipe) tRecipe.arecipes.get(index)).getFluidTanks();
84+
if (tanks.size() > 0 && handler instanceof NEIHandlerSqueezer) {
85+
FluidStack fluid = tanks.get(0).tank.getFluid();
86+
if (fluid != null) {
87+
tmp.add(new OrderStack<>(fluid, compressed.size()));
88+
}
89+
}
90+
else if (tanks.size() > 1) {
91+
FluidStack fluid = tanks.get(1).tank.getFluid();
92+
if (fluid != null) {
93+
tmp.add(new OrderStack<>(fluid, compressed.size()));
94+
}
95+
}
96+
}
97+
return tmp;
98+
}
99+
100+
private List<PositionedStack> compress(List<PositionedStack> list) {
101+
List<PositionedStack> comp = new LinkedList<>();
102+
for (PositionedStack positionedStack : list) {
103+
if (positionedStack == null) continue;
104+
ItemStack currentStack = positionedStack.items[0].copy();
105+
if (currentStack.stackSize == 0) continue;
106+
boolean find = false;
107+
for (PositionedStack storedStack : comp) {
108+
if (storedStack == null) continue;
109+
ItemStack firstStack = storedStack.items[0].copy();
110+
boolean areItemStackEqual = firstStack.isItemEqual(currentStack) && ItemStack.areItemStackTagsEqual(firstStack, currentStack);
111+
if (areItemStackEqual && (firstStack.stackSize + currentStack.stackSize) <= firstStack.getMaxStackSize()) {
112+
find = true;
113+
storedStack.items[0].stackSize = firstStack.stackSize + currentStack.stackSize;
114+
}
115+
}
116+
if (!find) {
117+
comp.add(positionedStack.copy());
118+
}
119+
}
120+
return comp.stream().filter(Objects::nonNull).collect(Collectors.toList());
121+
}
122+
123+
private void removeGlass(List<PositionedStack> list) {
124+
if (handler instanceof NEIHandlerFabricator) {
125+
list.remove(list.size() - 1);
126+
}
127+
}
128+
129+
}

src/main/java/com/glodblock/github/util/ModAndClassUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public final class ModAndClassUtil {
1414
public static boolean GT6 = false;
1515
public static boolean EC2 = false;
1616
public static boolean EIO = false;
17+
public static boolean FTR = false;
1718

1819
public static boolean isDoubleButton;
1920
public static boolean isSaveText;
@@ -71,6 +72,8 @@ public static void init() {
7172
EC2 = true;
7273
if (Loader.isModLoaded("EnderIO"))
7374
EIO = true;
75+
if (Loader.isModLoaded("Forestry"))
76+
FTR = true;
7477

7578
}
7679

0 commit comments

Comments
 (0)