Solved Check if a command exists.

Discussion in 'Plugin Development' started by GeorgeeeHD, Mar 15, 2014.

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

    GeorgeeeHD

    How to check if a command exists? I want to replace the default "Unknown command" message.

    Thanks
     
  2. Offline

    Gater12

    GeorgeeeHD
    I believe you would have to edit craftbukkit.jar for that.
     
  3. Offline

    AoH_Ruthless

    GeorgeeeHD Gater12
    In a PlayerCommandPreProcessEvent, cancel it to avoid the "unknown command" message and then send your own message to the player.

    I'm unsure what is a good way of checking if a command exists, if possible.
     
  4. Offline

    Keyboard

    This is totally irrelevant but George I'm a big sub on youtube lol
     
  5. Offline

    GeorgeeeHD


    AoH_Ruthless i understand that, but not how to do it xD
     
  6. Offline

    Wolfey

    Code:java
    1. Command command = Bukkit.getServer().getPluginCommand("command");
    2. if(command != null) {
    3. // command is not null
    4. }
    5. else {
    6. // command is null
    7. }
     
  7. Offline

    GeorgeeeHD


    Do i put this in a PlayerCommandPreprocessEvent or where? Also, do I replace "command" with anything or is it meant to be that? At the moment, I basically copied that to a PlayerCommandPreprocessEvent and it always runs what I put in the code

    Code:java
    1. Command command = Bukkit.getServer().getPluginCommand("command");
    2. if(command != null) {
    3. return;
    4. } else {
    5. e.setCancelled(true);
    6. p.sendMessage("Hi");
    7. }


    It always messages me "Hi", even if the command is valid
     
  8. Offline

    Wolfey

    GeorgeeeHD You have to actually put the command name in the getPluginCommand()...

    Of course it's always gonna send "Hi", there is no command "command".
     
  9. Offline

    GeorgeeeHD

    I think you misunderstood what I want. I want to be able to check if a player types something like /sdjfhskdhfj24!!sdfhsdf

    Obviously, that is not a command. If they type that, it will say "Unknown command." I need to change that message. I can check every single possibility that they could type o_o
     
  10. Offline

    pookeythekid

    GeorgeeeHD Perhaps something like this?
    Code:java
    1. @EventHandler
    2. public void onCmdPreProcess(PlayerCommandPreProcessEvent e) {
    3. Command command = Bukkit.getServer().getPluginCommand(e.getCommand().toString());
    4. if (command = null) {
    5. e.setCancelled(true);
    6. }
    7.  


    This may not work, just a theory. Haven't tested it.

    Wizehh : DDDDDD Yay! xD I didn't even expect that to work! I guess you're welcome, and thanks for allowing me the good feeling of helping someone. :)

    Edit: *facepalm* wait, you're not the one who posted this thread! xD But at least the guy who did post this thread will know it works.

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

    Wolfey

    pookeythekid Isn't that basically what I just posted, or am I missing something?

    And yes, GeorgeeeHD, I understood what you meant, and what I posted should solve your error, but you weren't using it right.
     
  12. Offline

    pookeythekid

    Wolfey It is, but you are missing a couple of things.
    1. You used a specific string as an example, which was "command". You didn't go fetch the command that was being entered instead.
    2. The code you posted didn't show that it was an EventHandler.
    But otherwise, yes I pretty much repeated what you said. : P

    Edit: Wait nvm it did, you have an e.setCancelled(true); in there.
     
  13. Offline

    GeorgeeeHD


    This is what I changed yours to.

    Code:java
    1. @EventHandler
    2. public void onCmdPreProcess(PlayerCommandPreprocessEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. Command command = Bukkit.getServer().getPluginCommand(e.getMessage());
    6.  
    7. if (command == null) {
    8. e.setCancelled(true);
    9. p.sendMessage(prefix + "That is not a valid command!");
    10. }
    11.  
    12. }


    no matter what command i type, it messages the player "prefix + That is not a valid command"
     
  14. Offline

    pookeythekid

    GeorgeeeHD Well sir, it appears that you broke my method in attempt to fix it. xD
    First off, change it back from "e.getMessage()" to "e.getCommand().toString()". Secondly, you should be getting an error if you haven't stated "prefix" as a variable earlier in your code, and if you have, then it would be impossible for it to say to you: "prefix + That is not a valid command".
     
  15. Offline

    GeorgeeeHD


    prefix is a variable i have in my code, and e.getCommand() does not exist in the PlayerCommandPreprocessEvent
     
  16. Offline

    Compressions

    GeorgeeeHD Try using:
    Code:
    e.getMessage().substring(1);
     
  17. Offline

    GeorgeeeHD

    Well I got it to work, the "/" at the beginning of commands messed it up. If you want the code, here it is:

    Code:java
    1. @EventHandler
    2. public void onCmdPreProcess(PlayerCommandPreprocessEvent e) {
    3.  
    4. Player p = e.getPlayer();
    5.  
    6. Command command = Bukkit.getServer().getPluginCommand(e.getMessage().toString().replace("/", ""));
    7.  
    8. if(command != null) return;
    9.  
    10. e.setCancelled(true);
    11. p.sendMessage("That is not a valid command!");
    12.  
    13. }



    ninja'd xD That would work too I guess :p

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

    Compressions

    GeorgeeeHD My method would only replace the first forward-slash, in case of there being forward-slashes later in the command arguments.

    EDIT: Why did you perform toString() on a String?
     
  19. Offline

    Arcoz

    Ahm on codename_B's under 50 lines challenge, a guy ( Zarko ) actually made this

    His post:
     
  20. Offline

    GeorgeeeHD

    tbh, i don't know, I just copied what someone wrote here earlier and worked off that, I removed it a bit later lol
     
  21. Offline

    Zarko

  22. Offline

    pookeythekid

    Ah, makes sense. Good job. Hope you like your new plugin now that you've solved its problems.

    :confused: Someone tested it and said it worked perfectly...

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

    GeorgeeeHD

    lol i dont know, there is defiantly no getCommand in PlayerCommandPreprocessEvent tho
     
  24. Offline

    lunkback

    I use
    Command command = Bukkit.getServer().getPluginCommand(event.getMessage().substring(1));
    and command == null all time(
     
  25. Offline

    Bammerbom

    GeorgeeeHD get the field commandmap in bukkit and check if the used command is inside the map
     
  26. Offline

    lunkback

    How to check of all commands if command exist?
     
Thread Status:
Not open for further replies.

Share This Page