mirror of
https://github.com/GTNewHorizons/JustEnoughCalculation.git
synced 2026-11-19 11:49:06 +00:00
chore: remove gt from libs, use reflection
This commit is contained in:
1 parent
b07551f3c0
commit
343cf48cfa
3 files changed
+66
-70
No files matched your search
@@ -1,7 +1,6 @@
|
||||
package me.towdium.jecalculation.nei.adapter;
|
||||
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
import gregtech.api.util.GTPP_Recipe;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.HashSet;
|
||||
@@ -28,14 +27,14 @@ public class GTPP extends GregTech {
|
||||
"GT_NEI_DefaultHandler", "GT_NEI_Dehydrator", "GT_NEI_MultiBlockHandler", "GTPP_NEI_CustomMapHandler",
|
||||
"GTPP_NEI_DefaultHandlerEx"
|
||||
|
||||
).map(name -> "gtPlusPlus.nei." + name).collect(Collectors.toList());
|
||||
).map(name -> "gtPlusPlus.nei." + name).collect(Collectors.toList());
|
||||
|
||||
defaultHandlers = new HashSet<>();
|
||||
for (String handler : handlers) {
|
||||
try {
|
||||
defaultHandlers.add(Class.forName(handler));
|
||||
} catch (ClassNotFoundException e) {
|
||||
// e.printStackTrace();
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,13 +44,12 @@ public class GTPP extends GregTech {
|
||||
Set<String> recipeNames = new HashSet<>();
|
||||
try {
|
||||
// add as many recipe as possible
|
||||
|
||||
recipeNames.addAll(
|
||||
GTPP_Recipe.GT_Recipe_Map.sMappings.stream().map(rm -> rm.mNEIName).collect(Collectors.toSet()));
|
||||
recipeNames.addAll(
|
||||
GTPP_Recipe.GTPP_Recipe_Map.sMappings.stream().map(rm -> rm.mNEIName).collect(Collectors.toSet()));
|
||||
recipeNames.addAll(GTPP_Recipe.GTPP_Recipe_Map_Internal.sMappingsEx.stream()
|
||||
.map(rm -> rm.mNEIName)
|
||||
.collect(Collectors.toSet()));
|
||||
this.reflectGetRecipeMapNEIName("gregtech.api.util.GTPP_Recipe$GTPP_Recipe_Map", "sMappings"));
|
||||
|
||||
recipeNames.addAll(this.reflectGetRecipeMapNEIName("gregtech.api.util.GTPP_Recipe$GTPP_Recipe_Map_Internal",
|
||||
"sMappingsEx"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,35 +1,66 @@
|
||||
package me.towdium.jecalculation.nei.adapter;
|
||||
|
||||
import codechicken.nei.NEIServerUtils;
|
||||
import codechicken.nei.PositionedStack;
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
import gregtech.api.enums.ItemList;
|
||||
import gregtech.api.util.GT_Recipe;
|
||||
import gregtech.nei.GT_NEI_AssLineHandler;
|
||||
import gregtech.nei.GT_NEI_DefaultHandler;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
public class GregTech implements IAdapter {
|
||||
@Override
|
||||
public Set<String> getAllOverlayIdentifier() {
|
||||
return GT_Recipe.GT_Recipe_Map.sMappings.stream()
|
||||
.map(gt_recipe_map -> gt_recipe_map.mNEIName)
|
||||
.collect(Collectors.toSet());
|
||||
return reflectGetRecipeMapNEIName("gregtech.api.util.GT_Recipe$GT_Recipe_Map", "sMappings");
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
protected Set<String> reflectGetRecipeMapNEIName(String clz, String staticField) {
|
||||
try {
|
||||
Class<?> gtRecipeMapClz = Class.forName(clz);
|
||||
Field sMappingsField = gtRecipeMapClz.getDeclaredField(staticField);
|
||||
Field mNEINameField = gtRecipeMapClz.getField("mNEIName");
|
||||
Collection sMappings = (Collection) sMappingsField.get(null);
|
||||
|
||||
return (Set<String>) sMappings.stream().map(sMapping -> {
|
||||
try {
|
||||
return mNEINameField.get(sMapping);
|
||||
} catch (IllegalAccessException e) {
|
||||
return null;
|
||||
}
|
||||
}).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return new HashSet<>();
|
||||
}
|
||||
}
|
||||
|
||||
private static final Class<?> gtDefault, gtAssLine;
|
||||
|
||||
static {
|
||||
Class<?> gtDf = null;
|
||||
Class<?> gtAL = null;
|
||||
try {
|
||||
gtDf = Class.forName("gregtech.nei.GT_NEI_DefaultHandler");
|
||||
gtAL = Class.forName("gregtech.nei.GT_NEI_AssLineHandler");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
gtDefault = gtDf;
|
||||
gtAssLine = gtAL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRecipe(IRecipeHandler recipe, int index, List<Object[]> inputs, List<Object[]> outputs) {
|
||||
if (recipe instanceof GT_NEI_DefaultHandler || recipe instanceof GT_NEI_AssLineHandler) {
|
||||
if (gtDefault.isInstance(recipe) || gtAssLine.isInstance(recipe)) {
|
||||
handleDefault(recipe, index, inputs, outputs);
|
||||
}
|
||||
}
|
||||
@@ -55,54 +86,26 @@ public class GregTech implements IAdapter {
|
||||
* For resolving version compatibility issues.
|
||||
* Copied from GTNewHorizons/GT5-Unofficial.
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private static FluidStack getFluidFromDisplayStack(ItemStack aDisplayStack) {
|
||||
if (!isStackValid(aDisplayStack) ||
|
||||
aDisplayStack.getItem() != ItemList.Display_Fluid.getItem() ||
|
||||
!aDisplayStack.hasTagCompound())
|
||||
try {
|
||||
Class itemListClz = Class.forName("gregtech.api.enums.ItemList");
|
||||
Enum display_fluid = Enum.valueOf(itemListClz, "Display_Fluid");
|
||||
Method getItem = itemListClz.getMethod("getItem");
|
||||
Item displayFluidItem = (Item) getItem.invoke(display_fluid);
|
||||
if (!isStackValid(aDisplayStack) || aDisplayStack.getItem() != displayFluidItem ||
|
||||
!aDisplayStack.hasTagCompound())
|
||||
return null;
|
||||
Fluid tFluid = FluidRegistry.getFluid(displayFluidItem.getDamage(aDisplayStack));
|
||||
return new FluidStack(tFluid, (int) aDisplayStack.getTagCompound().getLong("mFluidDisplayAmount"));
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
Fluid tFluid = FluidRegistry.getFluid(ItemList.Display_Fluid.getItem().getDamage(aDisplayStack));
|
||||
return new FluidStack(tFluid, (int) aDisplayStack.getTagCompound().getLong("mFluidDisplayAmount"));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isStackValid(Object aStack) {
|
||||
return (aStack instanceof ItemStack) && ((ItemStack) aStack).getItem() != null && ((ItemStack) aStack).stackSize >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* get from `gregtech.nei.GT_NEI_AssLineHandler$CachedDefaultRecipe::Constructor`
|
||||
*
|
||||
* @param recipe GT_Recipe
|
||||
* @param inputs input list
|
||||
* @param outputs output list
|
||||
*/
|
||||
@Deprecated
|
||||
private void getFromRecipe(GT_Recipe recipe, List<Object[]> inputs, List<Object[]> outputs) {
|
||||
for (int i = 0; i < recipe.mInputs.length; i++) {
|
||||
Object obj = recipe instanceof GT_Recipe.GT_Recipe_WithAlt ?
|
||||
((GT_Recipe.GT_Recipe_WithAlt) recipe).getAltRepresentativeInput(i) :
|
||||
recipe.getRepresentativeInput(i);
|
||||
if (obj != null) {
|
||||
inputs.add(NEIServerUtils.extractRecipeItems(obj));
|
||||
}
|
||||
}
|
||||
if (recipe.mSpecialItems != null) {
|
||||
inputs.add(NEIServerUtils.extractRecipeItems(recipe.mSpecialItems));
|
||||
}
|
||||
|
||||
if (recipe.getOutput(0) != null) {
|
||||
outputs.add(NEIServerUtils.extractRecipeItems(recipe.getOutput(0)));
|
||||
}
|
||||
|
||||
for (FluidStack mFluidInput : recipe.mFluidInputs) {
|
||||
if (mFluidInput != null && mFluidInput.getFluid() != null) {
|
||||
inputs.add(new Object[]{mFluidInput});
|
||||
}
|
||||
}
|
||||
|
||||
for (FluidStack mFluidOutput : recipe.mFluidOutputs) {
|
||||
if (mFluidOutput != null && mFluidOutput.getFluid() != null) {
|
||||
outputs.add(new Object[]{mFluidOutput});
|
||||
}
|
||||
}
|
||||
return (aStack instanceof ItemStack) && ((ItemStack) aStack).getItem() != null &&
|
||||
((ItemStack) aStack).stackSize >= 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user