feat: 添加本地化工具

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-09-12 01:38:55 +08:00
parent 8ad3e181c9
commit 0c7a71e138
1 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,127 @@
package pw.yumc.YumCore.misc;
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 20151214 1:33:52
* @author
*/
public class L10N {
private static String CONFIG_NAME = "Item_zh_CN.yml";
private static FileConfig custom;
private static Map<String, String> content;
static {
content = new HashMap<>();
Log.info("异步初始化本地化工具...");
load();
}
/**
* ()
*
* @param i
*
* @return
*/
public static final String getItemFullName(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 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<String, String> local = (Map<String, String>) YumConfig.getLocal(CONFIG_NAME).getContentMap();
final Map<String, String> remote = (Map<String, String>) 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();
}
}