Die instantly when touch lava

Discussion in 'Plugin Development' started by Glass_Eater84, Aug 24, 2014.

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

    Glass_Eater84

    Hey,
    I am working on a plugin, and one of the features I need is whenever someone touches lava they become eliminated. Is this possible? If so how can I do so?
     
  2. Offline

    TheMcScavenger

    Resource Intensive:
    • Check the PlayerMoveEvent
    • Get when the player is in Lava
    • If the player is in lava, set health to 0
    Less Resource Intensive:
    • Check the EntityDamageEvent
    • Get if the damage is caused by Lava / Fire
    • Set player health to 0
     
    teej107 likes this.
  3. Offline

    Glass_Eater84

    TheMcScavenger I tried this but had no success.
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void touchLava(PlayerMoveEvent e){
    4. Player p = e.getPlayer();
    5. if(p.getLocation().getBlock().getType() == Material.LAVA){
    6. p.setHealth(0); }
    7. }
    8. }
     
  4. Offline

    GeorgeeeHD

    Glass_Eater84 do the second version and and have you registered you event?
     
  5. Offline

    Glass_Eater84

    GeorgeeeHD
    Yes I have registered my events, they look like this.
    Code:java
    1. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    2. }
    3.  
    4. @SuppressWarnings("deprecation")
    5. @EventHandler
    6. public void touchLava(PlayerMoveEvent e){
    7. Player p = e.getPlayer();
    8. if(p.getLocation().getBlock().getType() == Material.LAVA){
    9. p.setHealth(0); }
    10. }
    11. }
    12.  
     
  6. Offline

    coasterman10

    A better way to do this would be to use EntityDamageEvent and check if the cause is DamageCause.LAVA. This will be triggered when the player is damaged by lava and you can set the damage to their health, killing them.
     
  7. Offline

    teej107

    Glass_Eater84
    It's more efficient since it doesn't have to run extra code every time somebody moves.
     
  8. Offline

    Glass_Eater84

    teej107 Can you please provide a small example? I don't want to be code fed but I have tried many things and it did not work. So an example would be greatly appreciated!
     
  9. Offline

    coasterman10

    Code:java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamagEvent e) {
    3. if (e.getCause() == DamageCause.LAVA) {
    4. // Player is in direct contact with lava, kill them
    5. }
    6. }

    This also eliminates issues with hitboxes since just checking the type of block at their foot location could still allow them to have their head in lava without getting killed.
     
  10. Offline

    Glass_Eater84

    coasterman10 Ok then would I do
    Code:java
    1. e.setHealth(0); }
    ?
     
  11. Offline

    coasterman10

    Glass_Eater84 Actually you would do e.getEntity().setHealth(0)
     
  12. Offline

    Glass_Eater84

    coasterman10 Just like this:
    Code:java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamagEvent e) {
    3. if (e.getCause() == DamageCause.LAVA) {
    4. e.getEntity().setHealth(0);
    5. }
    6. }
    7. }
     
  13. Offline

    coasterman10

  14. Offline

    teej107

    If "e" is the player then yes.
     
  15. Offline

    Glass_Eater84

    coasterman10 teej107 It did not work.
    Code:java
    1. package me.glasseater.fern;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageEvent;
    7. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Lava extends JavaPlugin implements Listener{
    11.  
    12. {
    13. Bukkit.getServer().getPluginManager().registerEvents(this,this);
    14. }
    15. @EventHandler
    16. public void onPlayerDamage(EntityDamageEvent e) {
    17. if (e.getCause() == DamageCause.LAVA) {
    18. e.getEntity().setHealth(0);
    19. }
    20. }
    21. }

    setHealth gives an error saying add cast to setHealth which i tried and still got an error
     
  16. I had a problem Similar to this a while back ago, its asking you to flag as a damagable right? This code should do ya ;) Your Welcome!


    Code:java
    1.  
    2.  
    3. @SuppressWarnings("deprecation")
    4. @EventHandler
    5. public void onPlayerDamage(EntityDamageEvent e) {
    6. Damageable d = (Damageable) e.getEntity();
    7. Player p = (Player) d;
    8. if (e.getCause() == DamageCause.LAVA) {
    9. p.setHealth(0);
    10. }
    11. }
     
  17. Offline

    coasterman10

    Tyler Christensen That's unnecessary and also has a dangerous cast to a Player since we don't actually know if it is a player or not.

    I found the fix to this is to add a cast to Damageable since bukkit devs did not override the getEntity method to return a damageable entity like they should have.

    Here's the correct code:
    Code:java
    1. ((Damageable) e.getEntity()).setHealth(0);
     
  18. Offline

    teej107

    Tyler Christensen Your code wouldn't work if setHealth() is saying it's ambiguous. You are casting the entity as a Damageable (which Player is already a Damageable) and then casting it to a Player (without even checking if the entity is) and calling the method setHealth(), which would still show that same error, casting as Damageable in the first place or not casting it. Unless you are using NMS, there is no reason for you to build against Craftbukkit. Building against Bukkit or calling the setHealth() method for the Damageable would fix the problem.

    Glass_Eater84 Are you using NMS? If not, build against Bukkit rather than Craftbukkit. It also won't work since you aren't registering your event inside a called method. Override the onEnable() method and register your events in there.
     
  19. This is the proper solution, i have no errors and its the perfect method if you would like to tag a player for something else :)

    teej107 I did my plugin this way with no errors or complications. But this is a working code for me xD

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onPlayerDamage(EntityDamageEvent e) {
    4. Damageable d = (Damageable) e.getEntity();
    5. if (e.getEntity() instanceof Player) {
    6. Player p = (Player) d;
    7. if (e.getCause() == DamageCause.LAVA) {
    8. p.setHealth(0);
    9. }
    10. }
    11. }
     
  20. Offline

    teej107

    Tyler Christensen Looks good if you were building against Bukkit. I'm not saying you aren't but you are unnecessarily casting the entity to a damageable.
     
  21. teej107 without flagging it as damageable is when the errors occur(well for me at least). this is what i had to do to make everything work in my plugin. Im not saying your wrong, I'm just saying what i had to do to get past the errors it sounds like he is having IF they are the same as mine. If the code doesn't work out, then so be it. Sorry if i seem rude atm, there are many reasons why if I do xD
     
  22. Offline

    AoH_Ruthless

    Tyler Christensen
    You can't cast Damageable to Entity before checking instance. Not all entities are damageable (i.e primed TNT). Perform instance checks first. While a primed TNT should never even trigger this event, it does not hurt to do this.
     
  23. Offline

    coasterman10

    AoH_Ruthless I'd like to point out that EntityDamageEvent can probably only happen with an entity that is also a Damageable but it would be notable if anyone can find an EntityDamageEvent where the entity isn't a Damageable.
     
  24. Offline

    teej107

    Tyler Christensen This should work.
    Code:java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamageEvent e) {
    3. if (e.getEntity() instanceof Player) {
    4. Player p = (Player) e.getEntity();
    5. if (e.getCause() == DamageCause.LAVA) {
    6. p.setHealth(0);
    7. }
    8. }
    9. }
    Removing the needless cast to a Damageable.
     
    MCMastery likes this.
  25. Offline

    AoH_Ruthless

    teej107
    The cast to damageable is sometimes used when the developer is compiling against CraftBukkit. While this is a bad practice and should never be used (always build against Bukkit, not CB!), a work-around is the damageable casting.
     
  26. Offline

    Glass_Eater84

    teej107 AoH_Ruthless So like this is what you're saying;
    Code:java
    1. package me.glasseater.fern;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageEvent;
    7. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Lava extends JavaPlugin implements Listener{
    11.  
    12. public void onDisable()
    13. {
    14.  
    15. }
    16. public void onEnable()
    17. {
    18. this.getServer().getPluginManager().registerEvents(this, this);
    19. }
    20.  
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void onPlayerDamage(EntityDamageEvent e) {
    24. if (e.getEntity() instanceof Player) {
    25. Player p = (Player) e.getEntity();
    26. if (e.getCause() == DamageCause.LAVA) {
    27. p.setHealth(0);
    28. }
    29. }
    30. }
    31. }


    AoH_Ruthless teej107 coasterman10 That did not work. I feel like 1. I cant have two listeners in separate classes. As for 2. I think I am registering the events wrong with the listener.
    Correct me if I am wrong.

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

    Zupsub

    Well looks right... Debug your code: is the event fired? What type of damage cause is it instead of lava, ...?

    1. Of course you can have thousands of listeners, you just need to register them all.
    2.Seems right to me...
     
  28. Offline

    Glass_Eater84

    Zupsub Not sure if its being fired its certainly not setting your health to 0 . You just take normal damage.
     
  29. Offline

    Zupsub

    Well, if you aren't sure, how should we know?
    It's your plugin on your debug/test/development server, so debug your plugin.

    Use your IDE to set breakpoints, to look for the value of differnt variables and so on. There are plenty of tutorials how to debug with your IDE and bukkit server.

    At least you can use simple System.out.printlns.
     
  30. Offline

    Glass_Eater84

    Zupsub AoH_Ruthless teej107 Ok, so. I think whats happening is its in my registering events. I was referred to this.
    Code:java
    1. import java.util.logging.Logger;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Damageable;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.EntityDamageEvent;
    9. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Lava extends JavaPlugin implements Listener{
    13.  
    14. public void onDisable()
    15. {
    16.  
    17. }
    18. public void onEnable()
    19. {
    20. this.getServer().getPluginManager().registerEvent(listener instance, this);
    21. }
    22.  
    23. @SuppressWarnings("deprecation")
    24. @EventHandler
    25. public void onPlayerDamage(EntityDamageEvent e) {
    26. Damageable d = (Damageable) e.getEntity();
    27. if (e.getEntity() instanceof Player) {
    28. Player p = (Player) d;
    29. if (e.getCause() == DamageCause.LAVA) {
    30. p.setHealth(0);
    31. }
    32. }
    33. }
    34. }


    EDIT: didn't work. Got an error in the register events part where it says "Listener and instance"
     
Thread Status:
Not open for further replies.

Share This Page