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; public class ConfigManager { private final PlayerBlockLife plugin; private FileConfiguration config; private File configFile; public ConfigManager(PlayerBlockLife plugin) { this.plugin = plugin; this.configFile = new File(plugin.getDataFolder(), "config.yml"); } /** * 加载配置 */ public void loadConfig() { // 确保配置文件夹存在 if (!plugin.getDataFolder().exists()) { plugin.getDataFolder().mkdirs(); } // 如果配置文件不存在,从JAR中复制默认配置 if (!configFile.exists()) { plugin.saveDefaultConfig(); plugin.logInfo("创建默认配置文件"); } // 重新加载配置 reloadConfig(); } /** * 重新加载配置 */ 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); } } /** * 更新配置文件 */ 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; } // 以下为配置项的获取方法 public int getBlocksPerPlayer() { return getConfig().getInt("blocks.amount", 5); } public int getSpreadRange() { return getConfig().getInt("blocks.spread", 5); } public int getDepth() { return getConfig().getInt("blocks.depth", -1); } public String getBlockMaterial() { return getConfig().getString("blocks.material", "player_head"); } public boolean isDieWhenBlocksGone() { return getConfig().getBoolean("game.die_when_blocks_gone", true); } public boolean isBecomeSpectator() { return getConfig().getBoolean("game.become_spectator", true); } public boolean isHealthSystemEnabled() { return getConfig().getBoolean("game.health_system", true); } public boolean isSkinSystemEnabled() { return getConfig().getBoolean("skin.enabled", true); } 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); } public String getMessage(String path, String defaultValue) { String message = getConfig().getString("messages." + path, defaultValue); if (message != null) { message = message.replace("&", "§"); } return message; } }