Files
src
build.gradle
settings.gradle
Code
11 542
src/main/java/com/seamlesstransfer/config/ConfigManager.java
package com.seamlesstransfer.config;
import com.seamlesstransfer.SeamlessTransfer;
import lombok.Getter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
@Getter
public class ConfigManager {
private final SeamlessTransfer plugin;
private final Map<String, Object> config;
private boolean enableChunkPreload;
private boolean enableFakeChunks;
private boolean blockEncryptionPacket;
private int preloadRadius;
private int maxCachedChunks;
private long cacheExpiryMs;
private String transitionMode;
public ConfigManager(SeamlessTransfer plugin) {
this.plugin = plugin;
this.config = new HashMap<>();
}
public void loadConfig() {
File configFile = new File(plugin.getDataDirectory().toFile(), "config.yml");
if (!configFile.exists()) {
createDefaultConfig(configFile);
}
try {
String content = new String(Files.readAllBytes(configFile.toPath()));
parseConfig(content);
} catch (IOException e) {
plugin.getLogger().error("Failed to load config", e);
loadDefaults();
}
}
private void createDefaultConfig(File file) {
file.getParentFile().mkdirs();
try (FileWriter writer = new FileWriter(file)) {
writer.write("enable-chunk-preload: true\n");
writer.write("enable-fake-chunks: true\n");
writer.write("block-encryption-packet: true\n");
writer.write("preload-radius: 8\n");
writer.write("max-cached-chunks: 1000\n");
writer.write("cache-expiry-ms: 30000\n");
writer.write("transition-mode: SEAMLESS\n");
} catch (IOException e) {
plugin.getLogger().error("Failed to create default config", e);
}
loadDefaults();
}
private void parseConfig(String content) {
for (String line : content.split("\n")) {
if (line.contains(":")) {
String[] parts = line.split(":", 2);
String key = parts[0].trim();
String value = parts[1].trim();
config.put(key, value);
}
}
enableChunkPreload = getBoolean("enable-chunk-preload", true);
enableFakeChunks = getBoolean("enable-fake-chunks", true);
blockEncryptionPacket = getBoolean("block-encryption-packet", true);
preloadRadius = getInt("preload-radius", 8);
maxCachedChunks = getInt("max-cached-chunks", 1000);
cacheExpiryMs = getLong("cache-expiry-ms", 30000L);
transitionMode = getString("transition-mode", "SEAMLESS");
}
private void loadDefaults() {
enableChunkPreload = true;
enableFakeChunks = true;
blockEncryptionPacket = true;
preloadRadius = 8;
maxCachedChunks = 1000;
cacheExpiryMs = 30000L;
transitionMode = "SEAMLESS";
}
private boolean getBoolean(String key, boolean defaultValue) {
Object value = config.get(key);
return value != null ? Boolean.parseBoolean(value.toString()) : defaultValue;
}
private int getInt(String key, int defaultValue) {
Object value = config.get(key);
try {
return value != null ? Integer.parseInt(value.toString()) : defaultValue;
} catch (NumberFormatException e) {
return defaultValue;
}
}
private long getLong(String key, long defaultValue) {
Object value = config.get(key);
try {
return value != null ? Long.parseLong(value.toString()) : defaultValue;
} catch (NumberFormatException e) {
return defaultValue;
}
}
private String getString(String key, String defaultValue) {
Object value = config.get(key);
return value != null ? value.toString() : defaultValue;
}
}