Jump to content

Recommended Posts

Posted (edited)

Sometimes I have feelings that forge developers are trying to make things as hardest as possible. Anyway...

How to create config with the new superultragreat @Annotation system? Because as always there is no documentation anywhere, except the Choonster example which I had tried to use. But I didn't understand where to "register" config because just getting Configuration instance in preInit does nothing.

Thanks in advance.

Edited by Evi1Casul
Posted (edited)

You don't need to register the config anywhere, Forge automatically loads the config from every class annotated with @Config.

 

You've linked an old version of my code, you can see the latest version (as of the writing of this post) here. You can use the branch/tag dropdown near the top of the page on GitHub to switch to a branch, which will show you the latest code in that branch.

 

Is there something specific you're struggling with? If you post your code we may be able to help you further.

 

There's a WIP documentation page for the annotation config system here.

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

Here is my basic ConfigHandler class, maybe help you.
 

package net.xwerswoodx.vrenchant;

import java.io.File;

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

public class ConfigHandler {
	public static Configuration config;
	private static String file = "config/modID.cfg";
	
	public static void init() {
		config = new Configuration(new File(file));
		try {
			config.load();
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	/*
	 * Removes specific category from configuration file.
	 */
	public static void removeConfig(String category) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.hasCategory(category))
				config.removeCategory(new ConfigCategory(category));
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	/*
	 * Removes specific key in specific category from configuration file.
	 */
	public static void removeConfig(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key))
				config.getCategory(category).remove(key);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	public static int getInt(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key)) {
				return config.get(category, key, 0).getInt();
			}
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return 0;
	}
	
	public static double getDouble(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key)) {
				return config.get(category, key, 0D).getDouble();
			}
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return 0D;
	}
	
	public static float getFloat(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key)) {
				return (float)config.get(category, key, 0D).getDouble();
			}
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return 0f;
	}
	
	public static String getString(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key)) {
				return config.get(category, key, "").getString();
			}
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return "";
	}
	
	public static long getLong(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key)) {
				return config.get(category, key, 0L).getLong();
			}
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return 0L;
	}
	
	public static short getShort(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key)) {
				return (short)config.get(category, key, (short)0).getInt();
			}
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return (short)0;
	}
	
	public static byte getByte(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key)) {
				return (byte)config.get(category, key, (byte)0).getInt();
			}
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return (byte)0;
	}
	
	public static boolean getBoolean(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (config.getCategory(category).containsKey(key))
				return config.get(category, key, false).getBoolean();
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return false;
	}
	
	public static void writeConfig(String category, String key, String value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			String set = config.get(category, key, value).getString();
			config.getCategory(category).get(key).set(value);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	public static void writeConfig(String category, String key, int value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			int set = config.get(category, key, value).getInt();
			config.getCategory(category).get(key).set(value);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	public static void writeConfig(String category, String key, boolean value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			boolean set = config.get(category, key, value).getBoolean();
			config.getCategory(category).get(key).set(value);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	public static void writeConfig(String category, String key, long value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			long set = config.get(category, key, value).getLong();
			config.getCategory(category).get(key).set(value);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	public static void writeConfig(String category, String key, double value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			double set = config.get(category, key, value).getDouble();
			config.getCategory(category).get(key).set(value);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	public static void writeConfig(String category, String key, short value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			int set = config.get(category, key, value).getInt();
			config.getCategory(category).get(key).set(Integer.valueOf(value));
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}

	public static void writeConfig(String category, String key, byte value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			int set = config.get(category, key, value).getInt();
			config.getCategory(category).get(key).set(Integer.valueOf(value));
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}

	public static void writeConfig(String category, String key, float value) {
		config = new Configuration(new File(file));
		try {
			config.load();
			double set = config.get(category, key, value).getDouble();
			config.getCategory(category).get(key).set(Double.valueOf(value));
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
	}
	
	public static boolean hasCategory(String category) {
		config = new Configuration(new File(file));
		try {
			config.load();
			return config.hasCategory(category);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return false;
	}
	
	public static boolean hasKey(String category, String key) {
		config = new Configuration(new File(file));
		try {
			config.load();
			if (!config.hasCategory(category))
				return false;
			return config.getCategory(category).containsKey(key);
		} catch (Exception e) {
			System.out.println("Cannot load configuration file!");
		} finally {
			config.save();
		}
		return false;
	}
	
	public static void setFile(String filename) {
		file = "config/" + filename;
	}
	
	public static String getFile() {
		return file;
	}
}

 

Just put this code to ConfigHandler.java (Or you can give another name) and you can write, remove or get values like;

 

writeConfig(category[String], key[String], value[String, int, long, short, byte, boolean, double, float]);

ConfigHandler.writeConfig("My Category", "Name", "Hamit"); //Write string ("Hamit") to config.

ConfigHandler.writeConfig("My Category", "isPlayer", true); //Write boolean ("true") to config.

 

setFile(fileName[String]);

ConfigHandler.setFile("Hamit.cfg"); //Changes default file (which is set at the beginning of ConfigHandler class)

 

getString(category[String], key[String]); //You can use getInt, getLong, getShort etc.

ConfigHandler.getString("My Category", "Name"); //Get string value of "Name" from the category - named "My Category".

out.println("isPlayer " + ConfigHandler.getString("My Category", "isPlayer").toString() + " boolean value has written in file: " + ConfigHandler.getFile());

 

 

    @EventHandler
    public void onInitialization(FMLInitializationEvent event) {

        ConfigHandler.writeConfig("My Category", "Name", "Hamit");
        System.out.println("Name " + ConfigHandler.getString("My Category", "Name") + " string value has written in file: " + ConfigHandler.getFile());


        ConfigHandler.setFile("Mehmet.cfg");
        ConfigHandler.writeConfig("My Category", "Age", 22);
        System.out.println("Name " + String.valueOf(ConfigHandler.getInt("My Category", "Age")) + " integer value has written in file: " + ConfigHandler.getFile());



        ConfigHandler.setFile("Batuhan.cfg");
        ConfigHandler.writeConfig("My Category", "isPlayer", false);
        System.out.println("isPlayer " + ConfigHandler.getBoolean("My Category", "isPlayer").toString() + " boolean value has written in file: " + ConfigHandler.getFile());

    }
Edited by xwerswoodx
  • Thanks 1

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.