|
| 1 | +package com.falsepattern.lib.internal.config; |
| 2 | + |
| 3 | +import com.falsepattern.lib.internal.Share; |
| 4 | +import lombok.AccessLevel; |
| 5 | +import lombok.NoArgsConstructor; |
| 6 | +import lombok.val; |
| 7 | + |
| 8 | +import net.minecraft.client.Minecraft; |
| 9 | +import net.minecraft.client.gui.GuiScreen; |
| 10 | +import net.minecraftforge.client.event.GuiOpenEvent; |
| 11 | +import net.minecraftforge.common.MinecraftForge; |
| 12 | +import cpw.mods.fml.client.FMLClientHandler; |
| 13 | +import cpw.mods.fml.client.GuiIngameModOptions; |
| 14 | +import cpw.mods.fml.client.GuiModList; |
| 15 | +import cpw.mods.fml.common.ModContainer; |
| 16 | +import cpw.mods.fml.common.eventhandler.SubscribeEvent; |
| 17 | + |
| 18 | +import java.lang.reflect.Field; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static lombok.AccessLevel.PRIVATE; |
| 24 | + |
| 25 | +@NoArgsConstructor(access = PRIVATE) |
| 26 | +public final class InGameModOptionsFix { |
| 27 | + private static boolean isInitialized = false; |
| 28 | + |
| 29 | + private static Field MODS_FIELD; |
| 30 | + |
| 31 | + public static void init() { |
| 32 | + if (isInitialized) |
| 33 | + return; |
| 34 | + |
| 35 | + try { |
| 36 | + MODS_FIELD = GuiModList.class.getDeclaredField("mods"); |
| 37 | + MODS_FIELD.setAccessible(true); |
| 38 | + } catch (NoSuchFieldException e) { |
| 39 | + MODS_FIELD = null; |
| 40 | + Share.LOG.error("Failed to get field: cpw.mods.fml.client.GuiModList.mods," |
| 41 | + + " In-Game Mod Options Fix will not work", e); |
| 42 | + return; |
| 43 | + } |
| 44 | + MinecraftForge.EVENT_BUS.register(new InGameModOptionsFix()); |
| 45 | + isInitialized = true; |
| 46 | + } |
| 47 | + |
| 48 | + @SubscribeEvent |
| 49 | + public void onGuiOpen(final GuiOpenEvent event) { |
| 50 | + if (!isInitialized) |
| 51 | + return; |
| 52 | + if (!LibraryConfig.IN_GAME_MOD_OPTIONS_FIX) |
| 53 | + return; |
| 54 | + |
| 55 | + if (event.gui instanceof GuiIngameModOptions) |
| 56 | + event.gui = new GuiModConfigList(Minecraft.getMinecraft().currentScreen); |
| 57 | + } |
| 58 | + |
| 59 | + private static class GuiModConfigList extends GuiModList { |
| 60 | + private GuiModConfigList(GuiScreen screen) { |
| 61 | + super(screen); |
| 62 | + val mods = new ArrayList<ModContainer>(); |
| 63 | + for (val mod : getModsFromPrivateField()) { |
| 64 | + val guiFactory = FMLClientHandler.instance().getGuiFactoryFor(mod); |
| 65 | + if (guiFactory == null) |
| 66 | + continue; |
| 67 | + if (guiFactory.mainConfigGuiClass() != null) |
| 68 | + mods.add(mod); |
| 69 | + } |
| 70 | + setModsToPrivateField(mods); |
| 71 | + } |
| 72 | + |
| 73 | + private List<ModContainer> getModsFromPrivateField() { |
| 74 | + try { |
| 75 | + return (List<ModContainer>) MODS_FIELD.get(this); |
| 76 | + } catch (Exception e) { |
| 77 | + return Collections.emptyList(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void setModsToPrivateField(List<ModContainer> mods) { |
| 82 | + try { |
| 83 | + MODS_FIELD.set(this, mods); |
| 84 | + } catch (Exception ignored) { |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments