Skip to content

Commit

Permalink
support foresty
Browse files Browse the repository at this point in the history
  • Loading branch information
GlodBlock committed May 12, 2022
1 parent d977fc7 commit 750703f
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.glodblock.github.nei.object;

public interface IRecipeExtractorLegacy extends IRecipeExtractor {

String getClassName();

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.glodblock.github.nei.recipes;

import com.glodblock.github.nei.recipes.extractor.EnderIORecipeExtractor;
import com.glodblock.github.nei.recipes.extractor.GregTech5RecipeExtractor;
import com.glodblock.github.nei.recipes.extractor.GregTech6RecipeExtractor;
import com.glodblock.github.nei.recipes.extractor.VanillaRecipeExtractor;
import com.glodblock.github.nei.recipes.extractor.*;
import com.glodblock.github.util.ModAndClassUtil;
import forestry.factory.recipes.nei.*;
import gregapi.recipes.Recipe;
import gregtech.api.util.GT_Recipe;

Expand Down Expand Up @@ -38,6 +36,17 @@ public void run() {
FluidRecipe.addRecipeMap("EnderIOVat", new EnderIORecipeExtractor());
}

if (ModAndClassUtil.FTR) {
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerBottler()));
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerCarpenter()));
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerCentrifuge()));
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerFabricator()));
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerFermenter()));
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerMoistener()));
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerSqueezer()));
FluidRecipe.addRecipeMap(null, new ForestryRecipeExtractor(new NEIHandlerStill()));
}

}

}
25 changes: 25 additions & 0 deletions src/main/java/com/glodblock/github/nei/recipes/FluidRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import codechicken.nei.recipe.IRecipeHandler;
import codechicken.nei.recipe.TemplateRecipeHandler;
import com.glodblock.github.nei.object.IRecipeExtractor;
import com.glodblock.github.nei.object.IRecipeExtractorLegacy;
import com.glodblock.github.nei.object.OrderStack;

