aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pers/gwyog/gtneioreplugin/util/GuiRecipeHelper.java
blob: 8cf6a8faacc8a028b49cc600dc385a985f4a71ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package pers.gwyog.gtneioreplugin.util;

import codechicken.nei.recipe.GuiRecipe;
import cpw.mods.fml.common.FMLLog;
import java.lang.reflect.Field;
import net.minecraft.client.Minecraft;
import org.apache.logging.log4j.Level;

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;

    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() {
        Class<GuiRecipe> 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());
        }
    }
}