Page 1 of 1
YML config using Key List
Posted: Mon Oct 10, 2011 10:31 pm
by Rayfe
I was following along with the Configuration File tutorial, but in the first video I'm a bit lost as to how I expand on the tutorial to accomodate my needs. Any help would be appreciated.
For my configuration file I would like it to look similar to this...
keywords:
topic-name:
words:
- word
- word
- word
- word
- word
reply:
how: Your auto-reply goes here.
what: Your auto-reply goes here.
where: Your auto-reply goes here.
when: Your auto-reply goes here.
why: Your auto-reply goes here.
Being that the tutorial is very basic and I'm new to Java, YML and the Bukkit API... I'm not really sure how this YML data would be set in config defaults, with the structure tiering and all. The tutorial says that periods distinguish a sub level, but i'm using a Key List and not sure how to translate that either.
Any help would be great. Let me know if I need to supply more information on my intents for this plugin. Thanks.
Re: YML config using Key List
Posted: Tue Oct 11, 2011 6:04 pm
by jacek
To use a list like that it is a little bit different, I usually use an ArrayList.
Assuming you used the same code as I did you would create this as your default HashMap
HashMap<String, Object> configDefaults = new HashMap<String, Object>();
ArrayList<String> defaultWords = new ArrayList<String>();
defaultWords.add('word');
defaultWords.add('word');
defaultWords.add('word');
defaultWords.add('word');
defaultWords.add('word');
configDefaults.put('keywords.topic-name.words', defaultWords);
configDefaults.put('keywords.topic-name.reply.how', 'Your auto-reply goes here.');
configDefaults.put('keywords.topic-name.reply.what', 'Your auto-reply goes here.');
configDefaults.put('keywords.topic-name.reply.where', 'Your auto-reply goes here.');
configDefaults.put('keywords.topic-name.reply.when', 'Your auto-reply goes here.');
configDefaults.put('keywords.topic-name.reply.why', 'Your auto-reply goes here.');
Re: YML config using Key List
Posted: Tue Oct 11, 2011 9:21 pm
by Rayfe
Thanks for your feedback. I have two more questions.
1) I was able to figure out what I needed with the code below. Does it yield a different end result compared to the code you suggested?
private HashMap<String, Object> configDefaults = new HashMap<String, Object>();
private String[] defaultWordList = {"word1","word2","word3"};
private String defaultReply = "Your auto-response goes here.";
public AssistantConfig(File configFile){
this.config = new Configuration(configFile);
this.configDefaults.put("keywords.topic-name.words", defaultWordList);
this.configDefaults.put("keywords.topic-name.reply.who", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.what", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.where", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.when", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.why", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.how", defaultReply);
2) When my version creates the config file, the defaultWordList is left justified in the same position as its parent "words:".. are those suppose to be indented? And if so, how can I specify that? I've tried a few things, but to no avail.
keywords:
topic-name:
words:
- "word1"
- "word2"
- "word3"
reply:
what: Your auto-response goes here.
how: Your auto-response goes here.
when: Your auto-response goes here.
where: Your auto-response goes here.
why: Your auto-response goes here.
who: Your auto-response goes here.
Also, I will test your suggested code and see if it makes a difference. Thanks for your help.
Re: YML config using Key List
Posted: Tue Oct 11, 2011 9:49 pm
by Rayfe
I actually tried to do it the way you suggested first, but now I remember why I was looking for another way (thinking I was doing it incorrectly).
When I use your suggested code I get this error:
package defaultWordList does not exist
<identifier> expected
illegal start of type
Re: YML config using Key List
Posted: Wed Oct 12, 2011 12:30 pm
by jacek
What you had looked fine, the words lines should not be indented.
Re: YML config using Key List
Posted: Wed Oct 12, 2011 7:05 pm
by Rayfe
Ok. If you could, I would like to understand how to fix that error though incase I need to use an ArrayList and add items.
On another note, rethinking my plugin structure I was able to do this:
keywords:
topic-name:
words:
negative:
- - word1
- word2
- - word1
- word2
positive:
- - word1
- word2
- - word1
- word2
reply:
what: Your auto-response goes here.
how: Your auto-response goes here.
when: Your auto-response goes here.
where: Your auto-response goes here.
why: Your auto-response goes here.
who: Your auto-response goes here.
using this:
private HashMap<String, Object> configDefaults = new HashMap<String, Object>();
private String[] defaultPositiveWordsList1 = {"word1","word2"};
private String[] defaultPositiveWordsList2 = {"word1","word2"};
private String[] defaultNegativeWordsList1 = {"word1","word2"};
private String[] defaultNegativeWordsList2 = {"word1","word2"};
private Object[] defaultPositiveWordList = {defaultPositiveWordsList1,defaultPositiveWordsList2};
private Object[] defaultNegativeWordList = {defaultNegativeWordsList1,defaultNegativeWordsList2};
private String defaultReply = "Your auto-response goes here.";
public AssistantConfig(File configFile){
this.config = new Configuration(configFile);
this.configDefaults.put("keywords.topic-name.words.positive", defaultPositiveWordList);
this.configDefaults.put("keywords.topic-name.words.negative", defaultNegativeWordList);
this.configDefaults.put("keywords.topic-name.reply.who", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.what", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.where", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.when", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.why", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.how", defaultReply);
But is there a way to make my config file look like this instead?
keywords:
topic-name:
words:
negative:
- [word1,word2]
- [word1,word2]
positive:
- [word1,word2]
- [word1,word2]
Again, thanks for your help.
Re: YML config using Key List
Posted: Wed Oct 12, 2011 7:33 pm
by jacek
For the error, could you post the full code you used ?
I'm not really sure how you are meant to specify that you wan to use the square bracket list. I assume that there would be a way, perhaps ask on the Bukkit forums ?
Re: YML config using Key List
Posted: Wed Oct 12, 2011 8:36 pm
by Rayfe
Here's the full code from that file.
package me.kevinhamil.plugins.assistant;
import java.io.File;
import java.util.HashMap;
import org.bukkit.util.config.Configuration;
public class AssistantConfig {
public static Assistant plugin;
private Configuration config;
private HashMap<String, Object> configDefaults = new HashMap<String, Object>();
private String[] defaultPositiveWordsList1 = {"word1","word2"};
private String[] defaultPositiveWordsList2 = {"word1","word2"};
private String[] defaultNegativeWordsList1 = {"word1","word2"};
private String[] defaultNegativeWordsList2 = {"word1","word2"};
private Object[] defaultPositiveWordList = {defaultPositiveWordsList1,defaultPositiveWordsList2};
private Object[] defaultNegativeWordList = {defaultNegativeWordsList1,defaultNegativeWordsList2};
private String defaultReply = "Your auto-response goes here.";
public AssistantConfig(File configFile){
this.config = new Configuration(configFile);
this.configDefaults.put("keywords.topic-name.words.positive", defaultPositiveWordList);
this.configDefaults.put("keywords.topic-name.words.negative", defaultNegativeWordList);
this.configDefaults.put("keywords.topic-name.reply.who", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.what", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.where", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.when", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.why", defaultReply);
this.configDefaults.put("keywords.topic-name.reply.how", defaultReply);
if (configFile.exists() == false) {
for (String key : this.configDefaults.keySet()){
this.config.setProperty(key, this.configDefaults.get(key));
}
this.config.save();
}else{
this.config.load();
}
}
}
As for the square brackets, I just didn't know if there was more less was a better way to display it.. it looked odd to me having it output with two dashes.. and also would like if each string list as an item was more visually separated from the others, rather than all being stacked in a vertical fashion. Square brackets didn't really have to do with anything.. perhaps if I could just avoid the line breaks in each parent item.
More like...
words:
positive:
- word1, word2
- word1, word2
Re: YML config using Key List
Posted: Wed Oct 12, 2011 9:23 pm
by jacek
Rayfe wrote:More like...
words:
positive:
- word1, word2
- word1, word2
That is not valid YML syntax though. YML files are very picky about the syntax, for a list you either have to use the dashed style list, or a command separated list in square brackets.
It does look a bit weird with the double - but unless you just write the file manually there is not much you can do, that I know of anyway.