EntityDamageEvent

Discussion in 'Plugin Development' started by Mother__, Mar 28, 2013.

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

    Mother__

    what problem this code ?
    Code:java
    1.  
    2. @EventHandler
    3. public void EntityDamage(EntityDamageEvent event) {
    4. Entity ent = event.getEntity();
    5. Player player = (Player)ent;
    6. if(player.getLastDamageCause().getCause().equals(DamageCause.LAVA) && player.isDead()){
    7. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " die by lava");
    8. }
    9.  
    10. if(player.getLastDamageCause().getCause().equals(DamageCause.FIRE) && player.isDead()){
    11. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " die burning ");
    12. }
    13.  
    14. if(player.getLastDamageCause().getCause().equals(DamageCause.FIRE_TICK) && player.isDead()){
    15. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " die buring haha ;d ");
    16. }
    17. if(player.getLastDamageCause().getCause().equals(DamageCause.FALL) && player.isDead()){
    18. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " falling from cloud 9 ....");
    19.  
    20. }
    21.  
    22. if(player.getLastDamageCause().getCause().equals(DamageCause.ENTITY_EXPLOSION) && player.isDead()){
    23. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " bum!");
    24. }
    25.  
    26. if(player.getLastDamageCause().getCause().equals(DamageCause.BLOCK_EXPLOSION) && player.isDead()){
    27. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " bumm");
    28. }
    29.  
    30. if(player.getLastDamageCause().getCause().equals(DamageCause.SUFFOCATION) && player.isDead()){
    31. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " die by suffocation");
    32. }
    33. if(player.getLastDamageCause().getCause().equals(DamageCause.DROWNING) && player.isDead()){
    34. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " die by drowning");
    35. }
    36.  
    37. if(player.getLastDamageCause().getCause().equals(DamageCause.SUICIDE) && player.isDead()){
    38. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " suicede ;( ");
    39. }
    40.  
    41. if(player.getLastDamageCause().getCause().equals(DamageCause.CONTACT) && player.isDead()){
    42. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " die by Contact ");
    43. }
    44.  
    45. if(player.getLastDamageCause().getCause().equals(DamageCause.STARVATION) && player.isDead()){
    46. Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + " die by Starvation ");
    47.  
    48. }
    49. }
    50.  
    51.  



    when i die show this erros
    Code:
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at net.minecraft.server.v1_5_R1.EntityLiving.x(EntityLiving.java:357)
    at net.minecraft.server.v1_5_R1.Entity.l_(Entity.java:248)
    at net.minecraft.server.v1_5_R1.EntityLiving.l_(EntityLiving.java:515)
    at net.minecraft.server.v1_5_R1.EntityHuman.l_(EntityHuman.java:158)
    at net.minecraft.server.v1_5_R1.EntityPlayer.g(EntityPlayer.java:216)
    at net.minecraft.server.v1_5_R1.PlayerConnection.a(PlayerConnection.java:352)
    at net.minecraft.server.v1_5_R1.Packet10Flying.handle(SourceFile:136)
    at net.minecraft.server.v1_5_R1.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R1.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_5_R1.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R1.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R1.MinecraftServer.r(MinecraftServer.java:580)
    at net.minecraft.server.v1_5_R1.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_5_R1.MinecraftServer.q(MinecraftServer.java:476)
    at net.minecraft.server.v1_5_R1.MinecraftServer.run(MinecraftServer.java:409)
    at net.minecraft.server.v1_5_R1.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    at myPlugin.game.Main.EntityDamageEvent(Main.java:1340)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 19 more
     
    
    Line 1340

    Code:java
    1.  
    2. if(player.getLastDamageCause().getCause().equals(DamageCause.LAVA) && player.isDead()){
    3.  
     
  2. Offline

    Codisimus

    You should learn about safe casting. Also reading up on switch statements couldnt hurt.
     
  3. Offline

    Mother__

    Can show me example ? I learn java i programing PHP and JAVASCRIPT
     
  4. Offline

    Ivan

    First of all, you really should be checking if the entity is a Player by using entity instanceof Player, also player.getLastDamageCause() can be null
     
  5. Offline

    Mother__

    I try this
    Code:java
    1.  
    2. Player p =event.getEntity();
    3. Entity ent = event.getEntity();
    4. Player player = (Player)ent;
    5. if(player instanceof Player)
    6. if(player.getLastDamageCause() != null)
    7. if(player.getLastDamageCause().........
    8. ................
    9. ...........
    10.  


    But not working...
     
  6. Offline

    soulbrander

    Code:
    Player p = event.getEntity();
    DamageCause cause = p.getLastDamageCause().getCause();
    if(cause == DamageCause.LAVA)
    {
        Bukkit.broadcastMessage(ChatColor.AQUA + p.getDisplayName() + ChatColor.RED + " died due to Lava.");
    }
    Using that, when someone dies by Lava this will trigger... You really need to work on your english, and decently present your problem. Anyway, try that. If someone dies by lava that should trigger unless some other plugin is turning that cause a null.
     
  7. Offline

    RingOfStorms

    Looks like you are doing death messages? Why not do PlayerDeathEvent?
     
  8. Offline

    WolfGangSen

    You should be using instanceof to check if it is safe to cast it to player.

    therefore checking instanceof after is pointless as you have already done the cast.
     
  9. Offline

    RingOfStorms

    I did the code below in the forum editor so may have mistakes, but that should give you an idea of what to do if you're doing death messages.

    Code:java
    1.  
    2. @EventHandler
    3. public void playerDeath (PlayerDeathEvent e) {
    4. Player player = e.getPlayer();
    5. switch(e.getCause()) {
    6. case Fire:
    7. e.setDeathMessage(player.getDisplayName() + " has burned to ashes!");
    8. break;
    9. case Drowning:
    10. break;
    11. }
    12. }
    13.  


    Note since this is the death event you use the event's set death message or whatever, because if you broadcast then you'll get two messages.

    And yea, should put the check before the cast.

    Code:java
    1.  
    2. Entity ent = event.getEntity();
    3. if(!(ent instanceof Player)) return;
    4.  
    5. Player player = (Player) ent;
    6.  
    7. switch(event.getCause()) {
    8. case FIRE:
    9. //Do stuff
    10. break;
    11. }
    12.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    Mother__

    Ok i from brazil and i try speak better =(
    i try make u say but show this erros

    Code:
    2013-03-28 15:37:32 [SEVERE] Could not pass event PlayerDeathEvent to MyPlugin v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_5_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:335)
    at net.minecraft.server.v1_5_R1.EntityPlayer.die(EntityPlayer.java:285)
    at net.minecraft.server.v1_5_R1.EntityLiving.damageEntity(EntityLiving.java:765)
    at net.minecraft.server.v1_5_R1.EntityHuman.damageEntity(EntityHuman.java:683)
    at net.minecraft.server.v1_5_R1.EntityPlayer.damageEntity(EntityPlayer.java:350)
    at org.bukkit.craftbukkit.v1_5_R1.entity.CraftLivingEntity.damage(CraftLivingEntity.java:190)
    at org.bukkit.craftbukkit.v1_5_R1.entity.CraftLivingEntity.damage(CraftLivingEntity.java:175)
    at myPlugin.game.Main.GodMode(Main.java:1312)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at net.minecraft.server.v1_5_R1.EntityLiving.a(EntityLiving.java:975)
    at net.minecraft.server.v1_5_R1.EntityHuman.a(EntityHuman.java:1275)
    at net.minecraft.server.v1_5_R1.Entity.a(Entity.java:779)
    at net.minecraft.server.v1_5_R1.EntityLiving.a(EntityLiving.java:242)
    at net.minecraft.server.v1_5_R1.EntityPlayer.b(EntityPlayer.java:455)
    at net.minecraft.server.v1_5_R1.PlayerConnection.a(PlayerConnection.java:428)
    at net.minecraft.server.v1_5_R1.Packet10Flying.handle(SourceFile:136)
    at net.minecraft.server.v1_5_R1.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R1.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_5_R1.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R1.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R1.MinecraftServer.r(MinecraftServer.java:580)
    at net.minecraft.server.v1_5_R1.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_5_R1.MinecraftServer.q(MinecraftServer.java:476)
    at net.minecraft.server.v1_5_R1.MinecraftServer.run(MinecraftServer.java:409)
    at net.minecraft.server.v1_5_R1.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    at myPlugin.Main.onPlayerDeath(Main.java:1970)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 35 more
     
    
    i try this

    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onPlayerDeath(PlayerDeathEvent event) {
    5. Entity ent = event.getEntity();
    6. Player player = (Player) ent;
    7. if(!(ent instanceof Player)) return;
    8.  
    9. switch(player.getLastDamageCause().getCause()) {
    10. case FALL:
    11. event.setDeathMessage(ChatColor.AQUA + player.getName() + " die by fall");
    12.  
    13. break;
    14. }
    15. }
    16.  


    Working but say this erros in console =(
     
  11. Offline

    RingOfStorms

    The PlayerDeathEvent has the function event.getPlayer() instead of Entity, beacuse of this there is no casting required, you simply get the player. If you looked at the code I posted above, then you could do something like that.

    Code:java
    1.  
    2. @EventHandler
    3. public void playerDeath (PlayerDeathEvent e) {
    4. Player player = e.getPlayer();
    5. switch(e.getCause()) {
    6. case Fire:
    7. e.setDeathMessage(player.getDisplayName() + " has burned to ashes!");
    8. break;
    9. case Drowning:
    10. break;
    11. }
    12. }
    13.  
     
  12. Offline

    Mother__

    Friend event PlayerDeath no have getPlayer() u need getEntity first
     
  13. Offline

    RingOfStorms

    Ah yea, but if you look the getEntity returns a player object, so use the above code and simply replace getPlayer with getEntity and it will work fine without casting.
     
Thread Status:
Not open for further replies.

Share This Page