EntityDamageEvent - transfer Player-Damage to other Players

Discussion in 'Plugin Development' started by VNGC, May 10, 2020.

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

    VNGC

    Hey, i want to transfer damage of a player to all other online players. so if for ex. "player a" gets 6HP damage, then "player b" and "player c" are getting the damage too. i tried this, but everytime a player gets damage, it doesnt stop damaging all players till he dies. idk what i did wrong.

    Code:
        @EventHandler
        public void damage(EntityDamageEvent event) {
    
            if ((Settings.splithearts) && (Main.timerRunning)) {
                if (event.getEntity() instanceof Player) {
    
                    double damage = event.getEntity().getLastDamageCause().getFinalDamage();
    
                    for (Player players : Bukkit.getOnlinePlayers()) {
                        players.damage(damage);
    
                    }
                }
            }
        }
     
  2. Offline

    KarimAKL

    @VNGC The 'players.damage(damage)' line is throwing the event again, causing an infinite loop until they die.

    You should set their health using #setHealth(getHealth() - damage) instead.
     
  3. Offline

    VNGC

    yeah, but i want it so that the players are getting the damage animation too. and aswell the player that is causing the damage is getting double damage
     
  4. Offline

    bowlerguy66

    You could add all players you're damaging to a list that exempts them from activating the event you posted; make sure you remove them from the list so when they receive damage from something other than the code it calls properly.
     
  5. Offline

    VNGC

    would this work too?
    Code:
    public void damage(EntityDamageEvent event) {
            if ((Settings.splithearts) && (Main.timerRunning)) {
                if (event.getEntity() instanceof Player) {
                    double damage = event.getEntity().getLastDamageCause().getFinalDamage();
    
                    for (Player players : Bukkit.getOnlinePlayers()) {
                        ((Player) event.getEntity()).setLastDamage(0);
                        players.setHealth(players.getHealth() - damage);
                    }
     
  6. Offline

    KarimAKL

    @VNGC You can play the "HURT" animation using Player#playEffect(EntityEffect.HURT).

    Yes, that should work.
     
  7. Offline

    VNGC

    i am still getting the double damage... hmm
     
  8. Offline

    bowlerguy66

    You're probably getting double damage because, in the code you posted, you're adding the damage to ALL online players, including the player that causes the damage event.
     
  9. Offline

    VNGC

    well how can i get the damage cause? as
    Code:
     if(event.getEntity().getLastDamageCause() == DamageCause.DROWNING)
    doesnt seem to work.

    then i could cancel the event, set the damage to all onlinePlayers and play the Animation.

    okay i got it work... but one thing, if the players have for ex. 2 hearts and a player gets more than this 2, nobody dies, and in console it shows up this:

    Code:
    Health must be between 0 and 20.0(-10.0)
    
    my code now:

    Code:
    package de.vngc.vutils.listeners;
    
    import org.bukkit.*;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    
    public class SplitHP implements Listener {
    
        @EventHandler
        public void damage(EntityDamageEvent event) {
            if ((Settings.splithearts) && (Main.timerRunning)) {
                if (event.getEntity() instanceof Player) {
                    double damage = event.getFinalDamage();
                    event.setCancelled(true);
                    for (Player players : Bukkit.getOnlinePlayers()) {
                        players.setHealth(players.getHealth() - damage);
                        players.playEffect(EntityEffect.HURT);
                    }
                }
            }
        }
    
    }
    
    edit: how can i delete my comment? i saw that i didnt editted, so sry but idk how to delete this

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

    bowlerguy66

    When you do the math for setting the damage test if it is below 0. If it is, kill the entity.
     
  11. Offline

    KarimAKL

    @VNGC Ah, i thought you wanted the double damage, that's my bad. :p
    You can simply check if it's the same player in the loop, instead of cancelling the event.

    Set the player's health to "Math.max(0, players.getHealth() - damage)", that way you won't have to worry about negatives.
     
Thread Status:
Not open for further replies.

Share This Page