Solved I am receiving an error with my plugin.

Discussion in 'Plugin Development' started by Kody_Jay, Oct 1, 2016.

Thread Status:
Not open for further replies.
  1. I am making an auto broadcast plugin, but I am receiving this error: http://prntscr.com/coo4ga
    I have no idea what I have to do to fix this, or what is causing it. Here is my code.

    Code:
    package me.kody_jay.UniqueBroadcast;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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;
    
    public class UniqueBroadcast extends JavaPlugin {
        public static UniqueBroadcast plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public static int currentLine = 0;
        public static int tid = 0;
        public static int running = 1;
        public static long interval = 60;
      
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now disabled.");
        }
      
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now enabled.");
          
            tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    try {
                        broadcastMessage("plugins/UniqueBroadcast/messages.txt");
                    } catch (IOException e) {
                      
                    }
                }
            }, 0, interval * 20);
        }
      
        public static void broadcastMessage(String fileName) throws IOException {
            FileInputStream fs;
            fs = new FileInputStream(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(fs));
            for(int i = 0; i < currentLine; ++i)
                br.readLine();
            String line = br.readLine();
            line = line.replaceAll("&f", ChatColor.WHITE + "");
            line = line.replaceAll("&e", ChatColor.YELLOW + "");
            line = line.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
            line = line.replaceAll("&c", ChatColor.RED + "");
            line = line.replaceAll("&b", ChatColor.AQUA + "");
            line = line.replaceAll("&a", ChatColor.GREEN + "");
            line = line.replaceAll("&0", ChatColor.BLACK + "");
            line = line.replaceAll("&9", ChatColor.BLUE + "");
            line = line.replaceAll("&8", ChatColor.DARK_GRAY + "");
            line = line.replaceAll("&7", ChatColor.GRAY + "");
            line = line.replaceAll("&6", ChatColor.GOLD + "");
            line = line.replaceAll("&5", ChatColor.DARK_PURPLE + "");
            line = line.replaceAll("&4", ChatColor.DARK_RED + "");
            line = line.replaceAll("&3", ChatColor.DARK_AQUA + "");
            line = line.replaceAll("&2", ChatColor.DARK_GREEN + "");
            line = line.replaceAll("&1", ChatColor.DARK_BLUE + "");
            Bukkit.getServer().broadcastMessage(ChatColor.LIGHT_PURPLE + "[UBroadcast] " + ChatColor.WHITE + line);
            LineNumberReader lnr = new LineNumberReader(new FileReader(new File(fileName)));
            lnr.skip(Long.MAX_VALUE);
            int lastLine = lnr.getLineNumber();
            if(currentLine + 1 == lastLine + 1) {
                currentLine = 0;
            } else {
                currentLine++;
            }
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("stopbroadcast")) {
                if(running == 1) {
                    Bukkit.getServer().getScheduler().cancelTask(tid);
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.LIGHT_PURPLE + "[UBroadcast] " + ChatColor.AQUA + "Cancelled broadcasts.");
                    running = 0;
                } else {
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.LIGHT_PURPLE + "[UBroadcast] " + ChatColor.RED + "Broadcasts were already stopped!");
                  
                }
            } else if (commandLabel.equalsIgnoreCase("startbroadcast")) {
                if(running == 1) {
                    Player player = (Player) sender;
                    player.sendMessage("[UBroadcast] " + ChatColor.RED + "They are still running!");
                } else {
                    tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            try {
                                broadcastMessage("plugins/UniqueBroadcast/messages.txt");
                            } catch (IOException e) {
                              
                            }
                        }
                    }, 0, interval * 20);
                    Player player = (Player) sender;
                    player.sendMessage("[UBroadcast] " + ChatColor.AQUA + "Started broadcasts.");
                    running = 1;
                }
            }
            return false;
        }
      
    }
     
    Last edited by a moderator: Oct 1, 2016
  2. I Al Istannen and mine-care like this.
  3. Offline

    Zombie_Striker

    WHO DARE SUMMON ME!


    @Kody_Jay
    1. You should never have a single class with more than 500 lines. If you do, you are either violating DRY or have too much content for one object
    2. Read this for NPES. You can fix this on your own.
    3. Java Naming conventions: Package names should be lowercase and all variables should start with a lowercase letter.
    4. Don't steal minecraft's logger. Not only do you not need the logger (more later), you are stealing base MC's logger. Even if you needed to log stuff (which you don't) use getLogger() instead
    5. As said before, you don't need to log anything. Bukkit does this for you. Remove all logs from your plugin.
    6. STOP ABUSING STATIC! There is no reason for any of that to be static. Remove all static modifiers.
    7. Because of #4 and #5, remove the onDisabled, since it will do nothing
    8. Don't use commandlabel. That does not support alaiases, Use command.getName instead
    9. Use ChatColor.translateAlternateColorCode() to add chatcolors.
    10. DON'T BLIDNLY CAST SENDER TO A PLAYER! What if the a plugin sends a command? What if it's the console? Those things are not players, so this would throw an CCA. Do an instanceof check before casting.
    11. You never try to cancel the task a 'tid' before creating new tasks. What this means is you can have multiple tasks running at the same time
    12. Don't create the player variable more than once. If you only want players to send the command, do an instanceof check before you even check for the command.
    13. Return false only when something fails. If it worked as it should, return true;
     
    Last edited: Oct 1, 2016
    I Al Istannen and bwfcwalshy like this.
  4. Offline

    Tecno_Wizard

    @bwfcwalshy

    A wild TheBCBroz influenced post is attacking!
    Go, Techno_Wizard!

    (I felt obliged to show @Zombie_Striker up)

    For starters, I can tell you've been watching TheBCBroz. Stop. He sucks.
    I am going to copy and paste my "You don't know Java" post, because I really get tired of writing it.

    This is a little harsh for your case though. You seem to know Java, but you don't know how to use it professionally or properly. Try to read some code style guides. The error you are getting is because your plugin yml is not formatted correctly. Look at the Bukkit docs.

    I should make a really nice narrative about this on my website and just post the link to it @bwfcwalshy

    Beautified code:
    Code:
    package me.kody_jay.UniqueBroadcast;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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;
    
    public class UniqueBroadcast extends JavaPlugin {
        public static UniqueBroadcast plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public static int currentLine = 0;
        public static int tid = 0;
        public static int running = 1;
        public static long interval = 60;
    
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now disabled.");
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now enabled.");
    
            tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    try {
                        broadcastMessage("plugins/UniqueBroadcast/messages.txt");
                    } catch (IOException e) {
    
                    }
                }
            }, 0, interval * 20);
        }
    
        public static void broadcastMessage(String fileName) throws IOException {
            FileInputStream fs;
            fs = new FileInputStream(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(fs));
            for (int i = 0; i < currentLine; ++i)
                br.readLine();
            String line = br.readLine();
            line = line.replaceAll("&f", ChatColor.WHITE + "");
            line = line.replaceAll("&e", ChatColor.YELLOW + "");
            line = line.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
            line = line.replaceAll("&c", ChatColor.RED + "");
            line = line.replaceAll("&b", ChatColor.AQUA + "");
            line = line.replaceAll("&a", ChatColor.GREEN + "");
            line = line.replaceAll("&0", ChatColor.BLACK + "");
            line = line.replaceAll("&9", ChatColor.BLUE + "");
            line = line.replaceAll("&8", ChatColor.DARK_GRAY + "");
            line = line.replaceAll("&7", ChatColor.GRAY + "");
            line = line.replaceAll("&6", ChatColor.GOLD + "");
            line = line.replaceAll("&5", ChatColor.DARK_PURPLE + "");
            line = line.replaceAll("&4", ChatColor.DARK_RED + "");
            line = line.replaceAll("&3", ChatColor.DARK_AQUA + "");
            line = line.replaceAll("&2", ChatColor.DARK_GREEN + "");
            line = line.replaceAll("&1", ChatColor.DARK_BLUE + "");
            Bukkit.getServer().broadcastMessage(ChatColor.LIGHT_PURPLE + "[UBroadcast] " + ChatColor.WHITE + line);
            LineNumberReader lnr = new LineNumberReader(new FileReader(new File(fileName)));
            lnr.skip(Long.MAX_VALUE);
            int lastLine = lnr.getLineNumber();
            if (currentLine + 1 == lastLine + 1) {
                currentLine = 0;
            } else {
                currentLine++;
            }
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("stopbroadcast")) {
                if (running == 1) {
                    Bukkit.getServer().getScheduler().cancelTask(tid);
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.LIGHT_PURPLE + "[UBroadcast] " + ChatColor.AQUA + "Cancelled broadcasts.");
                    running = 0;
                } else {
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.LIGHT_PURPLE + "[UBroadcast] " + ChatColor.RED + "Broadcasts were already stopped!");
    
                }
            } else if (commandLabel.equalsIgnoreCase("startbroadcast")) {
                if (running == 1) {
                    Player player = (Player) sender;
                    player.sendMessage("[UBroadcast] " + ChatColor.RED + "They are still running!");
                } else {
                    tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            try {
                                broadcastMessage("plugins/UniqueBroadcast/messages.txt");
                            } catch (IOException e) {
    
                            }
                        }
                    }, 0, interval * 20);
                    Player player = (Player) sender;
                    player.sendMessage("[UBroadcast] " + ChatColor.AQUA + "Started broadcasts.");
                    running = 1;
                }
            }
            return false;
        }
    
    }
     
    Last edited: Oct 1, 2016
    I Al Istannen and bwfcwalshy like this.
  5. @Tecno_Wizard @Zombie_Striker Thanks guys!

    You should!! Send me the link if/when you do ;)
     
    mine-care likes this.
  6. @bwfcwalshy @Tecno_Wizard @Zombie_Striker

    Thanks for all the advice, I have now used this to my advantage, and I am hoping to get my plugin up and running very soon. I will also do more research about plugin development and will learn from my mistakes. Thanks guys.

    EDIT: I feel like such an idiot but, I capitalized 'name:' in my plugin.yml and it said 'Name:'

    EDIT2: How would I make it, so that when the plugin is first added and the server is restarted, it automatically creates the folder and the messages.txt file?
     
    Last edited: Oct 1, 2016
  7. Offline

    Zombie_Striker

    @Kody_Jay
    Use the method "File#createFile()" to create the messages file. If the directory it is in does not exist, use "File#mkdrs()" for the directory you want to create.
     
  8. @Kody_Jay
    Another thing to mention is this:
    Which is rather unwieldy. Bukkit already has a convenient method for doing this. Just do
    Code:java
    1. line = ChatColor.translateAlternateColorCodes('&', line)
    and you will have saved yourself a lot of work.
     
  9. @AlvinB

    I knew bukkit already had a method for translating color codes, I just forgot how.
     
  10. Offline

    Zombie_Striker

    @Kody_Jay
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page