Descriptions are now stored to a file descriptions.txt every time the

GUI or the plugin get started.
The config file gets overwritten every time the GUI or the plugin get
started, to automatically remove no longer valid options and/or add
newly available options to it.
This commit is contained in:
Evenprime 2011-06-18 17:40:27 +02:00
parent a37daefa63
commit fda8ae4c06
6 changed files with 69 additions and 14 deletions

View File

@ -323,9 +323,9 @@ public class NoCheat extends JavaPlugin implements CommandSender {
*/
private void setupConfig() {
if(this.config == null)
this.config = new NoCheatConfiguration(new File(NoCheatConfiguration.configFile));
this.config = new NoCheatConfiguration(new File(NoCheatConfiguration.configFile), new File(NoCheatConfiguration.descriptionsFile));
else
this.config.config(new File(NoCheatConfiguration.configFile));
this.config.config(new File(NoCheatConfiguration.configFile), new File(NoCheatConfiguration.descriptionsFile));
config.setupFileLogger();

View File

@ -1,5 +1,6 @@
package cc.co.evenprime.bukkit.nocheat.config;
public abstract class ChildOption extends Option {
/**
@ -19,4 +20,9 @@ public abstract class ChildOption extends Option {
public String toYAMLString(String prefix) {
return prefix + getIdentifier() + ": \"" + getValue() + "\"\r\n";
}
@Override
public String toDescriptionString(String prefix) {
return prefix + getIdentifier() + ": \"" + getDescription().replace("\n", "\r\n" + prefix + "\t\t") + "\"\r\n";
}
}

View File

@ -31,6 +31,7 @@ public class NoCheatConfiguration {
public final static String configFile = "plugins/NoCheat/nocheat.yml";
public final static String descriptionsFile = "plugins/NoCheat/descriptions.txt";
private ParentOption root;
@ -45,17 +46,17 @@ public class NoCheatConfiguration {
// Our log output to a file
private FileHandler fh = null;
public NoCheatConfiguration(File configurationFile) {
public NoCheatConfiguration(File configurationFile, File descriptionsFile) {
// Setup the configuration tree
config(configurationFile);
config(configurationFile, descriptionsFile);
}
/**
* Read the configuration file and assign either standard values or whatever is declared in the file
* @param configurationFile
*/
public void config(File configurationFile) {
public void config(File configurationFile, File descriptionsFile) {
try {
@ -264,9 +265,8 @@ public class NoCheatConfiguration {
}
}
if(!configurationFile.exists()) {
writeConfigFile(configurationFile, this);
}
writeDescriptionFile(descriptionsFile, this);
}
public void setupFileLogger() {
@ -346,6 +346,26 @@ public class NoCheatConfiguration {
}
}
/**
* Write a file with the descriptions of all options
* @param f
*/
public static void writeDescriptionFile(File f, NoCheatConfiguration configuration) {
try {
if(f.getParentFile() != null)
f.getParentFile().mkdirs();
f.createNewFile();
BufferedWriter w = new BufferedWriter(new FileWriter(f));
w.write(configuration.getRoot().toDescriptionString(""));
w.flush(); w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public Action[] getActionValue(String optionName) throws ConfigurationException {
return stringToActions(getStringOption(optionName).getValue());
}

View File

@ -1,25 +1,32 @@
package cc.co.evenprime.bukkit.nocheat.config;
import cc.co.evenprime.bukkit.nocheat.wizard.gui.Explainations;
public abstract class Option {
private final String identifier;
private final String parentIdentifier;
private final String description;
public Option(String identifier, String parentIdentifier) {
this.identifier = identifier;
this.parentIdentifier = parentIdentifier;
this.description = Explainations.get(parentIdentifier == null || parentIdentifier.equals("") ? identifier : parentIdentifier + "." + identifier);
}
public String getIdentifier() {
public final String getIdentifier() {
return identifier;
}
public String getFullIdentifier() {
public final String getDescription() {
return description;
}
public final String getFullIdentifier() {
return (parentIdentifier == null || parentIdentifier == "") ? identifier : parentIdentifier + "." + identifier;
}
public String toYAMLString(String prefix) {
return prefix + "\r\n";
}
public abstract String toYAMLString(String prefix);
public abstract String toDescriptionString(String prefix);
}

View File

@ -59,4 +59,26 @@ public class ParentOption extends Option {
return s;
}
@Override
public String toDescriptionString(String prefix) {
String s = "";
if(getIdentifier().length() > 0) {
s += prefix + getIdentifier() + ":\r\n";
for(Option o : getChildOptions()) {
s += o.toDescriptionString(prefix + " ");
}
}
else
{
for(Option o : getChildOptions()) {
s += o.toDescriptionString(prefix);
}
}
return s;
}
}

View File

@ -35,7 +35,7 @@ public class Wizard extends JFrame {
inside.setLayout(new BoxLayout(inside,BoxLayout.Y_AXIS));
final NoCheatConfiguration config = new NoCheatConfiguration(new File("NoCheat/nocheat.yml"));
final NoCheatConfiguration config = new NoCheatConfiguration(new File("NoCheat/nocheat.yml"), new File("NoCheat/descriptions.txt"));
ParentOptionGui root2 = new ParentOptionGui(config.getRoot());