Commands issues :S

Discussion in 'Plugin Development' started by arnie231, Jun 12, 2012.

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

    arnie231

    Hey guys I'm having an issue when if I type "/sh2class check" it returns the players Groups
    but if I type "/sh2class check adv" it will return the same as doing "/sh2class check" :S

    Can someone point out my mistake please

    Code:java
    1. package com.arnie.sh2.Commands.Ranks;
    2.  
    3. import com.arnie.sh2.Main;
    4. import com.arnie.sh2.Util.Chat;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11.  
    12. public class CmdClasses implements CommandExecutor
    13. {
    14.  
    15. @Override
    16. public boolean onCommand(CommandSender s, Command c, String l, String[] args)
    17. {
    18. if (l.equalsIgnoreCase("SH2Class")) {
    19. if (args.length < 1) return false;
    20. if (!(s instanceof Player)) return false;
    21. if (!Main.permissionCheck((Player)s, "Stronghold2.User")) {
    22. Chat.noPermissionMessage((Player)s);
    23. return true;
    24. }
    25.  
    26. if (args[0].equalsIgnoreCase("?") || (args[0].equalsIgnoreCase("help"))){
    27.  
    28. ((Player)s).sendMessage(ChatColor.GRAY + "~~~~~~" + ChatColor.GOLD + "[Stronghold 2]"+ " " + ChatColor.DARK_AQUA + "Classes Page:" + " " + ChatColor.AQUA + "1" + ChatColor.GRAY + "~~~~~~");
    29. ((Player)s).sendMessage(ChatColor.GOLD + "Use /SH2Class ? - for help.");
    30. ((Player)s).sendMessage(ChatColor.GOLD + "Use /SH2Class Check - Check what Class Level you are.");
    31. ((Player)s).sendMessage(ChatColor.GOLD + "Use /SH2Class Check Adv - Get Advanced info about your class/s.");
    32. }
    33.  
    34. if (args[0].equalsIgnoreCase("Check")){
    35.  
    36. String[] user = Main.permission.getPlayerGroups((Player)s);
    37. ((Player)s).sendMessage(ChatColor.GOLD + "[Stronghold 2]"+ " " + ChatColor.RED + "You current Class");
    38. for (String rank: user){
    39. ((Player)s).sendMessage(ChatColor.GREEN + rank);
    40. }
    41. } else {
    42. if (args[0].equalsIgnoreCase("Check") || (args[1].equalsIgnoreCase("Adv"))){
    43.  
    44. ((Player)s).sendMessage(ChatColor.GRAY + "~~~~~~" + ChatColor.GOLD + "[Stronghold 2]"+ " " + ChatColor.DARK_AQUA + "Classes Page:" + " " + ChatColor.AQUA + "2" + ChatColor.GRAY + "~~~~~~");
    45. ((Player)s).sendMessage(ChatColor.GOLD + "Remember [YourClass] = WoodMiner for Example do not add the Faction tag at the start");
    46. ((Player)s).sendMessage(ChatColor.GOLD + "Use /SH2Class [YourClass] Use - Check what your class can use.");
    47. ((Player)s).sendMessage(ChatColor.GOLD + "Use /SH2Class [YourClass] Break - Check what you can Break.");
    48. ((Player)s).sendMessage(ChatColor.GOLD + "Use /SH2Class [YourClass] Place - Check what you can Place.");
    49. ((Player)s).sendMessage(ChatColor.GOLD + "Use /SH2Class [YourClass] Next - Check what level you need to rank up next.");
    50. }
    51. return true;
    52. }
    53. }
    54. return true;
    55. }
    56. }
    57.  
     
  2. Offline

    dxwarlock

    you need to have
    (args[0].equalsIgnoreCase("Check") && (args[1].equalsIgnoreCase("Adv")))

    you are doing an "or" so its saying if the second word is Check OR Adv
     
  3. Offline

    arnie231

    I changed it to what you said and it still returns the /sh2class check even tho ido /sh2class check adv
     
  4. Offline

    dxwarlock

    ohh wait, just noticed..your not doing an args.length == 0

    on the first one, so it goes down the list, and sees:
    if (args[0].equalsIgnoreCase("Check")){

    and does "yep arg[0] is check"..and does that because it doesnt care whats after that, its the first thing it sees as 'true'...you need

    if (args[0].equalsIgnoreCase("Check") && args.length == 0){
     
    CarlosArias604 likes this.
  5. Offline

    arnie231

    Thanks it works but i had to do

    if (args[0].equalsIgnoreCase("Check") && (args.length == 1)){
     
  6. Offline

    arnie231

  7. Do it the other way around, otherwise it'll throw you an error, indexoutofbounds, when the length is 0, because you're checking it after accessing it.
     
  8. Offline

    Ne0nx3r0

    This is just nick-picking, but you don't need the extra parenthesis.

    Additionally, unless you only want this to trigger when the command has exactly one argument:
    Code:java
    1.  
    2. if(args.length > 0 && args[0].equalsIgnoreCase("Check")){
    3.  
    4. }
    5.  

    Personal taste, but I prefer to allow extra parameters and just ignore them when not needed.
     
  9. Offline

    arnie231

    Thanks guys this helps a lot
     
Thread Status:
Not open for further replies.

Share This Page