|
| 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 | +} |
0 commit comments