Elix_x Posted June 7, 2015 Posted June 7, 2015 Do you know that you can Configuration.class??? Quote Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
NovaViper Posted June 7, 2015 Author Posted June 7, 2015 Do you know that you can Configuration.class??? *Facepalm* Thanks for that tip.. I didn't realize that before. Quote 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!
Elix_x Posted June 7, 2015 Posted June 7, 2015 Do you know that you can Configuration.class??? *Facepalm* Thanks for that tip.. I didn't realize that before. And also, i don't reccomend defining these as fields in class. Probably just in method... Quote Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
NovaViper Posted June 7, 2015 Author Posted June 7, 2015 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; } } Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 ... 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. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 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; } } Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 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. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 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? Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 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. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 Question, now that I have the method overriden, how do I arrange it to where it calls the methods like I want them called? Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 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. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 Then how am I suppose to change the sorting of the properties? Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 Then how am I suppose to change the sorting of the properties? When did you need the sorting? It depends. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 When the configuration file is being created for the first time, like how the game does it Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 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. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 How do I do that if I'm not using a custom class? Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 ... After you registers the properties you want to sort. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 I know, but how? There isn't exactly a method that allows to manually sort them Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 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. Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 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 Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 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(..) Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 Oh.. so basically, I get the names of the properties, then order then the way I want and set them as the propOrder? Quote 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!
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 Yes! That worked! Thanks for the help! Quote 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!
NovaViper Posted June 8, 2015 Author Posted June 8, 2015 Oh, one last question, is it possible for me put the mod's version and the config version inside the configuration file? Quote 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!
Abastro Posted June 8, 2015 Posted June 8, 2015 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? Quote I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
Recommended Posts
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.