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

Get hit entity on Projectile hit

Comments in 'Plugin Development' started by MyNameIsTriXz, Feb 17, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Getting the arrow and the arrow shooter do I already know, but how can I get the Player (yes, player), who got hit by the arrow?
  2. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    PHP:
    $event->getEntity()  //NOT GETPLAYER()
    Of course, use it with the EntityDamageEvent.
    Last edited: Feb 18, 2016
  3. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    use \pocketmine\event\entity\EntityDamageByChildEntityEvent [in EntityDamageEvent]
  4. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    There is no $event->getPlayer() at this event :D
    Nawaf_Craft1b likes this.
  5. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    Not getPlayer
    IS GetEntity
    Omar2s2 likes this.
  6. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Yeah, but getEntity() returns the arrow, how can I get the player then?
  7. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    For Example
    PHP:
    public function onDamage(EntityDamageEvent $event){
    if(
    $event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent){
    $damager $event->getDamager();
    if(
    $event instanceof \pocketmine\event\entity\EntityDamageByChildEntityEvent){
    if(
    $damager instanceof \pocketmine\Player){
    $player $event->getEntity();
    }
    }}}
    Omar2s2 and MyNameIsTriXz like this.
  8. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Oops... My mistake.:oops:
  9. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Try this.
    PHP:
    public function onDamage(EntityDamageEvent $e){
    $p $e->getEntity();
    $arrow $p->getLastDamageCause();
    if((
    $arrow instanceof Projectile) and ($arrow instanceof \pocketmine\entity\Arrow)){
    //do something
    }
    }
    MyNameIsTriXz likes this.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is not possible to get the target entity or block from ProjectileHitEvent right now. There will be improvements in the future that enable this.
  11. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I know, thats why I tried it on EntityDamageEvent, atleast it was succesful there
  12. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I know its getEntity()
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes, but that may not always be what you want. I am creating a pull request that adds ProjectileHitEntityEvent.
  14. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I know, you need to use instanceof to get the damager, projectile or the victim, but yeah a simple function would be always better
  15. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    For now, I just check if the item in the shooter's hand is a bow. This might not always work because if the shooter changes their held item before the arrow hits the victim, it would return something else.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That's not the point. The point is, EntityDamageEvent was not related to projectiles in the first place. ProjectileHitEvent is otherwise worthless.
    MyNameIsTriXz likes this.

Share This Page

Advertisement