Getting the player hit from EntityDamageByEntity event?

Discussion in 'Plugin Development' started by XlegitXcrazymanX, Apr 14, 2013.

Thread Status:
Not open for further replies.
  1. So I have this code:
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void playerHitPlayerEvent(EntityDamageByEntityEvent event) {
    5. Entity damager = event.getDamager();
    6. if (damager instanceof Player) {
    7. Player player = (Player) damager;
    8. if (player.getItemInHand().getType() == Material.DIAMOND_SWORD) {
    9.  
    10. Random chanceofblood = new Random();
    11. int b = chanceofblood.nextInt(6);
    12. if(b == 1) {
    13. event.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 10);
    14. System.out.println("Lets test stoof! ");
    15. }
    16. }
    17.  

    I am having trouble with the line:
    Code:java
    1.  
    2. event.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 10);
    3.  

    event.PlayEffect will not work because it is not a method of the EntityDamageByEntityEvent class. So how would I play the effect on the entity/player that is getting hit?

    Thanks in advanced everyone.
     
  2. Offline

    caseif

    You need to use NMS code to play an effect. I forget what the code is, but you could probably find it pretty easily with a quick Google search.
     
  3. AngryNerd Mob spawner flames work.... Anyways I should use NMS code anyways.

    I'm still asking how to get the Entity that was hurt from the damager.
     
  4. Offline

    ConflictRealms

    e.getDamager() is the damager. then e.getEntity() is the person who got hit
     
  5. ConflictRealms
    That won't work either because Entity is not defined for PlayEffect...
     
  6. Offline

    ConflictRealms

    Player hit = (Player)event.getEntity(); also think LivingEntity might work.
    LivingEntity hit (LivingEntity)event.getEntity();
     
  7. Offline

    pasow

    event.getEntity().getWorld.playEffect...
     
  8. Offline

    macguy8

    event.getEntity().getLocation().getWorld().playEffect(event.getEntity().getLocation(), Effect.MOBSPAWNER_FLAMES, 10); ?
     
  9. Offline

    Rocoty

    I'll make it clear:

    To get the entity involved in the event (the entity that took damage), use e.getEntity().

    Alright, playEffect won't accept an entity as the receiver of the effect. That's fine, because you can cast the entity to a player. But before doing this, make sure the entity is actually an instance of Player by checking if (e.getEntity() instanceof Player)

    Now, if you wanted to play the effect for every player in the world, you'd want to get the entity's world. An Entity's world is returned when you invoke getWorld() on it.

    This was my compilation of everything previously said in this thread, and I hope it made stuff clearer.


    EDIT: A quick look on the docs shows that playEffect can actually be invoked on an Entity instance.
    EDIT2: A more thorough look shows that this method has a slightly different, and probably not wanted, function
     
Thread Status:
Not open for further replies.

Share This Page