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

Solved Detect if a player have been hit by a snowball

Comments in 'Plugin Development' started by minebuilder0110, Nov 11, 2015.

  1. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    I am creating a plugin where it needs to detect if a player is hit by a special snowball. (which is done with namedtag stuff) How would I define the snowball that was thrown to the player?
    PHP:
            public function onDamage(EntityDamageEvent $event){
            
    $player $event->getEntity();
            
    $snowball // ???
            
    if($event->getCause() === EntityDamageEvent::CAUSE_PROJECTILE && /* What would go in here too?*/){
                    
    $event->setDamage(10);
                    
    $player->sendMessage("You have been hit by a bullet from other player.");
            }

        }
  2. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    $snowball = $event->getDamager()
    * && $snowball instanceof Snowball
    !! Just an idea, haven't checked it yet.
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Use if($event instanceof EntityDamageByEntityEvent), then if $event->getDamager() instanceof Snowball
    thebigsmileXD likes this.
  4. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Ty! I will try that out soon! So the projectile I was hit by can be defined with getDamager! I thought this meant something like getEntity... XD
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    It returns an Entity instance
    hoyinm14mc likes this.
  6. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Yup, I re-read the docs. I meant that I misunderstood that it will return the attacker, not the projectile entity.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    EntityDamageByChildEntityEvent::getChild()
  8. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    PHP:
    if($cause == EntityDamageEvent::CAUSE_PROJECTILE){
                    if(
    $event instanceof EntityDamageByChildEntityEvent){
                        
    $killer $event->getDamager();
    $snowball $event->getChild();
                        if(
    $snowball instanceof Snowball){
    //your stuff
                        
    }
                    }
    Thanks to @PEMapModder to remind me about getChild()
    Last edited: Nov 12, 2015
    minebuilder0110 likes this.

Share This Page

Advertisement