From 750703f7f8f48dd90e1119affb97672a1114959c Mon Sep 17 00:00:00 2001 From: GlodBlock <1356392126@qq.com> Date: Thu, 12 May 2022 20:05:01 +0800 Subject: [PATCH] support foresty --- .../nei/object/IRecipeExtractorLegacy.java | 7 + .../nei/recipes/DefaultExtractorLoader.java | 17 ++- .../github/nei/recipes/FluidRecipe.java | 25 ++++ .../extractor/ForestryRecipeExtractor.java | 129 ++++++++++++++++++ .../github/util/ModAndClassUtil.java | 3 + 5 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/glodblock/github/nei/object/IRecipeExtractorLegacy.java create mode 100644 src/main/java/com/glodblock/github/nei/recipes/extractor/ForestryRecipeExtractor.java diff --git a/src/main/java/com/glodblock/github/nei/object/IRecipeExtractorLegacy.java b/src/main/java/com/glodblock/github/nei/object/IRecipeExtractorLegacy.java new file mode 100644 index 00000000..8bca0304 --- /dev/null +++ b/src/main/java/com/glodblock/github/nei/object/IRecipeExtractorLegacy.java @@ -0,0 +1,7 @@ +package com.glodblock.github.nei.object; + +public interface IRecipeExtractorLegacy extends IRecipeExtractor { + + String getClassName(); + +} diff --git a/src/main/java/com/glodblock/github/nei/recipes/DefaultExtractorLoader.java b/src/main/java/com/glodblock/github/nei/recipes/DefaultExtractorLoader.java index a91856d8..9b0a1540 100644 --- a/src/main/java/com/glodblock/github/nei/recipes/DefaultExtractorLoader.java +++ b/src/main/java/com/glodblock/github/nei/recipes/DefaultExtractorLoader.java @@ -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; @@ -38,6 +36,17 @@ public class DefaultExtractorLoader implements Runnable { 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())); + } + } } diff --git a/src/main/java/com/glodblock/github/nei/recipes/FluidRecipe.java b/src/main/java/com/glodblock/github/nei/recipes/FluidRecipe.java index 80181a4a..1060871e 100644 --- a/src/main/java/com/glodblock/github/nei/recipes/FluidRecipe.java +++ b/src/main/java/com/glodblock/github/nei/recipes/FluidRecipe.java @@ -4,6 +4,7 @@ import codechicken.nei.PositionedStack; 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; @@ -14,14 +15,19 @@ import java.util.List; public final class FluidRecipe { private static final HashMap IdentifierMap = new HashMap<>(); + private static final HashMap 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> 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 tmp = new ArrayList<>(tRecipe.getIngredientStacks(index)); @@ -31,6 +37,7 @@ public final class FluidRecipe { public static List> 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 tmp = new ArrayList<>(Collections.singleton(tRecipe.getResultStack(index))); @@ -42,4 +49,22 @@ public final class FluidRecipe { return new ArrayList<>(IdentifierMap.keySet()); } + public static List> 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 tmp = new ArrayList<>(recipe.getIngredientStacks(index)); + return extractor.getInputIngredients(tmp, recipe, index); + } + + public static List> 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 tmp = new ArrayList<>(Collections.singleton(recipe.getResultStack(index))); + if (useOther) tmp.addAll(recipe.getOtherStacks(index)); + return extractor.getOutputIngredients(tmp, recipe, index); + } + + } diff --git a/src/main/java/com/glodblock/github/nei/recipes/extractor/ForestryRecipeExtractor.java b/src/main/java/com/glodblock/github/nei/recipes/extractor/ForestryRecipeExtractor.java new file mode 100644 index 00000000..15e4b130 --- /dev/null +++ b/src/main/java/com/glodblock/github/nei/recipes/extractor/ForestryRecipeExtractor.java @@ -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> getInputIngredients(List rawInputs) { + List> 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> getOutputIngredients(List rawOutputs) { + List> 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> getInputIngredients(List rawInputs, IRecipeHandler recipe, int index) { + TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe; + List> tmp = new ArrayList<>(); + List compressed = compress(rawInputs); + if (tRecipe.arecipes.get(index) instanceof RecipeHandlerBase.CachedBaseRecipe) { + tmp = getInputIngredients(compressed); + List 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> getOutputIngredients(List rawOutputs, IRecipeHandler recipe, int index) { + TemplateRecipeHandler tRecipe = (TemplateRecipeHandler) recipe; + removeGlass(rawOutputs); + List> tmp = new ArrayList<>(); + List compressed = compress(rawOutputs); + if (tRecipe.arecipes.get(index) instanceof RecipeHandlerBase.CachedBaseRecipe) { + tmp = getOutputIngredients(compressed); + List 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 compress(List list) { + List 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 list) { + if (handler instanceof NEIHandlerFabricator) { + list.remove(list.size() - 1); + } + } + +} diff --git a/src/main/java/com/glodblock/github/util/ModAndClassUtil.java b/src/main/java/com/glodblock/github/util/ModAndClassUtil.java index 30e142de..c95d9eb1 100644 --- a/src/main/java/com/glodblock/github/util/ModAndClassUtil.java +++ b/src/main/java/com/glodblock/github/util/ModAndClassUtil.java @@ -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; @@ -71,6 +72,8 @@ public final class ModAndClassUtil { EC2 = true; if (Loader.isModLoaded("EnderIO")) EIO = true; + if (Loader.isModLoaded("Forestry")) + FTR = true; }