Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

  • Replies 82
  • Views 493.9k
  • Created
  • Last Reply

Top Posters In This Topic

  • Author

Do you know that you can Configuration.class???

 

*Facepalm* Thanks for that tip.. I didn't realize that before.

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

  • Author

I think that was the problem and question, eclipse is now saying about the field the category String in the method mismatching (cannot convert from Object to ModConfigCategory). How should I have the fields as to solve this?

 

Here is my code

package common.zeroquest.core.configuration;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.TreeMap;

import net.minecraftforge.common.config.Configuration;

public class ModConfiguration extends Configuration {

public ModConfiguration() {}

/**
 * Create a configuration file for the file given in parameter.
 */
public ModConfiguration(File file) {
	super(file);
}

@Override
public ModConfigCategory getCategory(String category) {
	Class c = Class.forName(Configuration.class.toString());
	Field field1 = c.getDeclaredField("categories");
	Field field2 = c.getDeclaredField("changed");
	field1.setAccessible(true);
	field2.setAccessible(true);

	ModConfigCategory ret = field1.get(category); <<Error here

	if (ret == null) {
		if (category.contains(CATEGORY_SPLITTER)) {
			String[] hierarchy = category.split("\\" + CATEGORY_SPLITTER);
			ModConfigCategory parent = categories.get(hierarchy[0]);

			if (parent == null) {
				parent = new ModConfigCategory(hierarchy[0]);
				categories.put(parent.getQualifiedName(), parent);
				changed = true;
			}
			for (int i = 1; i < hierarchy.length; i++) {
				String name = ModConfigCategory.getQualifiedName(hierarchy[i], parent);
				ModConfigCategory child = categories.get(name);

				if (child == null) {
					child = new ModConfigCategory(hierarchy[i], parent);
					categories.put(name, child);
					changed = true;
				}

				ret = child;
				parent = child;
			}
		}
		else {
			ret = new ModConfigCategory(category);
			categories.put(category, ret);
			changed = true;
		}
	}
	return ret;
}
}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

...

ModConfigCategory ret = field1.get(category);

In this you are getting a field of ModConfigCategory from String!

I think  it should be field1.get(this).

Also what that will give you is instance of ConfigCategory, not your own class 'ModConfigCategory'.

+ Why are you using reflection in this case? You can just make another class copying that, and change some behaviors you want there. Configuration class is not intended to be inherited, so in this case this way is better.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

I started on this but now I'm getting an error from all of the reflection methods. They say to put a try and catch method in

 

package common.zeroquest.core.configuration;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.TreeMap;

import net.minecraftforge.common.config.Configuration;

public class ModConfiguration extends Configuration {

public ModConfiguration() {}

/**
 * Create a configuration file for the file given in parameter.
 */
public ModConfiguration(File file) {
	super(file);
}

@Override
public ModConfigCategory getCategory(String category) {
	Class c = Class.forName(Configuration.class.toString());
	Field field1 = c.getDeclaredField("categories");
	Field field2 = c.getDeclaredField("changed");
	Object a = field1.get(this);
	Object b = field2.get(this);
	field1.setAccessible(true);
	field2.setAccessible(true);

	ModConfigCategory ret = (ModConfigCategory) field1.get(this);

	if (ret == null) {
		if (category.contains(CATEGORY_SPLITTER)) {
			String[] hierarchy = category.split("\\" + CATEGORY_SPLITTER);
			ModConfigCategory parent = (ModConfigCategory)field1.get(hierarchy[0]);

			if (parent == null) {
				parent = new ModConfigCategory(hierarchy[0]);
				field1.set(parent.getQualifiedName(), parent);
				field2.setBoolean(b, true);
			}
			for (int i = 1; i < hierarchy.length; i++) {
				String name = ModConfigCategory.getQualifiedName(hierarchy[i], parent);
				ModConfigCategory child = (ModConfigCategory) field1.get(name);

				if (child == null) {
					child = new ModConfigCategory(hierarchy[i], parent);
					field1.set(name, child);
					field2.setBoolean(b, true);
				}

				ret = child;
				parent = child;
			}
		}
		else {
			ret = new ModConfigCategory(category);
			field1.set(category, ret);
			field2.setBoolean(b, true);
		}
	}
	return ret;
}
}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

That is natural, because reflection can sometimes throw exceptions like NoSuchFieldException.

 

Besides, I think you should not use reflection in this case.. You can sort the order of properties using Configuration#setCategoryPropertyOrder.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

Oh? So do I not override getCategory? And does this mean I do not need the ModConfigCategory class? Also, I try to override setCategoryPropertyOrder, there's a boolean that is private. How do I get it?

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Oh? So do I not override getCategory? And does this mean I do not need the ModConfigCategory class? Also, I try to override setCategoryPropertyOrder, there's a boolean that is private. How do I get it?

No, you can just use Configuration class. Your custom class is not needed.

You call Configuration#setCategoryPropertyOrder when you want.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

Question, now that I have the method overriden, how do I arrange it to where it calls the methods like I want them called?

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Question, now that I have the method overriden, how do I arrange it to where it calls the methods like I want them called?

No. In this way you should not make your own class. You should just use Configuration class.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

Then how am I suppose to change the sorting of the properties?

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Then how am I suppose to change the sorting of the properties?

When did you need the sorting? It depends.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

When the configuration file is being created for the first time, like how the game does it

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

I think you can get the value of the properties. Sort it with the name of the properties. Then provide the List<String> of the sorted order of the name of the properties to the method.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

How do I do that if I'm not using a custom class?

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

... After you registers the properties you want to sort.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

I know, but how? There isn't exactly a method that allows to manually sort them

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

I know, but how? There isn't exactly a method that allows to manually sort them

. Do it yourself. You should be able to write that logic.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

I don't know how.. thats why I'm asking. I've tried renaming and tried overriding the methods that would sort it and it didn't work

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

I don't know how.. thats why I'm asking. I've tried renaming and tried overriding the methods that would sort it and it didn't work

I said, don't override the method, and just use Configuration class.

I think you can get the properties as Collection.

Then you can sort it with your custom comparator using Collections#sort(List, Comparator).

The comparator should compare the properties with the value.

From the sorted list, build another list of List<String> containing the name of properties.

Then you can provide the list to the Configuration#setCategoryPropertyOrder(..)

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

  • Author

Oh.. so basically, I get the names of the properties, then order then the way I want and set them as the propOrder?

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

  • Author

Yes! That worked! Thanks for the help!

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

  • Author

Oh, one last question, is it possible for me put the mod's version and the config version inside the configuration file?

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Oh, one last question, is it possible for me put the mod's version and the config version inside the configuration file?

Where would you put them?

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.