import java.util.ArrayList;
Expand All @@ -14,14 +15,19 @@
public final class FluidRecipe {

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

public static void addRecipeMap(String recipeIdentifier, IRecipeExtractor extractor) {
IdentifierMap.put(recipeIdentifier, extractor);
if (recipeIdentifier == null && extractor instanceof IRecipeExtractorLegacy) {
IdentifierMapLegacy.put(((IRecipeExtractorLegacy) extractor).getClassName(), extractor);
}
}

public static List<OrderStack<?>> getPackageInputs(IRecipeHandler recipe, int index) {
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
if (tRecipe == null || !IdentifierMap.containsKey(tRecipe.getOverlayIdentifier())) return new ArrayList<>();
if (tRecipe.getOverlayIdentifier() == null) return getPackageInputsLegacy(recipe, index);
IRecipeExtractor extractor = IdentifierMap.get(tRecipe.getOverlayIdentifier());
if (extractor == null) return new ArrayList<>();
List<PositionedStack> tmp = new ArrayList<>(tRecipe.getIngredientStacks(index));
Expand All @@ -31,6 +37,7 @@ public static List<OrderStack<?>> getPackageInputs(IRecipeHandler recipe, int in
public static List<OrderStack<?>> getPackageOutputs(IRecipeHandler recipe, int index, boolean useOther) {
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
if (tRecipe == null || !IdentifierMap.containsKey(tRecipe.getOverlayIdentifier())) return new ArrayList<>();
if (tRecipe.getOverlayIdentifier() == null) return getPackageOutputsLegacy(recipe, index, useOther);
IRecipeExtractor extractor = IdentifierMap.get(tRecipe.getOverlayIdentifier());
if (extractor == null) return new ArrayList<>();
List<PositionedStack> tmp = new ArrayList<>(Collections.singleton(tRecipe.getResultStack(index)));
Expand All @@ -42,4 +49,22 @@ public static List<String> getSupportRecipes() {
return new ArrayList<>(IdentifierMap.keySet());
}

public static List<OrderStack<?>> getPackageInputsLegacy(IRecipeHandler recipe, int index) {
if (recipe == null || !IdentifierMapLegacy.containsKey(recipe.getHandlerId())) return new ArrayList<>();
IRecipeExtractor extractor = IdentifierMapLegacy.get(recipe.getHandlerId());
if (extractor == null) return new ArrayList<>();
List<PositionedStack> tmp = new ArrayList<>(recipe.getIngredientStacks(index));
return extractor.getInputIngredients(tmp, recipe, index);
}

public static List<OrderStack<?>> getPackageOutputsLegacy(IRecipeHandler recipe, int index, boolean useOther) {
if (recipe == null || !IdentifierMapLegacy.containsKey(recipe.getHandlerId())) return new ArrayList<>();
IRecipeExtractor extractor = IdentifierMapLegacy.get(recipe.getHandlerId());
if (extractor == null) return new ArrayList<>();
List<PositionedStack> tmp = new ArrayList<>(Collections.singleton(recipe.getResultStack(index)));
if (useOther) tmp.addAll(recipe.getOtherStacks(index));
return extractor.getOutputIngredients(tmp, recipe, index);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.glodblock.github.nei.recipes.extractor;

import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.IRecipeHandler;
import codechicken.nei.recipe.TemplateRecipeHandler;
import com.glodblock.github.nei.object.IRecipeExtractorLegacy;
import com.glodblock.github.nei.object.OrderStack;
import crazypants.enderio.nei.VatRecipeHandler;
import forestry.core.recipes.nei.PositionedFluidTank;
import forestry.core.recipes.nei.RecipeHandlerBase;
import forestry.factory.recipes.nei.NEIHandlerFabricator;
import forestry.factory.recipes.nei.NEIHandlerSqueezer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class ForestryRecipeExtractor implements IRecipeExtractorLegacy {

private final IRecipeHandler handler;

public ForestryRecipeExtractor(IRecipeHandler recipes) {
handler = recipes;
}

@Override
public String getClassName() {
return handler.getHandlerId();
}

@Override
public List<OrderStack<?>> getInputIngredients(List<PositionedStack> rawInputs) {
List<OrderStack<?>> tmp = new LinkedList<>();
for (int i = 0; i < rawInputs.size(); i ++) {
if (rawInputs.get(i) == null) continue;
OrderStack<?> stack = OrderStack.pack(rawInputs.get(i), i);
if (stack != null) tmp.add(stack);
}
return tmp;
}

@Override
public List<OrderStack<?>> getOutputIngredients(List<PositionedStack> rawOutputs) {
List<OrderStack<?>> tmp = new LinkedList<>();
for (int i = 0; i < rawOutputs.size(); i ++) {
if (rawOutputs.get(i) == null) continue;
OrderStack<?> stack = OrderStack.pack(rawOutputs.get(i), i);
if (stack != null) tmp.add(stack);
}
return tmp;
}

@Override
public List<OrderStack<?>> getInputIngredients(List<PositionedStack> rawInputs, IRecipeHandler recipe, int index) {
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
List<OrderStack<?>> tmp = new ArrayList<>();
List<PositionedStack> compressed = compress(rawInputs);
if (tRecipe.arecipes.get(index) instanceof RecipeHandlerBase.CachedBaseRecipe) {
tmp = getInputIngredients(compressed);
List<PositionedFluidTank> tanks = ((RecipeHandlerBase.CachedBaseRecipe) tRecipe.arecipes.get(index)).getFluidTanks();
if (tanks.size() > 0 && !(handler instanceof NEIHandlerSqueezer)) {
FluidStack fluid = tanks.get(0).tank.getFluid();
if (fluid != null) {
tmp.add(new OrderStack<>(fluid, compressed.size()));
}
}
}
return tmp;
}

@Override
public List<OrderStack<?>> getOutputIngredients(List<PositionedStack> rawOutputs, IRecipeHandler recipe, int index) {
TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe;
removeGlass(rawOutputs);
List<OrderStack<?>> tmp = new ArrayList<>();
List<PositionedStack> compressed = compress(rawOutputs);
if (tRecipe.arecipes.get(index) instanceof RecipeHandlerBase.CachedBaseRecipe) {
tmp = getOutputIngredients(compressed);
List<PositionedFluidTank> tanks = ((RecipeHandlerBase.CachedBaseRecipe) tRecipe.arecipes.get(index)).getFluidTanks();
if (tanks.size() > 0 && handler instanceof NEIHandlerSqueezer) {
FluidStack fluid = tanks.get(0).tank.getFluid();
if (fluid != null) {
tmp.add(new OrderStack<>(fluid, compressed.size()));
}
}
else if (tanks.size() > 1) {
FluidStack fluid = tanks.get(1).tank.getFluid();
if (fluid != null) {
tmp.add(new OrderStack<>(fluid, compressed.size()));
}
}
}
return tmp;
}

private List<PositionedStack> compress(List<PositionedStack> list) {
List<PositionedStack> comp = new LinkedList<>();
for (PositionedStack positionedStack : list) {
if (positionedStack == null) continue;
ItemStack currentStack = positionedStack.items[0].copy();
if (currentStack.stackSize == 0) continue;
boolean find = false;
for (PositionedStack storedStack : comp) {
if (storedStack == null) continue;
ItemStack firstStack = storedStack.items[0].copy();
boolean areItemStackEqual = firstStack.isItemEqual(currentStack) && ItemStack.areItemStackTagsEqual(firstStack, currentStack);
if (areItemStackEqual && (firstStack.stackSize + currentStack.stackSize) <= firstStack.getMaxStackSize()) {
find = true;
storedStack.items[0].stackSize = firstStack.stackSize + currentStack.stackSize;
}
}
if (!find) {
comp.add(positionedStack.copy());
}
}
return comp.stream().filter(Objects::nonNull).collect(Collectors.toList());
}

private void removeGlass(List<PositionedStack> list) {
if (handler instanceof NEIHandlerFabricator) {
list.remove(list.size() - 1);
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/glodblock/github/util/ModAndClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public final class ModAndClassUtil {
public static boolean GT6 = false;
public static boolean EC2 = false;
public static boolean EIO = false;
public static boolean FTR = false;

public static boolean isDoubleButton;
public static boolean isSaveText;
Expand Down Expand Up @@ -71,6 +72,8 @@ public static void init() {
EC2 = true;
if (Loader.isModLoaded("EnderIO"))
EIO = true;
if (Loader.isModLoaded("Forestry"))
FTR = true;

}

Expand Down

0 comments on commit 750703f

Please sign in to comment.