Jump to content

[1.8] Saving Configs [SOLVED]


JoelGodOfWar

Recommended Posts

I managed to load configs from a config file. But i can't seem to find where to add the save part to. If i add it to the keypress, it crashes the game. If i add it to the toggleenabled function called by the keypress it crashes the game. I have no gui, other than the keybindings added to Controls.

 

Here is the code i've been trying to use to save the configs.

Configuration config = new Configuration();
    		config.load();
    		Property p;
    		config.addCustomCategoryComment(Configuration.CATEGORY_GENERAL, "Allows you to Enable or Disable the display of Coordinates.");
    		p = config.get(Configuration.CATEGORY_GENERAL, "Enable/Disable", true);
    	    p.comment = "Enable/Disable display of coordinates";
    	    
    	    p.set(EventManager.Enabled);
    	    config.save();

Link to comment
Share on other sites

Well this is my preInit

@EventHandler
public void preInit(FMLPreInitializationEvent event){
	FMLCommonHandler.instance().bus().register(new com.datacraftcoords.KeyInputHandler());
	com.datacraftcoords.KeyBindings.init();
	Configuration config = new Configuration(event.getSuggestedConfigurationFile());

        config.load();
        EventManager.Enabled = config.get(Configuration.CATEGORY_GENERAL, "Enabled", false).getBoolean(true);

        config.save();
}

 

How would i save the config after the state of EventManager.Enabled has changed?

Link to comment
Share on other sites

Just in case someone else runs into this issue, here's the code i used to solve it with a little help from diesieben07.

 

This goes in your Mod loader code. The preInit, Init, postInit section.

private File configFile;

@EventHandler
public void preInit(FMLPreInitializationEvent event){
	FMLCommonHandler.instance().bus().register(new KeyInputHandler());
	KeyBindings.init();

	configFile = event.getSuggestedConfigurationFile();
	Configs.LoadConfigSettings(configFile);

 

Next create a new class and paste this code in.

package com.datacraftcoords;
import java.io.File;

import com.datacraftcoords.event.EventManager;

import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;


public class Configs {
public static final String CATEGORY_GENERAL = "general";
public static Configuration config = null;
public static void LoadConfigSettings(File configFile)
    {
    	ReadConfigSettings(configFile, true);
    }
public static void SaveConfigSettings()
    {
    	ReadConfigSettings(null, false);
    }
    
    /**
     * Creates the config file if it doesn't already exist.
     * It loads/saves config values from/to the config file.
     * @param configFile Standard Forge configuration file
     * @param loadSettings set to true to load the settings from the config file, 
     * or false to save the settings to the config file
     */
    private static void ReadConfigSettings(File configFile, boolean loadSettings)
    {
    	//NOTE: doing config.save() multiple times will bug out and add additional quotes to
    	//categories with more than 1 word
    	//Configuration config = null;
    	if(loadSettings)
    	{
            config = new Configuration(configFile);
            config.load();
    	}
        
        Property p;
        config.addCustomCategoryComment(CATEGORY_GENERAL, "Allows you to Enable or Disable the display of Coordinates.");
	p = config.get(CATEGORY_GENERAL, "Enabled", true);
    p.comment = "Enable/Disable display of coordinates";
    
    p.set(EventManager.Enabled);
    config.save();
    }
}

 

Next add this where you want to save your configs.

Configs.SaveConfigSettings();

 

 

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.