mirror of
https://github.com/ColdeZhang/Dominion.git
synced 2025-03-11 13:16:38 +08:00
完成了 flags.json 到 flags.yml 的配置内容迁移,使得权限也支持 i18n 国际化(language中的 Flags 段落) #8
This commit is contained in:
parent
63b1c31953
commit
4c5f3a6db1
@ -4,7 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "cn.lunadeer"
|
||||
version = "2.5.3-beta"
|
||||
version = "2.5.4-beta"
|
||||
|
||||
java {
|
||||
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
|
||||
|
@ -1,11 +1,15 @@
|
||||
package cn.lunadeer.dominion.dtos;
|
||||
|
||||
import cn.lunadeer.dominion.Dominion;
|
||||
import cn.lunadeer.dominion.managers.Translation;
|
||||
import cn.lunadeer.minecraftpluginutils.JsonFile;
|
||||
import cn.lunadeer.minecraftpluginutils.XLogger;
|
||||
import cn.lunadeer.minecraftpluginutils.i18n.Localization;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -85,7 +89,6 @@ public enum Flag {
|
||||
private Boolean default_value;
|
||||
private Boolean enable;
|
||||
private final Boolean dominion_only;
|
||||
private Boolean custom_text = false;
|
||||
private final String default_display_name;
|
||||
private final String default_description;
|
||||
|
||||
@ -136,6 +139,14 @@ public enum Flag {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
public String getDisplayNameKey() {
|
||||
return "Flags." + flag_name + ".DisplayName";
|
||||
}
|
||||
|
||||
public String getDescriptionKey() {
|
||||
return "Flags." + flag_name + ".Description";
|
||||
}
|
||||
|
||||
public static List<Flag> getAllFlags() {
|
||||
return Arrays.asList(Flag.values());
|
||||
}
|
||||
@ -209,84 +220,73 @@ public enum Flag {
|
||||
return Arrays.stream(Flag.values()).filter(flag -> flag.getFlagName().equals(flagName)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/*
|
||||
{
|
||||
flag_name: {
|
||||
display_name: "",
|
||||
description: "",
|
||||
default_value: true,
|
||||
enable: true
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public static JSONObject serializeToJson(Flag flag) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("display_name", flag.getDisplayName());
|
||||
json.put("description", flag.getDescription());
|
||||
json.put("default_value", flag.getDefaultValue());
|
||||
json.put("enable", flag.getEnable());
|
||||
json.put("custom_text", flag.custom_text);
|
||||
return json;
|
||||
}
|
||||
|
||||
public static JSONObject serializeToJson() {
|
||||
JSONObject json = new JSONObject();
|
||||
for (Flag flag : getAllFlags()) {
|
||||
JSONObject flagJson = serializeToJson(flag);
|
||||
json.put(flag.getFlagName(), flagJson);
|
||||
/**
|
||||
* 从文件中加载Flag配置
|
||||
*/
|
||||
public static void loadFromFile() {
|
||||
try {
|
||||
loadLegacyJsonFlags();
|
||||
loadFlagsConfiguration();
|
||||
} catch (Exception e) {
|
||||
XLogger.err(Translation.Config_Check_LoadFlagError, e.getMessage());
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
private static void loadLegacyJsonFlags() throws Exception {
|
||||
File jsonFile = new File(Dominion.instance.getDataFolder(), "flags.json");
|
||||
if (jsonFile.exists()) {
|
||||
JSONObject jsonObject = JsonFile.loadFromFile(jsonFile);
|
||||
if (jsonObject != null) {
|
||||
deserializeFromJson(jsonObject);
|
||||
}
|
||||
jsonFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadFlagsConfiguration() throws IOException {
|
||||
File yamlFile = new File(Dominion.instance.getDataFolder(), "flags.yml");
|
||||
if (!yamlFile.exists()) {
|
||||
Dominion.instance.saveResource("flags.yml", false);
|
||||
}
|
||||
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(yamlFile);
|
||||
for (Flag flag : getAllFlags()) {
|
||||
// load flags name & description translations
|
||||
((Translation)(Localization.instance)).loadOrSetFlagTranslation(flag);
|
||||
// load flags default value & enable
|
||||
String defaultValueKey;
|
||||
String enableKey;
|
||||
if (flag.dominion_only) {
|
||||
defaultValueKey = "environment." + flag.getFlagName() + ".default";
|
||||
enableKey = "environment." + flag.getFlagName() + ".enable";
|
||||
} else {
|
||||
defaultValueKey = "privilege." + flag.getFlagName() + ".default";
|
||||
enableKey = "privilege." + flag.getFlagName() + ".enable";
|
||||
}
|
||||
if (yaml.contains(defaultValueKey)) {
|
||||
flag.setDefaultValue(yaml.getBoolean(defaultValueKey));
|
||||
} else {
|
||||
yaml.set(defaultValueKey, flag.getDefaultValue());
|
||||
}
|
||||
if (yaml.contains(enableKey)) {
|
||||
flag.setEnable(yaml.getBoolean(enableKey));
|
||||
} else {
|
||||
yaml.set(enableKey, flag.getEnable());
|
||||
}
|
||||
}
|
||||
yaml.save(yamlFile);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void deserializeFromJson(JSONObject jsonObject) {
|
||||
for (Flag flag : getAllFlags()) {
|
||||
try {
|
||||
JSONObject flagJson = (JSONObject) jsonObject.get(flag.getFlagName());
|
||||
if (flagJson != null) {
|
||||
flag.custom_text = (Boolean) flagJson.getOrDefault("custom_text", false);
|
||||
flag.setDefaultValue((Boolean) flagJson.getOrDefault("default_value", flag.getDefaultValue()));
|
||||
flag.setEnable((Boolean) flagJson.getOrDefault("enable", flag.getEnable()));
|
||||
if (flag.custom_text) { // 如果使用自定义文本 则从配置文件中读取
|
||||
flag.setDisplayName((String) flagJson.getOrDefault("display_name", flag.getDisplayName()));
|
||||
flag.setDescription((String) flagJson.getOrDefault("description", flag.getDescription()));
|
||||
} else { // 否则设置为默认文本
|
||||
flag.setDisplayName(flag.default_display_name);
|
||||
flag.setDescription(flag.default_description);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.warn("读取权限 %s 配置失败:%s,已跳过,使用默认配置", flag.getFlagName(), e.getMessage());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadFromJson() {
|
||||
try {
|
||||
File flagFile = new File(Dominion.instance.getDataFolder(), "flags.json");
|
||||
if (!flagFile.exists()) {
|
||||
saveToJson();
|
||||
}
|
||||
JSONObject jsonObject = JsonFile.loadFromFile(flagFile);
|
||||
if (jsonObject == null) {
|
||||
XLogger.warn("读取权限配置失败,已重置");
|
||||
saveToJson();
|
||||
}
|
||||
deserializeFromJson(jsonObject);
|
||||
saveToJson(); // 复写一遍,确保文件中包含所有权限
|
||||
} catch (Exception e) {
|
||||
XLogger.err("读取权限配置失败:%s", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveToJson() {
|
||||
try {
|
||||
JSONObject json = serializeToJson();
|
||||
XLogger.debug("保存权限配置:%s", json.toJSONString());
|
||||
File flagFile = new File(Dominion.instance.getDataFolder(), "flags.json");
|
||||
JsonFile.saveToFile(json, flagFile);
|
||||
} catch (Exception e) {
|
||||
XLogger.err("保存权限配置失败:%s", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ public class ConfigManager {
|
||||
limits.putAll(GroupLimit.loadGroups(_plugin));
|
||||
|
||||
saveAll(); // 回写文件 防止文件中的数据不完整
|
||||
Flag.loadFromJson(); // 加载 Flag 配置
|
||||
Flag.loadFromFile(); // 加载 Flag 配置
|
||||
}
|
||||
|
||||
public void saveAll() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.lunadeer.dominion.managers;
|
||||
|
||||
|
||||
import cn.lunadeer.dominion.dtos.Flag;
|
||||
import cn.lunadeer.minecraftpluginutils.i18n.Localization;
|
||||
import cn.lunadeer.minecraftpluginutils.i18n.i18n;
|
||||
import cn.lunadeer.minecraftpluginutils.i18n.i18nField;
|
||||
@ -575,6 +576,8 @@ public class Translation extends Localization {
|
||||
public static i18n Config_Check_GroupPriceError;
|
||||
@i18nField(defaultValue = "权限组 %s 的 Refund 设置不合法,已重置为 0.85")
|
||||
public static i18n Config_Check_GroupRefundError;
|
||||
@i18nField(defaultValue = "读取权限配置失败:%s")
|
||||
public static i18n Config_Check_LoadFlagError;
|
||||
|
||||
@i18nField(defaultValue = "语言设置,参考 languages 文件夹下的文件名")
|
||||
public static i18n Config_Comment_Language;
|
||||
@ -645,4 +648,27 @@ public class Translation extends Localization {
|
||||
public Translation(JavaPlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
public void loadOrSetFlagTranslation(Flag flag) {
|
||||
String displayNameTranslation = loadOrSet(flag.getDisplayNameKey(), flag.getDisplayName());
|
||||
String descriptionTranslation = loadOrSet(flag.getDescriptionKey(), flag.getDescription());
|
||||
flag.setDisplayName(displayNameTranslation);
|
||||
flag.setDescription(descriptionTranslation);
|
||||
}
|
||||
|
||||
public void saveFlagTranslation(Flag flag) {
|
||||
set(flag.getDisplayNameKey(), flag.getDisplayName());
|
||||
set(flag.getDescriptionKey(), flag.getDescription());
|
||||
}
|
||||
|
||||
public void loadFlagTranslation(Flag flag) {
|
||||
String displayNameTranslation = load(flag.getDisplayNameKey());
|
||||
if (displayNameTranslation != null) {
|
||||
flag.setDisplayName(displayNameTranslation);
|
||||
}
|
||||
String descriptionTranslation = load(flag.getDescriptionKey());
|
||||
if (descriptionTranslation != null) {
|
||||
flag.setDescription(descriptionTranslation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
0
core/src/main/resources/flags.yml
Normal file
0
core/src/main/resources/flags.yml
Normal file
0
core/src/main/resources/languages/en-us.yml
Normal file
0
core/src/main/resources/languages/en-us.yml
Normal file
Loading…
Reference in New Issue
Block a user