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

    Jaaakee224

    Glass_Eater84
    Since that class extends JavaPlugin, you can just do:
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
     
  2. Offline

    Glass_Eater84

    Jaaakee224 But it also implements Listener. Also, my other classes
    SignCommand:
    Code:java
    1. publicclassSignCommandimplementsListener{
    2.  
    3. Main m;
    4. publicSignCommand(Maininstance)
    5. {
    6. m=instance;
    7. }


    Main:
    Code:java
    1. public class Main extends JavaPlugin
    2. {
    3. public final Logger logger = Logger.getLogger("Minecraft");
    4. public void onEnable()
    5.  
    6.  
    7. {
    8. this.saveDefaultConfig();
    9. this.level = getConfig().getInt("Knockback");
    10. getConfig().options().copyDefaults(true);
    11. Bukkit.getServer().getPluginManager().registerEvents(new SignCommand(this), this);
     
  3. Offline

    Jaaakee224

    Glass_Eater84 Something like this would work, you just have the manage it only killing the player once because now it spams that the player died.

    Code:java
    1. public class Tester extends JavaPlugin implements Listener {
    2.  
    3. @Override
    4. public void onEnable() {
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. }
    7.  
    8. @EventHandler
    9. public void onPlayerDamageEvent(EntityDamageEvent e) {
    10. Player p = (Player) e.getEntity();
    11. if (p.getLastDamageCause().getCause() == DamageCause.LAVA) {
    12. if (e.getEntity() instanceof Player) {
    13. p.setHealth(0.0);
    14. }
    15. }
    16.  
    17. }
    18. }
     
  4. Offline

    Glass_Eater84

    Jaaakee224 Very weird still not working.

    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.entity.Player;
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.entity.EntityDamageEvent;
    6. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Lava extends JavaPlugin implements Listener{
    10.  
    11. @Override
    12. public void onEnable() {
    13. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerDamageEvent(EntityDamageEvent e) {
    18. Player p = (Player) e.getEntity();
    19. if (p.getLastDamageCause().getCause() == DamageCause.LAVA) {
    20. if (e.getEntity() instanceof Player) {
    21. p.setHealth(0.0);
    22. }
    23. }
    24.  
    25. }
    26. }

    I've tried debugging many times, with no success.
    EDIT: It works in its own plugin, but I would like to implement it.
     
  5. Offline

    Jaaakee224

    Glass_Eater84 You're probably implementing your listeners wrong.
     
  6. Offline

    Glass_Eater84

    Jaaakee224 What do you mean? Should I change the way I register my events in all classes? to like this?
    Code:java
    1. this.getServer().getPluginManager().registerEvent(listener instance, this);
    Or how should I do it?
     
  7. Offline

    teej107

    No. Let's get down to the basics to make sure we aren't missing anything stupid. Is your plugin even loading? Stacktrace? And in your most current posted class, you aren't safely casting.

    Yes, but in his example, he wasn't even calling anything from the Damageable. He was calling it from the Player.
     
  8. Offline

    Jimfutsu

    This can be completed easily, I dunno why you are using player damage event.

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent event)
    3. {
    4. final Player player = event.getPlayer();
    5. Material m = event.getPlayer().getLocation().getBlock().getType();
    6. if ((m == Material.STATIONARY_LAVA) || (m == Material.LAVA)) {
    7. player.setHealth(0);
    8. }
    9. }


    was that really hard?
     
  9. Offline

    coasterman10

    Jimfutsu This would only work for the feet of the player; they could have their head in lava but feet out of lava and not be killed. Also it is a bad idea to use PlayerMoveEvent when you don't have to.
     
  10. Offline

    Jimfutsu

    coasterman10
    Then you can easily check the location of the head. getLocation.add(0, 1, 0);

    :)
     
  11. Offline

    Glass_Eater84

    teej107 Jimfutsu coasterman10 Plugin is loading fine. All commands work. The only thing that isn't working is the death with contact with lava. It works in its own plugin. However, does not work when its implemented into my current plugin, which is the issue. As Jaaakee224 said I could be implementing my listeners wrong, but don't think I am. Thanks for the help so far, and I hope we can squash this bug! (Don't even think it's a bug.)
    I can post the beginning of my code so you can see how I implement listeners, and what not.
    Main:
    Code:java
    1. public class Main extends JavaPlugin
    2. {
    3. static String permdenied = ChatColor.RED + "Sorry, you can't do this!";
    4.  
    5. int level;
    6.  
    7.  
    8. public void onDisable()
    9. {
    10. System.out.println(disabled);
    11. }
    12. public final Logger logger = Logger.getLogger("Minecraft");
    13. public void onEnable()
    14.  
    15.  
    16. {
    17. this.saveDefaultConfig();
    18. this.level = getConfig().getInt("Knockback");
    19. getConfig().options().copyDefaults(true);
    20. Bukkit.getServer().getPluginManager().registerEvents(new SignCommand(this), this); // This is what i've changed.
    21. }

    That is only the beginning of the Main class (The most important)

    SignCommand:
    Code:java
    1. public class SignCommand implements Listener {
    2.  
    3. Main m;
    4. public SignCommand(Main instance)
    5. {
    6. m= instance;
    7. }

    I do not register my events in my SignCommand Class. Because I do not need to.
    (ALSO, I AM TRYING TO DO THIS IN A SEPARATE CLASS)
    - Glass
     
  12. Offline

    Jimfutsu

    I would recommend putting debug messages.
     
  13. Offline

    teej107

    Jimfutsu Reason why he is using EntityDamageEvent:
    Glass_Eater84
    Yes put in debug code.
     
  14. Offline

    Glass_Eater84

  15. Offline

    Jimfutsu

    Absolutely nothing? not even the first message?
     
  16. Offline

    teej107

  17. Offline

    connorlinfoot


    Check to see if it is still lava too?
     
  18. Offline

    AoH_Ruthless

    connorlinfoot
    Wait a second. Forget about the PlayerMoveEvent. Keep what you have.

    Post the full code again. You say it's not your main class? Why does it extend JavaPlugin??
     
  19. Offline

    Zupsub

    By the way: you have to check for Material.LAVA and for Material.STATIONARY_LAVA
     
  20. Offline

    Glass_Eater84

    AoH_Ruthless
    Code:java
    1. public class Main extends JavaPlugin
    2. {
    3.  
    4. static String enabled = " Has been enabled!";
    5. static String disabled = " Has been disabled!";
    6. int level;
    7.  
    8.  
    9. public void onDisable()
    10. {
    11. System.out.println(disabled);
    12. }
    13. public final Logger logger = Logger.getLogger("Minecraft");
    14. public void onEnable()
    15.  
    16.  
    17. {
    18. this.saveDefaultConfig();
    19. this.level = getConfig().getInt("Knockback");
    20. getConfig().options().copyDefaults(true);
    21. Bukkit.getServer().getPluginManager().registerEvents(new SignCommand(this), this); // This is what i've changed.
    22. }
    23.  
    24. @SuppressWarnings("deprecation")
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    26. {
    27. if (cmd.getName().equalsIgnoreCase("*****")) {
    28. if (sender.hasPermission("feather.use")) {
    29. sender.sendMessage(featherswag);
    30. Player player = (Player)sender;
    31. ItemStack ****** = new ItemStack(Material.******, 1);
    32. featherswag.addUnsafeEnchantment(Enchantment.******, level);
    33. player.getInventory().addItem(new ItemStack[] { featherswag });
    34. player.updateInventory();
    35. return false;
    36. }
    37. if (!sender.hasPermission("*****")) {
    38. sender.sendMessage(*****);
    39. return false;
    40. }
    41. }
    42. else if (cmd.getName().equalsIgnoreCase("******")) {
    43. if (sender.hasPermission("****")) {
    44. sender.sendMessage(*****);
    45. Player player = (Player)sender;
    46. ItemStack featherswag1 = new ItemStack(Material.*****, 1);
    47. featherswag1.addUnsafeEnchantment(Enchantment.******, 2);
    48. featherswag1.addUnsafeEnchantment(Enchantment.*****, level);
    49. player.getInventory().addItem(new ItemStack[] { featherswag1 });
    50. player.updateInventory();
    51. return false;
    52. }
    53. if (!sender.hasPermission("****")) {
    54. sender.sendMessage(*****);
    55. return false;
    56. }
    57. }
    58. return false;
    59. }

    This is my main class. (yes I put stars there for a reason. Why should it implement Listener?
     
  21. Offline

    fireblast709

    Glass_Eater84
    • Do not use Logger.getLogger("Minecraft"), use the getLogger() method you get by extending JavaPlugin
    • No need for printing enabled/disabled messages, Bukkit does this for you
    • Just use the EntityDamageEvent like mentioned before. Properly implementing it with the methods found in the documentation is not rocket science.
    • If something doesn't work, debug it. Debug it till the point where you can see every single method call and every single logical operation/assignment, and you are guaranteed to find the cause for any bug.
    • Do not perform any cast before you can guarantee that the instance is indeed the subclass you cast it to.
      Code:java
      1. instanceof
      will provide you that check in most cases, and in the case you need to check if something is a certain Material (for example if you want a chest inventory), Material comparison is more efficient than constructing a BlockState.
      Code:java
      1. block.getType() == Material.CHEST
    For the rest of the people: if you spoonfeed, get out.
     
  22. Offline

    AoH_Ruthless

    Glass_Eater84
    You are extending JavaPlugin in your listener class. Don't do that .... Only one class can inherit JavaPlugin. This is your main class.
     
  23. Offline

    werter318

    Glass_Eater84 Learn basic Java and Bukkit is what you should do, a problem as simple as this doesn't need a 2-paged thread. I'm sorry.

    Glass_Eater84 Stating that you are a beginner at java doesn't change the fact that it is not good to start coding Bukkit if you don't know basic Java.

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

    Glass_Eater84

    AoH_Ruthless fireblast709 teej107 Ok cool! So this worked. However its by every entity.
    Code:java
    1. @EventHandler
    2. public void onPlayerDamageEvent(EntityDamageEvent e) {
    3. Player p = (Player) e.getEntity();
    4. if (p.getLastDamageCause().getCause() == DamageCause.LAVA){
    5. p.setHealth(0.0);
    6. }
    7. }
    8. }

    EDIT: its only when you spawn them
     
  25. Offline

    fireblast709

    Glass_Eater84 the only thing that you miss is an instanceof Player check ;3
     
  26. Offline

    Glass_Eater84

    fireblast709 thanks bud! Its working! teej107 Also, it spams the chat... http://i.imgur.com/hYiHzse.jpg I think its because essentials is registering it as someone dying every time they lose a heart. Is there anyway to fix this within the code? Or is it something with essentials, that can not be fixed.
     
  27. Offline

    teej107

    Glass_Eater84 Add in debug code in your event. In your event, the player doesn't lose hearts. You are just setting their health to 0.
     
Thread Status:
Not open for further replies.

Share This Page