Cannot figure out args..

Discussion in 'Plugin Development' started by HeadGam3z, Feb 17, 2014.

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

    HeadGam3z

    Alright so, I had my plugin lookup a .txt file upon /lookup <name> and it worked perfectly. But I wanted to change it to /whatever lookup <name> instead. So I tried to tweak it around and such, but it cannot seem to work anymore. I removed the less important stuff as that's not needed here, but this is my main problem that I cannot get the args to work correctly..
    What am I doing wrong? Here's the code:
    Code:java
    1. String prefix = getConfig().getString("prefix").replaceAll("&", "§")
    2. + " ";
    3. String errorMessage = prefix + ChatColor.DARK_RED
    4. + "Error: Illegal arguments.";
    5.  
    6. if (cmd.getName().equalsIgnoreCase("test")) {
    7. if (args.length == 1) {
    8. if (args[0].equalsIgnoreCase("help")) {
    9. if (!sender.hasPermission("test.help")) {
    10. sender.sendMessage("Blah");
    11. return true;
    12. }
    13. if (sender.hasPermission("test.help")) {
    14. sender.sendMessage(prefix + "Beta");
    15. return true;
    16. }
    17. } else if (args[0].equalsIgnoreCase("lookup")) {
    18. if (!sender.hasPermission("test.lookup")) {
    19. sender.sendMessage(prefix + ChatColor.DARK_RED
    20. + "Error: You do not have the " + ChatColor.RED
    21. + "pavilion.lookup " + ChatColor.DARK_RED
    22. + "permission.");
    23. return true;
    24. }
    25. if (sender.hasPermission("test.lookup")) {
    26. if (!(args.length == 2)) {
    27. sender.sendMessage(errorMessage);
    28. return true;
    29. }
    30. if (args.length == 2) {
    31. sender.sendMessage("Test");
    32. return true;
    33. }
    34. }
    35. }
    36. }
    37. }


    What's happening is, if I do /test lookup without <name>, it sends the errorMessage (good), but when I do /test lookup with the <name> (/test lookup name), it doesn't send the "Test" message (bad). Would anyone happen to know why?
     
  2. Offline

    adam753

    You're checking if args.length == 1 near the start, so the part at the end where you're checking whether or not it's 2 is pointless because it's always going to be 1 there.
     
  3. Offline

    HeadGam3z

    adam753
    Well.. I deleted the args.length == 1 as a test before I made this post, and it still made no change whatsoever then or now.

    Anyone have any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    random_username


    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("test")){
    3. if(args.length == 2){
    4. if(args[0].equalsIgnoreCase("lookup")){
    5. //name would be the <name> argument in /test lookup <name>
    6. String name = args[1];
    7. //Do WhatEver with the name.
    8. }
    9. }
    10. }
    11. return false;
    12. }

    That'd be /test lookup name
     
  5. Offline

    HeadGam3z

    random_username
    Yes, I know that. But I don't understand why my code above wouldn't work. Would you happen to know why by any chance?
     
    random_username likes this.
  6. Offline

    random_username

    You're checking if args length is 2 after you check if the length is 1, insied the same if. If you make the command with 2 args, the code in the if(args.length == 1) won't be executed, as that isn't the args length.
     
  7. Offline

    HeadGam3z

    random_username
     
  8. Offline

    mazentheamazin

    HeadGam3z
    Please try removing the return true lines.
     
  9. HeadGam3z
    try:
    Code:java
    1. String prefix = getConfig().getString("prefix").replaceAll("&", "§")
    2. + " ";
    3. String errorMessage = prefix + ChatColor.DARK_RED
    4. + "Error: Illegal arguments.";
    5.  
    6. if (cmd.getName().equalsIgnoreCase("test")) {
    7. if (args.length > 0) {
    8. if (args[0].equalsIgnoreCase("help")) {
    9. if (!sender.hasPermission("test.help")) {
    10. sender.sendMessage("Blah");
    11. return true;
    12. }
    13. if (sender.hasPermission("test.help")) {
    14. sender.sendMessage(prefix + "Beta");
    15. return true;
    16. }
    17. } else if (args[0].equalsIgnoreCase("lookup")) {
    18. if (!sender.hasPermission("test.lookup")) {
    19. sender.sendMessage(prefix + ChatColor.DARK_RED
    20. + "Error: You do not have the " + ChatColor.RED
    21. + "pavilion.lookup " + ChatColor.DARK_RED
    22. + "permission.");
    23. return true;
    24. }
    25. if (sender.hasPermission("test.lookup")) {
    26. if (args.length != 2) {
    27. sender.sendMessage(errorMessage);
    28. return true;
    29. }
    30. if (args.length == 2) {
    31. sender.sendMessage("Test");
    32. return true;
    33. }
    34. }
    35. }
    36. }else{
    37. //send player a usage message!
    38. }
    39.  
    40. }
     
Thread Status:
Not open for further replies.

Share This Page