Solved DamageEvent ?

Discussion in 'Plugin Development' started by Intelli65, Nov 25, 2020.

  1. Offline

    Intelli65

    I need help with detecting damage taken by a player. Can I listen for a PlayerDamageEvent (Haven't found a event like that yet) If listeners don't work any alternatives ?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Intelli65

    Hmm ok, how can I get the player who received damage, getEntity() Gets an entity right ? So how can I link Player to getEntity()
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Intelli65 You can check if it is a Player and then cast it to one.
     
  5. Offline

    Intelli65

    So this is the code I came up with is the Player p going to be the player here ?

    @EventHandler
    public void onDamage(EntityRegainHealthEvent hlth) {
    Player p = (Player) hlth.getEntity();
    .......
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Intelli65 You need to check if it is a player first.
    Can be any entity.
     
  7. Offline

    Intelli65

    ok the plugin is finally error free I checked and casted it but now it doesn't work when I drop it into the plugins folder
    Main File :
    Code:
    package me.intelligamez.damageone;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.intelligamez.damageone.listeners.DamageListener;
    import me.intelligamez.damageone.listeners.HealthListener;
    
    public class Main extends JavaPlugin {
       
        @Override 
        public void onEnable() {
           
            new DamageListener(this);
            new HealthListener(this);
        }
    }
     
    Last edited by a moderator: Nov 26, 2020
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    Intelli65

    Listener 1:
    Code:
    package me.intelligamez.damageone.listeners;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    
    import me.intelligamez.damageone.Main;
    
    public class DamageListener implements Listener {
    
        private Main plugin;
       
        public DamageListener(Main plugin) {
            this.plugin = plugin;
           
            Bukkit.getPluginManager().registerEvents(this,plugin);
        }
       
        @EventHandler
        public void onDamage(EntityDamageEvent dmg) {
            Entity e = dmg.getEntity();
            if(e instanceof Player) {
            Player p = (Player) e;
            double initialh = p.getHealth();
            double taken = dmg.getFinalDamage();
            double pop = taken*1.25;
            p.setHealth(pop+initialh);
            }
        }
    }
    Listener 2:
    Code:
    package me.intelligamez.damageone.listeners;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityRegainHealthEvent;
    
    import me.intelligamez.damageone.Main;
    
    public class HealthListener implements Listener {
    
        private Main plugin;
       
        public HealthListener(Main plugin) {
            this.plugin = plugin;
           
            Bukkit.getPluginManager().registerEvents(this,plugin);
        }
       
        @EventHandler
        public void onDamage(EntityRegainHealthEvent hlth) {
            Entity e = hlth.getEntity();
            if(e instanceof Player) {
            Player p = (Player) e;
            double afterh = p.getHealth();
            double gain = hlth.getAmount();
            double pop = gain*1.25;
            p.setHealth(afterh-pop);
            }
        }
    }
    Plugin.yml:
    Code:
    main: me.intelligamez.damageone.Main
    name: damageone
    version: 1.0
    author: intelligamez
    description: Srsly who reads this you already know !
    
    Main:
    Code:
    package me.intelligamez.damageone;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.intelligamez.damageone.listeners.DamageListener;
    import me.intelligamez.damageone.listeners.HealthListener;
    
    public class Main extends JavaPlugin {
    
    @Override
    public void onEnable() {
    
    new DamageListener(this);
    new HealthListener(this);
    }
    }
     
    Last edited by a moderator: Nov 26, 2020
  10. Offline

    timtower Administrator Administrator Moderator

    @Intelli65 And what does not work then? How does it not work?
     
  11. Offline

    Intelli65

    I export it as .jar file and put it inside my plugins folder. It doesn't show up when I use /plugins and doesn't run the code as well
     
  12. Offline

    timtower Administrator Administrator Moderator

    @Intelli65 Then take a look at your terminal.
     
  13. Offline

    Intelli65

    me/intelligamez/damageone/Main has been compiled by a more recent version of the Java Runtime (class file version 58.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    Found the problem how can I solve this ?

    Full Log: https://pastebin.com/QscJ834m
     
  14. Offline

    timtower Administrator Administrator Moderator

    @Intelli65 Always look at your logs, they are a life saver for programming.
    You need to change the java that your IDE is using, change it to 8
     
  15. Offline

    Intelli65

    Thanks a lot timtower you're a legend :)
     
    timtower likes this.

Share This Page