Updated Source Files for Version 0.3-Pre
This commit is contained in:
@ -1,16 +1,16 @@
|
||||
package com.adzel.velocitybroadcast;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
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.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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;
|
||||
@ -20,14 +20,34 @@ public class ConfigHandler {
|
||||
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());
|
||||
@ -39,38 +59,34 @@ public class ConfigHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> lines = Files.readAllLines(configPath);
|
||||
String fileVersion = null;
|
||||
|
||||
for (String line : lines) {
|
||||
if (line.trim().startsWith("Plugin Version:")) {
|
||||
fileVersion = line.replaceAll(".*'(.*?)'.*", "$1").trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = Files.newBufferedReader(configPath)) {
|
||||
Map<String, String> configMap = reader.lines()
|
||||
.filter(line -> line.contains(":") && !line.trim().startsWith("#"))
|
||||
.map(line -> line.replaceAll("#.*", "").split(":", 2))
|
||||
.collect(Collectors.toMap(
|
||||
a -> a[0].trim(),
|
||||
a -> a[1].trim().replaceAll("^['\"]|['\"]$", ""),
|
||||
(a, b) -> b,
|
||||
LinkedHashMap::new
|
||||
));
|
||||
Map<String, Object> root = yaml.load(Files.newBufferedReader(configPath));
|
||||
if (root == null) root = new LinkedHashMap<>();
|
||||
|
||||
debugEnabled = Boolean.parseBoolean(configMap.getOrDefault("debug-messages-enabled", "false"));
|
||||
versionCheckEnabled = Boolean.parseBoolean(configMap.getOrDefault("version-check-enabled", "true"));
|
||||
prefix = configMap.getOrDefault("prefix", "&9&l[&3&lServer&9&l]&r ");
|
||||
}
|
||||
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(); // Update config with new version and preserve user values
|
||||
save();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
@ -82,26 +98,28 @@ public class ConfigHandler {
|
||||
try {
|
||||
Files.createDirectories(configPath.getParent());
|
||||
|
||||
String editableSection = "";
|
||||
if (Files.exists(configPath)) {
|
||||
editableSection = Files.readAllLines(configPath).stream()
|
||||
.dropWhile(line -> !line.trim().equalsIgnoreCase("# ONLY EDIT BELOW THIS LINE"))
|
||||
.skip(1)
|
||||
.collect(Collectors.joining("\n"));
|
||||
}
|
||||
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");
|
||||
writer.write("# ONLY EDIT BELOW THIS LINE\n");
|
||||
|
||||
if (!editableSection.isEmpty()) {
|
||||
writer.write(editableSection + "\n");
|
||||
} else {
|
||||
writer.write("debug-messages-enabled: false # Enables/disables debug messages (Default: false)\n");
|
||||
writer.write("version-check-enabled: true # Toggles version update messages for admins (Default: true)\n");
|
||||
writer.write("prefix: '&9&l[&3&lServer&9&l]&r ' # The prefix for broadcasts and messages\n");
|
||||
}
|
||||
yaml.dump(root, writer);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to save VelocityBroadcast config!", e);
|
||||
}
|
||||
@ -127,4 +145,28 @@ public class ConfigHandler {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user