diff --git a/src/main/java/pw/yumc/YumCore/global/L10N.java b/src/main/java/pw/yumc/YumCore/global/L10N.java new file mode 100644 index 0000000..f2e5c89 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/global/L10N.java @@ -0,0 +1,141 @@ +package pw.yumc.YumCore.global; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.material.SpawnEgg; + +import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.config.FileConfig; +import pw.yumc.YumCore.config.YumConfig; + +/** + * 本地化工具类 + * + * @since 2015年12月14日 下午1:33:52 + * @author 喵♂呜 + */ +public class L10N { + private static String CONFIG_NAME = "Item_zh_CN.yml"; + private static FileConfig custom; + private static Map content; + + static { + content = new HashMap<>(); + Log.info("异步初始化本地化工具..."); + load(); + } + + private L10N() { + } + + /** + * 获取物品完整汉化名称(包括原版) + * + * @param i + * 物品实体 + * @return 物品名称 + */ + public static final String getFullName(final ItemStack i) { + return getItemName(getItemType(i)) + (i.hasItemMeta() && i.getItemMeta().hasDisplayName() ? "§r(" + i.getItemMeta().getDisplayName() + "§r)" : ""); + } + + /** + * 获取物品汉化名称 + * + * @param i + * 物品实体 + * @return 物品名称 + */ + public static final String getItemName(final ItemStack i) { + return getItemName(getItemType(i)); + } + + /** + * 获取物品汉化名称(优先显示名称) + * + * @param i + * 物品实体 + * @return 物品名称 + */ + public static final String getName(final ItemStack i) { + return i.hasItemMeta() && i.getItemMeta().hasDisplayName() ? i.getItemMeta().getDisplayName() : getItemName(getItemType(i)); + } + + /** + * 重载LocalUtil + */ + public static void reload() { + Log.info("异步重载本地化工具..."); + content.clear(); + load(); + } + + /** + * 获取物品汉化名称 + * + * @param iname + * 物品类型名称 + * @return 物品名称 + */ + private static final String getItemName(final String iname) { + String aname = content.get(iname); + if (aname == null) { + aname = iname; + if (custom != null) { + custom.set(iname, iname); + custom.save(); + } + } + return aname; + } + + /** + * 获取物品类型名称 + * + * @param i + * 物品实体 + * @return 物品类型 + */ + private static final String getItemType(final ItemStack i) { + String name = i.getType().name(); + String dura = ""; + if (i.getType() == Material.MONSTER_EGG) { + name = ((SpawnEgg) i.getData()).getSpawnedType().name(); + } else { + final int dur = i.getDurability(); + dura = (i.getMaxStackSize() != 1 && dur != 0) ? Integer.toString(dur) : ""; + } + return (name + (dura.isEmpty() ? "" : "-" + dura)).toUpperCase(); + } + + /** + * 载入数据 + */ + private static void load() { + new Thread(new Runnable() { + @SuppressWarnings("unchecked") + @Override + public void run() { + try { + final Map local = YumConfig.getLocal(CONFIG_NAME).getContentMap(); + final Map remote = YumConfig.getRemote(CONFIG_NAME).getContentMap(); + if (local != null) { + Log.info("本地汉化文件词条数量: " + local.size()); + content.putAll(local); + } + if (remote != null) { + Log.info("远程汉化文件词条数量: " + remote.size()); + content.putAll(remote); + } + Log.info("本地化工具初始化完毕..."); + } catch (final Exception e) { + Log.warning(String.format("本地化工具初始化失败: %s %s", e.getClass().getName(), e.getMessage())); + Log.debug(CONFIG_NAME, e); + } + } + }).start(); + } +} diff --git a/src/main/java/pw/yumc/YumCore/misc/package-info.java b/src/main/java/pw/yumc/YumCore/misc/package-info.java new file mode 100644 index 0000000..2458e87 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/misc/package-info.java @@ -0,0 +1,7 @@ +/** + * 杂项包 + * + * @since 2016年9月27日 下午3:11:15 + * @author 喵♂呜 + */ +package pw.yumc.YumCore.misc; \ No newline at end of file