"Unknown command" - Plugin commands not working

Discussion in 'Plugin Development' started by Firemx, Jul 1, 2013.

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

    Firemx

    My plugin is not recognising any commands I put into the server; it simply emits the "Unknown Command" text in chat.

    I have attempted to use a CommandExecutor but I have stopped due to:
    A: I don't quite understand how they are implemented into my plugins.
    B: It crashes whenever I call on an executor in my main class file.

    I will post the error log I get with CommandExecutors tomorrow morning.
    Thanks for the help, Firemx.

    Main class:
    Code:java
    1. package invincibility;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.player.PlayerMoveEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class AfkInvincibility extends JavaPlugin
    13. {
    14. boolean Wait = true;
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16. private void interrupt() {
    17. }
    18.  
    19. public void onEnable()
    20. {
    21. logger.info("AFK INVINCIBILITY ENABLED");
    22. getConfig().options().copyDefaults();
    23. saveConfig();
    24. }
    25.  
    26. public void onDisable()
    27. {
    28. logger.info("AFK INVINCIBILITY DISABLED");
    29. }
    30. public void onPlayerMove (PlayerMoveEvent evt) {
    31. if (Wait = true){
    32. interrupt();
    33. Wait = false;
    34. }
    35. }
    36. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String label, String[] args)
    37. {
    38. if(cmd.getName().equalsIgnoreCase("AFKI"))
    39. {
    40. if (args.length > 1)
    41. {
    42. sender.sendMessage(ChatColor.RED + "Incorrect command syntax");
    43. }
    44. else
    45. {
    46. if(args.length == 0)
    47. {
    48. sender.sendMessage(ChatColor.BLUE + "AFK Invincibility disabled.");
    49. Player player = (Player) sender;
    50.  
    51. player.setWalkSpeed(1);
    52. player.setFlying(false);
    53. player.setNoDamageTicks(0);
    54. }
    55. else
    56. {
    57. if(args[0].equalsIgnoreCase("on"))
    58. {
    59. if(!(sender instanceof Player))
    60. {
    61. sender.sendMessage(ChatColor.RED + "This command can only be used by players.");
    62. }
    63. else
    64. {
    65. int WaitTime;
    66. WaitTime = getConfig().getInt("Wait Time:", 30);
    67. sender.sendMessage(ChatColor.BLUE + "AFK wait timer activated, moving will cause the timer to reset. (" + WaitTime + ")");
    68. Wait = true;
    69. try {
    70. Thread.sleep(WaitTime);
    71. } catch(InterruptedException ex) {
    72. Thread.currentThread().interrupt();
    73. }
    74. Wait = false;
    75. sender.sendMessage(ChatColor.BLUE + "AFK Invincibility activated, type /awayfromkeyboard to reset status.");
    76. Player player = (Player) sender;
    77.  
    78. player.setFlying(true);
    79. player.setWalkSpeed(0);
    80. player.setNoDamageTicks(-1);
    81. }
    82. }
    83. else
    84. {
    85. sender.sendMessage(ChatColor.RED + "Unknown Arguement");
    86. }
    87. }
    88. return true;
    89.  
    90. }
    91.  
    92. return false;
    93. }
    94. return false;
    95. }
    96. }

    Plugin.yml:
    Code:
    name: AFK Invincibility
    main: invincibility.AfkInvincibility
    version: 1.0
    Description: Provides an AFK Invincibility command.
    Commands:
      AFKI:
        description: Disables AFK Invincibility.
        usage: /AFKI
        permission: AFKInvincibility.AFKI
        permission-message: You don't have permission to access this command.
      AFKI on:
        description: Enables counter towards AFK Invincibility.
        usage: /AFKI on
        permission: AFKInvincibility.AFKI.on
        permission-message: You don't have permission to access this command.
     
  2. Offline

    pope_on_dope

    When you run it without the command executors functions do you get an error?
     
  3. Offline

    MP5K

    Hello Firemx,
    you cant have an space in a command name "AFKI on".
     
  4. Offline

    Josh014

    Make sure you putted the command in the plugin.yml
     
  5. Offline

    Hatchcatch020

    in the onEnable() don't you need to define getCommand("command here").setExecutor(this);
     
  6. on the check if the first argument is on, you didn't check if there was typed a first argument
     
  7. Offline

    Firemx

    I infact, have.

    Code:java
    1. if (args.length > 1)
    2. {
    3. sender.sendMessage(ChatColor.RED + "Incorrect command syntax");
    4. }
    5. else
    6. {
    7. if(args.length == 0)
    8. {
    9. sender.sendMessage(ChatColor.BLUE + "AFK Invincibility disabled.");
    10. Player player = (Player) sender;
    11.  
    12. player.setWalkSpeed(1);
    13. player.setFlying(false);
    14. player.setNoDamageTicks(0);
    15. }
    16. else
    17. {
    18. if(args[0].equalsIgnoreCase("on"))



    The server gives me an error when I do that.


    It's in there. ^_^


    This isn't the problem for now, but i'll definitely keep this in mind.


    The very opposite.
     
  8. Offline

    morha13

    type: /pl
    check if the plugin is on the list.
    if dont have error try in plugin.yml to change that:
    from:
    Code:
    Commands:
      AFKI:
    
    to:
    Code:
    commands:
      AFKI:
    
    maybe the capital C is the reson..
     
  9. Offline

    Firemx

    The plugin is on the list, I'll check the C now. :)

    Edit: It worked, but now it's just sending back: "/AFKI" whenever I attempt a command in my code. (/AFKI and /AFKI on)
     
  10. Offline

    Chinwe


    That's what I thought, as well as needing to getCommand("cmd").setExecutor(this) ?
    What error do you get when you register the command like this in onEnable()?
     
  11. Offline

    Firemx

    Give me a second, I will check. (This post approval thing is getting annoying, when does it shut off?)

    Edit: Without any reference to an external class nothing changes.

    getCommand("AFKI").setExecutor(this);
     
  12. Offline

    morha13

    try to change in plugin.yml the usage of the command...
    the usage of the command its the same as the command so maybe its sending you the usage...
    if its actually sending you the usage so thats says that its think that you write the command with now enogh arguments...
     
  13. Offline

    Firemx

    I know for certain the usage part of the Plugin.yml isn't used by Bukkit.

    I've tested out some possible solutions, none fixed it...

    The code is still displaying "/AFKI" whenever the command is used.

    New Code:
    Code:java
    1. package invincibility;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.player.PlayerMoveEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class AfkInvincibility extends JavaPlugin
    13. {
    14. boolean Wait = true;
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16. private void interrupt() {
    17. }
    18.  
    19. public void onEnable()
    20. {
    21. logger.info("AFK INVINCIBILITY ENABLED");
    22. getConfig().options().copyDefaults();
    23. saveConfig();
    24. getCommand("afki").setExecutor(this);
    25. }
    26.  
    27. public void onDisable()
    28. {
    29. logger.info("AFK INVINCIBILITY DISABLED");
    30. }
    31. @EventHandler
    32. public void onPlayerMove (PlayerMoveEvent evt) {
    33. if (Wait = true){
    34. interrupt();
    35. Wait = false;
    36. }
    37. }
    38. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String label, String[] args)
    39. {
    40. if(cmd.getName().equalsIgnoreCase("afki"))
    41. {
    42. if (args.length > 1)
    43. {
    44. sender.sendMessage(ChatColor.RED + "Incorrect command syntax");
    45. }
    46. else
    47. {
    48. if(args.length == 0)
    49. {
    50. sender.sendMessage(ChatColor.BLUE + "AFK Invincibility disabled.");
    51. Player player = (Player) sender;
    52.  
    53. player.setWalkSpeed(1);
    54. player.setFlying(false);
    55. player.setNoDamageTicks(0);
    56. }
    57. else
    58. {
    59. if(args[0].equalsIgnoreCase("on"))
    60. {
    61. if(!(sender instanceof Player))
    62. {
    63. sender.sendMessage(ChatColor.RED + "This command can only be used by players.");
    64. }
    65. else
    66. {
    67. int WaitTime;
    68. WaitTime = getConfig().getInt("Wait Time:", 30);
    69. sender.sendMessage(ChatColor.BLUE + "AFK wait timer activated, moving will cause the timer to reset. (" + WaitTime + ")");
    70. Wait = true;
    71. try {
    72. Thread.sleep(WaitTime);
    73. } catch(InterruptedException ex) {
    74. Thread.currentThread().interrupt();
    75. }
    76. Wait = false;
    77. sender.sendMessage(ChatColor.BLUE + "AFK Invincibility activated, type /awayfromkeyboard to reset status.");
    78. Player player = (Player) sender;
    79.  
    80. player.setFlying(true);
    81. player.setWalkSpeed(0);
    82. player.setNoDamageTicks(-1);
    83. }
    84. }
    85. else
    86. {
    87. sender.sendMessage(ChatColor.RED + "Unknown Arguement");
    88. }
    89. }
    90. return true;
    91.  
    92. }
    93.  
    94. return false;
    95. }
    96. return false;
    97. }
    98. }


    Plugin.yml:
    Code:
    name: AFK Invincibility
    main: invincibility.AfkInvincibility
    version: 1.0
    Description: Provides an AFK Invincibility command.
    commands:
      afki:
        description: Disables AFK Invincibility.
        usage: /AFKI
        permission: AFKInvincibility.AFKI
        permission-message: You don't have permission to access this command.
    #  on:
    #    description: Enables counter towards AFK Invincibility.
    #    usage: /AFKI on
    #    permission: AFKInvincibility.AFKI.on
    #    permission-message: You don't have permission to access this command.
    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  14. Firemx no you didn't, you only checked if the arguments where 0, or bigger than 1
     
  15. Offline

    Firemx

    I don't need to check.

    If it's not 0 and it's not anything higher than 1 it's obviously 1.
     
  16. You're class with the "onCommand" needs to implement "CommandExecutor" like this:
    Code:java
    1. public class AfkInvincibility extends JavaPlugin implements CommandExecutor


    Also, if you set the CommandExecutor, you do NOT need to check if cmd equals "AFKI"
    Additionally, don't let your thread sleep. This won't allow other players to use the command. (You could make a new thread so)

    It would be much better to have a HashMap with all AFK-Players.
    Then, start with Bukkit a "repeating task" that ticks every second (you can define how often it should ticks)
    In that, you can decrease a players "cooldown".
    And add players to that hashmap when they type "/afki"

    (Sry if I missunderstood something in your code.)

    Just ask if you want to know something :)

    EDIT:
    To check if a player moves or something you need to use listeners. So if a player does something and he is in the hashmap, reset his timer.
     
  17. Offline

    AmShaegar

    JavaPlugin already implements CommandExecutor. No need to add it manually. Also, you do not need to add it as CommandExecutor. The onCommand() method of the main class is called for every command. That's why you need to check the command label.

    Thread.sleep(WaitTime); causes the whole server to wait. Don't use it.

    The only reason, you did not note this yet is, that your wait time is 30 milliseconds.

    getConfig().getInt("Wait Time:", 30);
    "Wait Time:" is no valid config node. Use something like "settings.waittime" for:
    Code:
    settings:
      waittime: 30
    Do not use spaces!

    Your main problem:
    Code:
        usage: /AFKI
    Whnever onCommand returns false, bukkit prints the "usage" message because return false indicates, that the player failed to use the command properly.

    Code:
        @EventHandler
        public void onPlayerMove (PlayerMoveEvent evt) {
    Will never be fired. You have to make a Listener and register it.
    http://wiki.bukkit.org/Event_API_Reference
     
  18. Oh I'm sorry. Thank you AmShaegar!
     
  19. Offline

    Firemx

    I know about the hashmaps, though I have yet to fork that code over to them. (Thanks to everyone who did mention it though. :D)

    So why would bukkit be returning false in the first place?
     
  20. Offline

    AmShaegar

    Why does your onCommand have 2 String parameters? The overridden onCommand looks like this:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
     
  21. Offline

    Firemx

    ughh..... *hides* First plugin? :3

    Either way, I still can't get it to not return false. :p
     
  22. Offline

    AmShaegar

    could you repost your current code?
     
  23. Offline

    Firemx

    It's still the same for now, i'm not at my computer. :p
     
  24. Offline

    AmShaegar

    Oh. If you use the correct onCommands method your error should be solved.
     
  25. Offline

    Firemx

    Wait, what do you mean? What exact part of the method is incorrect?

    Sorry for being a bit of a nub. :p
     
  26. Offline

    AmShaegar

    compare:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String label, String[] args)
    
    In other words: Your method(2nd one) will never be called. So internally you will always get false. This is because your second String label parameter prevents onCommand from being overridden rather than just being added.
     
  27. Offline

    Firemx

    It appears you were right.

    Now to fix my other countless problems with my code...

    Could you possibly explain Hashmaps to me? I get the general concept and how to use them for basic operations but how would I make a counter inside their Hash's? :p
     
  28. Offline

    AmShaegar

    Do not make a counter. Save timestamps. A basic use of HashMaps would look like this:

    Code:java
    1. HashMap<String, Long> startTimes = new HashMap<String, Long>;
    2.  
    3. // Check if player is in the timeframe
    4. long start = startTimes.get(player.getName()),
    5. end = start + duration; // duration in ms. E.g. 30*1000 = 30 seconds
    6. if(end < System.currentTimeMillis()) { // means if current time is after the end
    7. // do something that starts the countdown
    8. startTimes.put(player.getName(), System.currentTimeMillis());
    9. } else {
    10. long timeToWait = end - System.currentTimeMillis(); // in ms
    11. // print time to wait
    12. }
    13.  


    This is kind of a cooldown system. You could also use it to start a reapeating task that checks all times every second to do something if time is up.
     
  29. Offline

    Firemx

    Also, how would I call on Player.getName() outside of an onCommand method?

    It appears to need the variable "Player" which I have specified inside onCommand due to its dependency on the "Sender" arg of the onCommand method.
     
  30. Offline

    AmShaegar

    Where outside?

    In another method: Pass th player reference to that method.
    Code:
    public void method(Player player)
    In a countdown where you loop throug all entries:
    Code:java
    1. for(Entry<String, Long> e : startTimes.entrySet()) {
    2. String name = e.getKey();
    3. Long time = e.getValue();
    4. Player player = Bukkit.getPlayerExact(name);
    5. }
     
Thread Status:
Not open for further replies.

Share This Page