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("auto-generation.enabled")) { config.set("auto-generation.enabled", true); config.set("auto-generation.require_open_sky", true); config.set("auto-generation.max_attempts", 50); config.set("auto-generation.on_failure", "notify"); } // 添加命令启用配置 if (!config.contains("commands.setlifeblocks.enabled")) { config.set("commands.setlifeblocks.enabled", true); config.set("commands.setlifeblocks.allow_self_use", true); config.set("commands.setlifeblocks.allow_admin_use", true); config.set("commands.checklifeblocks.enabled", true); config.set("commands.checklifeblocks.allow_self_use", true); config.set("commands.checklifeblocks.allow_admin_use", true); config.set("commands.pblreload.enabled", true); config.set("commands.pblreload.admin_only", true); config.set("commands.pbldelete.enabled", true); config.set("commands.pbldelete.admin_only", true); config.set("commands.pblrevive.enabled", true); config.set("commands.pblrevive.admin_only", true); config.set("commands.pblstats.enabled", true); config.set("commands.pblstats.admin_only", true); } // 更新消息配置 if (!config.contains("messages.use_external_file")) { config.set("messages.use_external_file", true); config.set("messages.external_file", "messages.yml"); } // 更新版本号 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 boolean isAutoGenerationEnabled() { return getConfig().getBoolean("auto-generation.enabled", true); } public boolean isRequireOpenSky() { return getConfig().getBoolean("auto-generation.require_open_sky", true); } public int getMaxAttempts() { return getConfig().getInt("auto-generation.max_attempts", 50); } public String getOnFailureAction() { return getConfig().getString("auto-generation.on_failure", "notify"); } // 命令启用配置获取方法 public boolean isCommandEnabled(String commandName) { return getConfig().getBoolean("commands." + commandName + ".enabled", true); } public boolean isSelfUseAllowed(String commandName) { return getConfig().getBoolean("commands." + commandName + ".allow_self_use", true); } public boolean isAdminUseAllowed(String commandName) { return getConfig().getBoolean("commands." + commandName + ".allow_admin_use", true); } public boolean isAdminOnly(String commandName) { return getConfig().getBoolean("commands." + commandName + ".admin_only", false); } // 消息文件配置获取方法 public boolean useExternalMessageFile() { return getConfig().getBoolean("messages.use_external_file", true); } public String getExternalMessageFileName() { return getConfig().getString("messages.external_file", "messages.yml"); } public String getMessage(String path, String defaultValue) { // 优先从外部消息文件获取 if (useExternalMessageFile()) { // 这里应该调用MessageManager来获取消息 // 暂时返回默认值,MessageManager会处理具体逻辑 return defaultValue; } // 从config.yml获取 String message = getConfig().getString("messages." + path, defaultValue); if (message != null) { message = message.replace("&", "§"); } return message; } }