Tutorial Properly use the PlayerCommandPreprocessEvent

Discussion in 'Resources' started by teej107, Dec 27, 2014.

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


    I started to see too many of these posts involving the PlayerCommandPreprocessEvent and they are using it badly. So I am making this resource so I don't repeat myself over and over again like a crazy person and on the behalf of @SuperOriginal.

    JavaDoc link to PlayerCommandPreprocessEvent

    Whether you want to block a command or change the outcome, this thread applies to every usage of the event.

    1. Get the message (of course)
    2. Split the String message by whitespaces.
      String[] array = message.split(" "); //Single whitespace
    3. Perform the right checks for your desired outcome
          //Your code here
    Other important info:
    If you wanted to override/block a command such as /heal <player>, these would be the steps to use.

    public void onCommandPreprocess(PlayerCommandPreprocessEvent event)
        //Just establishing a context
        // All further code will go inside this method
    1. Code:
      String message = event.getMessage(); // Getting the message
    2. Code:
      String[] array = message.split(" ");
      //Splitting the message
    3. Code:
      //Performing the right checks.
      if(array.length == 2)
          if(array[0].equalsIgnoreCase("/heal") && array[1].equals("teej107"))
              event.getPlayer().sendMessage("Game too hard for ya?");
    The don'ts and whys
    1. Check the command message directly using .equals() or String#startsWith().​
    a. "/thisCommand" does not equal "/THISCommand" when using .equals(). Players can easily bypass your check.

    b. By checking to see if the message startsWith() a certain String, you are literally changing the outcome of all commands that start with the given String. If you want to change the "/op" command with startsWith(), you are changing commands such as "/open" or "/optimize" which you may not want.
    To save your self from developing a useless plugin, make sure you revoke the right permissions from the players. I've seen many questions about trying to block the /pl command and its aliases. The best way in that
    scenario is to revoke the permission bukkit.command.plugins as said here: http://wiki.bukkit.org/CraftBukkit_Commands#Additional_Permissions


    That's it for the tutorial! If you think I forgot something, say so. If I made a dumb mistake with the writing, speak up so we don't have to deal with bad tutorials in the resources section.

    Last edited: Oct 28, 2015
  2. Offline


    Thank you! ;)
    teej107 likes this.
  3. Offline


    @teej107 Nice tutorial. A little bit easy, but super useful for beginners!
    I speak for all: Thank you!
    teej107 likes this.
  4. Offline


    I just want to block /command:something :|
  5. Offline


    Ah yes, let me add something in about that.
  6. Offline


    Haha ok thanks you sooo much :)

    Sorry :p I splitt with ":" ^^' :p
    too lazy today... to much noobs killed me :x
    Last edited: Dec 27, 2014
  7. Offline


    Don't forget to add that this is not the way to make commands; I've seen a lot of people use this event to make commands, which is rude because they usually cancel the event and leave any other plugins that use that command useless
    teej107 likes this.
  8. Offline


    Not that I would want to clean up after somebody else's command if they're doing that.
  9. Offline


    Could be helpful to give your plugin DA POWA and have all other plugins cancelled. Think about it ^^)
  10. Offline


    Jerk plugin right there. It won't have much power when I uninstall it!
  11. Offline


    Cancel all commands >;D
  12. @teej107 Your tutorial kinda contradicts itself now - it starts to guide someone how to block a command (example used, /heal) but then goes on to suggest revoking the permission when someone brings up the /essentials:heal system... what was the point of the blocking of /heal, then? :p

    Other than that, a good enough tutorial. Now if only the people that needed to read this did. Hopefully we'd have less /pl blocking plugins. Or at least better made ones.
  13. Offline


    @AdamQpzm /heal was just an example. If there is a better command out there that would be better suited, tell me.
  14. @teej107 Imo, none of them :p As your post says, negating the permission works. It's clearly the superior way of absolute blocking. Only reason to block with preprocess is for custom criteria (i.e. cooldown, disabled for certain targets, etc)
    timtower and teej107 like this.
  15. Offline


    What if an author didn't add permissions to a command :3
  16. @RingOfStorms Shun the author? :p Well, fair point I suppose. For "custom criteria", or for cleaning up someone else's mess.
  17. Nice tutorial for the beginners.
  18. Offline


    Would use this <3 Thanks
Thread Status:
Not open for further replies.

Share This Page