diff --git a/README.md b/README.md index a7ba410..37353fc 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,15 @@ # Just Enough Calculation Mod -Hey, this is a small add on to [NEI-GTNH-2.1](https://github.com/GTNewHorizons/NotEnoughItems) (Not Enough Items), in +Hey, this is a small add-on to [NEI-GTNH-2.1](https://github.com/GTNewHorizons/NotEnoughItems) (Not Enough Items), in order to help you calculate the amount of resources to craft a specific amount of item. . -This might be a little confusing, here's a example: +This might be a little confusing, here's an example: -- you want to know how many wood log do you need to craft 40 stairs, simply load the recipe you want to use and it will +- you want to know how many wood logs do you need to craft 40 stairs, simply load the recipe you want to use, and it will tell you 13 logs should be enough, that's all. -This mod is focused on calculation so you might find it powerful and high performance in some complicated cases. Have fun! +This mod is focused on calculation, so you might find it powerful and high performance in some complicated cases. Have fun! For all the release files, description or videos, refer to the [release page](https://minecraft.curseforge.com/projects/just-enough-calculation). @@ -22,6 +22,9 @@ the [release page](https://minecraft.curseforge.com/projects/just-enough-calcula - [ ] OreDict 识别存在问题 - [ ] capability - [ ] gui 界面 z-level 过高,会遮住 nei 的各种 gui +- [ ] 重启游戏之后 最近物品 存在问题 +- [ ] 判断 NEI 版本,适配旧版本 + [1]: http://cf.way2muchnoise.eu/full_just-enough-calculation_downloads.svg diff --git a/build.gradle b/build.gradle index b4184ef..adeafff 100644 --- a/build.gradle +++ b/build.gradle @@ -86,6 +86,7 @@ dependencies { compile "codechicken:NotEnoughItems:${mc_version}-${nei_version}:universal" compile 'codechicken:CodeChickenLib:1.7.10-1.1.3.138:dev' compile 'codechicken:CodeChickenCore:1.7.10-1.0.7.+:dev' + compile 'forestry:forestry_1.7.10-4.3.1.44' compile 'org.reflections:reflections:0.9.12' testCompile 'org.junit.jupiter:junit-jupiter-api:5.2.0' } diff --git a/libs/forestry_1.7.10-4.3.1.44.jar b/libs/forestry_1.7.10-4.3.1.44.jar new file mode 100644 index 0000000..2909524 Binary files /dev/null and b/libs/forestry_1.7.10-4.3.1.44.jar differ diff --git a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java index 07683c8..123ee54 100644 --- a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java +++ b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java @@ -1,11 +1,13 @@ package me.towdium.jecalculation; +import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import me.towdium.jecalculation.nei.Adapter; import me.towdium.jecalculation.nei.NEIPlugin; import me.towdium.jecalculation.network.ClientHandler; import org.apache.logging.log4j.LogManager; @@ -41,6 +43,8 @@ public class JustEnoughCalculation { @Mod.EventHandler public static void init(FMLInitializationEvent event) { handler.init(); + Adapter.init(); + } @Mod.EventHandler diff --git a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java index 7435d63..9c7348a 100644 --- a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java +++ b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java @@ -86,7 +86,8 @@ public class JecaGui extends GuiContainer { */ public static boolean onMouse() { Optional optionalJecaGui = currentJecaGui(); - if(!optionalJecaGui.isPresent()) return false; + if (!optionalJecaGui.isPresent()) + return false; JecaGui gui = optionalJecaGui.get(); int mouseX = getMouseX(); int mouseY = getMouseY(); @@ -348,7 +349,8 @@ public class JecaGui extends GuiContainer { public void drawFluid(Fluid f, int xPos, int yPos, int xSize, int ySize) { IIcon fluidIcon = f.getFlowingIcon(); - if(fluidIcon == null) fluidIcon = f.getStillIcon(); + if (fluidIcon == null) + fluidIcon = f.getStillIcon(); mc.renderEngine.bindTexture(TextureMap.locationBlocksTexture); setColor(f.getColor() & 0x00FFFFFF); if (fluidIcon != null) diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java index ff08fc3..34f5b00 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java @@ -2,6 +2,7 @@ package me.towdium.jecalculation.gui.guis; import codechicken.nei.PositionedStack; import codechicken.nei.recipe.IRecipeHandler; +import me.towdium.jecalculation.nei.Adapter; import me.towdium.jecalculation.data.Controller; import me.towdium.jecalculation.data.label.ILabel; import me.towdium.jecalculation.data.structure.CostList; @@ -12,7 +13,6 @@ import me.towdium.jecalculation.nei.NEIPlugin; import me.towdium.jecalculation.utils.Utilities; import me.towdium.jecalculation.utils.wrappers.Pair; import me.towdium.jecalculation.utils.wrappers.Trio; -import net.minecraft.item.ItemStack; import org.lwjgl.input.Keyboard; import javax.annotation.ParametersAreNonnullByDefault; @@ -147,15 +147,15 @@ public class GuiRecipe extends WContainer implements IGui { ArrayList> output = new ArrayList<>(); disambCache = new HashMap<>(); + + List recipeInputs = new ArrayList<>(); + List recipeOutputs = new ArrayList<>(); + Adapter.handleRecipe(recipe, recipeIndex, recipeInputs, recipeOutputs); + // input - recipe.getIngredientStacks(recipeIndex) - .stream() - .map((positionedStack) -> positionedStack.items) - .forEach(i -> merge(input, Arrays.asList(i), recipe)); + recipeInputs.forEach(i -> merge(input, Arrays.asList(i), recipe)); // output - PositionedStack resultStack = recipe.getResultStack(recipeIndex); - if (resultStack != null) - merge(output, Arrays.asList(resultStack.items), recipe); + recipeOutputs.forEach(o -> merge(output, Arrays.asList(o), recipe)); // catalyst. Ignore multiple catalyst NEIPlugin.getCatalyst(recipe) @@ -170,7 +170,7 @@ public class GuiRecipe extends WContainer implements IGui { refresh(); } - private void merge(ArrayList> dst, List gi, IRecipeHandler context) { + private void merge(ArrayList> dst, List gi, IRecipeHandler context) { List list = gi.stream() .map(ILabel.Converter::from) .filter(i -> i != ILabel.EMPTY) diff --git a/src/main/java/me/towdium/jecalculation/nei/Adapter.java b/src/main/java/me/towdium/jecalculation/nei/Adapter.java new file mode 100644 index 0000000..7a0b537 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/nei/Adapter.java @@ -0,0 +1,39 @@ +package me.towdium.jecalculation.nei; + +import codechicken.nei.PositionedStack; +import codechicken.nei.recipe.IRecipeHandler; +import cpw.mods.fml.common.Loader; +import me.towdium.jecalculation.JustEnoughCalculation; +import me.towdium.jecalculation.nei.adapter.Forestry; +import me.towdium.jecalculation.nei.adapter.IAdapter; + +import java.util.ArrayList; +import java.util.List; + +public class Adapter { + public static List adapters = new ArrayList<>(); + + public static void init() { + if (Loader.isModLoaded("Forestry")) { + JustEnoughCalculation.logger.info("Forestry is installed"); + adapters.add(new Forestry()); + } + } + + public static void handleRecipe(IRecipeHandler recipe, int index, List inputs, List outputs) { + // raw inputs + recipe.getIngredientStacks(index) + .stream() + .map((positionedStack) -> (Object[]) positionedStack.items) + .forEach(inputs::add); + + // raw outputs + PositionedStack resultStack = recipe.getResultStack(index); + if (resultStack != null) + outputs.add(resultStack.items); + + for (IAdapter adapter : adapters) { + adapter.handleRecipe(recipe, index, inputs, outputs); + } + } +} diff --git a/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java b/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java index e09cbaf..296e7ca 100644 --- a/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java +++ b/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java @@ -13,8 +13,7 @@ import net.minecraft.client.gui.inventory.GuiContainer; public class JecaOverlayHandler implements IOverlayHandler { @Override public void overlayRecipe(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex, boolean shift) { - JustEnoughCalculation.logger.info("shift ? " + shift); - if (shift && firstGui instanceof JecaGui) { + if (firstGui instanceof JecaGui) { JecaGui gui = (JecaGui) firstGui; if (gui.root instanceof GuiRecipe) { ((GuiRecipe) gui.root).transfer(recipe, recipeIndex); @@ -23,9 +22,6 @@ public class JecaOverlayHandler implements IOverlayHandler { JecaGui.displayGui(true, true, guiRecipe); guiRecipe.transfer(recipe, recipeIndex); } - } else { - System.out.println(firstGui.getClass().toString()); } - } } diff --git a/src/main/java/me/towdium/jecalculation/nei/adapter/AdapterUtil.java b/src/main/java/me/towdium/jecalculation/nei/adapter/AdapterUtil.java new file mode 100644 index 0000000..a011de1 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/nei/adapter/AdapterUtil.java @@ -0,0 +1,22 @@ +package me.towdium.jecalculation.nei.adapter; + +import codechicken.nei.recipe.IRecipeHandler; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@SuppressWarnings("rawtypes") +@ParametersAreNonnullByDefault +public class AdapterUtil { + + static Optional getClass(String classPath) { + try { + return Optional.of(Class.forName(classPath)); + } catch (ClassNotFoundException e) { + return Optional.empty(); + } + } + +} diff --git a/src/main/java/me/towdium/jecalculation/nei/adapter/Forestry.java b/src/main/java/me/towdium/jecalculation/nei/adapter/Forestry.java new file mode 100644 index 0000000..eeac2c9 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/nei/adapter/Forestry.java @@ -0,0 +1,148 @@ +package me.towdium.jecalculation.nei.adapter; + +import codechicken.nei.PositionedStack; +import codechicken.nei.recipe.IRecipeHandler; +import forestry.core.recipes.nei.PositionedFluidTank; +import forestry.core.recipes.nei.RecipeHandlerBase; +import forestry.factory.recipes.nei.*; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTank; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@ParametersAreNonnullByDefault +public class Forestry implements IAdapter { + @Override + public void handleRecipe(IRecipeHandler recipe, int index, List inputs, List outputs) { + if (recipe instanceof RecipeHandlerBase) { + RecipeHandlerBase base = (RecipeHandlerBase) recipe; + bottler(base, index, inputs); + carpenter(base, index, inputs); + centrifuge(base, index, outputs); + fabricator(base, index, inputs); + fermenter(base, index, inputs, outputs); + moistener(base, index, inputs, outputs); + squeezer(base, index, inputs, outputs); + still(base, index, inputs, outputs); + } + } + + private void bottler(RecipeHandlerBase recipe, int recipeIndex, List inputs) { + if (recipe instanceof NEIHandlerBottler) { + List> fluids = getFluids(recipe, recipeIndex); + if(fluids.size() >= 1) + inputs.add(fluids.get(0).toArray()); + } + } + + private void carpenter(RecipeHandlerBase recipe, int recipeIndex, List inputs) { + if (recipe instanceof NEIHandlerCarpenter) { + List> fluids = getFluids(recipe, recipeIndex); + if(fluids.size() >= 1) + inputs.add(fluids.get(0).toArray()); + } + } + + private void centrifuge(RecipeHandlerBase recipe, int recipeIndex, List outputs) { + if (recipe instanceof NEIHandlerCentrifuge) { + List otherStacks = recipe.getOtherStacks(recipeIndex); + otherStacks.stream() + .map(positionedStack -> positionedStack.items) + .forEach(outputs::add); + } + } + + private void fabricator(RecipeHandlerBase recipe, int recipeIndex, List inputs) { + if (recipe instanceof NEIHandlerFabricator) { + List> fluids = getFluids(recipe, recipeIndex); + if(fluids.size() >= 1) + inputs.add(fluids.get(0).toArray()); + } + } + + private void fermenter(RecipeHandlerBase recipe, int recipeIndex, List inputs, List outputs) { + if(recipe instanceof NEIHandlerFermenter) { + List tanks = getTanks(recipe, recipeIndex); + tanks.forEach(tank -> { + Object[] fluids = Arrays.stream(tank.tanks).map(FluidTank::getFluid).toArray(); + if (tank.position.x == 30) { + // source + inputs.add(fluids); + } else { + // outputs + outputs.add(fluids); + } + }); + } + } + + private void moistener(RecipeHandlerBase recipe, int recipeIndex, List inputs, List outputs) { + if(recipe instanceof NEIHandlerMoistener) { + List> fluids = getFluids(recipe, recipeIndex); + if(fluids.size() >= 1) + inputs.add(fluids.get(0).toArray()); + List otherStacks = recipe.getOtherStacks(recipeIndex); + otherStacks.forEach(positionedStack -> { + ItemStack[] items = positionedStack.items; + if(positionedStack.relx == 34) { + // item + inputs.add(items); + } else { + // product + outputs.add(items); + } + }); + } + } + + private void squeezer(RecipeHandlerBase recipe, int recipeIndex, List inputs, List outputs) { + if(recipe instanceof NEIHandlerSqueezer) { + List> fluids = getFluids(recipe, recipeIndex); + if(fluids.size() >= 1) + outputs.add(fluids.get(0).toArray()); + } + } + + private void still(RecipeHandlerBase recipe, int recipeIndex, List inputs, List outputs) { + if(recipe instanceof NEIHandlerStill) { + getTanks(recipe, recipeIndex).forEach(tank -> { + Object[] fluids = Arrays.stream(tank.tanks).map(FluidTank::getFluid).toArray(); + if(tank.position.x == 30) { + // input + inputs.add(fluids); + } else { + // output + outputs.add(fluids); + } + }); + } + } + + /** + * It won't check the type + * + * @param recipe recipe + * @param recipeIndex index + * @return fluid tanks + */ + private List> getFluids(RecipeHandlerBase recipe, int recipeIndex) { + return ((RecipeHandlerBase.CachedBaseRecipe) recipe.arecipes.get(recipeIndex)).getFluidTanks() + .stream() + .map(positionedFluidTank -> Arrays + .stream(positionedFluidTank.tanks) + .map(FluidTank::getFluid) + .collect( + Collectors.toList())) + .collect(Collectors.toList()); + + } + + private List getTanks(RecipeHandlerBase recipe, int recipeIndex) { + return ((RecipeHandlerBase.CachedBaseRecipe) recipe.arecipes.get(recipeIndex)).getFluidTanks(); + } + +} diff --git a/src/main/java/me/towdium/jecalculation/nei/adapter/IAdapter.java b/src/main/java/me/towdium/jecalculation/nei/adapter/IAdapter.java new file mode 100644 index 0000000..a23f2a9 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/nei/adapter/IAdapter.java @@ -0,0 +1,11 @@ +package me.towdium.jecalculation.nei.adapter; + +import codechicken.nei.recipe.IRecipeHandler; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.List; + +@ParametersAreNonnullByDefault +public interface IAdapter { + void handleRecipe(IRecipeHandler recipe, int index, List inputs, List outputs); +}