Any method that gets hearts

Discussion in 'Plugin Development' started by MonkeyPlays, Feb 23, 2014.

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


    Hello bukkit, I was relived when getHealth(); worked I was really happy but I realized it was not what I needed because this is what would come up player was killed by play 4.2234567667 hearts which is annoying if u ask me so is there a method to get the HEARTS that the player is on instead of the health.

    Thank you.
  2. Offline


    I believe hearts are just health / 2.

    (Someone correct me if I'm wrong)

    Use Math.floor to round down to the nearest whole number of hearts. Rounding to the nearest half of a heart might be trickier, I'm sure there's some clever math to do that but offhand I can't think of how :)
    Datdenkikniet and FirecatHD like this.
  3. Offline


  4. Offline


    Assist it doesn't seem to work at all it's the same thing, heres my code, correct me if im wrong.

    package me.BuissnessMonkey.RadeArmor;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    public class DeathMessage implements Listener {
        public void onDeath(PlayerDeathEvent e){
            double hearts = e.getEntity().getKiller().getHealth();
            String killer = e.getEntity().getKiller().getName();
            String killed = e.getEntity().getName();
            Player SendKiller = (Player) e.getEntity().getKiller();
            Player SendKilled = (Player) e.getEntity();
            SendKiller.sendMessage(ChatColor.WHITE + killed + " " + ChatColor.RED + "was destroyed by" + ChatColor.WHITE + " " + killer + " " + hearts + ChatColor.RED + "♥");
            SendKilled.sendMessage(ChatColor.WHITE + killed + " " + ChatColor.RED + "was destroyed by" + ChatColor.WHITE + " " + killer + " " + hearts + ChatColor.RED + "♥");
    And im also generally not good in math so I didn't get how to go on about this.
  5. Offline


    Why are you doing Floor and Round?
    If you're trying to get his hearts you should just do something like
    1. double hearts = e.getEntity().getKiller().getHealth() / 2;
  6. Offline


    McMhz do you know how to fix the decimal numbers?
  7. Offline


    I had suggested Floor as a way to get the nearest whole number of hearts (the OP's main issue is how to display the number without a lot of decimal places).

    If you want to print this, I'm assuming you want a string, ultimately? If that's the case, this should work, though I still feel like this should be doable with pure math and not logic :)

    1. // Start with the full-precision health value
    2. double health = e.getEntity().getKiller().getHealth();
    4. // Use Math.floor to find the nearest whole number of hearts
    5. // e.g. 2.3 -> 2, 5.8 -> 5
    6. int wholeHearts = (int)Math.floor(health);
    8. // Now find the remainder portion
    9. // e.g. 2.3 -> 0.3, 5.8 -> 0.8
    10. double remainder = health - (double)wholeHearts;
    12. // Start building a string representation of our health.
    13. String displayHealth = wholeHearts;
    15. // Now figure out if we have half a heart or not.
    16. if (remainder > 0.5) displayHealth += ".5";
    18. // displayHealth now has a string which will be the player's heart level.
    19. // This should either be a whole number (1, 2, 4, 10) or a number with a 0.5 at the end.

    Hope that helps!
  8. MonkeyPlays, it´s not a math problem.

    You get the double hearts, then you floor it and do nothing with result, then you round it and again do nothing with result.
    you should do:
    1. double hearts = e.getEntity().getKiller().getHealth();
    2. hearts = Math.floor(hearts); // you must place the result back in your variable

    Then the Math.round() is redundant
  9. Offline


    1. Integer intHearts = Integer.parseInt(hearts);

    I guess that'd work, Might not I didn't test at all
  10. Offline


    McMhz parseint only works with strings
  11. Offline


    meh I dont know then
  12. Offline


    Maybe just throw a String into it? ;)

    1. Integer intHearts = Integer.parseInt(String.valueOf(hearts));

    or something like

    1. int intHearts = Double.valueOf(hearts).intValue();
Thread Status:
Not open for further replies.

Share This Page