173 lines
5.5 KiB
Java
173 lines
5.5 KiB
Java
package com.adzel.velocitybroadcast;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.yaml.snakeyaml.DumperOptions;
|
|
import org.yaml.snakeyaml.Yaml;
|
|
import org.yaml.snakeyaml.representer.Representer;
|
|
|
|
public class ConfigHandler {
|
|
private final Path configPath;
|
|
private final Logger logger;
|
|
|
|
private boolean debugEnabled = false;
|
|
private boolean versionCheckEnabled = true;
|
|
private String prefix = "&9&l[&3&lServer&9&l]&r ";
|
|
|
|
private String dbType = "sqlite";
|
|
private String dbHost = "localhost";
|
|
private int dbPort = 3306;
|
|
private String dbName = "velocitybroadcast";
|
|
private String dbUser = "vb_user";
|
|
private String dbPassword = "securepassword";
|
|
|
|
private static final String CURRENT_VERSION = VelocityBroadcast.PLUGIN_VERSION;
|
|
private static final String VERSION_LINE = "# DO NOT EDIT\nPlugin Version: '" + CURRENT_VERSION + "' # Do not edit this value, as it will mess up version checking and break the plugin";
|
|
|
|
private final Yaml yaml;
|
|
|
|
public ConfigHandler(Path configPath, Logger logger) {
|
|
this.configPath = configPath;
|
|
this.logger = logger;
|
|
|
|
DumperOptions options = new DumperOptions();
|
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
options.setIndent(2);
|
|
options.setPrettyFlow(true);
|
|
|
|
Representer representer = new Representer(options);
|
|
representer.getPropertyUtils().setSkipMissingProperties(true);
|
|
|
|
yaml = new Yaml(representer, options);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public void load() {
|
|
try {
|
|
Files.createDirectories(configPath.getParent());
|
|
|
|
boolean shouldSave = false;
|
|
|
|
if (!Files.exists(configPath)) {
|
|
save();
|
|
return;
|
|
}
|
|
|
|
String fileVersion = Files.readAllLines(configPath).stream()
|
|
.filter(line -> line.startsWith("Plugin Version:"))
|
|
.map(line -> line.replaceAll(".*'(.*?)'.*", "$1").trim())
|
|
.findFirst()
|
|
.orElse(null);
|
|
|
|
if (fileVersion == null || !fileVersion.equals(CURRENT_VERSION)) {
|
|
shouldSave = true;
|
|
}
|
|
|
|
Map<String, Object> root = yaml.load(Files.newBufferedReader(configPath));
|
|
if (root == null) root = new LinkedHashMap<>();
|
|
|
|
Map<String, Object> general = (Map<String, Object>) root.getOrDefault("general", new LinkedHashMap<>());
|
|
debugEnabled = Boolean.parseBoolean(String.valueOf(general.getOrDefault("debug-messages-enabled", "false")));
|
|
versionCheckEnabled = Boolean.parseBoolean(String.valueOf(general.getOrDefault("version-check-enabled", "true")));
|
|
prefix = String.valueOf(general.getOrDefault("prefix", "&9&l[&3&lServer&9&l]&r "));
|
|
|
|
Map<String, Object> database = (Map<String, Object>) root.getOrDefault("database", new LinkedHashMap<>());
|
|
dbType = String.valueOf(database.getOrDefault("type", "sqlite"));
|
|
dbHost = String.valueOf(database.getOrDefault("host", "localhost"));
|
|
dbPort = Integer.parseInt(String.valueOf(database.getOrDefault("port", "3306")));
|
|
dbName = String.valueOf(database.getOrDefault("name", "velocitybroadcast"));
|
|
dbUser = String.valueOf(database.getOrDefault("user", "vb_user"));
|
|
dbPassword = String.valueOf(database.getOrDefault("password", "securepassword"));
|
|
|
|
if (shouldSave) {
|
|
save();
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
logger.error("Failed to load VelocityBroadcast config!", e);
|
|
}
|
|
}
|
|
|
|
public void save() {
|
|
try {
|
|
Files.createDirectories(configPath.getParent());
|
|
|
|
Map<String, Object> general = new LinkedHashMap<>();
|
|
general.put("debug-messages-enabled", debugEnabled);
|
|
general.put("version-check-enabled", versionCheckEnabled);
|
|
general.put("prefix", prefix);
|
|
|
|
Map<String, Object> database = new LinkedHashMap<>();
|
|
database.put("type", dbType);
|
|
database.put("host", dbHost);
|
|
database.put("port", dbPort);
|
|
database.put("name", dbName);
|
|
database.put("user", dbUser);
|
|
database.put("password", dbPassword);
|
|
|
|
Map<String, Object> root = new LinkedHashMap<>();
|
|
root.put("general", general);
|
|
root.put("database", database);
|
|
|
|
try (BufferedWriter writer = Files.newBufferedWriter(configPath)) {
|
|
writer.write(VERSION_LINE + "\n\n");
|
|
yaml.dump(root, writer);
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
logger.error("Failed to save VelocityBroadcast config!", e);
|
|
}
|
|
}
|
|
|
|
public void reload() {
|
|
load();
|
|
}
|
|
|
|
public boolean isDebugEnabled() {
|
|
return debugEnabled;
|
|
}
|
|
|
|
public boolean isVersionCheckEnabled() {
|
|
return versionCheckEnabled;
|
|
}
|
|
|
|
public String getPrefix() {
|
|
return prefix;
|
|
}
|
|
|
|
public void setPrefix(String newPrefix) {
|
|
this.prefix = newPrefix;
|
|
save();
|
|
}
|
|
|
|
public String getDbType() {
|
|
return dbType;
|
|
}
|
|
|
|
public String getDbHost() {
|
|
return dbHost;
|
|
}
|
|
|
|
public int getDbPort() {
|
|
return dbPort;
|
|
}
|
|
|
|
public String getDbName() {
|
|
return dbName;
|
|
}
|
|
|
|
public String getDbUser() {
|
|
return dbUser;
|
|
}
|
|
|
|
public String getDbPassword() {
|
|
return dbPassword;
|
|
}
|
|
}
|