What's getOrigin and getTarget at EntityLevelChangeEvent?

Discussion in 'Help' started by NakiFx, Sep 13, 2020.

  1. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    Can you teach me how to use getOrigin and getTarget?
     
  2. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    Pls teach me how to use getOrigin and getTarget in EntityLevelChangeEvent
     
  3. mmm545

    mmm545 Slime

    Messages:
    98
    GitHub:
    mmm545
    getOrigin() Returns a level instance of the level the entity originated from.
    getTarget() Returns a level instance of the level the entity wants to go to.
    So you can do it like this (assuming the variable name is $event):
    PHP:
    $originalLevel $event->getOrigin();
    $targetLevel $event->getTarget();
    And then you can do stuff with with these two levels
     
  4. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    How to get players in target/origin world?
     
  5. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    PHP:
    public function onLevelChange(EntityLevelChangeEvent $event){
      
    $player $event->getEntity();
      
    $origin $event->getOrigin();
      
    $target $event->getTarget();
      if(
    $player instanceof Player) return;
      
      if(
    $origin instanceof Level){
       
    //Like this? sorry I'm trying to learning
      
    }
         if(
    $target instanceof Level) {
          
    //Like this? sorry I'm trying to learning
      
    }
     }
     
  6. mmm545

    mmm545 Slime

    Messages:
    98
    GitHub:
    mmm545
    you don't need to check if it's an instance of a Level since it only returns a Level
    so you can remove these two
    PHP:
    if($origin instanceof Level){
       
    //Like this? sorry I'm trying to learning
    }
    if(
    $target instanceof Level) {
        
    //Like this? sorry I'm trying to learning
    }
    also what are you using that for?
    PHP:
    if($player instanceof Player) return;
    Can you tell me what are you trying to do so i can help :>
     
  7. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    When one player enters the target level, all players at the target level will get a message, if 1 player leaves the world, all players in origin level will get a message
     
  8. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    Like this
    If player join the target world, all players will get message like this:
    » <player name> Joined the world

    If player leave the world, all players will get message like this:
    » <player name> Leave the world
     
  9. Primus

    Primus Zombie Pigman

    Messages:
    434
    PHP:
    public function onLevelChange(EntityLevelChangeEvent $event){
        if(!(
    $player $event->getEntity()) instanceof Player) {
           return; 
    // Ignore if not player
        
    }
        
    $sendMessage = function(Level $levelstring $message) use ($player) {
           foreach(
    $level->getPlayers() as $p) {
              if(
    $p !== $player$p->sendMessage($player);
           }
        };

        
    $sendMessage($event->getOrigin(), "{$player->getDisplayName()} left the level";
        
    $sendMessage($event->getTarget(), "{$player->getDisplayName()} entered the level";
    }
     
    Last edited: Sep 14, 2020
    mmm545 likes this.
  10. mmm545

    mmm545 Slime

    Messages:
    98
    GitHub:
    mmm545
    ^
     
    Primus likes this.
  11. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    Not Working :(
     
  12. mmm545

    mmm545 Slime

    Messages:
    98
    GitHub:
    mmm545
    did you register the event?
    it's easy to do it
    first you implement the Listener interface by adding this after your class name
    PHP:
    implements Listener
    then register it in onEnable function
    PHP:
    public function onEnable(){
               
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }
     
    Last edited: Sep 14, 2020
  13. NakiFx

    NakiFx Silverfish

    Messages:
    15
    GitHub:
    nakifx
    Yes i registered the events
    PHP:
    $this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
     
  14. Primus

    Primus Zombie Pigman

    Messages:
    434
    Thanks for feedback! I'm really loosing faith in humanity.
     

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.