mirror of
https://github.com/darksaid98/AntiCheatReloaded.git
synced 2026-12-01 14:38:56 +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
@@ -19,7 +19,10 @@
|
||||
package net.h31ix.anticheat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
@@ -85,6 +88,34 @@ public class Anticheat extends JavaPlugin
|
||||
cleanup();
|
||||
}
|
||||
|
||||
private void save(InputStream in, File file)
|
||||
{
|
||||
if (!file.exists())
|
||||
{
|
||||
file.getParentFile().mkdirs();
|
||||
try
|
||||
{
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while((len=in.read(buf))>0)
|
||||
{
|
||||
out.write(buf,0,len);
|
||||
}
|
||||
out.close();
|
||||
in.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (verbose)
|
||||
{
|
||||
getLogger().log(Level.INFO, file.getName()+" created.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setupXray()
|
||||
{
|
||||
final XRayTracker xtracker = manager.getXRayTracker();
|
||||
@@ -208,7 +239,7 @@ public class Anticheat extends JavaPlugin
|
||||
private void setupConfig()
|
||||
{
|
||||
config = manager.getConfiguration();
|
||||
checkConfig();
|
||||
checkConfigs();
|
||||
verbose = config.verboseStartup();
|
||||
if (verbose)
|
||||
{
|
||||
@@ -229,32 +260,11 @@ public class Anticheat extends JavaPlugin
|
||||
}
|
||||
}
|
||||
|
||||
public void checkConfig()
|
||||
public void checkConfigs()
|
||||
{
|
||||
if (!new File(getDataFolder() + "/config.yml").exists())
|
||||
{
|
||||
saveDefaultConfig();
|
||||
if (verbose)
|
||||
{
|
||||
getLogger().log(Level.INFO, "Config file created.");
|
||||
}
|
||||
}
|
||||
if (!new File(getDataFolder() + "/lang.yml").exists())
|
||||
{
|
||||
saveResource("lang.yml", false);
|
||||
if (verbose)
|
||||
{
|
||||
getLogger().log(Level.INFO, "Lang file created.");
|
||||
}
|
||||
}
|
||||
if (!new File(getDataFolder() + "/magic.yml").exists())
|
||||
{
|
||||
saveResource("magic.yml", false);
|
||||
if (verbose)
|
||||
{
|
||||
getLogger().log(Level.INFO, "Magic file created.");
|
||||
}
|
||||
}
|
||||
save(getResource("config.yml"), new File(getDataFolder() + "/config.yml"));
|
||||
save(getResource("lang.yml"), new File(getDataFolder() + "/lang.yml"));
|
||||
save(getResource("magic.yml"), new File(getDataFolder() + "/magic.yml"));
|
||||
}
|
||||
|
||||
public static Anticheat getPlugin()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user