Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kmccmk9.HelpMeAdvanced;
- // All the imports
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Hashtable;
- import java.util.Scanner;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.Server;
- /**
- * HelpMeAdvanced for Bukkit
- *
- * @author kmccmk9
- */
- //Starts the class
- public class HelpMeAdvanced extends JavaPlugin{
- Server server;
- String variable;
- Scanner msgs;
- String description;
- ArrayList<String> messagestoprint = new ArrayList<String>();
- Hashtable<String, String> playerdescriptions = new Hashtable<String, String>();
- Hashtable<String, World> playerlocations = new Hashtable<String, World>();
- ArrayList<String> playernames = new ArrayList<String>();
- Location gotolocation;
- Player gotoplayer;
- int counter = 1;
- int number;
- String numberstring;
- int arraylength;
- //Define File Variables
- static String mainDirectory = "plugins/HelpMeAdvanced"; //sets the main directory for easy reference
- 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.
- // onDisable
- public void onDisable() {
- PluginDescriptionFile pdfFile = this.getDescription();
- System.out.println( pdfFile.getName() + " is disabled!" );
- }
- // onEnable
- public void onEnable() {
- server = this.getServer();
- PluginDescriptionFile pdfFile = this.getDescription();
- System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
- //Setup Files
- new File(mainDirectory).mkdir(); //makes the Zones directory/folder in the plugins directory
- 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 ! turns the whole statement around, checking that the file doesn't exist instead of if it exists.
- try { //try catch clause explained below in tutorial
- messages.createNewFile(); //creates the file zones.dat
- FileWriter writer = new FileWriter(messages,true);
- BufferedWriter out = new BufferedWriter(writer);
- out.write("An Admin or SuperAdmin will come and help you soon as possible.");
- out.newLine();
- out.write("If no one come and helps you within 5 minutes, perform the command again.");
- out.newLine();
- out.write("Message sent to Admins");
- out.close();
- } catch (IOException ex) {
- ex.printStackTrace(); //explained below.
- }
- } else {
- openFile();
- readFile();
- closeFile();
- }
- }
- private void openFile() {
- try{
- msgs = new Scanner(messages);
- }catch (Exception ex){
- System.out.println("[HelpMeAdvanced] Could not find messages.txt, did you delete it?");
- }
- }
- private void readFile() {
- if (!msgs.hasNextLine()){ //Check if there are no lines at all
- server.broadcastMessage("[HelpMeAdvanced] Messages.txt is empty.");
- }else{
- messagestoprint.clear(); //Clear the ArrayList (For using the reload command)
- while (msgs.hasNextLine()){
- String messagetofile = msgs.nextLine(); //Set the next line to a string
- messagetofile = messagetofile.replaceAll("(&([a-f0-9]))", "\u00A7$2"); //Replace colour codes
- messagestoprint.add(messagetofile); //Add to the ArrayList
- }
- }
- }
- private void closeFile() {
- msgs.close();
- }
- public String arrayToString(String[] array) {
- StringBuilder sb = new StringBuilder();
- for (int a = 1; a < array.length; a++) {
- sb.append(array[a]);
- sb.append(" ");
- }
- String newString = sb.toString();
- return newString;
- }
- // Command system
- public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
- Player[] players;
- int total;
- if (sender instanceof Player) {
- Player player = (Player) sender;
- if(commandLabel.equalsIgnoreCase("helpme")) {
- if (args.length == 1)
- {
- variable = args[0];
- if (variable.equalsIgnoreCase("help"))
- {
- player.sendMessage("Typing in \"/helpme will alert Admins that you need help!\"");
- player.sendMessage("Typing in \"/helpme description \"Description here\" (Without quotes) will alert Admins that you need help and add a description");
- player.sendMessage("Typing in \"/helpme reload will reload the messages file\"");
- player.sendMessage("Typing in \"/helpme list will list the players that need help if you are the admin\"");
- 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\"");
- player.sendMessage("Typing in \"/helpme remove \"Number from list here\" (Without quotes) will remove that player from the list\"");
- }
- if (variable.equalsIgnoreCase("description"))
- {
- player.sendMessage(ChatColor.DARK_RED + "Missing description. Please add a one word description.");
- }
- if (variable.equalsIgnoreCase("reload"))
- {
- readFile();
- }
- if (variable.equalsIgnoreCase("list"))
- {
- for (int i=0;i<playernames.size();i++)
- {
- player.sendMessage(ChatColor.GREEN + String.valueOf(counter) + '.' + " " + playernames.get(i).toString() + '-' + " " + playerlocations.get(playernames.get(i).toString()) + " " + '-' + " " + playerdescriptions.get(playernames.get(i).toString()));
- counter++;
- }
- counter = 1;
- }
- if (!variable.equalsIgnoreCase("help") && !variable.equalsIgnoreCase("description") && !variable.equalsIgnoreCase("reload") && !variable.equalsIgnoreCase("list"))
- {
- player.sendMessage(ChatColor.DARK_RED + "Unrecognized argument. Typing in /helpme will alert Admins that you need help!");
- }
- }
- else if (args.length > 1)
- {
- if (variable.equalsIgnoreCase("description"))
- {
- description = arrayToString(args);
- this.getServer().broadcastMessage(description);
- }
- //Add Players to List
- playerlocations.put(player.toString(), player.getLocation().getWorld());
- playerdescriptions.put(player.toString(), description);
- playernames.add(player.toString());
- //Degbug
- //Dispatch Message
- for (int j = 0;j < arraylength;j++)
- {
- player.sendMessage(messagestoprint.get(j));
- }
- players = server.getOnlinePlayers();
- total = players.length;
- int i = 0;
- String worldname = player.getWorld().getName();
- server.broadcastMessage(worldname);
- while (i < total)
- {
- if (players[i].isOp() || players[i].hasPermission("helpmeadvanced.admin")) {
- 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);
- }
- i++;
- }
- if (variable.equalsIgnoreCase("goto"))
- {
- numberstring = arrayToString(args);
- number = Integer.parseInt(numberstring);
- gotoplayer = server.getPlayer(playernames.get(number));
- gotolocation = gotoplayer.getLocation();
- playerlocations.remove(playernames.get(number));
- playerdescriptions.remove(playernames.get(number));
- player.teleport(gotolocation);
- }
- if (variable.equalsIgnoreCase("remove"))
- {
- numberstring = arrayToString(args);
- number = Integer.parseInt(numberstring);
- playerlocations.remove(playernames.get(number));
- }
- }
- else if (args.length == 0)
- {
- //Debug
- arraylength = messagestoprint.size();
- //Add Player to list
- playerlocations.put(player.toString(), player.getLocation().getWorld());
- playerdescriptions.put(player.toString(), " ");
- playernames.add(player.toString());
- for (int j = 0;j < arraylength;j++)
- {
- player.sendMessage(messagestoprint.get(j));
- }
- players = server.getOnlinePlayers();
- total = players.length;
- int i = 0;
- while (i < total)
- {
- if (/*HelpMeAdvanced.permissionHandler.has(players[i], "helpmeadvanced.admin") ||*/ players[i].isOp() || players[i].hasPermission("helpmeadvanced.admin")) {
- 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() + ")");
- }
- i++;
- }
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement