1.15.2 Command args not working

Discussion in 'Spigot Plugin Development' started by Phoenix852, Mar 9, 2020.

  1. I have recently made a plugin called RandomMOTD and now I want to add some cool commands with args. I did made args correctly when I was learning to make plugins but now what the problem is I can't figure it out.
    This is my code for command (I put it in my main class)
    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("randommotd")) {
                if (args != null) {
                    if (args.length == 1) {
                        if(args[0] == "motd"){
                            List<String> ingamemotdlist = getConfig().getStringList("in-game.motd");
                            String ingamemotd;
                            sender.sendMessage(ChatColor.AQUA + "in-game MOTDs: ");
                            for (String loopingame : ingamemotdlist) {
                                sender.sendMessage("- " + loopingame);
                            }
                            String systemmotd;
                            sender.sendMessage(ChatColor.AQUA + "System MOTDs: ");
                            List<String> systemmotdlist = getConfig().getStringList("system.motd");
                            for (String loopsystem : systemmotdlist) {
                                sender.sendMessage("- " + loopsystem);
                            }
                            return true;
                        }
                        if(args[0] == "reload") {
                            reloadConfig();
                            sender.sendMessage("[RandomMOTD] Config reloaded!");
                            return true;
                        }
                        if(args[0] == "version") {
                            sender.sendMessage("RandomMOTD v" + ChatColor.GREEN + getDescription().getVersion());
                            return true;
                        }
                    }
                }
            }
            return true;
        }
    and when I do /random <any args> it does nothing
     
  2. SteelPhoenix

    Moderator

    Comparing == only checks if it is the same instance for reference types
    Code (Java):
    String s1 = "abc";
    String s2 = "abc";
    String s3 = s1;

    s1 == s2 // false
    s1 == s3 // true
    Use #equals(Object) (or for Strings you can also use String#equalsIgnoreCase(String) for case insensitive comparison)
     
    • Informative Informative x 1
  3. The code and output example provided is not necessarily correct.
    Take a look at Java String Pool
     
    • Informative Informative x 1
  4. Also args can't be null, if no args are supplied itll just be an empty list of strings, in which case check for

    Code (Java):
    args.length() == 0
     
  5. Should i remove the if(args !=null)?
     
  6. Yes, args will never be null
     
    • Agree Agree x 1
  7. SteelPhoenix

    Moderator

    It was an example of reference type comparison and my point is still valid. Since you want to get political here is a non-string equivalient:
    Code (Java):
    Object o1 = new Object();
    Object o2 = new Object();
    Object o3 = o1;

    o1 == o2 // false
    o1 == o3 // true
     
    • Informative Informative x 1
  8. I removed that code but it still doesn't work.
     
  9. That wasn't a fix, it was a suggestion to improve readability I guess.

    Have you debugged? Print messages here and there to see where code is going. Have you added your command in your plugin.yml?
     
  10. SteelPhoenix

    Moderator

    Or have you seen my comment pointing out your mistake @OP?
     
    • Agree Agree x 2
  11. Strahan

    Benefactor

    Also why are you declaring variables that you never use? ingamemotd and systemmotd are just there for looks, lol. If your class is only handling the /randommotd command there is no need to check cmd.getName() as it will only ever be that.

    Lastly I'd also suggest a switch instead of a bunch of if statements. Switch allows for handling a non-match case so you can throw help messages. Like:
    Code (Text):
    switch(args[0].toLowerCase()) {
    case "motd":
      // do stuff
      break;

    case "reload":
      // do stuff
      break;

    case "version":
      // do stuff
      break;

    default:
      // send help message to player as they used the wrong arg
      break;
    }
     
    • Informative Informative x 1
  12. Yes i did