Modify Player Max Health

Discussion in 'Plugin Development' started by Shad, Oct 9, 2012.

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

    Shad

    Heroes and other plugins alike can modify the player health pool and after sometime sifting through their code, I couldn't figure out how they were doing it. Are they modifying the amount of damage done to the player? It would be wonderful if someone could inform me on how this is being done.
     
  2. Offline

    Bit

  3. Yea, if you do something like
    event.setDamage(event.getDamage() / 2);
    in the event you would emulate a health bar 2x the size of a normal. A problem could be handling a damage of one, as the damage uses ints you can't set 0.5...
     
  4. Offline

    Comphenix

    Yes, they're setting the damage value of the event.

    Now, since the health is stored as an integer, it's not possible to simply remove 0.5 health. But you can do one of two things:
    1. Use a HashMap to store the actual health of a player. This value would be under your control, so you could easily set the maximum to 100 instead of 20. Then, whenever the player receives damage you decrement your own health variable with that amount instead. Then you set the damage amount to:
      Code:
      int correspondingHealth = (int) (event.getPlayer().getMaxHealth() * (YOUR_MAXIMUM / (double)YOUR_VARIABLE));
      event.setDamage(event.getPlayer.getHealth() - correspondingHealth);
    2. Handle fractional damage values by using Math.random() to either round up or round down. So, if you have a damage value of 0.4, you should give the chance of rounding down 60% and rounding up 40%.

      This is basically what I did in ExperienceMod, though for experience instead of damage. You can find my implementation here.
     
  5. Offline

    andf54

    How did you handle armour?
     
  6. Offline

    Comphenix

    Oh, I didn't really.

    But armor damage mitigation is multiplicative, so it doesn't matter if you apply the damage reduction before or after. A * B = B * A, you know.
     
  7. Offline

    andf54

    But event.getDamage() doesn't include damage reduction from armour. Health will decrease like there is no armour. Or am I missing something?

    You will be removing -5 damage from the custom variable whether the player has armour or not.

    Also you can write [A,B] = 0 :p
     
  8. Offline

    gamerzap

    Make a method to get the players armor strength:
    PHP:
    public double getDamagedReduced(Player p){
            
    org.bukkit.inventory.PlayerInventory inv p.getInventory();
            
    ItemStack boots inv.getBoots();
            
    ItemStack helmet inv.getHelmet();
            
    ItemStack chest inv.getChestplate();
            
    ItemStack pants inv.getLeggings();
            
    double red 0.0;
            if(
    helmet.getType() == Material.LEATHER_HELMET)red red 0.04;
            else if(
    helmet.getType() == Material.GOLD_HELMET)red red 0.08;
            else if(
    helmet.getType() == Material.CHAINMAIL_HELMET)red red 0.08;
            else if(
    helmet.getType() == Material.IRON_HELMET)red red 0.08;
            else if(
    helmet.getType() == Material.DIAMOND_HELMET)red red 0.12;
            
    //
            
    if(boots.getType() == Material.LEATHER_BOOTS)red red 0.04;
            else if(
    boots.getType() == Material.GOLD_BOOTS)red red 0.04;
            else if(
    boots.getType() == Material.CHAINMAIL_BOOTS)red red 0.04;
            else if(
    boots.getType() == Material.IRON_BOOTS)red red 0.08;
            else if(
    boots.getType() == Material.DIAMOND_BOOTS)red red 0.12;
            
    //
            
    if(pants.getType() == Material.LEATHER_LEGGINGS)red red 0.08;
            else if(
    pants.getType() == Material.GOLD_LEGGINGS)red red 0.12;
            else if(
    pants.getType() == Material.CHAINMAIL_LEGGINGS)red red 0.16;
            else if(
    pants.getType() == Material.IRON_LEGGINGS)red red 0.20;
            else if(
    pants.getType() == Material.DIAMOND_LEGGINGS)red red 0.24;
            
    //
            
    if(chest.getType() == Material.LEATHER_CHESTPLATE)red red 0.12;
            else if(
    chest.getType() == Material.GOLD_CHESTPLATE)red red 0.20;
            else if(
    chest.getType() == Material.CHAINMAIL_CHESTPLATE)red red 0.20;
            else if(
    chest.getType() == Material.IRON_CHESTPLATE)red red 0.24;
            else if(
    chest.getType() == Material.DIAMOND_CHESTPLATE)red red 0.32;
            return 
    red;
        }
    Multiply the damage by 1-Armor Strength
     
    andf54 likes this.
  9. Offline

    andf54

    Where did you get those values. Also, what about durability.

    I my plugin has been using the round up/down method, but I want to change it to hp method. I had real trouble implementing it.
     
  10. Offline

    gamerzap

    I got this from a forum page a couple weeks ago, and durability doesn't affect armor strngth since like 1.8
     
    andf54 likes this.
  11. Offline

    andf54

    Comphenix gamerzap

    Tried implementing this and failed miserably. The problem is that if you can't account for armour, because the amount absorbed is random. I did some tests and it seems that it rounds the actual damage up or down based on how close the damage is to those integers (4.4 gives 60% for 4.0 and 40% for 5.0).

    You can take control of the event, by setting the damage to 0 and damaging with entity.damage(...), but you will loose some sounds when doing that (bone cracking when falling).
     
  12. Offline

    bergerkiller

    It is actually possible to change the maxHealth field in CraftBukkit using nms. But, an additional health calculation is the needed when sending messages to the client, and I have no idea how you can prevent that. Maybe overriding the 'player stats packet' with your own that has this health factor in mind.
     
Thread Status:
Not open for further replies.

Share This Page