Files
PlayerBlockLife/src/main/java/com/playerblocklife/ConfigManager.java

212 lines
6.0 KiB
Java
Raw Normal View History

2026-02-13 18:50:05 +08:00
package com.playerblocklife;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
2026-02-13 18:50:05 +08:00
public class ConfigManager {
private final PlayerBlockLife plugin;
private FileConfiguration config;
private File configFile;
2026-02-13 18:50:05 +08:00
public ConfigManager(PlayerBlockLife plugin) {
this.plugin = plugin;
this.configFile = new File(plugin.getDataFolder(), "config.yml");
2026-02-13 18:50:05 +08:00
}
/**
* 加载配置
*/
2026-02-13 18:50:05 +08:00
public void loadConfig() {
// 确保配置文件夹存在
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdirs();
}
// 如果配置文件不存在从JAR中复制默认配置
if (!configFile.exists()) {
plugin.saveDefaultConfig();
plugin.logInfo("创建默认配置文件");
}
// 重新加载配置
reloadConfig();
2026-02-13 18:50:05 +08:00
}
/**
* 重新加载配置
*/
2026-02-13 18:50:05 +08:00
public void reloadConfig() {
// 重新从磁盘加载配置
config = YamlConfiguration.loadConfiguration(configFile);
// 加载默认配置作为后备
InputStream defaultConfigStream = plugin.getResource("config.yml");
if (defaultConfigStream != null) {
YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(
new InputStreamReader(defaultConfigStream, StandardCharsets.UTF_8));
config.setDefaults(defaultConfig);
}
// 检查配置版本,如果需要则更新
checkConfigVersion();
plugin.logInfo("配置已加载");
}
/**
* 检查配置版本并更新
*/
private void checkConfigVersion() {
int currentVersion = config.getInt("config-version", 1);
int latestVersion = 1; // 最新配置版本
if (currentVersion < latestVersion) {
plugin.logWarning("检测到旧版配置文件,正在更新...");
updateConfig(currentVersion, latestVersion);
}
2026-02-13 18:50:05 +08:00
}
/**
* 更新配置文件
*/
private void updateConfig(int fromVersion, int toVersion) {
if (fromVersion == 1 && toVersion == 2) {
// 示例:添加新配置项
if (!config.contains("new-feature.enabled")) {
config.set("new-feature.enabled", true);
config.set("new-feature.duration", 60);
}
// 更新版本号
config.set("config-version", toVersion);
try {
config.save(configFile);
plugin.logInfo("配置文件已更新到版本 " + toVersion);
} catch (IOException e) {
plugin.logError("保存更新后的配置文件失败", e);
}
}
}
/**
* 保存配置
*/
public void saveConfig() {
try {
config.save(configFile);
} catch (IOException e) {
plugin.logError("保存配置文件失败", e);
}
}
/**
* 获取配置对象
*/
public FileConfiguration getConfig() {
if (config == null) {
reloadConfig();
}
return config;
}
// 以下为配置项的获取方法
2026-02-13 18:50:05 +08:00
public int getBlocksPerPlayer() {
return getConfig().getInt("blocks.amount", 5);
2026-02-13 18:50:05 +08:00
}
public int getSpreadRange() {
return getConfig().getInt("blocks.spread", 5);
2026-02-13 18:50:05 +08:00
}
public int getDepth() {
return getConfig().getInt("blocks.depth", -1);
}
public String getBlockMaterial() {
return getConfig().getString("blocks.material", "player_head");
2026-02-13 18:50:05 +08:00
}
public boolean isDieWhenBlocksGone() {
return getConfig().getBoolean("game.die_when_blocks_gone", true);
2026-02-13 18:50:05 +08:00
}
public boolean isBecomeSpectator() {
return getConfig().getBoolean("game.become_spectator", true);
2026-02-13 18:50:05 +08:00
}
public boolean isHealthSystemEnabled() {
return getConfig().getBoolean("game.health_system", true);
2026-02-13 18:50:05 +08:00
}
public boolean isSkinSystemEnabled() {
return getConfig().getBoolean("skin.enabled", true);
2026-02-13 18:50:05 +08:00
}
public String getSkinSource() {
return getConfig().getString("skin.source", "player_profile");
}
public int getCacheExpireDays() {
return getConfig().getInt("skin.cache.expire_days", 7);
}
public boolean isAutoSaveEnabled() {
return getConfig().getBoolean("storage.auto_save.enabled", true);
}
public int getAutoSaveInterval() {
return getConfig().getInt("storage.auto_save.interval", 300);
}
public String getStorageType() {
return getConfig().getString("storage.type", "yaml");
}
public boolean isBroadcastOnBlockBreak() {
return getConfig().getBoolean("game.broadcast.on_block_break", true);
}
public boolean isBroadcastOnPlayerDeath() {
return getConfig().getBoolean("game.broadcast.on_player_death", true);
}
public int getBroadcastRange() {
return getConfig().getInt("game.broadcast.range", 30);
}
public boolean isGiveExpReward() {
return getConfig().getBoolean("game.break_rewards.give_exp", true);
}
public int getExpRewardAmount() {
return getConfig().getInt("game.break_rewards.exp_amount", 5);
}
public boolean isProtectFromExplosions() {
return getConfig().getBoolean("protection.protect_from_explosions", true);
}
public boolean isProtectFromFire() {
return getConfig().getBoolean("protection.protect_from_fire", true);
}
public boolean isProtectFromPistons() {
return getConfig().getBoolean("protection.protect_from_pistons", true);
2026-02-13 18:50:05 +08:00
}
public String getMessage(String path, String defaultValue) {
String message = getConfig().getString("messages." + path, defaultValue);
2026-02-13 18:50:05 +08:00
if (message != null) {
message = message.replace("&", "§");
}
return message;
}
}