1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Ding on arrow hit

Discussion in 'Requests' started by NickTehUnicorn, Sep 30, 2017.

  1. NickTehUnicorn

    NickTehUnicorn Zombie

    Messages:
    200
    GitHub:
    unickorn
    Basically I want the player who shot the arrow to get a ding sound if it hits a player.
    I've seen another thread but it wasn't really answered.

    If possible, can I get code with explanations?

    PHP:
    public function ding(EntityDamageEvent $e){
        if(
    $e instanceof EntityDamageByEntityEvent && $e->getDamager() === Arrow){
            
    //I have no idea what I'm doing
        
    }
    }
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you play a sound to the player using
    which just plays a sound on the player's level to the player only
    $player->getLevel()->addSound(new AnvilFallSound($player), [$player]);

    i am not very certain about this but damager might be the one who shoot the arrow
    as for how to ensure it's an arrow i am not certain either
     
  3. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    You can play sounds by using:
    PHP:
    /**
     * @var Player $player
     * @param Sound $sound
     * @param Player $player
     */
    $player->getLevel()->addSound(new ClickSound($player), $player);
    You can find a list of sounds here.
    Also, change the === in $e->getDamager() === Arrow with instanceof.
    Now your code should play ClickSound when you get hit by an arrow.
     
    Last edited: Oct 6, 2017
    NickTehUnicorn likes this.
  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.