2026-02-13 18:50:05 +08:00
|
|
|
|
package com.playerblocklife;
|
|
|
|
|
|
|
|
|
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
2026-02-13 20:56:03 +08:00
|
|
|
|
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;
|
2026-02-13 20:56:03 +08:00
|
|
|
|
private File configFile;
|
2026-02-13 18:50:05 +08:00
|
|
|
|
|
|
|
|
|
|
public ConfigManager(PlayerBlockLife plugin) {
|
|
|
|
|
|
this.plugin = plugin;
|
2026-02-13 20:56:03 +08:00
|
|
|
|
this.configFile = new File(plugin.getDataFolder(), "config.yml");
|
2026-02-13 18:50:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 20:56:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 加载配置
|
|
|
|
|
|
*/
|
2026-02-13 18:50:05 +08:00
|
|
|
|
public void loadConfig() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
// 确保配置文件夹存在
|
|
|
|
|
|
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 20:56:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 重新加载配置
|
|
|
|
|
|
*/
|
2026-02-13 18:50:05 +08:00
|
|
|
|
public void reloadConfig() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
// 重新从磁盘加载配置
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 20:56:03 +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() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
return getConfig().getInt("blocks.amount", 5);
|
2026-02-13 18:50:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getSpreadRange() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
return getConfig().getInt("blocks.spread", 5);
|
2026-02-13 18:50:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getDepth() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
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() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
return getConfig().getBoolean("game.die_when_blocks_gone", true);
|
2026-02-13 18:50:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isBecomeSpectator() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
return getConfig().getBoolean("game.become_spectator", true);
|
2026-02-13 18:50:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isHealthSystemEnabled() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
return getConfig().getBoolean("game.health_system", true);
|
2026-02-13 18:50:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isSkinSystemEnabled() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
return getConfig().getBoolean("skin.enabled", true);
|
2026-02-13 18:50:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getSkinSource() {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
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) {
|
2026-02-13 20:56:03 +08:00
|
|
|
|
String message = getConfig().getString("messages." + path, defaultValue);
|
2026-02-13 18:50:05 +08:00
|
|
|
|
if (message != null) {
|
|
|
|
|
|
message = message.replace("&", "§");
|
|
|
|
|
|
}
|
|
|
|
|
|
return message;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|