Solved Event to check if player got in to lava?

Discussion in 'Plugin Development' started by Arencos, Jun 29, 2020.

  1. Offline

    Arencos

    I am trying to make a plugin where everything is harder and I want to add something like this: If the player touches lava, they instantly get killed. I tried EntityDamageByBlockEvent but I cant check if its lava and kill the player with EntityDamageByBlockEvent. So is there an event where I can check if the player collided with lava?
     
  2. Online

    timtower Moderator Moderator

  3. Offline

    Arencos

    @timtower thanks!

    @timtower Ok I just had the time to try this, but it doesnt work? Here is my code:

    Code:
    @EventHandler
        public void EntityDamageEvent(EntityDamageEvent event) {
            DamageCause cause = event.getCause();
            Entity player = event.getEntity();
           
            if(cause.equals(Material.LAVA)) {
                player.sendMessage("Do something.");
            }
        }
    
    I tried this, but it doesnt work. Sorry if its just me being dumb, I started learning plugin development today.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2020
  4. Online

    timtower Moderator Moderator

    @Arencos Because you need to check on DamageCause.LAVA
     
  5. Offline

    Arencos

    Ok but how do I apply damage to the player? I can not find it out. If it was a Player player, i could to this: player.setHealth(player.getHealth() - 20);
    But, its an Entity player. I cant find something like setHealth.
    Thanks for answering so quick, by the way! :D
     
  6. Online

    timtower Moderator Moderator

    Check if it is a Player, cast it to Player, apply damage.
     
  7. Offline

    Arencos

    How do I do that? I tried it a bit for like 15 minutes, but I cant find anything. Maybe player.getClass()?? But when I try that it gives an error.
     
  8. Online

    timtower Moderator Moderator

    @Arencos instanceof should be used here
     
  9. Offline

    Arencos

    Hi, Thanks for replying again :D. I wrote some code, but I dont know if it will work or not. I dont have my main laptop with me, so I wrote this from mind. Will this work:
    if(player instanceof Player) {
    ((Player) player).setHealth(((Player) player).getHealth() - 20);
    }

    EDIT: I just noticed I already had a server set up in this laptop :D. It gives an error:
    Code:
     Could not pass event EntityDamageByBlockEvent to Impossible_Mode v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:236) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:828) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:772) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:961) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.damageEntity0(EntityLiving.java:1755) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityHuman.damageEntity0(EntityHuman.java:828) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.damageEntity(EntityLiving.java:1138) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityHuman.damageEntity(EntityHuman.java:745) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityPlayer.damageEntity(EntityPlayer.java:715) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.Entity.burnFromLava(Entity.java:464) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.Entity.entityBaseTick(Entity.java:418) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.entityBaseTick(EntityLiving.java:261) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.Entity.tick(Entity.java:367) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.tick(EntityLiving.java:2338) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityHuman.tick(EntityHuman.java:153) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityPlayer.playerTick(EntityPlayer.java:442) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.PlayerConnection.tick(PlayerConnection.java:143) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.NetworkManager.a(NetworkManager.java:223) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.ServerConnection.c(ServerConnection.java:128) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.b(MinecraftServer.java:1126) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.DedicatedServer.b(DedicatedServer.java:354) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.a(MinecraftServer.java:1007) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:846) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    Caused by: java.lang.IllegalArgumentException: Health must be between 0 and 20.0(-14.666666030883789)
            at org.bukkit.craftbukkit.v1_16_R1.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:110) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at me.arencos.impossiblemode.events.EventsClass.EntityDamageEvent(EventsClass.java:55) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            ... 28 more
    [22:31:22] [Server thread/ERROR]: Could not pass event EntityDamageByBlockEvent to Impossible_Mode v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:236) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:828) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:772) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:961) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.damageEntity0(EntityLiving.java:1755) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityHuman.damageEntity0(EntityHuman.java:828) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.damageEntity(EntityLiving.java:1130) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityHuman.damageEntity(EntityHuman.java:745) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityPlayer.damageEntity(EntityPlayer.java:715) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.Entity.burnFromLava(Entity.java:464) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.Entity.entityBaseTick(Entity.java:418) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.entityBaseTick(EntityLiving.java:261) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.Entity.tick(Entity.java:367) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityLiving.tick(EntityLiving.java:2338) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityHuman.tick(EntityHuman.java:153) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.EntityPlayer.playerTick(EntityPlayer.java:442) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.PlayerConnection.tick(PlayerConnection.java:143) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.NetworkManager.a(NetworkManager.java:223) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.ServerConnection.c(ServerConnection.java:128) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.b(MinecraftServer.java:1126) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.DedicatedServer.b(DedicatedServer.java:354) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.a(MinecraftServer.java:1007) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:846) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    Caused by: java.lang.IllegalArgumentException: Health must be between 0 and 20.0(-19.66666603088379)
            at org.bukkit.craftbukkit.v1_16_R1.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:110) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            at me.arencos.impossiblemode.events.EventsClass.EntityDamageEvent(EventsClass.java:55) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot.jar:git-Spigot-758abbe-2b00831]
            ... 28 more
    OOOH, I just figured it out! Thanks for your help :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2020

Share This Page