How do I implement the new PLAYER_COMMAND_PREPROCESS event?

Discussion in 'Plugin Development' started by zezioen, Mar 27, 2011.

Thread Status:
Not open for further replies.
  1. Hey,

    It's been some time since I developed a plugin and I noticed that the PLAYER_COMMAND has been removed and replaced with PLAYER_COMMAND_PREPROCESS. But somehow I can't get it to work.

    Also I read somewhere on this forum that I need to add stuff into my plugin.yml file in order to get them to work.

    Is there a 'up to date' tutorial on how to do that?

    Thanks in advance.
     
  2. Offline

    Sammy

    Override onCommand on the main file (the one that extends JavaPlugin)
    OR
    Code:
     public void onEnable() {
    getCommand("bounty").setExecutor(new sdBountyHunterCmds(this));
    }
    and than make a class implementing CommandExecutor
    Code:
    public class sdBountyHunterCmds implements CommandExecutor
    
     
  3. Never mind I already found it.

    public void onPlayerCommandPreprocess(PlayerChatEvent e)
    {
    //Process the command here
    //No need to change anything in the plugin.yml to get it workin
    //Make sure to call e.setCancelled(true); if you are responding to this command to remove the Unknown command error
    }
     
  4. Offline

    Sammy

    You found it, but that's not the best way to make cmds ;)
    in Java docs about onPlayerCommandPreprocess:
    "Called early in the command handling process.
    This event is only for very exceptional cases and you should not normally use it."
     
  5. Thanks Sammy I'll implement my commands the right way than.
     
  6. Offline

    Sammy

    No problem ;)
     
  7. Offline

    Mixcoatl


    I rather dislike this wording (which is not your fault, obviously). The JavaDocs are supposed to explain what something does and typically the motivations and contractual obligations of that class, field, or method. It's quite bad when better than 50% of the description text explains that the reader should not be using it.
    It would be infinitely better to remove both of these lines and explain enough about this method and how the corresponding event is raised that the reader can postulate, "This is complicated or dangerous and it probably doesn't do what I would like."
    Sorry. I'm feeling pedantic this afternoon.
     
  8. Offline

    Sammy

    I completely agree with you, that's why so many ppl ask what it does...
    Still, I'm almost 99% sure that he doesn't need to use PLAYER_COMMAND_PREPROCESS
    And you could be messing with he's head with your "pedantic feelings" ^^

    In other words: If you aren't sure use OnCommand ^^
     
  9. Offline

    Plague

    Exactly. Developers knowing what they're doing will get that the preprocess is not something evil and it can be freely used and it'S just not wise to do so every time.
    Those who do not understand that by themselves are better off thinking it's evil, thus the javadocs wording :)
     
  10. Offline

    Mixcoatl

    I agree. I'm fairly certain this is unnecessary in the vast majority of cases.

    It was off the cuff and random. Sorry. :p One of my pet peeves is poorly written or inaccurate JavaDocs documentation.

    I respectfully decline to agree. ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  11. Wauw big conversation here!

    I've got just one big problem with using the Command Executor,
    How am I going to get the player who executed the command? Both the Command and CommandSender classes have no information on this at all in the docs.
     
  12. Offline

    Edward Hand

    The Player class implements CommandSender, so you can cast like this:
    Code:
    if(commandSender instanceof Player)
    {
       Player player = (Player)commandSender
       //some player-related code goes here
    }
    else
    {
       //console may be issuing the command
    }
     
  13. Thank you Edward Hand!
     
  14. Offline

    Sammy

    How can the console issue the command ? that could be useful for me in the future :)

    The big problem is that making new coders understand how the pre process works is difficult so making it look like a binary code Satan is easier ^^
     
  15. Offline

    Edward Hand

    Well...you just type it in :p

    like you can type 'give Sammy 46 64' in console and it would give you a stack of TNT
     
  16. Hmmm

    Code:
    this.getCommand("skill").setExecutor(new SkillCommandExecutor(this));
    Gives me the error

    The SkillCommandExecutor implements the CommandExecutor as noted above.
    Am I missing something here?
     
  17. Offline

    Edward Hand

    That would suggest that it can't find a command called "skill"
    you did register it in your plugin.yml didn't you?
     
  18. No I didn't :oops:.
    Could you please give me the syntax
     
  19. Offline

    Edward Hand

    Code:
    name: YourPlugin
    main: com.yourname.ClassFile
    version: 0.1
    commands:
      skill:
        description: does some skills stuff
        usage:
    something like that
     
  20. Yay :D it's working! Thank you for all the help.
     
  21. Offline

    Sammy

    I'm so dumb sometimes [pig]
    I thought that commands from plugin could only be used on the chat console... silly me
     
  22. Doesn't matter Sammy were all humans and we can all make mistakes.
     
Thread Status:
Not open for further replies.

Share This Page