0
\$\begingroup\$

So I have recently been working on a minecraft 1.8.9 forge mod, just to play around and see what there is (eventually I am planning on making a utilities mod with versions for 1.8-1.19, which is why I'm so far back). I have had no problems... until I got to configs. I already have code to create and read from a config file, but I can not figure out how to use MinecraftForge's mod options > config menu to allow GUI modification of the settings. I have had no luck finding any information on how to do this back in 1.8, so if anyone could help me out a bit (linking sources, giving example code, or referring me to a forge class that has the relevant code in it), I would greatly appreciate it.

TL;DR I need help implementing forge's config gui system.

My current config code (in the Main class):

public static Configuration config;
    private File confDir;
    
    public static Logger lmao;

    public static Configuration getConfig()
    {
        return config;
    }
    @EventHandler
    public void preinit(FMLPreInitializationEvent event) {
        lmao = event.getModLog();

        confDir = event.getModConfigurationDirectory();
        config = null;
        File configFile = new File(confDir,"config.cfg");
        config = new Configuration(configFile);
        syncConfig(true);
        event.getModLog().log(Level.ALL,"Preinitialization complete.");
    }
    private static void syncConfig(boolean load)
    {
        List<String> propOrder = new ArrayList<String>();

        if (!config.isChild)
        {
            if (load)
            {
                config.load();
            }
            Property enableGlobalCfg = config.get(CATEGORY_GENERAL, "enableGlobalConfig", false).setShowInGui(false);
            if (enableGlobalCfg.getBoolean(false))
            {
                Configuration.enableGlobalConfig();
            }
        }

        Property prop;

        //set prop value, then:
        //propOrder.add(prop.getName());

        prop = config.get(CATEGORY_GENERAL,"testbool",false);
        prop.comment = "for testing purposes";
        prop.setLanguageKey("mod.conf.testbool");
        propOrder.add(prop.getName());

        prop = reConf.get(CATEGORY_GENERAL,"testvar","can you see this?");
        prop.comment = "for testing purposes only";
        prop.setLanguageKey("mod.conf.teststring");
        propOrder.add(prop.getName());

        config.setCategoryPropertyOrder(CATEGORY_GENERAL, propOrder);


        if (config.hasChanged())
        {
            config.save();
        }
    }
          public static void syncConf(boolean load1) {
          //public configuration sync method
        syncConfig(load1);
    }
    @SubscribeEvent
    public void onConfigChangedEvent(OnConfigChangedEvent event)
    {
        if (event.modID.equals(MODID))
        {
            if (Configuration.CATEGORY_GENERAL.equals(event.configID))
            {
                syncConfig(false);
            }
        }
    }
          //much of this was taken from or inspired by forge's source code, and adapted to work with this mod.

I have written some GUI code, but managed to mess it up enough that I will just rewrite it based off ForgeGuiFactory.java later, which is why I have attached no GUI code here.

If anyone can help, please do. If this post is breaking a forum rule, DEFINITELY tell me, since I need to know.

Thanks ahead of time,

-MiSt_Rocky

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

To do this you need 3 things:

  • A class that extends net.minecraftforge.fml.client.config.GuiConfig:
public class ExampleConfig extends GuiConfig {
        ExampleConfig(GuiScreen parent) {
            super(
                    parent,
                    new ConfigElement(config.getCategory(Configuration.CATEGORY_CLIENT)).getChildElements(),
                    "modid",
                    false,
                    false,
                    "title"
            );
        }
    }

Take a look at the GuiConfig class for what the parameters mean. The most important one here is the 2nd one, which is the list of elements this gui will have.

new ConfigElement(config.getCategory(Configuration.CATEGORY_CLIENT)).getChildElements() will get all the elements inside the "client" category. (You do not need to do this, I just thought it'd be cool to mention)

  • A class that implements net.minecraftforge.fml.client.IModGuiFactory:
public class GuiFactory implements IModGuiFactory {
    @Override
    public void initialize(Minecraft minecraft) {}

    @Override
    public Class<? extends GuiScreen> mainConfigGuiClass() {
        return ExampleConfig.class;
    }

    @Override
    public Set<RuntimeOptionCategoryElement> runtimeGuiCategories() {
        return null;
    }

    @Override
    public RuntimeOptionGuiHandler getHandlerFor(RuntimeOptionCategoryElement runtimeOptionCategoryElement) {
        return null;
    }
}

The only notable thing here is the mainConfigGuiClass method, which returns the class made on the step before.

  • Add the GuiFactory to your Mod annotation:
@Mod(
    // ... previous arguments
    guiFactory = "path.to.GuiFactory"
)
public class ExampleMod {
    // ...
}

With all of this done, once you launch the game, the Config options in the mod options menu should no longer be greyed out.

Hope this helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .