Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.risenphoenix.jnk.votelinks;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.plugin.java.JavaPlugin;
- public final class VoteLinks extends JavaPlugin{
- // ***VOTELINKS V1.0.3 BY: JNK1296***
- // Declare Variables
- // Storage for the configuration file
- public static String[] lines = new String[64];
- public static char[] color = new char[64];
- // plugin version string and (chat) name
- private static final String verString = "v1.0.3";
- private static final String plugName = "[VoteLinks] ";
- // File paths
- File dir = new File("plugins/VoteLinks");
- File path = new File("plugins/VoteLinks/URLs.txt");
- // Output Strings
- private static final String noPermErr = "You don't have permission to use this command. Please contact your server administrator.";
- private static final String configErrRead = "Exception occurred while attempting to read configuration file!";
- private static final String configErrWrite = "Failed to generate default configuration file!";
- private static final String configSucWrite = "Generated default configuration file!";
- private static final String defaultConfigMsg = "# VoteLinks " + verString + ". \r\nDefault Configuration File. Please define URLs here.";
- private static final String onLoad = "Reading URLs.txt...";
- private static final String onDisable = "VoteLinks " + verString + " shutting down...";
- private static final String sucReload = "VoteLinks configuration reloaded successfully!";
- private static final String failReload = "An error occurred while attempting to reload the configuration. :(";
- private static final String cmdErr = "Improper syntax or undefined command.";
- public void onEnable() {
- // Console Output
- getLogger().info(onLoad);
- // Method Calls
- defaultConfiguration();
- loadConfig();
- }
- // If URLs.txt does not exist, then generate the default configuration file.
- public void defaultConfiguration() {
- FileWriter f = null;
- try {
- // If VoteLinks folder does not exist, create it.
- if (!dir.exists()) dir.mkdir();
- // If configuration file does not exist, create it.
- if (!path.exists()) {
- f = new FileWriter(path, true);
- f.write(defaultConfigMsg);
- f.close();
- // Console Output
- getLogger().info(configSucWrite);
- }
- } catch (IOException e) {
- // Console Output (in the event of an exception)
- getLogger().severe(configErrWrite);
- e.printStackTrace();
- }
- }
- public boolean loadConfig() {
- // Load the configuration file into lines[] array. (Line formatting takes place here as well.)
- for (int i = 0; i < color.length; i++) {
- color[i] = 'x';
- }
- try {
- FileInputStream fstream = new FileInputStream(path);
- DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- // Parse Data for color codes and store the data in corresponding arrays color[] and lines[]
- for (int i = 0; i < lines.length; i++) {
- if ((strLine = br.readLine()) != null) {
- // If the color-code character is detected at the beginning of the line, the execute the following.
- if (strLine.startsWith("&")) {
- // assign a color code value to the colors array based on the code given within URLs.txt.
- char code = strLine.charAt(1);
- if ((code >= '0' && code <= '9') || (code >= 'a' && code <= 'f') || (code >= 'k' && code <= 'o')) {
- color[i] = code;
- // If an uppercase, but otherwise valid code was passed, convert it to lowercase and pass it to the array.
- } else if ((code >= 'A' && code <= 'F') || (code >= 'K' && code <= 'O')) {
- code = Character.toLowerCase(code);
- color[i] = code;
- // A character value of 'q' indicates that an invalid color code was given. (q represents null in this case)
- } else {
- color[i] = 'q';
- }
- // Remove the color code from the line so that it doesn't appear in the output.
- strLine = strLine.replaceFirst("&" + strLine.charAt(1), "");
- }
- lines[i] = strLine;
- } else {
- break;
- }
- }
- br.close();
- return true;
- } catch (Exception e) {
- // Console Output in the event we fail to read the configuration file.
- getLogger().severe(configErrRead);
- return false;
- }
- }
- public boolean reload() {
- // Set all elements of lines[] to null, then reload the configuration file.
- for (int i = 0; i < lines.length; i++) {
- lines[i] = null;
- }
- // Load the Configuration, check for success
- return loadConfig();
- }
- public ChatColor getColor(int index) {
- // If the color index for the line is null or undefined, then return the default plugin color (GOLD), else return the color specified.
- if (color[index] == 'q' || color[index] == 'x') {
- return ChatColor.WHITE;
- } else {
- return ChatColor.getByChar(color[index]);
- }
- }
- public void onDisable() {
- // Console output
- getLogger().info(onDisable);
- }
- public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
- boolean hasParameters = false;
- // Check for command arguments
- try {
- if (args[0] != null) {
- hasParameters = true;
- }
- } catch (Exception e) {
- hasParameters = false;
- }
- // If we get the 'vote' command with parameters specified.
- if(cmd.getName().equalsIgnoreCase("vote") && hasParameters) {
- if (args[0].equalsIgnoreCase("reload")) {
- if (sender.hasPermission("VoteLinks.reload") || sender.isOp()) {
- boolean success = reload();
- if (success) {
- sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + sucReload);
- } else {
- sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + failReload);
- }
- return true;
- } else {
- sender.sendMessage(noPermErr);
- return true;
- }
- // if "about" then display the plugin name and version number.
- } else if (args[0].equalsIgnoreCase("about")) {
- sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + "VoteLinks " + verString + " by Jnk1296.");
- return true;
- // all else
- } else {
- sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + cmdErr);
- return true;
- }
- } else if (!hasParameters) {
- // Else if no parameters are specified, perform main function. (Display Links)
- for (int i = 0; i < lines.length; i++) {
- if (lines[i] != null) {
- sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.GOLD + "["+ (i+1) + "] " + getColor(i) + lines[i]);
- } else {
- break;
- }
- }
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement