Check if Command exist

Discussion in 'Plugin Development' started by guitargun, May 6, 2014.

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

    guitargun

    hello

    My plugin uses a feature where server admins type a command without the / in the chat. What I try to make is that the plugin checks if this command exists then stores it for later use. I am trying to make it with a try/catch approach but I dont know how exactly check if this command exist. the commands that are used can vary from /pex user <player> group set <group> to /give <player> <random item> <random ammount>, this offcourse without the /. the admin will use it for example as pex user player group set Demote. when the plugin/server uses the command the "player" will automatticly replaced by the player who triggers the command.

    does anyone have a idea what catch I need to use?
     
  2. Offline

    Fozie

    guitargun
    Try something like this:
    Bukkit.getServer().getPluginCommand(Command).execute(Player, Lable, Args[]);

    Edit:
    For non plugin command i dont really know but you can maybe add / in the begin of the chat message
     
  3. Offline

    Garris0n

    Seems legit.
     
    Minnymin3 likes this.
  4. Fozie Lol.

    Admin: "Hey, how are you?"
    Server: Hey isn't a command

    guitargun For every command? While I could tell you how to check if a command exists, I'd be better off just telling not to do that. You'll run into conflicts sooner rather than later.

    Admin [to a rule breaker]: "Stop breaking rules please"
    *server closed*

    See the problem? How was the server supposed to know that the stop was meant to be said rather than done? It is a command, after all.
     
    Fozie likes this.
  5. Offline

    Fozie

    AdamQpzm
    Whoops did not think of that xD

    You have to enter somekind of "command mode" then, but for my self i thinks its useless then and only take memory.
     
    AdamQpzm likes this.
  6. Fozie I remember the Factions plugin used to do this, but that was fine. It only attempted to do it for its own command, so there wasn't really any conflict. Sure you couldn't say a message like "f hello" but who the hell wanted to start a message with "f " anyway? :p Besides, iirc, there was a config option to disable the feature.
     
  7. Offline

    guitargun


    thats not what is happening. I am making a quest plugin. when the user is done with the quest a command triggers. this can vary from a /give to a pex command. the admin stores the command when create the quest. its not when he says stop breaking the rules that the server checks for a command. it only checks when storing the command
     
  8. guitargun Oh, I think I know what you mean though. And you just want to check before storing that command that it is actually a command?
     
  9. Offline

    guitargun


    exactly
     
  10. Offline

    Onlineids


    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    3. if (!event.isCancelled()) {
    4. Player p = event.getPlayer();
    5. String command = event.getMessage().split(" ") [0];
    6. HelpTopic htopic = Bukkit.getServer().getHelpMap().getHelpTopic(command);
    7. if (htopic == null) {
    8. p.sendMessage(COMMAND DOES NOT EXIST);
    9. event.setCancelled(true);
    10. }
    11. }
    12. }
     
  11. Offline

    guitargun

    if I understand that correctly it checks always for the command to exists but not if there is a typo in the command for example you want to use /pex user guitargun group set User and you typ /pex user guitargun goup ste User. will that send the command does not exists also?
     
  12. Offline

    Onlineids

    No it gets the command not args so /pex
     
  13. Offline

    guitargun


    is there an method to check if the args are correct as well?
     
  14. Offline

    Necrodoom

    Impossible, since args being correct is subjective based on plugin, EX: plugin returns 'player does not exist'.

    Hence, making sure command is correct is the job of the admin.
     
Thread Status:
Not open for further replies.

Share This Page