From 3fc6674aba22b41ca9e289d7d7b832f57b097ecc Mon Sep 17 00:00:00 2001 From: CoolV1994 Date: Tue, 11 Aug 2015 13:24:58 -0400 Subject: [PATCH] Add color code support --- src/CommandListener.java | 92 ++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/src/CommandListener.java b/src/CommandListener.java index c2a5d21..b75e66a 100644 --- a/src/CommandListener.java +++ b/src/CommandListener.java @@ -5,6 +5,7 @@ import com.vexsoftware.votifier.model.Vote; import com.vexsoftware.votifier.model.VoteListener; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; /** * Created by Vinnie on 12/17/14. @@ -14,48 +15,7 @@ public class CommandListener implements VoteListener { private ArrayList commands = new ArrayList(); public CommandListener() { - File configFile = new File("./plugins/Votifier/CommandListener.txt"); - if (!configFile.exists()) - { - String defaultCommand = "say Thanks {username} for voting on {serviceName}!"; - commands.add(defaultCommand); - try { - configFile.createNewFile(); - FileWriter fw = new FileWriter(configFile); - BufferedWriter bw = new BufferedWriter(fw); - bw.write("# CommandListener Configuration"); - bw.newLine(); - bw.write(defaultCommand); - bw.newLine(); - bw.close(); - } catch (IOException e) { - log.warning("Error creating default CommandListener configuration."); - } - } - else - { - BufferedReader br = null; - try { - String currentLine; - br = new BufferedReader(new FileReader(configFile)); - while ((currentLine = br.readLine()) != null) { - // Ignore comment - if (currentLine.startsWith("#")) { - continue; - } - commands.add(currentLine); - } - } catch (IOException e) { - log.warning("Error loading CommandListener configuration."); - } finally { - try { - if (br != null) - br.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } - } + loadConfig(); } @Override @@ -82,4 +42,52 @@ public void voteMade(Vote vote) { Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); } } + + private void loadConfig() { + File configFile = new File("plugins/Votifier/CommandListener.txt"); + String currentLine; + try { + BufferedReader br = new BufferedReader(new FileReader(configFile)); + while ((currentLine = br.readLine()) != null) { + // Ignore comment + if (currentLine.startsWith("#")) { + continue; + } + if (currentLine.startsWith("color=")) { + String msg = currentLine.substring(6); + if (msg != null) { + commands.add(ChatColor.translateAlternateColorCodes('&', msg)); + } + continue; + } + commands.add(currentLine); + } + br.close(); + } catch (FileNotFoundException ex) { + log.warning("Could not find CommandListener configuration. Creating default."); + createDefaultConfig(configFile); + } catch (IOException e) { + log.severe("Error loading CommandListener configuration."); + } + } + + private void createDefaultConfig(File configFile) { + String default1 = "color=say Thanks &b{username}&r for voting on &9{serviceName}!"; + String default2 = "give {username} diamond 1"; + commands.add(default1); + commands.add(default2); + try { + FileWriter fw = new FileWriter(configFile); + BufferedWriter bw = new BufferedWriter(fw); + bw.write("# CommandListener Configuration"); + bw.newLine(); + bw.write(default1); + bw.newLine(); + bw.write(default2); + bw.newLine(); + bw.close(); + } catch (IOException e) { + log.warning("Error creating default CommandListener configuration."); + } + } }