Health decimal

Discussion in 'Plugin Development' started by MCraftGamer35, Aug 22, 2014.

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

    MCraftGamer35

    I need help, when the player dies, it returns the health as like 12.63527873
    But instead, I want the health to goto 10, not 20, and not 10 numbers after decimal, like 2.
    For example, I died to someone and they had 6 hearts, it would say,
    Your killer had 6.28 health.
    Code:
    package me.Alex.FrostHealth;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class FrostHealth extends JavaPlugin implements Listener {
       
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onPlayerKill(PlayerDeathEvent e) {
            Player p = e.getEntity();
            Player killer = p.getKiller();
            p.sendMessage("§aYour killers health was §7" + killer.getHealth() + "§a." + "\n§aYou were killed by " + killer.getDisplayName() + "§a.");
        }
    }
    
    Anyways, may you add what needs to go into the code, thanks!
     
  2. Offline

    GeorgeeeHD

  3. Offline

    MCraftGamer35

    That didn't work, the health still goes by 20, and I want 2 decimal numbers after the decimal. Sorry for the troubles. GeorgeeeHD
     
  4. Offline

    macboinc

    You could use
    Math.floor()
    or
    You could do this:
    Code:java
    1. Double health_raw = killer.getHealth();
    2.  
    3. int health = (int) health_raw.intValue();



    Important Notice: don't use lowercase 'd' in Double.
     
  5. Offline

    Necrodoom

    macboinc ...
     
  6. Offline

    MCraftGamer35

    Also, can you add it to the code. I really am kinda new, so I had troubles adding that in the first place.
     
  7. Code:java
    1. p.sendMessage("blahblah" + (int) killer.getHealth() + "blahblah");

    Putting the "(int)" there should work, if not use:
    Code:java
    1. Math.round(killer.getHealth())
     
  8. Offline

    MCraftGamer35

    You don't understand, when the player has full health, it says 20 health, I want it to only go up to 10. and have 2 decimal points, so like: 5.21 hearts. BlubDaLegend
     
  9. Offline

    DevSock

    MCraftGamer35
    EDIT: Watch out, you're going to have major issues here if the thing that killed the player isn't a player. Add some conditionals to make sure the killer is an instance of a player!

    Try this code, I haven't tested it but it should work.

    Code:java
    1. @EventHandler
    2. public void onPlayerKill(PlayerDeathEvent e)
    3. {
    4. Player p = e.getEntity();
    5. if(p.getKiller() instanceof Player)
    6. {
    7. Player killer = p.getKiller();
    8. DecimalFormat df = new DecimalFormat("##.##");
    9. String killerHealth = df.format(killer.getHealth() / 2);
    10. p.sendMessage("§aYour killers health was §7" + killerHealth + "§a." + "\n§aYou were killed by " + killer.getDisplayName() + "§a.");
    11. }
    12. }
     
  10. Offline

    nlthijs48

    MCraftGamer35 Then get the health of the player, divide it by 2 and then round it to 2 decimal places, you can use this piece of code for that:
    Code:java
    1. BigDecimal bigDecimal = new BigDecimal(killer.getHealth()/2.0);
    2. bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP);
    3. double health = bigDecimal.doubleValue();

    At the end 'health' will contain the correct health and can be used in messages and such.
     
  11. Offline

    MCraftGamer35

    Works great, one small problem, it still dosen't say 2.52 it says 2.0 DevSock
     
  12. Offline

    nlthijs48

  13. Offline

    PluginMaker

    double health = ((int)(killer.getHealth() * 100))/100.0;

    this way, if u got 1.2897453154 it will become to 128 (integer) and then go back to 1.28.
     
Thread Status:
Not open for further replies.

Share This Page