Solved Set health of a Player by calling events

Discussion in 'Plugin Development' started by Kaelinator, Nov 7, 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.
     
  2. Offline

    MaxFireIce

    @Kaelinator
    I believe deprecation makes a difference. Have you tried creating a separate class for each event and then registering each class?
     
  3. Offline

    HeartandSoul

    Just calling the event won't work. All that does is it tells bukkit that an event has occurred. To so something in that event, make a class implementing listener, and use those events in your class.

    Edit: What's the map field in your code?

    Edit 2: Its not a field, Its a static method. Such a dummy am I.
     
  4. Offline

    Zombie_Striker

    @Kaelinator
    As has been said, you can't just call events. Even if you could, this is not what you want because it is easier to use player#setHealth(). I have no idea why you can't use it. It works on all versions of bukkit.
     
  5. Offline

    Kaelinator

    The reason I didn't use player#setHealt() is because I need the respective event to be called when I want to set a player's health.

    I didn't realize it, but, I'll just add player#setHealth() to the bottom of the setPlayerHealth() method.
     
Thread Status:
Not open for further replies.

Share This Page