diff options
author | Alexdoru <57050655+Alexdoru@users.noreply.github.com> | 2024-09-14 19:45:56 +0200 |
---|---|---|
committer | boubou19 <miisterunknown@gmail.com> | 2024-09-17 22:55:46 +0200 |
commit | fa9244831214fe88f3801fd42f87c92ae9f33113 (patch) | |
tree | 95709e998e6a0417966396d9a80d9426796b4462 /src/main/java/detrav/utils/GTppHelper.java | |
parent | 9e74ed7a31b9bdfe41f337d93ec719f5ac6d4a3e (diff) | |
download | GT5-Unofficial-fa9244831214fe88f3801fd42f87c92ae9f33113.tar.gz GT5-Unofficial-fa9244831214fe88f3801fd42f87c92ae9f33113.tar.bz2 GT5-Unofficial-fa9244831214fe88f3801fd42f87c92ae9f33113.zip |
initialize the material maps used by detrav ore scanner only when they are used
Diffstat (limited to 'src/main/java/detrav/utils/GTppHelper.java')
-rw-r--r-- | src/main/java/detrav/utils/GTppHelper.java | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/src/main/java/detrav/utils/GTppHelper.java b/src/main/java/detrav/utils/GTppHelper.java index 985ca3386b..483e8cfd5e 100644 --- a/src/main/java/detrav/utils/GTppHelper.java +++ b/src/main/java/detrav/utils/GTppHelper.java @@ -1,9 +1,11 @@ package detrav.utils; +import java.lang.reflect.Field; import java.util.HashMap; import net.minecraft.block.Block; +import gregtech.GTMod; import gtPlusPlus.core.block.base.BlockBaseOre; import gtPlusPlus.core.material.Material; import gtPlusPlus.core.material.MaterialMisc; @@ -12,23 +14,27 @@ import gtPlusPlus.core.material.MaterialsElements; import gtPlusPlus.core.material.MaterialsOres; import gtPlusPlus.core.material.nuclear.MaterialsFluorides; -/** - * Created by bartimaeusnek on 19.04.2018. - */ public class GTppHelper { - public static final HashMap<Short, Material> decodeoresGTpp = new HashMap<>(); - public static final HashMap<Material, Short> encodeoresGTpp = new HashMap<>(); + private static boolean initialized; + private static final HashMap<Short, Material> decodeoresGTpp = new HashMap<>(); + private static final HashMap<Material, Short> encodeoresGTpp = new HashMap<>(); - public static void generate_OreIDs() { + private static void generate_OreIDs() { short n = 0; - for (; n < MaterialsOres.class.getFields().length; ++n) { + final Field[] fields = MaterialsOres.class.getFields(); + for (; n < fields.length; ++n) { try { - Short i = (short) (n + 1); - Material m = ((Material) MaterialsOres.class.getFields()[n].get(MaterialsOres.class.getFields()[n])); - decodeoresGTpp.put(i, m); - encodeoresGTpp.put(m, i); - } catch (Exception ignored) {} + final Object o = fields[n].get(null); + if (o instanceof Material m) { + Short i = (short) (n + 1); + decodeoresGTpp.put(i, m); + encodeoresGTpp.put(m, i); + } + } catch (Exception e) { + GTMod.GT_FML_LOGGER + .error("Exception caught when trying to generate GT++ ore ids for detrav ore scanner", e); + } } // Manually add ores from other places than the ore class // Fluorite @@ -52,16 +58,28 @@ public class GTppHelper { encodeoresGTpp.put(MaterialsElements.STANDALONE.GRANITE, (short) (n + 1)); } - public static boolean isGTppBlock(Block tBlock) { - return tBlock instanceof BlockBaseOre; + public static short getMetaFromBlock(Block block) { + if (!initialized) { + generate_OreIDs(); + initialized = true; + } + return (short) (GTppHelper.encodeoresGTpp.get(((BlockBaseOre) block).getMaterialEx()) + 7000); + } + + public static Material getMatFromMeta(int meta) { + if (!initialized) { + generate_OreIDs(); + initialized = true; + } + return GTppHelper.decodeoresGTpp.get((short) (meta - 7000)); } - public static short getGTppMeta(Block tBlock) { - return (short) (GTppHelper.encodeoresGTpp.get(((BlockBaseOre) tBlock).getMaterialEx()) + 7000); + public static boolean isGTppBlock(Block block) { + return block instanceof BlockBaseOre; } - public static String getGTppVeinName(Block tBlock) { - return tBlock.getLocalizedName(); + public static String getGTppVeinName(Block block) { + return block.getLocalizedName(); } } |