Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.12] How to create config?


Recommended Posts

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
Link to post
Share on other sites

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.

Link to post
Share on other sites

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
Link to post
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.

Guest
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.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • So I thought that I had gotten pretty far on my own but now apparently my code crashes the game. I am new to using the forge API and modding in general and some help on why this crashes the game and how to fix it would be greatly appreciated:   public class SpecialItem extends ShootableItem {      public SpecialItem(Properties properties){ super(properties); }      @Override      public Predicate<ItemStack> getInventoryAmmoPredicate() { return null; }      @Override      public int func_230305_d_() { return 15; }      public int tcount=0;      @SubscribeEvent public void ontick(TickEvent.ClientTickEvent event){            tcount++;      }      @Override      public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {      if (!playerIn.getCooldownTracker().hasCooldown(this)) {           playerIn.getCooldownTracker().setCooldown(this, 40);           Vector3d v3 = playerIn.getLook(1);           for (tcount = 0; tcount <= 40;) {                if (tcount%10 == 0 && tcount!= 40) {                     final SmallFireballEntity entitysmallfireball = new SmallFireballEntity(playerIn.getEntityWorld(), playerIn.getPosX(), playerIn.getPosY() + playerIn.getEyeHeight() - 0.4, playerIn.getPosZ(), v3.x + playerIn.getRNG().nextGaussian() * 0.05, v3.y, v3.z);                     entitysmallfireball./* shoot */func_234612_a_(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0f, 1.5f, 1.0f); worldIn.addEntity(entitysmallfireball); } } tcount = 0;                     return ActionResult.resultSuccess(playerIn.getHeldItem(handIn));            } return ActionResult.resultFail(playerIn.getHeldItem(handIn));      } }
    • I found a way to handle this, using javapoet as you suggested. For those who are interested in how it works, you can take a look at my sample here: https://github.com/DarkShadow44/compatibilitymod/tree/sourcegen Might not work with eclipse yet, but it builds properly.
    • It's fired on the logical client and logical server. Most likely you'll only need to execute the logic on the server.
    • If you're translating individual files, there's not much you can do about it. You could use the updateMappings gradlew task, but that's better for large batches of code. As for JSON item models, that is a much more complicated issue that involves layers and custom transform types. While still possible, the tradeoff isn't really worth it.
    • it keeps saying when i i try to install it, it says These libraries failed to download. Try again   
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.