Why do my commands not work (multiple commands with multiple arguments)

Discussion in 'Plugin Development' started by yewtree8, Feb 26, 2014.

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

    yewtree8

    The commands /effectsigns & /effectsigns help work but not the /effectsigns help speed why?
    Code:java
    1. package me.mat.effectsigns;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.PluginManager;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.fusesource.jansi.Ansi.Color;
    10.  
    11. public class Main extends JavaPlugin {
    12.  
    13.  
    14. String Prefix = ChatColor.GREEN + "[" + ChatColor.AQUA + "EffectSigns" + ChatColor.GREEN + "]";
    15.  
    16.  
    17. public void onEnable() {
    18.  
    19. System.out.println("EffectSigns Enabled");
    20.  
    21. PluginManager pm = getServer().getPluginManager();
    22.  
    23. pm.registerEvents(new PlayerListener(), this);
    24. pm.registerEvents(new SignListener(), this);
    25. pm.registerEvents(new BasicEvents(), this);
    26.  
    27.  
    28. }
    29.  
    30.  
    31. public void onDisable() {
    32.  
    33. System.out.println("EffectSigns Disabled");
    34.  
    35. }
    36.  
    37.  
    38. public boolean onCommand(CommandSender sender, Command command, String name, String[] args) {
    39. Player player = (Player) sender; {
    40. if(command.getName().equalsIgnoreCase("effectsigns")) {
    41. if(args.length == 0) {
    42.  
    43. player.sendMessage(Prefix + ChatColor.DARK_AQUA + " [" + ChatColor.AQUA + "V 0.1" + ChatColor.DARK_AQUA + " ]"
    44. + ChatColor.GREEN +
    45. " This Plugin Was Created by Mat" + ChatColor.DARK_AQUA + " / " + ChatColor.YELLOW + "yewtree8");
    46.  
    47.  
    48. return true;
    49. }
    50. }
    51.  
    52. if(command.getName().equalsIgnoreCase("effectsigns")) {
    53. if(args.length == 1) {
    54. if(args[0].equalsIgnoreCase("help")) {
    55. player.sendMessage(Prefix + ChatColor.GREEN + " Commands:" );
    56. player.sendMessage(Prefix + ChatColor.GOLD + " /EffectSigns "
    57. + ChatColor.LIGHT_PURPLE + "Gives Plugin Info & Version");
    58. player.sendMessage(Prefix + ChatColor.GOLD + "/EffectSigns help" + ChatColor.LIGHT_PURPLE + " Shows This Menu");
    59. }
    60. }
    61.  
    62.  
    63.  
    64.  
    65.  
    66. return true;
    67. }
    68. if(command.getName().equalsIgnoreCase("effectsigns")) {
    69. if(args.length == 1) {
    70. if(args[0].equalsIgnoreCase("help")) {
    71. if(args[1].equalsIgnoreCase("speed")) {
    72. if(player.hasPermission("effectsigns.command.speed")) {
    73. player.sendMessage(Prefix + ChatColor.DARK_RED + "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|");
    74. player.sendMessage(Prefix + ChatColor.DARK_AQUA + " Creating A Speed Sign");
    75. player.sendMessage(Prefix + ChatColor.DARK_RED + "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|");
    76. player.sendMessage(Prefix + ChatColor.GOLD + " [ES]");
    77. player.sendMessage(Prefix + ChatColor.GOLD + "Speed");
    78. player.sendMessage(Prefix + ChatColor.DARK_RED + "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|");
    79. player.sendMessage(Prefix + ChatColor.RED + "You Must Have The Permission " + ChatColor.GREEN + "effectsigns.admin.speed");
    80. } else {
    81. player.sendMessage(Prefix + ChatColor.RED + " You Do Not Have The Permission" + ChatColor.GREEN + "effectsigns.command.speed");
    82. }
    83.  
    84. }
    85. }
    86. }
    87.  
    88.  
    89.  
    90.  
    91. }
    92.  
    93. return true;
    94.  
    95. }
    96.  
    97. }
    98.  
    99. }
    100.  


    Pretty confused
     
  2. Offline

    Brendyn Todd

    yewtree8
    You're checking for too few arguments with (args.length == 1) Remember in programming arrays always start with 0. So change (args.length == 2) and /effectsigns help speed should work.


    Scratch that, you're writing the code wrong. You don't need to create a new if statement for the same command, just check for different args under it.
     
  3. Offline

    nitrousspark

    args.length is always 1 ahead of getting the argument. so /effectsigns help speed is args.length == 2 but to get speed you would do args[1];
     
  4. Offline

    yewtree8

    Brendyn Todd changed it to if(args.length == 2) but still no change?
     
  5. Offline

    Brendyn Todd

    yewtree8
    I posted to soon, I've changed my post. You don't need to create a new if statement for a command to create each set arguments for the same command. You can just start a new (args.length == X) and check it that way.
     
  6. Offline

    yewtree8

    Brendyn Todd can you edit my code then? i think i get what you mean but i'm not sure?
     
  7. Offline

    Brendyn Todd

    Notice how I don't use if(command.getName().equalsIgnoreCase("effectsigns") more than once. All you need to do is add a new if statement for if(args.length == X).
    (May need reformatted, did without IDE)

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command command, String name, String[] args) {
    3. Player player = (Player) sender; {
    4.  
    5. if(command.getName().equalsIgnoreCase("effectsigns")) {
    6. if(args.length == 0) {
    7.  
    8. player.sendMessage(Prefix + ChatColor.DARK_AQUA + " [" + ChatColor.AQUA + "V 0.1" + ChatColor.DARK_AQUA + " ]"
    9. + ChatColor.GREEN +
    10. " This Plugin Was Created by Mat" + ChatColor.DARK_AQUA + " / " + ChatColor.YELLOW + "yewtree8");
    11. }
    12. }
    13.  
    14. if(args.length == 1) {
    15. if(args[0].equalsIgnoreCase("help")) {
    16. player.sendMessage(Prefix + ChatColor.GREEN + " Commands:" );
    17. player.sendMessage(Prefix + ChatColor.GOLD + " /EffectSigns "
    18. + ChatColor.LIGHT_PURPLE + "Gives Plugin Info & Version");
    19. player.sendMessage(Prefix + ChatColor.GOLD + "/EffectSigns help" + ChatColor.LIGHT_PURPLE + " Shows This Menu");
    20. }
    21. }
    22. }
    23.  
    24. if(args.length == 2) {
    25. if(args[0].equalsIgnoreCase("help")) {
    26. if(args[1].equalsIgnoreCase("speed")) {
    27. if(player.hasPermission("effectsigns.command.speed")) {
    28. player.sendMessage(Prefix + ChatColor.DARK_RED + "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|");
    29. player.sendMessage(Prefix + ChatColor.DARK_AQUA + " Creating A Speed Sign");
    30. player.sendMessage(Prefix + ChatColor.DARK_RED + "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|");
    31. player.sendMessage(Prefix + ChatColor.GOLD + " [ES]");
    32. player.sendMessage(Prefix + ChatColor.GOLD + "Speed");
    33. player.sendMessage(Prefix + ChatColor.DARK_RED + "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|");
    34. player.sendMessage(Prefix + ChatColor.RED + "You Must Have The Permission " + ChatColor.GREEN + "effectsigns.admin.speed");
    35. } else {
    36. player.sendMessage(Prefix + ChatColor.RED + " You Do Not Have The Permission" + ChatColor.GREEN + "effectsigns.command.speed");
    37. }
    38.  
    39. }
    40. }
    41. }
    42. return true;
    43. }
    44.  
    45. }
     
    yewtree8 likes this.
  8. Offline

    yewtree8

  9. Offline

    Brendyn Todd

    yewtree8
    Guess it worked then, make sure to set this thread to solved so others don't waste their time.
     
Thread Status:
Not open for further replies.

Share This Page