Java tutorial
/* * LanguageHandler.java * * Copyright (c) 2015 ITSTAKE * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package tk.itstake.util; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import java.io.*; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by bexco on 2015-07-26. */ public class LanguageHandler { JSONObject language = null; Plugin plugin = null; public LanguageHandler() { // Get Language Folder plugin = Bukkit.getPluginManager().getPlugin("SteakGUI"); if (plugin != null && plugin.getDataFolder() != null) { languageLoad(); } else { language = getDefaultLanguage(); } } public void languageLoad() { plugin = Bukkit.getPluginManager().getPlugin("SteakGUI"); File langfolder = new File(plugin.getDataFolder().toString() + File.separator + "lang"); if (!langfolder.exists()) { langfolder.mkdir(); } // Get Current Selected Language final String sellang; if (ConfigHandler.hasConfig("lang")) { sellang = ConfigHandler.getConfig("lang").toString(); } else { sellang = "ko_KR"; } // Load Current Selected Language if (langfolder.exists() && langfolder.isDirectory()) { File[] langlist = langfolder.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { if (name.equals(sellang + ".json")) { return true; } else { return false; } } }); // If Detected Language if (langlist.length > 0) { // Load Language try { FileReader fr = new FileReader(langlist[0]); JSONParser jp = new JSONParser(); language = (JSONObject) jp.parse(fr); boolean notupdated = false; for (Object key : language.keySet()) { if (!getDefaultLanguage().containsKey(key)) { notupdated = true; } } if (notupdated) { updateFile(); } fr.close(); } catch (Exception e) { language = getDefaultLanguage(); makeDefaultFile(); } } else { // If Can't Detect Language File language = getDefaultLanguage(); makeDefaultFile(); ConfigHandler.setConfig("lang", "ko_KR"); } } } private void updateFile() { // If Can't Load Language File plugin = Bukkit.getPluginManager().getPlugin("SteakGUI"); File langfolder = new File(plugin.getDataFolder().toString() + File.separator + "lang"); File langfile = new File( langfolder.toString() + File.separator + ConfigHandler.getConfig("lang").toString() + ".json"); // Write New File try { FileWriter fw = new FileWriter(langfile); JSONObject defaultlang = getDefaultLanguage(); for (Object key : defaultlang.keySet()) { if (!language.containsKey(key)) { defaultlang.put(key, language.get(key)); } } fw.write(JSONUtil.getPretty(defaultlang.toJSONString())); language = defaultlang; fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } private void makeDefaultFile() { // If Can't Load Language File plugin = Bukkit.getPluginManager().getPlugin("SteakGUI"); File langfolder = new File(plugin.getDataFolder().toString() + File.separator + "lang"); File langfile = new File( langfolder.toString() + File.separator + ConfigHandler.getConfig("lang").toString() + ".json"); // Write New File try { FileWriter fw = new FileWriter(langfile); language = getDefaultLanguage(); fw.write(JSONUtil.getPretty(language.toJSONString())); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } private JSONObject getDefaultLanguage() { JSONObject defaultlang = new JSONObject(); defaultlang.put("editorhelp", " ?? ? ??? http://wiki.itstake.tk/index.php?title=SteakGUI/ ? !"); defaultlang.put("console.onenable", "&c[&2SteakGUI&c] &6?(SteakGUI) v%0% ? ?!"); defaultlang.put("console.ondisable", "&c[&2SteakGUI&c] &6?(SteakGUI) v%0% ? ?. ?."); defaultlang.put("command.wronguse", "&c[&2SteakGUI&c] &c:%0%"); defaultlang.put("command.help.intro", "&c[&2SteakGUI&c] &b?(SteakGUI) %0% ?? -----"); defaultlang.put("command.help.open", "&c[&2SteakGUI&c] &6/%0% open < ?> &e- ."); defaultlang.put("command.wronghelp.arg.player", "?"); defaultlang.put("command.wronghelp.arg.menu", " ?"); defaultlang.put("command.help.open", "&c[&2SteakGUI&c] &6/%0% open < ?> &e- ."); defaultlang.put("command.help.open.others", "&c[&2SteakGUI&c] &6/%0% open < ?> [?] &e- . ? ?? ?? ."); defaultlang.put("command.help.open.console", "&c[&2SteakGUI&c] &6/%0% open < ?> <?> &e- . ? ?? ?? ."); defaultlang.put("command.help.setting", "&c[&2SteakGUI&c] &6/%0% setting < ?> &e- ."); defaultlang.put("command.help.create", "&c[&2SteakGUI&c] &6/%0% create < ?> &e- ."); defaultlang.put("command.help.delete", "&c[&2SteakGUI&c] &6/%0% delete < ?> &e- ."); defaultlang.put("command.help.list", "&c[&2SteakGUI&c] &6/%0% list &e- ? ?? ."); defaultlang.put("command.help.reload", "&c[&2SteakGUI&c] &6/%0% reload &e- ? ?? ."); defaultlang.put("command.list.intro", "&c[&2SteakGUI&c] &b ?---------"); defaultlang.put("command.list.format", "&c[&2SteakGUI&c]&6 %0%"); defaultlang.put("command.list.nomenu", "&c[&2SteakGUI&c] &c !"); defaultlang.put("command.reloaded", "&c[&2SteakGUI&c] &6? ?."); defaultlang.put("nopermission", "&c[&2SteakGUI&c] &c? !"); defaultlang.put("noconsole", "&c[&2SteakGUI&c] &c? !"); defaultlang.put("menu.deleted", "&c[&2SteakGUI&c] &c %0% ? ?!"); defaultlang.put("menu.created", "&c[&2SteakGUI&c] &6 %0% ? !"); defaultlang.put("menu.notexist", "&c[&2SteakGUI&c] &c !"); defaultlang.put("offlineplayer", "&c[&2SteakGUI&c] &c??? ? !"); defaultlang.put("command.help.author", "&6?(SteakGUI) v%0%, ?: ITSTAKE(http://itstake.tk), ?: ITSTAKE(http://itstake.tk)"); defaultlang.put("menu.noitemtask", "&c? ? . \n&b/sg setting < ?> &c? ? ."); defaultlang.put("menu.wrongsetting", "&c[&2SteakGUI&c] ? ? ? . ? ? ?."); defaultlang.put("menufile.wannahelp", "&c[&2SteakGUI&c] &6?? ? ?\n http://wiki.itstake.tk/index.php?title=SteakGUI/?_ ."); defaultlang.put("existpermission", "&c[&2SteakGUI&c] &c? !"); defaultlang.put("command.notmatchedformat", "&c[&2SteakGUI&c] &c ? ? ! ? ? , /sg setting ? ? ? !"); defaultlang.put("novault", "&c[&2SteakGUI&c] &cVault ! ?? ."); // TO-DO: Language Insert return defaultlang; } public String getLanguage(String path, ArrayList<String> args) { return getLanguage(path, (String[]) args.toArray()); } public String getLanguage(String path) { return getLanguage(path, new String[] {}); } public String getLanguage(String path, String[] args) { if (language != null) { if (language.containsKey(path)) { String lango = (String) language.get(path); Matcher mat = Pattern.compile("%*[0-9]%").matcher(lango); String lang = lango; while (mat.find()) { String sen = mat.group(); if (sen.startsWith("%") && sen.endsWith("%") && isNum(sen.replace("%", "")) && args.length > Integer.parseInt(sen.replace("%", ""))) { lang = lang.replace(mat.group(), args[Integer.parseInt(sen.replace("%", ""))]); } } return lang; } else { updateFile(); if (language.containsKey(path)) { String lango = (String) language.get(path); Matcher mat = Pattern.compile("%*[0-9]%").matcher(lango); String lang = lango; while (mat.find()) { String sen = mat.group(); if (sen.startsWith("%") && sen.endsWith("%") && isNum(sen.replace("%", "")) && args.length > Integer.parseInt(sen.replace("%", ""))) { lang = lang.replace(mat.group(), args[Integer.parseInt(sen.replace("%", ""))]); } } return lang; } else { return "Language file error! Please call admin( ? . ?? ? !)"; } } } else { makeDefaultFile(); if (language.containsKey(path)) { String lango = (String) language.get(path); Matcher mat = Pattern.compile("%*[0-9]%").matcher(lango); String lang = lango; while (mat.find()) { String sen = mat.group(); if (sen.startsWith("%") && sen.endsWith("%") && isNum(sen.replace("%", "")) && args.length > Integer.parseInt(sen.replace("%", ""))) { lang = lang.replace(mat.group(), args[Integer.parseInt(sen.replace("%", ""))]); } } return lang; } else { return "Language file error! Please call admin( ? . ?? ? !)"; } } } public boolean isNum(String num) { try { Integer.parseInt(num); return true; } catch (Exception e) { return false; } } }