When player hit a entity play a sound

Discussion in 'Plugin Development' started by EnchantedMiners, Jan 26, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    EnchantedMiners

    What i am trying to do is that if you hit a player or a mob it plays a sound only to the player who hit the entity.
    can someone write an example of the event cus i tried some stuff like this:
    Code:java
    1. @EventHandler
    2. public void HitSound(EntityDamageEvent e){
    3. EntityDamageEvent en = ((Entity) e).getLastDamageCause();
    4. Player p = (Player)e.getEntity();
    5.  
    6. if(en instanceof Zombie){
    7. p.getWorld().playSound(p.getEyeLocation(), Sound.BLAZE_HIT, 1F, 1F);
    8. }
    9.  
    10. }

    it sends an error... cant pass the event.
     
  2. Offline

    Gater12

    EnchantedMiners Try EntityDamageByEntityEvent and check if the Damager is a player, then play the sound to the player.
     
  3. Offline

    EnchantedMiners

    Gater12 ok i tried it i used it like this
    Code:java
    1. @EventHandler
    2. public void HitSound(EntityDamageByEntityEvent e){
    3. Entity ed = e.getDamager();
    4. Zombie p = (Zombie)e.getEntity();
    5.  
    6. if(ed instanceof p){
    7. p.getWorld().playSound(p.getEyeLocation(), Sound.BLAZE_HIT, 1F, 1F);
    8. }
    9. }


    didnt worked and there were no errors now :/
     
  4. Offline

    Gater12

    EnchantedMiners
    Code:java
    1. if(event.getDamager() instanceof Player){
    2. Player p = (Player) event.getDamager();
    3. /* Code to execute */
    4. }
     
  5. Offline

    EnchantedMiners

    Gater12 it worked but the original sound of the zombie/entity doesnt let me hear the sound i put any idea ?
     
  6. Offline

    Th3Br1x

    Play the new sound louder :p
     
  7. Offline

    EnchantedMiners

    Th3Br1x How ? and i think it would be anoying to have two sounds the zombie and the new sound... can the zombie sound be cancelled ?

    Surry im kinda new to this so yeah and thanks for the help Gater12 and Th3Br1x
     
  8. Offline

    xTrollxDudex

  9. Offline

    EnchantedMiners

    xTrollxDudex if i use the code:
    it will check it its a zombie but what actually can i do with it i know i can cancel the damage but i don't really want to cancel the damage i want the sound the damage make to be cancelled...
     
  10. Offline

    Th3Br1x

    EnchantedMiners The playSound() method hast 4 parameters.
    The first is the location, the second is the sound, that should be played, the third is the volume, the fourth parameter is the pitch of the sound.
    The last two ones are floats - means you have te end the number with a "F": example: 1F or 10F, etc.
     
Thread Status:
Not open for further replies.

Share This Page