Solved Strange "Unknown Command" Issue

Discussion in 'Plugin Development' started by NaruSenpai, Apr 26, 2016.

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

    NaruSenpai

    Main - http://pastebin.com/nS083dKv
    onCommand - http://pastebin.com/RKwu3Pdk
    ^^/guildadmin LINE 3 /rank LINE 139 /guild LINE 194
    Plugin.yml -
    Code:
    name: Guilds
    version: 1.1.1
    description: Gives players a way to connect and protect land.
    author: NaruSenpai
    main: me.narusenpai.guilds.Main
    commands:
      guild:
      guildadmin:
      rank:
    
    To be clear my onCommand is within my Main.java, I just didn't know if posting the whole file would be too cluttered, so I posted the onCommand on it's own as well to ensure everyone could find what they needed to try to help me.

    I'm hoping I can make this easy to follow, like a lot of people on here I'm quite the noob but attempting to learn so please be gentle with me.
    I'm making a guilds plugin and have the base coded out and am attempting to go through to test and tweak each command I've created, but am having trouble with getting the commands to execute. I have three command labels: rank, guild, and guildadmin. The /rank command seems to work just fine, it tells me I cannot execute it in game, as it should, and works in the console. The /guild and /guildadmin commands are not working, if performed in the console they do work. They inform me that those commands can only be used in game, if I do either of the commands in game I get no console errors and an in game message saying "Unknown command. Type "/help" for help." If I do "/help guilds" it shows me my list of commands: rank, guild, and guildadmin.
    In short, it seems that the server can see my commands, the console can perform them, the /rank command works in game, but the /guild and /guildadmin commands are running into a problem when trying to be performed in game.
    At the moment I'm not asking for any help with the content within the commands, I'd just like to get them working so that I can problem solve for myself if some of them don't do what I'd like. Without an error message I can't think of much to try on my own to solve this problem, I've rewritten the sections causing trouble, checked spacing in the onCommand and plugin.yml, everything seems like it should work to me. I hope one of you out there can find where I'm going wrong!
     
    Last edited: Apr 26, 2016
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Gonmarte

    OFFTOPIC:
    Do you have multile classes inside your 2 classes, or you are only using 2 classes for all the commands/listener/objects/etc...
    This makes hard trying to troubleshot your own mistakes.
    If you are only using 2 classes, i suggest you split all of them in multiple classes. This will make your plugin organized and easy to read :)
     
  4. Offline

    NaruSenpai

    @timtower If I'm understanding you correctly you're critiquing my plugin.yml, saying I need to adding something more to the command section. I've added a usage attribute to each command, like so:
    Code:
    commands:
      guild:
        usage: Did you mean /guild?
      guildadmin:
        usage: Did you mean /guildadmin?
      rank:
        usage: Did you mean /rank?
    
    And now whenever I do the /rank command in game it says "This command can only be used by the console." and also "Did you mean /rank?", because I used "return false" there. So the /rank command is still working as it should. The /guild and /guildadmin commands both give the unknown command message and then the usage message. I think whenever "return false" is hit the usage message is returned so I'm going to look into that, but otherwise it is still not functioning properly.

    @Gonmarte I would really like to have everything in separate classes but I haven't really learned how to do that yet. I'm attempting to learn through some of the posts on here, but at the moment it is all in my Main.java. (Yikes, I know.)
     
  5. Offline

    Davesan

    May some1 will offend me for that, but it's fine if you return false nowhere within the onCommand(..) method, since only those commands will call it, which you registered this executor (/onCommand method) to. If you return false, it means, that you do not want to handle the command, you are not cared about it. Bukkit will see, that no plugins are there to handle it, so it thinks, that the command is invalid. This is also good, if you want to hide a command from generals users completely:
    Code:
    if (sender.hasPermission("myplugin.op_perm")) {
        sender.sendMessage("You have the rights to see this command!");
        return true;
    } else
        return false;
        //mainstream users will see, that there is no such registered command by any plugin
        //if false returned, bukkit prints the player the wellknown lines about "For help: /help" or like that.
    
    Yeah, checked your code on pasteBin, this is wrong:
    Code:
    [... ... ...]
    
    if(commandLabel.equalsIgnoreCase("rank"))
            {
                if(theSender instanceof Player)
                {
                    theSender.sendMessage("ยง4This command can only be used by the console.");
                    return false; // <-- this shall be replaced with "return true;" !
                }
    
    [... ... ...]
    Returnning false means, that the player is used an illegal syntax, a wrong command, and you refuse to handle it in any way. That's another thing, that you actually handle it before refusing to handle :D Telling the player, that the command is not available for him is also a way of handling, and you should return true. That's why I'm telling, that you should like never return false. By returning false it tells bukkit to show the default usage to the player described in the plugin.yml file for the given command. If you return true, the default usage is not shown ;)
     
    Last edited: Apr 27, 2016
  6. Offline

    NaruSenpai

    @Davesan That makes a lot of sense! I learned (from who knows where) that you return false when the command was not completed "correctly," like if a player used the wrong format. I'll go through and remove all of my return false statements that I used that way, see if it clears up my issue. Thank you for explaining that!

    A breakthrough! At last. I want to thank those of you who posted trying to help me, and I have incorporated all of your critiques. After separating my commands into their own classes, removing all uses of return false, and jazzing up my plugin.yml my commands were still not working properly, but I have found my silly mistake.
    Code:
     thePlayer.performCommand("/guildadmin help"); 
    I suppose I don't know how to use performCommand, and by removing lines where I used that and replacing them with a simple error message my commands are now working just fine. Setting this thread to solved, and thanks again to all who helped me out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 27, 2016
Thread Status:
Not open for further replies.

Share This Page