From 90da8971ace72a7c2311c32116f6bf29c3b658c7 Mon Sep 17 00:00:00 2001 From: Discreater Date: Fri, 26 Nov 2021 22:08:38 +0800 Subject: [PATCH] feat: add support of GregTech6 --- CHANGELOG.md | 4 + README.md | 1 + README_CN.md | 1 + gradle.properties | 2 +- .../me/towdium/jecalculation/nei/Adapter.java | 15 ++- .../jecalculation/nei/adapter/GregTech6.java | 122 ++++++++++++++++++ 6 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 src/main/java/me/towdium/jecalculation/nei/adapter/GregTech6.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 47cd27c..74ea778 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ To be consistent with the main branch, it may miss some versions +## 3.8.2 + +- Feat: add support of `GregTech6`. + ## 3.8.1 - Fixed: unmatched JSON writer and reader diff --git a/README.md b/README.md index e383cba..43df654 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ the [release page](https://minecraft.curseforge.com/projects/just-enough-calcula - Recipe can be transferred from NEI recipe overlay gui (`shift` + click `?` button) - [x] vanilla - [x] gregtech + - [x] gregtech6 - [x] Forestry (`?` button may overlap with the fluid tank. Click carefully) - [x] Avaritia (`?` button may be covered by the NEI search bar) - [x] GT++ (support GT++ 1.7.05.68 (GTNH 2.1.0.0), have not tested old versions) diff --git a/README_CN.md b/README_CN.md index 26bccc2..ba9b078 100644 --- a/README_CN.md +++ b/README_CN.md @@ -25,6 +25,7 @@ This mod is focused on calculation, so you might find it powerful and high perfo - 可以从 NEI 合成表 gui 页面直接获取合成表 (`shift` + 单击 `?` 按钮) - [x] 原生MC - [x] gregtech + - [x] gregtech6 - [x] Forestry (`?` 按钮可能与流体重叠,不是很好点到) - [x] Avaritia (`?` 按钮可能会被NEI搜索栏遮盖) - [x] GT++ (支持 GT++ 1.7.05.68 (GTNH 2.1.0.0),老版本没有测试过) diff --git a/gradle.properties b/gradle.properties index 7def9f7..f61210b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,4 +4,4 @@ org.gradle.jvmargs=-Xmx3G # suppress inspection "UnusedProperty" for whole file mc_version=1.7.10 nei_version=2.1.5-GTNH -proj_version=3.8.1 \ No newline at end of file +proj_version=3.8.2 \ No newline at end of file diff --git a/src/main/java/me/towdium/jecalculation/nei/Adapter.java b/src/main/java/me/towdium/jecalculation/nei/Adapter.java index 1f63fbe..d036e34 100644 --- a/src/main/java/me/towdium/jecalculation/nei/Adapter.java +++ b/src/main/java/me/towdium/jecalculation/nei/Adapter.java @@ -19,7 +19,11 @@ public class Adapter { String modId = name.substring(0, name.indexOf(":")); String itemId = name.substring(name.indexOf(":") + 1); if (Loader.isModLoaded("gregtech")) { - return GregTech.convertFluid(itemStack); + if(GregTech6.isGT6()) { + return GregTech6.convertFluid(itemStack); + } else { + return GregTech.convertFluid(itemStack); + } } return itemStack; } @@ -35,8 +39,13 @@ public class Adapter { adapters.add(new AE2()); } if (Loader.isModLoaded("gregtech")) { - JustEnoughCalculation.logger.info("gregtech installed"); - adapters.add(new GregTech()); + if(GregTech6.isGT6()) { + JustEnoughCalculation.logger.info("gregtech6 installed"); + adapters.add(new GregTech6()); + } else { + JustEnoughCalculation.logger.info("gregtech5 installed"); + adapters.add(new GregTech()); + } } try { if (Loader.isModLoaded("miscutils")) { diff --git a/src/main/java/me/towdium/jecalculation/nei/adapter/GregTech6.java b/src/main/java/me/towdium/jecalculation/nei/adapter/GregTech6.java new file mode 100644 index 0000000..958b732 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/nei/adapter/GregTech6.java @@ -0,0 +1,122 @@ +package me.towdium.jecalculation.nei.adapter; + +import codechicken.nei.PositionedStack; +import codechicken.nei.recipe.IRecipeHandler; +import codechicken.nei.recipe.TemplateRecipeHandler; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@ParametersAreNonnullByDefault +public class GregTech6 implements IAdapter { + public static boolean isGT6() { + try { + Class buildInfoclz = Class.forName("gregtech.BuildInfo"); + Field versionField = buildInfoclz.getField("version"); + String version = (String) versionField.get(null); + return version.startsWith("6."); + } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { + return false; + } + } + + @SuppressWarnings("rawtypes") + @Override + public Set getAllOverlayIdentifier() { + Set recipeNames = new HashSet<>(); + try { + Class recipeMapClz = Class.forName("gregapi.recipes.Recipe$RecipeMap"); + Field recipeMapField = recipeMapClz.getDeclaredField("RECIPE_MAPS"); + Field mNameNEIField = recipeMapClz.getDeclaredField("mNameNEI"); + Map maps = (Map) recipeMapField.get(null); + for (Object value : maps.values()) { + try { + recipeNames.add((String) mNameNEIField.get(value)); + } catch (IllegalAccessException ignored) { + } + } + } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + return recipeNames; + } + + public static final Class gtRecipeMap; + public static final Class gtRecipeMapCachedRecipe; + public static final Class gtRecipe; + + static { + Class gtRM = null; + Class gtRMCR = null; + Class gtR = null; + try { + gtRM = Class.forName("gregapi.NEI_RecipeMap"); + gtRMCR = Class.forName("gregapi.NEI_RecipeMap$CachedDefaultRecipe"); + gtR = Class.forName("gregapi.recipes.Recipe"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + gtRecipeMap = gtRM; + gtRecipeMapCachedRecipe = gtRMCR; + gtRecipe = gtR; + } + + @Override + public void handleRecipe(IRecipeHandler recipe, int index, List inputs, List outputs) { + if (gtRecipeMap.isInstance(recipe)) { + TemplateRecipeHandler a = (TemplateRecipeHandler)recipe; + TemplateRecipeHandler.CachedRecipe cachedRecipe = a.arecipes.get(index); + try { + Field mRecipe = gtRecipeMapCachedRecipe.getDeclaredField("mRecipe"); + Object re = mRecipe.get(cachedRecipe); + Field mInputs = gtRecipe.getDeclaredField("mInputs"); + Field mOutputs = gtRecipe.getDeclaredField("mOutputs"); + Field mFluidInputs = gtRecipe.getDeclaredField("mFluidInputs"); + Field mFluidOutputs = gtRecipe.getDeclaredField("mFluidOutputs"); + ItemStack[] input =(ItemStack[]) mInputs.get(re); + ItemStack[] output = (ItemStack[]) mOutputs.get(re); + FluidStack[] fluidInputs = (FluidStack[]) mFluidInputs.get(re); + FluidStack[] fluidOutputs = (FluidStack[]) mFluidOutputs.get(re); + inputs.clear(); + inputs.addAll(Stream.concat(Arrays.stream(input), Arrays.stream(fluidInputs)).map(i -> new Object[] {i}).collect(Collectors.toList())); + outputs.clear(); + outputs.addAll(Stream.concat(Arrays.stream(output), Arrays.stream(fluidOutputs)).map(o -> new Object[] {o}).collect(Collectors.toList())); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } +// for (int i = 0; i < inputs.size(); i++) { +// Object[] objects = Arrays.stream(inputs.get(i)).map(o -> GregTech6.convertFluid((ItemStack) o)).toArray(); +// inputs.set(i, objects); +// } +// List otherStacks = recipe.getOtherStacks(index); +// outputs.addAll(otherStacks +// .stream() +// .map(stack -> stack.items) +// .map(is -> Arrays.stream(is).map(GregTech6::convertFluid).toArray()) +// .collect(Collectors.toList())); + } + } + + public static Object convertFluid(ItemStack itemStack) { + FluidStack fluidStack = getFluidFromDisplayStack(itemStack); + return fluidStack == null ? itemStack : fluidStack; + } + + private static FluidStack getFluidFromDisplayStack(ItemStack aDisplayStack) { + try { + Class itemListClz = Class.forName("gregapi.data.FL"); + Method getFluidMethod = itemListClz.getDeclaredMethod("getFluid", ItemStack.class, boolean.class); + return (FluidStack) getFluidMethod.invoke(null, aDisplayStack, true); + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + return null; + } + } +}