aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pers/gwyog/gtneioreplugin/util
diff options
context:
space:
mode:
authorYannickMG <yannickmg@gmail.com>2022-01-22 00:04:58 -0500
committerJason Mitchell <mitchej+github@gmail.com>2022-01-21 21:18:04 -0800
commitb0655be5f677f03bb6dfa22101a82609f233e2d1 (patch)
treecaebf77e9a614466e440a1fcd45adeec75383f90 /src/main/java/pers/gwyog/gtneioreplugin/util
parentd53a56a239c88c51c7438125fcba5a6d850a95e6 (diff)
downloadGT5-Unofficial-b0655be5f677f03bb6dfa22101a82609f233e2d1.tar.gz
GT5-Unofficial-b0655be5f677f03bb6dfa22101a82609f233e2d1.tar.bz2
GT5-Unofficial-b0655be5f677f03bb6dfa22101a82609f233e2d1.zip
Replacing access transformers by reflection
Diffstat (limited to 'src/main/java/pers/gwyog/gtneioreplugin/util')
-rw-r--r--src/main/java/pers/gwyog/gtneioreplugin/util/GuiRecipeHelper.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/main/java/pers/gwyog/gtneioreplugin/util/GuiRecipeHelper.java b/src/main/java/pers/gwyog/gtneioreplugin/util/GuiRecipeHelper.java
new file mode 100644
index 0000000000..78d43f1bdd
--- /dev/null
+++ b/src/main/java/pers/gwyog/gtneioreplugin/util/GuiRecipeHelper.java
@@ -0,0 +1,96 @@
+package pers.gwyog.gtneioreplugin.util;
+
+import java.lang.reflect.Field;
+
+import org.apache.logging.log4j.Level;
+
+import codechicken.nei.recipe.GuiRecipe;
+import cpw.mods.fml.common.FMLLog;
+import net.minecraft.client.Minecraft;
+
+public class GuiRecipeHelper {
+ private static final String INIT_ERROR = "pers.gwyog.gtneioreplugin.util.GuiRecipeHelper failed setting up reflection : ";
+ private static final int DEFAULT_XSIZE = 176;
+
+ private static Class<GuiRecipe> guiRecipeClass;
+ public static Field xSizeField;
+ public static Field guiLeftField;
+ public static Field guiTopField;
+
+ /**
+ * Access the xSize field of a GuiRecipe instance, or use a fallback hardcoded value if that fails
+ *
+ * @param gui GuiRecipe object
+ * @return Integer value of the xSize field of that object
+ */
+ public static int getXSize(GuiRecipe gui) {
+ if (xSizeField != null) {
+ try {
+ return (int) xSizeField.get(gui);
+ } catch (IllegalAccessException e) {
+ // Fail silently, hoping that it it fails it will be during initialization
+ }
+ }
+
+ // Fallback should work unless codechicken.nei.recipe.GuiRecipe implementation changes
+ return DEFAULT_XSIZE;
+ }
+
+
+ /**
+ * Access the guiLeft field of a GuiRecipe instance, or use a fallback hardcoded value if that fails
+ *
+ * @param gui GuiRecipe object
+ * @return Integer value of the guiLeft field of that object
+ */
+ public static int getGuiLeft(GuiRecipe gui) {
+ if (guiLeftField != null) {
+ try {
+ return (int) guiLeftField.get(gui);
+ } catch (IllegalAccessException e) {
+ // Fail silently, hoping that it it fails it will be during initialization
+ }
+ }
+
+ // Fallback should work unless codechicken.nei.recipe.GuiRecipe implementation changes
+ return (Minecraft.getMinecraft().currentScreen.width - DEFAULT_XSIZE) / 2;
+ }
+
+ /**
+ * Access the guiTop field of a GuiRecipe instance, or use a fallback hardcoded value if that fails
+ *
+ * @param gui GuiRecipe object
+ * @return Integer value of the guiTop field of that object
+ */
+ public static int getGuiTop(GuiRecipe gui) {
+ if (guiTopField != null) {
+ try {
+ return (int) guiTopField.get(gui);
+ } catch (IllegalAccessException e) {
+ // Fail silently, hoping that it it fails it will be during initialization
+ }
+ }
+
+ // Fallback should work unless codechicken.nei.recipe.GuiRecipe implementation changes
+ int height = Minecraft.getMinecraft().currentScreen.height;
+ int ySize = Math.min(Math.max(height - 68, 166), 370);
+ return (height - ySize) / 2 + 10;
+ }
+
+ /**
+ * Initialize the GuiRecipe Field accessors through reflection
+ */
+ public GuiRecipeHelper() {
+ guiRecipeClass = GuiRecipe.class;
+ try {
+ guiLeftField = guiRecipeClass.getField("guiLeft");
+ guiLeftField.setAccessible(true);
+ guiTopField = guiRecipeClass.getField("guiTop");
+ guiTopField.setAccessible(true);
+ xSizeField = guiRecipeClass.getField("xSize");
+ xSizeField.setAccessible(true);
+ } catch(NoSuchFieldException|SecurityException e) {
+ FMLLog.log(Level.ERROR, INIT_ERROR + e.getMessage());
+ }
+ }
+}