From fda8ae4c06f9582fda35113c7ceb9cf0684ba9be Mon Sep 17 00:00:00 2001 From: Evenprime Date: Sat, 18 Jun 2011 17:40:27 +0200 Subject: [PATCH] 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. --- .../co/evenprime/bukkit/nocheat/NoCheat.java | 4 +-- .../bukkit/nocheat/config/ChildOption.java | 6 ++++ .../nocheat/config/NoCheatConfiguration.java | 32 +++++++++++++++---- .../bukkit/nocheat/config/Option.java | 17 +++++++--- .../bukkit/nocheat/config/ParentOption.java | 22 +++++++++++++ .../bukkit/nocheat/wizard/Wizard.java | 2 +- 6 files changed, 69 insertions(+), 14 deletions(-) diff --git a/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java b/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java index 0947ef03..97ec1f6a 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java +++ b/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java @@ -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(); diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/ChildOption.java b/src/cc/co/evenprime/bukkit/nocheat/config/ChildOption.java index aa876a17..3c4b6db2 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/ChildOption.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/ChildOption.java @@ -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"; + } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java b/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java index 8ce2ce26..aa11d3b1 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java @@ -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); - } + writeConfigFile(configurationFile, this); + writeDescriptionFile(descriptionsFile, this); } public void setupFileLogger() { @@ -345,6 +345,26 @@ public class NoCheatConfiguration { e.printStackTrace(); } } + + /** + * 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()); diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/Option.java b/src/cc/co/evenprime/bukkit/nocheat/config/Option.java index e02d0dca..0f7cd374 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/Option.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/Option.java @@ -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); } diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/ParentOption.java b/src/cc/co/evenprime/bukkit/nocheat/config/ParentOption.java index af9ff529..abbdaa44 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/ParentOption.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/ParentOption.java @@ -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; + + } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/Wizard.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/Wizard.java index 2b78b0fe..ed9e36f8 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/wizard/Wizard.java +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/Wizard.java @@ -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());