Advertisement
Jnk1296

VoteLinks

Mar 25th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.02 KB | None | 0 0
  1. package net.risenphoenix.jnk.votelinks;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataInputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10.  
  11. import org.bukkit.ChatColor;
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15.  
  16. public final class VoteLinks extends JavaPlugin{
  17.    
  18.     // ***VOTELINKS V1.0.3 BY: JNK1296***
  19.     // Declare Variables
  20.    
  21.     // Storage for the configuration file
  22.     public static String[] lines = new String[64];
  23.     public static char[] color = new char[64];
  24.    
  25.     // plugin version string and (chat) name
  26.     private static final String verString = "v1.0.3";
  27.     private static final String plugName = "[VoteLinks] ";
  28.    
  29.     // File paths
  30.     File dir = new File("plugins/VoteLinks");
  31.     File path = new File("plugins/VoteLinks/URLs.txt");
  32.    
  33.     // Output Strings
  34.     private static final String noPermErr = "You don't have permission to use this command. Please contact your server administrator.";
  35.     private static final String configErrRead = "Exception occurred while attempting to read configuration file!";
  36.     private static final String configErrWrite = "Failed to generate default configuration file!";
  37.     private static final String configSucWrite = "Generated default configuration file!";
  38.     private static final String defaultConfigMsg = "# VoteLinks " + verString + ". \r\nDefault Configuration File. Please define URLs here.";
  39.     private static final String onLoad = "Reading URLs.txt...";
  40.     private static final String onDisable = "VoteLinks " + verString + " shutting down...";
  41.     private static final String sucReload = "VoteLinks configuration reloaded successfully!";
  42.     private static final String failReload = "An error occurred while attempting to reload the configuration. :(";
  43.     private static final String cmdErr = "Improper syntax or undefined command.";
  44.  
  45.     public void onEnable() {
  46.         // Console Output
  47.         getLogger().info(onLoad);
  48.        
  49.         // Method Calls
  50.         defaultConfiguration();
  51.         loadConfig();
  52.     }
  53.    
  54.     // If URLs.txt does not exist, then generate the default configuration file.
  55.     public void defaultConfiguration() {
  56.         FileWriter f = null;
  57.        
  58.         try {
  59.             // If VoteLinks folder does not exist, create it.
  60.             if (!dir.exists()) dir.mkdir();
  61.            
  62.             // If configuration file does not exist, create it.
  63.             if (!path.exists()) {
  64.                 f = new FileWriter(path, true);
  65.                
  66.                 f.write(defaultConfigMsg);
  67.                 f.close();
  68.                
  69.                 // Console Output
  70.                 getLogger().info(configSucWrite);
  71.             }
  72.  
  73.         } catch (IOException e) {
  74.             // Console Output (in the event of an exception)
  75.             getLogger().severe(configErrWrite);
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.    
  80.     public boolean loadConfig() {
  81.         // Load the configuration file into lines[] array. (Line formatting takes place here as well.)
  82.        
  83.         for (int i = 0; i < color.length; i++) {
  84.             color[i] = 'x';
  85.         }
  86.        
  87.         try {
  88.             FileInputStream fstream = new FileInputStream(path);
  89.             DataInputStream in = new DataInputStream(fstream);
  90.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  91.             String strLine;
  92.            
  93.             // Parse Data for color codes and store the data in corresponding arrays color[] and lines[]
  94.             for (int i = 0; i < lines.length; i++) {
  95.                 if ((strLine = br.readLine()) != null) {
  96.                     // If the color-code character is detected at the beginning of the line, the execute the following.
  97.                     if (strLine.startsWith("&")) {
  98.                        
  99.                         // assign a color code value to the colors array based on the code given within URLs.txt.
  100.                         char code = strLine.charAt(1);
  101.                         if ((code >= '0' && code <= '9') || (code >= 'a' && code <= 'f') || (code >= 'k' && code <= 'o')) {
  102.                             color[i] = code;
  103.                            
  104.                         // If an uppercase, but otherwise valid code was passed, convert it to lowercase and pass it to the array.
  105.                         } else if ((code >= 'A' && code <= 'F') || (code >= 'K' && code <= 'O')) {
  106.                             code = Character.toLowerCase(code);
  107.                             color[i] = code;
  108.                        
  109.                         // A character value of 'q' indicates that an invalid color code was given. (q represents null in this case)
  110.                         } else {
  111.                             color[i] = 'q';
  112.                         }
  113.                        
  114.                         // Remove the color code from the line so that it doesn't appear in the output.
  115.                         strLine = strLine.replaceFirst("&" + strLine.charAt(1), "");
  116.                     }
  117.                    
  118.                     lines[i] = strLine;
  119.                 } else {
  120.                     break;
  121.                 }
  122.             }
  123.            
  124.             br.close();
  125.             return true;
  126.         } catch (Exception e) {
  127.             // Console Output in the event we fail to read the configuration file.
  128.             getLogger().severe(configErrRead);
  129.            
  130.             return false;
  131.         }
  132.     }
  133.    
  134.     public boolean reload() {
  135.         // Set all elements of lines[] to null, then reload the configuration file.
  136.         for (int i = 0; i < lines.length; i++) {
  137.             lines[i] = null;
  138.         }
  139.        
  140.         // Load the Configuration, check for success
  141.         return loadConfig();
  142.     }
  143.    
  144.     public ChatColor getColor(int index) {
  145.         // If the color index for the line is null or undefined, then return the default plugin color (GOLD), else return the color specified.
  146.         if (color[index] == 'q' || color[index] == 'x') {
  147.             return ChatColor.WHITE;
  148.         } else {
  149.             return ChatColor.getByChar(color[index]);
  150.         }
  151.     }
  152.    
  153.     public void onDisable() {
  154.         // Console output
  155.         getLogger().info(onDisable);
  156.     }
  157.    
  158.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  159.         boolean hasParameters = false;
  160.        
  161.         // Check for command arguments
  162.         try {
  163.             if (args[0] != null) {
  164.                 hasParameters = true;
  165.             }
  166.         } catch (Exception e) {
  167.             hasParameters = false;
  168.         }
  169.        
  170.         // If we get the 'vote' command with parameters specified.
  171.         if(cmd.getName().equalsIgnoreCase("vote") && hasParameters) {
  172.             if (args[0].equalsIgnoreCase("reload")) {
  173.                 if (sender.hasPermission("VoteLinks.reload") || sender.isOp()) {
  174.                     boolean success = reload();
  175.                     if (success) {
  176.                         sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + sucReload);
  177.                     } else {
  178.                         sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + failReload);
  179.                     }
  180.                     return true;
  181.                 } else {
  182.                     sender.sendMessage(noPermErr);
  183.                     return true;
  184.                 }
  185.             // if "about" then display the plugin name and version number.
  186.             } else if (args[0].equalsIgnoreCase("about")) {
  187.                 sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + "VoteLinks " + verString + " by Jnk1296.");
  188.                 return true;
  189.                
  190.             // all else
  191.             } else {
  192.                 sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.DARK_RED + cmdErr);
  193.                 return true;
  194.             }
  195.         } else if (!hasParameters) {
  196.             // Else if no parameters are specified, perform main function. (Display Links)
  197.             for (int i = 0; i < lines.length; i++) {
  198.                 if (lines[i] != null) {
  199.                     sender.sendMessage(ChatColor.YELLOW + plugName + ChatColor.GOLD + "["+ (i+1) + "] " + getColor(i) + lines[i]);
  200.                 } else {
  201.                     break;
  202.                 }
  203.             }
  204.             return true;
  205.         }
  206.        
  207.         return false;
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement