Skip to content

Commit

Permalink
Add color code support
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolV1994 committed Aug 11, 2015
1 parent 56a03c0 commit 3fc6674
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions src/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,48 +15,7 @@ public class CommandListener implements VoteListener {
private ArrayList<String> commands = new ArrayList<String>();

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
Expand All @@ -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.");
}
}
}

0 comments on commit 3fc6674

Please sign in to comment.