Advertisement
kmccmk9

HelpMe

Jun 20th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.95 KB | None | 0 0
  1. package com.kmccmk9.HelpMeAdvanced;
  2.  
  3. // All the imports
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Hashtable;
  10. import java.util.Scanner;
  11.  
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.Location;
  14. import org.bukkit.World;
  15. import org.bukkit.command.Command;
  16. import org.bukkit.command.CommandSender;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.plugin.PluginDescriptionFile;
  19. import org.bukkit.plugin.java.JavaPlugin;
  20.  
  21. import org.bukkit.Server;
  22.  
  23. /**
  24.  * HelpMeAdvanced for Bukkit
  25.  *
  26.  * @author kmccmk9
  27.  */
  28.  
  29. //Starts the class
  30. public class HelpMeAdvanced extends JavaPlugin{
  31.     Server server;
  32.     String variable;
  33.     Scanner msgs;
  34.     String description;
  35.     ArrayList<String> messagestoprint = new ArrayList<String>();
  36.     Hashtable<String, String> playerdescriptions = new Hashtable<String, String>();
  37.     Hashtable<String, World> playerlocations = new Hashtable<String, World>();
  38.     ArrayList<String> playernames = new ArrayList<String>();
  39.     Location gotolocation;
  40.     Player gotoplayer;
  41.     int counter = 1;
  42.     int number;
  43.     String numberstring;
  44.     int arraylength;
  45.    
  46.     //Define File Variables
  47.     static String mainDirectory = "plugins/HelpMeAdvanced"; //sets the main directory for easy reference
  48.     static File messages = new File(mainDirectory + File.separator + "messages.txt"); //the file separator is the / sign, this will create a new Zones.dat files in the mainDirectory variable listed above, if no Zones directory exists then it will automatically be made along with the file.
  49.     // onDisable
  50.     public void onDisable() {
  51.         PluginDescriptionFile pdfFile = this.getDescription();
  52.         System.out.println( pdfFile.getName() + " is disabled!" );
  53.     }
  54.     // onEnable
  55.     public void onEnable() {
  56.         server = this.getServer();
  57.         PluginDescriptionFile pdfFile = this.getDescription();
  58.         System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
  59.         //Setup Files
  60.         new File(mainDirectory).mkdir(); //makes the Zones directory/folder in the plugins directory
  61.         if(!messages.exists()){ //Checks to see if the zones file exists, defined above, if it doesn't exist then it will do the following. the&nbsp;! turns the whole statement around, checking that the file doesn't exist instead of if it exists.
  62.             try { //try catch clause explained below in tutorial
  63.                 messages.createNewFile(); //creates the file zones.dat
  64.                 FileWriter writer = new FileWriter(messages,true);
  65.                 BufferedWriter out = new BufferedWriter(writer);
  66.                 out.write("An Admin or SuperAdmin will come and help you soon as possible.");
  67.                 out.newLine();
  68.                 out.write("If no one come and helps you within 5 minutes, perform the command again.");
  69.                 out.newLine();
  70.                 out.write("Message sent to Admins");
  71.                 out.close();
  72.             } catch (IOException ex) {
  73.                 ex.printStackTrace(); //explained below.
  74.             }
  75.  
  76.         } else {
  77.  
  78.             openFile();
  79.             readFile();
  80.             closeFile();
  81.  
  82.         }
  83.     }
  84.  
  85.     private void openFile() {
  86.         try{
  87.             msgs = new Scanner(messages);
  88.         }catch (Exception ex){
  89.             System.out.println("[HelpMeAdvanced] Could not find messages.txt, did you delete it?");
  90.         }
  91.     }
  92.  
  93.     private void readFile() {
  94.         if (!msgs.hasNextLine()){ //Check if there are no lines at all
  95.             server.broadcastMessage("[HelpMeAdvanced] Messages.txt is empty.");
  96.         }else{
  97.             messagestoprint.clear(); //Clear the ArrayList (For using the reload command)
  98.             while (msgs.hasNextLine()){
  99.                 String messagetofile = msgs.nextLine(); //Set the next line to a string
  100.                 messagetofile = messagetofile.replaceAll("(&([a-f0-9]))", "\u00A7$2"); //Replace colour codes
  101.                 messagestoprint.add(messagetofile); //Add to the ArrayList
  102.             }
  103.         }
  104.     }
  105.    
  106.     private void closeFile() {
  107.         msgs.close();
  108.     }
  109.    
  110.     public String arrayToString(String[] array) {
  111.         StringBuilder sb = new StringBuilder();
  112.         for (int a = 1; a < array.length; a++) {
  113.         sb.append(array[a]);
  114.         sb.append(" ");
  115.         }
  116.         String newString = sb.toString();
  117.         return newString;
  118.         }
  119.    
  120.     // Command system
  121.     public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
  122.         Player[] players;
  123.         int total;
  124.         if (sender instanceof Player) {
  125.             Player player = (Player) sender;
  126.             if(commandLabel.equalsIgnoreCase("helpme")) {
  127.                 if (args.length == 1)
  128.                 {
  129.                     variable = args[0];
  130.                     if (variable.equalsIgnoreCase("help"))
  131.                     {
  132.                         player.sendMessage("Typing in \"/helpme will alert Admins that you need help!\"");
  133.                         player.sendMessage("Typing in \"/helpme description \"Description here\" (Without quotes) will alert Admins that you need help and add a description");
  134.                         player.sendMessage("Typing in \"/helpme reload will reload the messages file\"");
  135.                         player.sendMessage("Typing in \"/helpme list will list the players that need help if you are the admin\"");
  136.                         player.sendMessage("Typing in \"/helpme goto \"Number from list here\" (Without quotes) will teleport you to the person in need and remove them from the list\"");
  137.                         player.sendMessage("Typing in \"/helpme remove \"Number from list here\" (Without quotes) will remove that player from the list\"");
  138.                     }
  139.                     if (variable.equalsIgnoreCase("description"))
  140.                     {
  141.                         player.sendMessage(ChatColor.DARK_RED + "Missing description. Please add a one word description.");
  142.                     }
  143.                     if (variable.equalsIgnoreCase("reload"))
  144.                     {
  145.                         readFile();
  146.                     }
  147.                     if (variable.equalsIgnoreCase("list"))
  148.                     {
  149.                         for (int i=0;i<playernames.size();i++)
  150.                         {
  151.                             player.sendMessage(ChatColor.GREEN + String.valueOf(counter) + '.' + " " + playernames.get(i).toString() + '-' + " " + playerlocations.get(playernames.get(i).toString()) + " " + '-' + " " + playerdescriptions.get(playernames.get(i).toString()));
  152.                             counter++;
  153.                         }
  154.                         counter = 1;
  155.                     }
  156.                     if (!variable.equalsIgnoreCase("help") && !variable.equalsIgnoreCase("description") && !variable.equalsIgnoreCase("reload") && !variable.equalsIgnoreCase("list"))
  157.                     {
  158.                         player.sendMessage(ChatColor.DARK_RED + "Unrecognized argument. Typing in /helpme will alert Admins that you need help!");
  159.                     }
  160.                 }
  161.                 else if (args.length > 1)
  162.                 {
  163.                     if (variable.equalsIgnoreCase("description"))
  164.                     {
  165.                         description = arrayToString(args);
  166.                         this.getServer().broadcastMessage(description);
  167.                     }
  168.                     //Add Players to List
  169.                     playerlocations.put(player.toString(), player.getLocation().getWorld());
  170.                     playerdescriptions.put(player.toString(), description);
  171.                     playernames.add(player.toString());
  172.                     //Degbug
  173.                     //Dispatch Message
  174.                     for (int j = 0;j < arraylength;j++)
  175.                     {
  176.                     player.sendMessage(messagestoprint.get(j));
  177.                     }
  178.                     players = server.getOnlinePlayers();
  179.                     total = players.length;
  180.                     int i = 0;
  181.                     String worldname = player.getWorld().getName();
  182.                     server.broadcastMessage(worldname);
  183.                     while (i < total)
  184.                     {
  185.                         if (players[i].isOp() || players[i].hasPermission("helpmeadvanced.admin")) {
  186.                             players[i].sendMessage(ChatColor.DARK_RED + player.getDisplayName() + ChatColor.DARK_GREEN + " needs some help!" + " (" + worldname + player.getLocation().getBlockX() + ',' + player.getLocation().getBlockY() + ',' + player.getLocation().getBlockZ() + ") Description: " + description);  
  187.                         }
  188.                         i++;
  189.                     }
  190.                     if (variable.equalsIgnoreCase("goto"))
  191.                     {
  192.                         numberstring = arrayToString(args);
  193.                         number = Integer.parseInt(numberstring);
  194.                         gotoplayer = server.getPlayer(playernames.get(number));
  195.                         gotolocation = gotoplayer.getLocation();
  196.                         playerlocations.remove(playernames.get(number));
  197.                         playerdescriptions.remove(playernames.get(number));
  198.                         player.teleport(gotolocation);
  199.                     }
  200.                     if (variable.equalsIgnoreCase("remove"))
  201.                     {
  202.                         numberstring = arrayToString(args);
  203.                         number = Integer.parseInt(numberstring);
  204.                         playerlocations.remove(playernames.get(number));
  205.                     }
  206.                 }
  207.                 else if (args.length == 0)
  208.                 {
  209.                     //Debug
  210.                     arraylength = messagestoprint.size();
  211.                     //Add Player to list
  212.                     playerlocations.put(player.toString(), player.getLocation().getWorld());
  213.                     playerdescriptions.put(player.toString(), " ");
  214.                     playernames.add(player.toString());
  215.                     for (int j = 0;j < arraylength;j++)
  216.                     {
  217.                     player.sendMessage(messagestoprint.get(j));
  218.                     }
  219.                     players = server.getOnlinePlayers();
  220.                     total = players.length;
  221.                     int i = 0;
  222.                     while (i < total)
  223.                     {
  224.                         if (/*HelpMeAdvanced.permissionHandler.has(players[i], "helpmeadvanced.admin") ||*/ players[i].isOp() || players[i].hasPermission("helpmeadvanced.admin")) {
  225.                             players[i].sendMessage(ChatColor.DARK_RED + player.getDisplayName() + ChatColor.DARK_GREEN + " needs some help!" + " (" + player.getLocation().getWorld() + player.getLocation().getBlockX() + ',' + player.getLocation().getBlockY() + ',' + player.getLocation().getBlockZ() + ")");  
  226.                         }
  227.                         i++;
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.         return true;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement