Jump to content

[1.7.10] [Solved] How do I use config file correctly


Erfurt

Recommended Posts

Hey guys, so I'm at a point where I want to learn how to use a config file to allow/disallow certain things in my mod. However, I haven't been able to find anything about the proper way to use a config file, only how to create it and add things to it. If someone here could point me in the right direction that would be great.

Link to comment
Share on other sites

when you add things to the config file you are actually reading it. you add the default or get the value set in the file. in other words, if that thing you're adding is already there and if it has another value, then it reads that value from the file. if what you're adding is not in the file, then it adds it:

 

@EventHandler
public static void preInit(FMLPreInitializationEvent event)
{
    //this line either creates the file if it doesn't exist or opens it if it already exists.
    Configuration config = new Configuration(event.getSuggestedConfigurationFile());
    config.load();//reads the contents of the file into the Configuration object.

    //use the config.get* methods to add/get values from the config as I explained above.

    //after you're done:
    config.save();//saves the Configuration content into the file.
}

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

when you add things to the config file you are actually reading it. you add the default or get the value set in the file. in other words, if that thing you're adding is already there and if it has another value, then it reads that value from the file. if what you're adding is not in the file, then it adds it:

 

@EventHandler
public static void preInit(FMLPreInitializationEvent event)
{
    //this line either creates the file if it doesn't exist or opens it if it already exists.
    Configuration config = new Configuration(event.getSuggestedConfigurationFile());
    config.load();//reads the contents of the file into the Configuration object.

    //use the config.get* methods to add/get values from the config as I explained above.

    //after you're done:
    config.save();//saves the Configuration content into the file.
}

 

Okay, so if I were to use a boolean for a item, how would I do that? Isn't this code only for making the config file, with the default values. Could you maybe give me an example for an item if true is in the mod, if false isn't in the mod?

Link to comment
Share on other sites

it creates the file or reads it if it already exists.

 

boolean shouldRegisterItem;

@EventHandler
public static void preInit(FMLPreInitializationEvent event)
{
    //this line either creates the file if it doesn't exist or opens it if it already exists.
    Configuration config = new Configuration(event.getSuggestedConfigurationFile());
    config.load();//reads the contents of the file into the Configuration object.

    boolean defaultValue = true;
    
    shouldRegisterItem = config.get(Configuration.CATEGORY_GENERAL, "should_register_item", defaultValue);

    //after you're done:
    config.save();//saves the Configuration content into the file.
}

@EventHandler
public static void init(FMLInitializationEvent event)
{
    if(shouldRegisterItem)
    {
        //invoke GameRegistry.registerItem() for your item.
    }
}

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

it creates the file or reads it if it already exists.

 

boolean shouldRegisterItem;

@EventHandler
public static void preInit(FMLPreInitializationEvent event)
{
    //this line either creates the file if it doesn't exist or opens it if it already exists.
    Configuration config = new Configuration(event.getSuggestedConfigurationFile());
    config.load();//reads the contents of the file into the Configuration object.

    boolean defaultValue = true;
    
    shouldRegisterItem = config.get(Configuration.CATEGORY_GENERAL, "should_register_item", defaultValue);

    //after you're done:
    config.save();//saves the Configuration content into the file.
}

@EventHandler
public static void init(FMLInitializationEvent event)
{
    if(shouldRegisterItem)
    {
        //invoke GameRegistry.registerItem() for your item.
    }
}

I feel a bit studip now :D

Thanks for helping me :)

Link to comment
Share on other sites

hey I just know that because someone taught me too. nobody's stupid :)

 

just notice the category can be any string you want, and if you want sub categories all you have to do is to name them them like this: "parentCategory.childCategory". You can use config.addCustomCategoryComment(categoryName, "categoryComment")

 

it's a lot easier than it seems at first.

  • Like 1

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.