Development Assistance Set health of a Player by calling events

Discussion in 'Plugin Help/Development/Requests' started by Kaelinator, Nov 6, 2016.

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

    Kaelinator

    So, instead of using player.setHealth(...), I need to use events such as EntityDamageEvent and EntityRegainHealthEvent. I've used Bukkit.getServer.getPluginManager().callEvent(new EntityRegainHealthEvent(...)) and such, and they call, but, the players health isn't changed to the amount specified withing the event's constructor.

    Also, for the EntityDamageEvent, the constructor I'm using is deprecated, which may have made a difference, yet the EntityRegainHealthEvent isn't and it still doesn't actually change the health.

    Here's my code.
    Code:
    @SuppressWarnings("deprecation")
        public static void setPlayerHealth(UUID id, double health) {
    
            double amount = health - getPlayerHealth(id);
    
            if (amount > 0) {
                Bukkit.getServer().getPluginManager()
                        .callEvent(new EntityRegainHealthEvent((Entity) Bukkit.getPlayer(id), amount, RegainReason.CUSTOM));
                Bukkit.broadcastMessage(Map.getPlayerHealth(id) + "");
            } else {
                Bukkit.getServer().getPluginManager()
                        .callEvent(new EntityDamageEvent((Entity) Bukkit.getPlayer(id), DamageCause.CUSTOM, amount));
                Bukkit.broadcastMessage(Map.getPlayerHealth(id) + "");
            }
        }
    Again, the EntityDamageEvent and EntityRegainHealthEvent are actually being called (I have event handlers flagging them) but the player's health isn't actually changed, that's the issue.

    Also, I'm not too sure how to use the constructor for EntityDamageEvent that isn't deprecated, but I don't think it makes a difference.
     
    Last edited: Nov 6, 2016
Thread Status:
Not open for further replies.

Share This Page