mirror of
https://github.com/darksaid98/AntiCheatReloaded.git
synced 2026-12-03 14:38:46 +00:00
Saves configurations with comments.
This commit is contained in:
1 parent
6647161a73
commit
4e02646681
6 files changed
+189
-61
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user