Java tutorial
/* * Copyright (c) 2014 portalBlock. This work is provided AS-IS without any warranty. * You must provide a link back to the original project and clearly point out any changes made to this project. * This license must be included in all project files. * Any changes merged with this project are property of the copyright holder but may include the author's name. */ package net.portalblock.untamedchat.bungee; import org.json.JSONArray; import org.json.JSONObject; import java.io.*; import java.util.LinkedList; /** * Created by portalBlock on 12/19/2014. */ public class UCConfig { public static String TARGET_FORMAT; public static String SENDER_FORMAT; public static String SOCIAL_SPY_FORMAT; public static String GLOBAL_FORMAT; public static String SPAM_MESSAGE; private static String[] msgAliases; private static String[] replyAliases; private static String[] globalAliases; private static String[] socialSpyAliases; private static boolean gcDefault, chatCoolDowns, spDefault; private static long chatCooldown; static { load(); } public static void load() { final String NEW_LINE = System.getProperty("line.separator"); File cfgDir = new File("plugins/UntamedChat"); if (!cfgDir.exists()) { UntamedChat.getInstance().getLogger().info("No config directory found, generating one now!"); cfgDir.mkdir(); } File configFile = new File(cfgDir + "/config.json"); if (!configFile.exists()) { UntamedChat.getInstance().getLogger().info("No config file found, generating one now!"); try { configFile.createNewFile(); InputStream is = UCConfig.class.getResourceAsStream("/config.json"); String line; if (is == null) throw new NullPointerException("is"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); FileWriter configWriter = new FileWriter(configFile); while ((line = reader.readLine()) != null) { configWriter.write(line + NEW_LINE); } configWriter.flush(); configWriter.close(); reader.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } try { BufferedReader reader = new BufferedReader(new FileReader(configFile)); StringBuilder configBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { configBuilder.append(line); } JSONObject config = new JSONObject(configBuilder.toString()); TARGET_FORMAT = config.optString("target_format", "&6{sender} &7-> &6Me&7: {msg}"); SENDER_FORMAT = config.optString("sender_format", "&6Me &7-> &6{target}&7: {msg}"); SOCIAL_SPY_FORMAT = config.optString("social_spy_format", "{sender} -> {target}: {msg}"); GLOBAL_FORMAT = config.optString("global_format", "&7[&6{server}&7] [&6{sender}&7]: &r{msg}"); gcDefault = config.optBoolean("global_chat_default", false); chatCoolDowns = config.optBoolean("enable_chat_cooldown", true); spDefault = config.optBoolean("social_spy_default", false); chatCooldown = config.optLong("chat_cooldown", 10); SPAM_MESSAGE = config.optString("spam_message", "&7[&cUntamedChat&7] &cDon't spam the chat!"); JSONObject commands = config.optJSONObject("commands"); if (commands == null) { msgAliases = new String[] { "msg", "m" }; replyAliases = new String[] { "reply", "r" }; globalAliases = new String[] { "globalchat", "global", "g" }; socialSpyAliases = new String[] { "socialspy", "sp", "spy" }; } else { msgAliases = makeCommandArray(commands.optJSONArray("msg"), "msg", "m"); replyAliases = makeCommandArray(commands.optJSONArray("reply"), "reply", "r"); globalAliases = makeCommandArray(commands.optJSONArray("global_chat"), "globalchat", "global", "g"); socialSpyAliases = makeCommandArray(commands.optJSONArray("social_spy"), "socialspy", "sp", "spy"); } } catch (IOException e) { e.printStackTrace(); } } private static String[] makeCommandArray(JSONArray array, String... defs) { if (array == null) return defs; LinkedList<String> val = new LinkedList<String>(); for (int i = 0; i < array.length(); i++) { val.add(array.getString(i)); } return val.toArray(new String[val.size()]); } public static String compileMessage(String format, String msg, String server, String sender, String target) { msg = msg.replace("$", "\\$"); return format.replaceAll("\\{server\\}", (server != null ? server : "")) .replaceAll("\\{sender\\}", (sender != null ? sender : "")) .replaceAll("\\{target\\}", (target != null ? target : "")) .replaceAll("\\{msg\\}", (msg != null ? msg : "")); } public static String getRootForGlobal() { return globalAliases[0]; } public static String getRootForSocialSpy() { return socialSpyAliases[0]; } public static String getRootForMsg() { return msgAliases[0]; } public static String getRootForReply() { return replyAliases[0]; } public static String[] getMsgAliases() { return msgAliases; } public static String[] getReplyAliases() { return replyAliases; } public static String[] getGlobalAliases() { return globalAliases; } public static String[] getSocialSpyAliases() { return socialSpyAliases; } public static boolean isGcDefault() { return gcDefault; } public static long getChatCooldown() { return chatCooldown; } public static boolean isChatCoolDowns() { return chatCoolDowns; } public static boolean isSpDefault() { return spDefault; } }