Jump to content

[1.8]Making configurations for Biome and Dimension IDs


NovaViper

Recommended Posts

  • Replies 82
  • Created
  • Last Reply

Top Posters In This Topic

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!

Link to comment
Share on other sites

...

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.




×
×
  • Create New...

Important Information

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