Block players from using specified commands and block them also from using it in capital letters!

Discussion in 'Resources' started by Peter25715, Sep 28, 2014.

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

    Peter25715

    Hi, My name is Peter and today I will teach you how to block some people from using specified commands for example: /restart and blocking them from adding capital letters to the command and this will make it passable..

    Events that we will use :
    • PlayerCommandPreprocessEvent.
    The PlayerCommandPreprocessEvent is for getting players messages (commands), Cancel them .. Etc.

    First thing, You should add the event to your class..
    Code:java
    1. @EventHandler
    2. public void onPlayerCommand(PlayerCommandPreprocessEvent e) {
    3.  
    4. }

    We are going to use:
    • e.setCancelled();
    • e.getMessage();
    • toLowerCase(); (in string).
    • e.setMessage();
    Second thing, We make the plugin get the player's command and check it, If it starts with "/restart" then cancel it and send a message to the player and adding the string.
    Code:java
    1. @EventHandler
    2. public void onPlayerCommand(PlayerCommandPreprocessEvent e) {
    3. String lowercase = e.getMessage().toLowerCase();
    4.  
    5. e.setMessage(lowercase);
    6.  
    7. if (e.getMessage().startsWith("/restart")) {
    8. //Lines of code here!
    9. }
    10. }

    String toLowerCase means edit the message (command) and switch it to lowercase. So the player can't do /ResTarT.
    e.setMessage(lowercase); means set every command that player sends to lowercase.

    So now, We should insert out code where //lines of code here is.
    Here we will use e.setCancelled(true);
    Code:java
    1. @EventHandler
    2. public void onPlayerCommand(PlayerCommandPreprocessEvent e) {
    3. String lowercase = e.getMessage().toLowerCase();
    4.  
    5. e.setMessage(lowercase);
    6.  
    7. if (e.getMessage().startsWith("/restart")) {
    8. e.setCancelled(true);
    9. e.getPlayer().sendMessage("This command is disabled or you are not allowed to use!");
    10. }
    11. }

    When a player does /restart or /ResTarT it's going to block and do nothing then it will send a message to the player you can't do this..

    You can add permissions or make it for OP players only..

    Did I help you? (open)

    Just give a good reply! :)

    Got a bug / problem? (open)

    Just reply or send me a PM with what is your problem in it.. :)!



     
  2. Offline

    teej107

    What's wrong with using permissions? And I thought registered commands were case-sensitive? If not, you could always use .equals() to compare in your onCommand() method.
     
  3. Offline

    Peter25715

    Using startWith is better cause sometimes it will be tricky and the command contains args[1] for example or /time set 0
     
  4. Offline

    teej107

    What's wrong with using onCommand() method? All of the arguments are stored in a String array with that method.
     
  5. Oh dear, not another one of these.

    Peter25715 No offence, but this method has multiple flaws.

    1) What if a plugin makes a command like /restartarena? Your code now blocks this without reason to.
    2) What about aliases? Your code will block /restart but not /bukkit:restart

    If you want to block a command, negate the permission node. If the command does not have a permission node, ask the author to add one. If they refuse, see if you can add one yourself. If both the above fail, only then try something like this. And don't do it with startsWith() - split the String by space, and that will give you the actual command used, so you won't be blocking commands you don't want to.
     
    ChipDev, teej107 and rbrick like this.
  6. Offline

    ChipDev

    Exactly, Some people block startWith - "tp". Thats stupid >.<
     
    AdamQpzm likes this.
  7. Offline

    RawCode

    blocking them from adding capital letters

    for what reason?
    i see absolutely no possible usages of this
     
    ChipDev likes this.
  8. Offline

    Garris0n

    From what I understand, this is a tutorial for people who have created command-blockers that failed due to the fact that they didn't think about case sensitivity. The OP found a magical way to check for case sensitivity and posted it in resources because of how proud he was of his accomplishment. Or something.
    In other words, ಠ_ಠ
     
    mrCookieSlime, rbrick and DJSkepter like this.
  9. Offline

    ChipDev

    Just one thing, I see no reason to block caps.. :p
    Look, The ChipDev shortened your post!
     
  10. He means that the program will still recognize the command even if capital letters were used.
     
  11. Offline

    RawCode

    there is magical arcane secret method of anciend priests of forbidden gods that called equalsIgnoreCase
     
    xTrollxDudex likes this.
Thread Status:
Not open for further replies.

Share This Page