Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Overriding default commands

Comments in 'Plugin Development' started by Gamecrafter, May 4, 2015.

  1. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Can someone please explain to me how you can override default PocketMine commands? I want to override the default /say command, but what would I need to override it?
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    public function onSay(PlayerCommandPreProcessEvent $event){
    $msg $event->getMessage();
    if(
    msg[0] == "/" && msg[1] == "s" && msg[2] == "a" && msg[3] == "y"){
    // Do something
    $event-setCancelled(true); //Doesn't do the old command
    }
    }
    I don't think that's the easiest way but it is one and I don't know another one:)
  3. shoghicp
    Offline

    shoghicp Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    433
    Plugins:
    14
    Minecraft User:
    shoghicp
    That's the hacks way. Use the redirection trick that I posted in gist (@iksaku knows about this)
  4. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    which is here if you guys dont know:
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Is it the one used in Lifeboat? Because in Lifeboat /pocketmine:help still works.
  6. shoghicp
    Offline

    shoghicp Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    433
    Plugins:
    14
    Minecraft User:
    shoghicp
    They used a different thing, for example, preprocess. That's why it won't work most of the time, you have to use the proper way to do it even if it takes you a bit of research
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Yep, PreProcess is caps sensitive so it would be very much work to block every way to write the command. Could you please post a snippet of an easier code or a link?
  8. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    I use this for /help on my server, and I can have multiple custom pages of custom help:
    PHP:
    $command explode(" "strtolower($event->getMessage()));
    if(
    $command[0] === "/help"){
      
    $event->setCancelled(true);
      if(
    $command[1] === "page-here"){
       
    //messages-here
      
    }
    }
    Last edited: May 4, 2015
    Andrey Nazarchuk likes this.
  9. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    It isn't caps sensitive at all. Just use strtolower().
    PocketKiller and PEMapModder like this.
  10. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    You don't have to check every single letter. You can just use explode(" ", strtolower($event->getMessage())).
    PocketKiller and thebigsmileXD like this.
  11. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    *facepalm* You're so true!:D
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or just substr($message, 0, 4) or strstr($message, " ", true)

    Also, it is (negligibly) faster if you use strtolower(explode(" ", strtolower($event->getMessage()))[0]). (If you have two functions to process a string with, put the one that trims/shortens the string inside, put the one that lengthens the string outside, or if both shortens the string, put the one that runs faster inside. This usually won't affect the server very much, but it is a good practice to use)
  13. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Why is Gamecrafter banned?
  14. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    *quote from @PEMapModder *
    The staff will not tell us why the user is banned, as it is kept secret.

    *quote from Me*
    He did something bad. Duh.
    Andrey Nazarchuk likes this.
  15. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Obviously, you don't have to be a jerk about it, he was my friend... :(
  16. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    Well, just in case that he could get the message or someone is interested...

    To safely override a command you need to use a command class, like PocketMine does with its default commands.

    After that, try this:



    This method is used inside EssentialsPE to override the commands /tell, /kill and /gamemode
    64FF00 and PEMapModder like this.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually if it is a private plugin, overriding it by PlayerCommandPreprocessEvent is the easiest way.
    64FF00 likes this.
  18. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    It "could" be the easiest, but not the better one :p Also, it's not hard to code the way PocketMine does, or the way that @shoghicp recommends... It's way easier and gives a better structure organization :3
    64FF00 likes this.
  19. shoghicp
    Offline

    shoghicp Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    433
    Plugins:
    14
    Minecraft User:
    shoghicp
    If it's a plugin command, you can set an executor for it :)
    64FF00 and iksaku like this.
  20. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    Well, he is asking to override the default "help" command :p
    PEMapModder likes this.

Share This Page

Advertisement