Calling and listening to events

Discussion in 'Plugin Development' started by brord, Mar 22, 2013.

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

    brord

    Hello guys!

    Ive had a lot of trouble registering events, and listening to those.

    Events: EntityDamageByEntityEvent, PlayerDeathEvent

    Whenever a player does a certain action, i want other players to be damaged. If i just do:
    player.setHealth(player.getHealth() - 1) (taking his health is more then 1)
    The event gets called just fine, i checked it with a system.out.print.

    But whenever i call the event and the player is suppoosed to die, so when the PlayerDeathEvent gets called, it doesnt register the lastDamageCause.

    I grap that fine, because it works for everything else.
    I have it setup so every event has a system.out.println(). so when someone dies, i see the following:
    - Registered EntityDamageByEntityEvent!
    - Registered EntityDamageEvent!
    - Registered PlayerDeathEvent!

    and then a nullpointer because i try to grab the lastDamageCause, which appareantely isnt beeing called.
    I have also tried to add a delay to the .setHealth() method, with no luck.

    Is there something im missing?
    Should i call the events different?

    Code snippets:


    Event calling:
    Code:
    Bukkit.getServer().getPluginManager().callEvent(new EntityDamageEvent(player.getPlayer(), DamageCause.ENTITY_EXPLOSION, totalDamage));
    Bukkit.getServer().getPluginManager().callEvent(new EntityDamageByEntityEvent(Bukkit.getServer().getPlayer(name),
                        ent, DamageCause.ENTITY_EXPLOSION, totalDamage));
    player.getPlayer().setHealth(player.getPlayer().getHealth() - totalDamage);
    
    Death event:
    Code:
    EntityDamageEvent lastDamageCause = e.getEntity().getLastDamageCause();
    System.out.println("deathcause: " + lastDamageCause.getCause());  <--- nullpointer sometimes, lastDamageCause == null
    
    I am aware that some people dont like calling and listening to methods in the same plugin, but i have all my code in the listener, and i dont want to have double code. Its just convenient in my opinion.

    Thanks for reading!
     
  2. Offline

    Nitnelave

    Check player.damage (int amount, (Entity source))
     
    brord likes this.
  3. Offline

    brord

    Holy Sh**t, never saw that one, ill test it right away

    Thanks man, that fixed it!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page