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

you must Pay attention to event use

Comments in 'Plugin Development' started by forthxu, Nov 30, 2015.

  1. forthxu
    Offline

    forthxu New Member

    Joined:
    Nov 13, 2015
    Posts:
    4
    Minecraft User:
    forthxu
    one of the plugin's main work is once we register a listener to listen the even happen and do something.
    https://github.com/PocketMine/SimpleAuth/blob/master/src/SimpleAuth/SimpleAuth.php#L366 ,like:

    PHP:
           $this->listener = new EventListener($this);
            
    $this->getServer()->getPluginManager()->registerEvents($this->listener$this);
    the pocketmine-pm use the pocketmine\plugin\PluginManager::registerEvents to register the event by the parameter but it will throw a error if you use the second function. you can only use EntityDamageEvent not EntityDamageByEntityEvent,
    PHP:
        public function onEntityDamage(EntityDamageEvent $event){
            if(
    $event instanceof EntityDamageByEntityEvent){
                         
    //you can do EntityDamageByEntityEvent event here
             
    }
            if(
    $event->getEntity() instanceof Player and !$this->plugin->isPlayerAuthenticated($event->getEntity())){
                
    $event->setCancelled(true);
            }
        } 
        public function 
    EntityDamageByEntityEvent(EntityDamageByEntityEvent $event){
            if((
    $event->getEntity() instanceof Player and !$this->plugin->isPlayerAuthenticated($event->getEntity())) or ($event->getDamager() instanceof Player and !$this->plugin->isPlayerAuthenticated($event->getDamager()))){
                
    $event->setCancelled(true);
            }
        }
    because:
    pocketmine-pm call the plugins event use
    pocketmine\plugin\PluginManager::callEvent(Event $event);

    PHP:
        public function callEvent(Event $event){
            foreach(
    $event->getHandlers()->getRegisteredListeners() as $registration){
    //...
    }
    }
    we found $event->getHandlers() is to get public static $handlerList = null;
    but class EntityDamageByEntityEvent extends EntityDamageEvent
    so we can onlye call a EntityDamageEvent.


    you can only use the event witch have
    ,children just judge.
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Well, I don't really get the point in this thread..
    PocketKiller and Legoboy0215 like this.
  3. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Shouldn't this be in the Recourses section
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    In simple words, you should only register event handlers for events that have the
    PHP:
    public static $handlerList null;
    line in the source code file.
    PocketKiller and CrazedMiner like this.
  5. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Basically what your trying to say is that you can only "listen" to events that actually get called?
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. EntityDamageByEntityEvent gets called, but it doesn't have its own handler list, and it falls back to the EntityDamageEvent handler list. So in simple words, only hook to events that have their own handler lists.
    CraftYourBukkit likes this.

Share This Page

Advertisement