Saves configurations with comments.

This commit is contained in:
h31ix committed 2012-09-29 00:07:05 -04:00
1 parent 6647161a73
commit 4e02646681
6 files changed
+189 -61

No files matched your search

@@ -25,10 +25,9 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.h31ix.anticheat.manage.AnticheatManager;
import net.h31ix.anticheat.util.yaml.CommentedConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
public class Configuration
{
@@ -37,10 +36,10 @@ public class Configuration
private File levelFile = null;
private File langFile = null;
private File magicFile = null;
private FileConfiguration config;
private FileConfiguration level;
private FileConfiguration lang;
private FileConfiguration magic;
private CommentedConfiguration config;
private CommentedConfiguration level;
private CommentedConfiguration lang;
private CommentedConfiguration magic;
private boolean logConsole;
private boolean logXRay;
private boolean alertXRay;
@@ -151,11 +150,11 @@ public class Configuration
public final void load()
{
micromanage.getPlugin().checkConfig();
config = YamlConfiguration.loadConfiguration(configFile);
level = YamlConfiguration.loadConfiguration(levelFile);
lang = YamlConfiguration.loadConfiguration(langFile);
magic = YamlConfiguration.loadConfiguration(magicFile);
micromanage.getPlugin().checkConfigs();
config = CommentedConfiguration.loadConfig(configFile);
level = CommentedConfiguration.loadConfig(levelFile);
lang = CommentedConfiguration.loadConfig(langFile);
magic = CommentedConfiguration.loadConfig(magicFile);
language = new Language(lang);
updateFolder = Bukkit.getUpdateFolder();
@@ -248,7 +247,7 @@ public class Configuration
return levelFile;
}
public FileConfiguration getMagic()
public CommentedConfiguration getMagic()
{
return magic;
}
@@ -0,0 +1,117 @@
/*
* AntiCheat for Bukkit.
* Copyright (C) 2012 AntiCheat Team | http://gravitydevelopment.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.h31ix.anticheat.util.yaml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
public class CommentedConfiguration extends YamlConfiguration
{
private Map<Integer, String> comments = new HashMap<Integer, String>();
private final DumperOptions yamlOptions = new DumperOptions();
private final Representer yamlRepresenter = new YamlRepresenter();
private final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
@Override
public String saveToString() {
yamlOptions.setIndent(options().indent());
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
String dump = yaml.dump(getValues(false));
if (dump.equals(BLANK_CONFIG)) {
dump = "";
} else {
StringBuilder builder = new StringBuilder();
int i = 0;
for(String string : dump.split("\n")) {
i++;
while(comments.containsKey(i)) {
builder.append(comments.get(i));
builder.append("\n");
i++;
}
builder.append(string);
builder.append("\n");
}
dump = builder.toString();
}
return dump;
}
@Override
public void load(InputStream stream) throws IOException, InvalidConfigurationException {
InputStreamReader reader = new InputStreamReader(stream);
StringBuilder builder = new StringBuilder();
BufferedReader input = new BufferedReader(reader);
try {
String line;
int i = 0;
while ((line = input.readLine()) != null) {
i++;
if(line.contains(COMMENT_PREFIX)) {
comments.put(i, line);
}
builder.append(line);
builder.append('\n');
}
} finally {
input.close();
}
loadFromString(builder.toString());
}
public static CommentedConfiguration loadConfig(File file) {
CommentedConfiguration config = new CommentedConfiguration();
try {
config.load(file);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
} catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file , ex);
}
return config;
}
}