See both implementations have their advantages/disadvantages, Retatop's would require far less upkeep as (from the looks of the code) it automatically sorts the properties file. However you cannot write comments in the config file besides the header.
Mine is essentually the opposite, you can write commanets and stuff but it would require more work each time you want to add new properties (and by more work i mean a 5-10 lines or so depending on how long your comments are)
The Config file could become quite long so his way does seem like it may be better but, I would prefer more work and making the user's live a tad bit easier but having the config generated rather then having to place it somewhere. Though I guess that really isn't that big of a deal. Is it easy though to understand in your way how to actually read in more variables or is your way complicated to add new ones.
Also I would need it to recognize {var1} and {var2} as equalling a certain variable. I guess the easier way is always better but I'm not sure.
Yeah no problem, although it may take me a bit to do it as i am about to run away to work. PM me with the details you need and the example output (if not I will loose it)
Could you write the file you needed within java using normal output instead of a properties file then get it with properties?
The data is read in as a string normally, thats why i needed an integer cast. To read it in, all you would be doing is calling properties.load and it would do it automatically
This is asking a ton but is it possible for you to modify your code or give me a hand actually implementing this into my mod to read the file. The writing I will worry about later because in reality if that fails I can just have the user manually put the config in but the reading needs to work.
The properties class automatically takes out any line preceded by a "#".
The only downside of mine is that it actually takes "[Item Ids]" as a property. It isn't really a problem because you don't ever see the actual hashtable (its abstracted) and as long as you don't try and retrieve these dummy properties, there is no problem
Well I'm going to be using strings though not ints or doubles. Will this still work? I still don't understand how I would read in something from that example config I have on a post on this page.
No you can't that's why i write my properties file manually
The properties class automatically considers the pound symbol to be a comment, and the data is read in as a string so you could make a custom parser to find the varibales
Thats what the purpose of my sortedproperties extended class was, i can post that if you want
But using this would I be able to generate the config successfully in the manner I displayed in my example or would the user of the mod have to place the config there manually?
I have mad my own custom config file. Give me a couple of minutes and I will upload it for you.
Uploaded now.
A bit of explination, this uses the default java properties implementation only for reading from the file & retrieving information at runtime. However when writing the java properties will scramble the order, so I write the file manually.
package net.minecraft.src.battlegear.misc;
import java.io.*;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
import net.minecraft.src.ModLoader;
import org.lwjgl.input.Keyboard;
public class Config {
public static final File ConfigLocation = new File(ModLoader.getMinecraftInstance().getMinecraftDir()+File.separator+"Battlegear.ini");
public Properties props;
//Compatibility Patches
public static final String currencyCompatibilityKey = "CurrencyCompatibility";
//Item Ids
public static final String coinIDKey = "CoinID";
public static final String[] waraxeIdKeys = new String[5];
public static final String[] maceIdKeys = new String[5];
public static final String[] spearIdKeys = new String[5];
public static final String[] shieldIds = new String[5];
public static final String rock = "RockId";
public static final String chainId = "ChainID";
//Block Ids
public static final String bannerIDKey = "BannerID";
public static final String trainingDummyIDKey = "TrainingDummyID";
//GUI Settings
public static final String guiPosition = "GUIPosition";
//NPC Settings
public static final String bannersDespawnKey = "BannersDespawn";
public static final String meeleAttackCreeper = "MeleeAttacksCreepers";
public static final String rangedAttackCreeper = "RangedAttacksCreepers";
public static final String displayIconKey = "DispaySoldierIcons";
public static final String soldiersAttack = "SoldiersAttackSoldiers";
//Entity Names
public static final String militiaNameKey = "MilitiaName";
public static final String footmanNameKey = "FootmanName";
public static final String knightNameKey = "KnightName";
public static final String archerNameKey = "ArcherName";
public static boolean shouldRenderIcon;
public static boolean shouldSoldiersAttackSoldiers;
private static String[] toolTypes = {"Wood", "Stone", "Iron", "Diamond", "Gold"};
static{
//TODO Change numbers to material names (EnumMaterial)
for(int i = 0; i < waraxeIdKeys.length; i++){
waraxeIdKeys[i] = "Battleaxe("+toolTypes[i]+")";
}
for(int i = 0; i < maceIdKeys.length; i++){
maceIdKeys[i] = "Warhammer("+toolTypes[i]+")";
}
for(int i = 0; i < spearIdKeys.length; i++){
spearIdKeys[i] = "Spear ("+toolTypes[i]+")";
}
for(int i = 0; i < shieldIds.length; i++){
shieldIds[i] = "Shield("+EnumShieldMaterial.values()[i].name()+")";
}
}
public static Properties defaultProps(){
Properties p = new Properties();
p.put(meeleAttackCreeper, "0");
p.put(rangedAttackCreeper, "1");
p.put(displayIconKey, "1");
p.put(currencyCompatibilityKey, "0");
p.put(coinIDKey, "14050");
for(int i = 0; i < waraxeIdKeys.length; i++){
p.put(waraxeIdKeys[i], String.valueOf(14052+i));
}
for(int i = 0; i < maceIdKeys.length; i++){
p.put(maceIdKeys[i], String.valueOf(14057+i));
}
for(int i = 0; i < spearIdKeys.length; i++){
p.put(spearIdKeys[i], String.valueOf(14062+i));
}
for(int i = 0; i < 2; i++){
p.put(shieldIds[i], String.valueOf(14100+i));
}
for(int i = 2; i < shieldIds.length; i++){
p.put(shieldIds[i], String.valueOf(14100+(i-1)*17));
}
p.put(chainId, "14067");
p.put(rock, "14068");
p.put(bannerIDKey, "211");
p.put(trainingDummyIDKey, "212");
p.put(guiPosition, "0");
p.put(militiaNameKey, "MaB Militia");
p.put(footmanNameKey, "MaB Footman");
p.put(knightNameKey, "MaB Knight");
p.put(archerNameKey, "MaB Archer");
return p;
}
public Config(){
props = new Properties(defaultProps());
}
public String getString(String key){
return props.getProperty(key);
}
public int getInt(String key){
return Integer.parseInt(props.getProperty(key));
}
public boolean getBoolean(String key){
return ! props.getProperty(key, "1").equals("0");
}
public void load(File file) throws IOException{
props.load(new BufferedReader(new FileReader(file)));
shouldRenderIcon = getBoolean(displayIconKey);
shouldSoldiersAttackSoldiers = getBoolean(soldiersAttack);
}
public void save(File outFile) throws IOException{
if(outFile.exists())
outFile.delete();
BufferedWriter out=new BufferedWriter(new FileWriter(outFile));
out.write("#Battlecraft Settings");
out.newLine();
out.write("#Generated "+DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));
out.newLine();
//Compatibility Patches
out.newLine();
out.write("[Compatibility Patches]");
out.newLine();
out.write("//This will change the default coin recepie from 1 nugget -> 1 coin\n" +
"//to 2 nuggets next to each other -> 2 coins");
out.write(currencyCompatibilityKey+"="+props.getProperty(currencyCompatibilityKey, "0"));
out.newLine();
//Item IDs
out.newLine();
out.write("[Item Ids]");
out.newLine();
out.write(coinIDKey+"="+props.getProperty(coinIDKey, "14050"));
out.newLine();
out.write(chainId+"="+props.getProperty(chainId, "14067"));
out.newLine();
for(int i = 0; i < waraxeIdKeys.length; i++){
out.write(waraxeIdKeys[i]+"="+props.getProperty(waraxeIdKeys[i], String.valueOf(14052+i)));
out.newLine();
}
out.newLine();
for(int i = 0; i < maceIdKeys.length; i++){
out.write(maceIdKeys[i]+"="+props.getProperty(maceIdKeys[i], String.valueOf(14057+i)));
out.newLine();
}
out.newLine();
for(int i = 0; i < spearIdKeys.length; i++){
out.write(spearIdKeys[i]+"="+props.getProperty(spearIdKeys[i], String.valueOf(14062+i)));
out.newLine();
}
out.newLine();
for(int i = 0; i < shieldIds.length; i++){
out.write(shieldIds[i]+"="+props.getProperty(shieldIds[i], String.valueOf(14067+i)));
out.newLine();
}
//Block Ids
out.newLine();
out.write("[Block Ids]");
out.newLine();
out.write(bannerIDKey+"="+props.getProperty(bannerIDKey, "211"));
out.newLine();
out.newLine();
out.write("[GUI Settings]");
out.newLine();
out.write("//Changes the position of the currently selected weapon slots");
out.newLine();
out.write("//0 = default, bottom corners");
out.newLine();
out.write("//1 = alternate, left and right sides of the screen");
out.newLine();
out.write(guiPosition+"="+props.getProperty(guiPosition, "0"));
out.newLine();
out.close();
}
}
And the output
#Battlecraft Settings
#Generated 11/02/2012 10:26:50 PM
[Compatibility Patches]
//This will change the default coin recepie from 1 nugget -> 1 coin
//to 2 nuggets next to each other -> 2 coinsCurrencyCompatibility=0
[Item Ids]
CoinID=14050
ChainID=14067
Battleaxe(Wood)=14052
Battleaxe(Stone)=14053
Battleaxe(Iron)=14054
Battleaxe(Diamond)=14055
Battleaxe(Gold)=14056
Warhammer(Wood)=14057
Warhammer(Stone)=14058
Warhammer(Iron)=14059
Warhammer(Diamond)=14060
Warhammer(Gold)=14061
Spear (Wood)=14062
Spear (Stone)=14063
Spear (Iron)=14064
Spear (Diamond)=14065
Spear (Gold)=14066
Shield(WOOD)=14100
Shield(HIDE)=14101
Shield(IRON)=14117
Shield(DIAMOND)=14134
Shield(GOLD)=14151
[Block Ids]
BannerID=211
[GUI Settings]
//Changes the position of the currently selected weapon slots
//0 = default, bottom corners
//1 = alternate, left and right sides of the screen
GUIPosition=0
Thanks! I understand the writing part but the actual reading part confuses me a little. If I wanted to from the config below only read in the the lines that have something on them and that have no # how would I go about doing that then?
#Example Config File
#--------------------
#Command Helper Button Configuration
#The first variable is the name of the button. Keep it short to preserve proper formmating
#The second variable is the command that will be performed.
#(Make sure to put the exact command including the slash if it has one.)
#(Use {var1} if you are required to enter some kind of variable such as an item number
# use {var2} if you are required to enter a player name or in some cases will be used as a second variable.
#------------------------------------
#Button 1
button1name=Rollback
button1command=/hk rollback p:{var2}
#This list would go on for quite a few buttons but this is
#just an example of what it would be like.
#The pound signs (#) are suppose to be comments thus meaning
#the entire line after it should be ignored.
#{var2} and {var1} would need to be regonized as variables.
#These variables will contain text strings.
#Say var1 holds "Hello World" and var2 holds "Bob"
#When the user hits Button3 it would do /ban Bob(var2) Hello world(var1)
Note: The sortedproperties class is just something i made that sorts the properties hastable
Your's seems like it would have the same issue as ModLoader has where it writes out in a random order which of course wouldn't work for me since it has to be in order.
I actually didnt know that modloader could to the config file so i just did it with javas properties class
No offence to the Modloader creator but it doesn't work that good. How did you do it with the Properties class? Mind posting some of your code for the config part of it? Really want to know how to do it and I learn best from example.
You might want to take a look at the BufferedReader and BufferedWriter classes.
No experience with java, but I think those are the classes needed for reading/writing files, you can use those to read and write lines, it's up to you to parse those lines.
Well here is an example of what my config would look like. My issue is going to be trying to actually read it and store this info:
#Example Config File
#--------------------
#Command Helper Button Configuration
#The first variable is the name of the button. Keep it short to preserve proper formmating
#The second variable is the command that will be performed.
#(Make sure to put the exact command including the slash if it has one.)
#(Use {var1} if you are required to enter some kind of variable such as an item number
# use {var2} if you are required to enter a player name or in some cases will be used as a second variable.
#------------------------------------
#Button 1
button1name=Rollback
button1command=/hk rollback p:{var2}
#This list would go on for quite a few buttons but this is
#just an example of what it would be like.
#The pound signs (#) are suppose to be comments thus meaning
#the entire line after it should be ignored.
#{var2} and {var1} would need to be regonized as variables.
#These variables will contain text strings.
#Say var1 holds "Hello World" and var2 holds "Bob"
#When the user hits Button3 it would do /ban Bob(var2) Hello world(var1)
I was having a lot of trouble getting MC to start with Modloader enabled (Standard Mojang screen freezes and black screens), but then I loaded it with MCPatcher and it worked perfectly. You may want to try this if nothing else works.
Did you remove the META-INF folder in the minecraft.jar ?
Then google it. If you don't want to use ModLoader's easy functions for this then go find a tutorial for it. At this point it's not minecraft specific so you should be able to find dozens of them.
Well with the items not being arranged in the order I would like it kinda ruins it. I will have like 20 custimizable things that each have a number.
Button1
Button2
Button3
Etc
If these are scrambled then this is VERY inconvenient for the user.
I assume there are other ways; but I don't know the exact ways they do it. This is the standard ModLoader config file setup. However, by the looks of it, you want something more like the Options file or even maybe a language file, not a standard mod configuration file.
Sorry I can't be of more help; I'd recommend looking at whatever classes generate options.txt and see how Notch did it.
I know how to write and read from a file but I don't have enough understanding on how exactly to parse this file and only take in the certain parts.
Anyway to fix the issue with using MLProp's configuration feature? The issue I'm having is that the items I want to generate in the file are not being placed in the file in the order the code is. It should go like:
button1
button2
button3
button4
but instead it goes:
button3
button1
button4
button2
Is there a way to fix this at all? Also how do we add comments to be generated as. (our own custom comments)
Copy the source files you've modded and put them somewhere safe, then decompile a ModLoader minecraft.jar, then add the source files back in -- it's unlikely ModLoader edits the exact source files you are.
Okay I got the file but the config generated seems very un-organized and has some weird variables on it:
#button1name (java.lang.String:Time Day)
#button2name (java.lang.String:Time Night)
#button3name (java.lang.String:Vote Day)
#button4name (java.lang.String:Vote Night)
#
#Sun Feb 12 18:31:58 EST 2012
button3name=Vote Day
button2name=Time Night
checksum=-11hylm
button4name=Vote Night
button1name=Time Day
Here is the code for it I'm using:
@MLProp public static String button1name = "Time Day";
@MLProp public static String button2name = "Time Night";
@MLProp public static String button3name = "Vote Day";
@MLProp public static String button4name = "Vote Night";
It's also placing them out of order which of course for the naming I'm doing for the variables it needs to be in numerical order. Is there a fix for this?
0
The Config file could become quite long so his way does seem like it may be better but, I would prefer more work and making the user's live a tad bit easier but having the config generated rather then having to place it somewhere. Though I guess that really isn't that big of a deal. Is it easy though to understand in your way how to actually read in more variables or is your way complicated to add new ones.
Also I would need it to recognize {var1} and {var2} as equalling a certain variable. I guess the easier way is always better but I'm not sure.
0
Well you don't really have to both do it lol but the config is located on this post in a quoted text. It's the output. It will be a lot longer then shown there but it will take some time to repeat that same thing for all 32 buttons.
http://www.minecraftforum.net/topic/1021521-making-a-configuration-file/page__view__findpost__p__12823239
0
This is asking a ton but is it possible for you to modify your code or give me a hand actually implementing this into my mod to read the file. The writing I will worry about later because in reality if that fails I can just have the user manually put the config in but the reading needs to work.
0
Well I'm going to be using strings though not ints or doubles. Will this still work? I still don't understand how I would read in something from that example config I have on a post on this page.
I don't mind doing this.
0
But using this would I be able to generate the config successfully in the manner I displayed in my example or would the user of the mod have to place the config there manually?
0
Thanks! I understand the writing part but the actual reading part confuses me a little. If I wanted to from the config below only read in the the lines that have something on them and that have no # how would I go about doing that then?
Your's seems like it would have the same issue as ModLoader has where it writes out in a random order which of course wouldn't work for me since it has to be in order.
0
No offence to the Modloader creator but it doesn't work that good. How did you do it with the Properties class? Mind posting some of your code for the config part of it? Really want to know how to do it and I learn best from example.
0
Well here is an example of what my config would look like. My issue is going to be trying to actually read it and store this info:
0
0
Did you remove the META-INF folder in the minecraft.jar ?
0
Well with the items not being arranged in the order I would like it kinda ruins it. I will have like 20 custimizable things that each have a number.
Button1
Button2
Button3
Etc
If these are scrambled then this is VERY inconvenient for the user.
0
I know how to write and read from a file but I don't have enough understanding on how exactly to parse this file and only take in the certain parts.
0
0
but instead it goes:
Is there a way to fix this at all? Also how do we add comments to be generated as. (our own custom comments)
0
Okay I got the file but the config generated seems very un-organized and has some weird variables on it:
Here is the code for it I'm using:
It's also placing them out of order which of course for the naming I'm doing for the variables it needs to be in numerical order. Is there a fix for this?