Plugin Code Cleanup

Discussion in 'Plugin Development' started by ShadowRanger, Nov 30, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    ShadowRanger

    Hey everyone, i'm almost finished my plugin but it's a bit messy and whenever I try and clean it up it doesn't work. It's basically a plugin with a few commands I made that I wanted to combine to one plugin. Could someone please fix it up a bit for me? Thanks in advance.

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                if (command.getName().equalsIgnoreCase("yell")) {
                Player p = (Player) sender;
                String pname = p.getName();
                if (args.length < 1) {
                    sender.sendMessage(ChatColor.RED + "Usage: /yell <message>");
                    return false;
                }
                if (args.length > 0) {
                    String msg = "";
                    for (String x: args){
                            msg += x+" ";
                    }
                    Bukkit.broadcastMessage(ChatColor.RED + "[Yell] " + pname + ChatColor.WHITE + ": " + msg);
        }
                if (command.getName().equalsIgnoreCase("trademsg")) {
                if (args.length < 1) {
                    sender.sendMessage(ChatColor.RED + "Usage: /trademsg <message>");
                    return false;
                }
                if (args.length > 0) {
                    String msg = "";
                    for (String x: args){
                            msg += x+" ";
                    }
                    Bukkit.broadcastMessage(ChatColor.RED + "[TradeMsg] " + pname + ChatColor.WHITE + ": " + msg);
                    return true;
        }
                if (command.getName().equalsIgnoreCase("number")) {
                    Random rand = new Random();
                    int randomNumber = rand.nextInt(100);
         
                        Bukkit.broadcastMessage(ChatColor.RED + "[Number] " + pname + ": " + Integer.toString(randomNumber));
         
                        return true;
                }
                return false;
            }
        }
                return false;
        }
    }
    
    Almost 50 views? Come onnn, please help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    Drew1080

    Phoenix_Mawson1

    By cleanup do you mean have it space correctly?

    If so:

    If you are using eclipse:

    1st: Select all the source code (Ctrl + A)

    Then use Ctrl + I and it should space it out properly again

    If you are on a mac just use the command key instead of the control key.
     
  3. Offline

    Bryguy

    Honestly there are only two things that I can see that might help: indentation and spacing.

    Code:
     @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                if (command.getName().equalsIgnoreCase("yell")) {
                          Player p = (Player) sender;
                          String pname = p.getName();
     
                          if (args.length < 1) {
                                    sender.sendMessage(ChatColor.RED + "Usage: /yell <message>");
                                    return false;
                          }
       
                          if (args.length > 0) {
                                  String msg = "";
             
                                  for (String x: args){
                                            msg += x+" ";
                          }
     
                          Bukkit.broadcastMessage(ChatColor.RED + "[Yell] " + pname + ChatColor.WHITE + ": " + msg);
                }
     
                if (command.getName().equalsIgnoreCase("trademsg")) {
         
                          if (args.length < 1) {
                                sender.sendMessage(ChatColor.RED + "Usage: /trademsg <message>");
                                return false;
                          }
     
                          if (args.length > 0) {
                                String msg = "";
                     
                                for (String x: args) {
                                            msg += x+" ";
                          }
             
                          Bukkit.broadcastMessage(ChatColor.RED + "[TradeMsg] " + pname + ChatColor.WHITE + ": " + msg);
                     
                          return true;
            }
       
            if (command.getName().equalsIgnoreCase("number")) {
                        Random rand = new Random();
                        int randomNumber = rand.nextInt(100);
     
                        Bukkit.broadcastMessage(ChatColor.RED + "[Number] " + pname + ": " + Integer.toString(randomNumber));
     
                        return true;
                }
     
                return false;
        /*    }                              //Seems unnecessary?
        }
                return false;*/
        }
    }
    General housekeeping rule:
    Leave an open line between lines with variables and lines with loops/conditionals/method calls.
    Leave an open line between the end of one conditional and the start of another, unless it's an if... else... type conditional (Or similar)
    Check the number of closing brackets you have. For every one opening bracket there should only be one closing bracket.
    Keep areas within brackets "lined up". Indent after each opening bracket and make sure the contents within "line up", unless in another bracketed section (In which case indent more and line up there. Hard to explain, sorry. Bit tired, might've explained it poorly)

    Does that help at all?

    On a side note, you might want a check to insure that the caller of the command is a player. Commands can be called via console, meaning right now you setting it first to a player instance might not always be accurate.
     
  4. Offline

    ShadowRanger

    Thanks for the help and I removed the parts that were not needed but now i'm getting a red underlined error on the very last '}' and on the '{' below my onDisable part. How can I fix this?
     
  5. Offline

    Cammy_the_block

    add or remove some '{' or '}' somewhere
     
  6. Offline

    Bryguy

    Could be I miscounted and you do need one more }. That or one less. I'd try out the former and then the latter. Experimentation is how I do most things, eventually ending up with the answer I was looking for.
     
  7. Offline

    ShadowRanger

    Here's the code so far, can anyone decide what I should add/remove to fix the red underlined errors on the very last '}' and on the '{' below my onDisable part?

    Code:
        @Override
        public void onDisable(){
            getLogger().info("PhoenixCommands has been disabled!");
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("yell")) {
                Player p = (Player) sender;
                String pname = p.getName();
                if (args.length < 1) {
                    sender.sendMessage(ChatColor.BLUE + "Usage: /yell <message>");
                    return false;
                }
                if (args.length > 0) {
                    String msg = "";
                    for (String x: args){
                        msg += x+" ";
                    }
                    Bukkit.broadcastMessage(ChatColor.BLUE + "[Yell] " + pname + ChatColor.WHITE + ": " + msg);
                    return true;
                }
                if (command.getName().equalsIgnoreCase("trademsg")) {
                    if (args.length < 1) {
                        sender.sendMessage(ChatColor.BLUE + "Usage: /trademsg <message>");
                        return false;
                    }
                    if (args.length > 0) {
                        String msg = "";
                        for (String x: args){
                            msg += x+" ";
                        }
                        Bukkit.broadcastMessage(ChatColor.BLUE + "[TradeMsg] " + pname + ChatColor.WHITE + ": " + msg);
                        return true;
                    }
                    if (command.getName().equalsIgnoreCase("Number")) {
                        Random rand = new Random();
                        int randomNumber = rand.nextInt(100);
     
                        Bukkit.broadcastMessage(ChatColor.BLUE + "[Number] " + pname + ": " + Integer.toString(randomNumber));
     
                        return true;
                    }
                    return false;
                }
            }
     
  8. Offline

    Bryguy

    Try adding a } to the very end of the onCommand method... looks like there might've been one that was needed there that I missed. Not positive, experiment.

    EDIT: Just inserted it into Eclipse, sure enough needs one last } to end the method. My apologies, I miscounted originally.
     
  9. Offline

    Cammy_the_block

    Can you send us the whole class file?
    Thanks

    EDIT: Maybe not needed?
     
  10. Offline

    ShadowRanger

    I added one '}' to the very end of the code, it fixed the other errors but now that last one I added is underlined in red?
     
  11. There's still one missing. Look at the spacing, it shows you exactly where. That's why you shouldn't mess up spacing...

    //EDIT: What I wrote hardly depends on your spacing to be correct which I didn't check through.
     
Thread Status:
Not open for further replies.

Share This Page