Creating Custom Events!

Discussion in 'Low-Quality / Outdated' started by xXSirButterXx, Aug 24, 2017.

?

Did i do this right?

  1. Yes

    3 vote(s)
    27.3%
  2. No

    8 vote(s)
    72.7%
  1. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    So recently I figured out how to create custom events and nnow i would like to share it with everyone who doesnt know.

    First and foremost we must create the code where the event should be called, in this case we are going to call the event if the player runs the command "/event-test".
    PHP:
    public function onEventCommand(CommandSender $senderCommand $cmdstring $label, array $args){
    if(
    $cmd->getName() == "event-test"){
    $sender->sendMessage("This is a test command for TestCommandEvent");
    }
    }
    Now we have the command "/event-test", after this we shall create the event. Make sure to use your own namepsace (Dont copy unlesss you know what your doing!).
    PHP:
    <?php

    namespace event\command;

    use 
    pocketmine\event\plugin\PluginEvent;
    use 
    pocketmine\event\Cancellable;


    class 
    TestCommandEvent extends PluginEvent implements Cancellable{
        private 
    $player;
        private 
    $cmd;
        public static 
    $handlerList;
     
        public function 
    __construct(Main $plugin$player$cmd){
            
    parent::__construct($plugin);
         
            
    $this->player $player;
            
    $this->cmd $cmd
        
    }
     
        public function 
    getPlayer(){
            return 
    $this->player;
        }
     
        public function 
    getCommand(){
            return 
    $this->cmd;
        }
    }
    This will be our event that will be called when a player runs "/event-command". If you want the event to be able to be cancelled make sure to implement Cancellable, otherwise it cant be cancelled. The next step is to put callEvent() where we want TestCommandEvent to be called at, for this we are going to go back to the command.
    PHP:
    public function onEventCommand(CommandSender $senderCommand $cmdstring $label, array $args){
    if(
    $cmd->getName() == "event-test"){
    $sender->sendMessage("This is a test command for TestCommandEvent");
    $this->getServer()->getPluginManager()->callEvent(new TestCommandEvent($this$sender$cmd->getName())); //This is where the command return true so the event should be called using this line of code.
    }
    }
    So now our event should be called when the player runs /event-test
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,760
    GitHub:
    sof3
    This tutorial is too nanny and contains more irrelevant code than things that are actually relevant. Also does not explain about how events can be used to modify behaviour.
     
    Jack Noordhuis likes this.
  3. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
  4. Remarkabless

    Remarkabless Slime

    Messages:
    83
    GitHub:
    Remakem
    Can you make a tutorial on custom events!? Please thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.