Round to the nearest half a heart?

Discussion in 'Plugin Development' started by edm wasrevilosmith1999, Mar 27, 2014.

Thread Status:
Not open for further replies.
  1. I have this as part of my plugin which will send a message to someone when they died how many hearts their killer was on. Here is the code, How can i round the hearts to the nearest half a heart?
    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e){
    3.  
    4. Player killed = e.getEntity();
    5. Player killer = e.getEntity().getKiller();
    6.  
    7. if((killed instanceof Player) && (killer instanceof Player)){
    8.  
    9. killed.sendMessage(ChatColor.AQUA + "[Minecentre] " + ChatColor.YELLOW + "Your killer was " + ChatColor.AQUA + killer.getName() + ChatColor.YELLOW + " and they were on" + ChatColor.AQUA + killer.getHealth()/2 + ChatColor.YELLOW + " hearts!");
    10. killer.sendMessage(ChatColor.AQUA + "[Minecentre] " + ChatColor.RED + "You have slain " + e.getEntity().getName());}
    11. e.setDeathMessage(ChatColor.AQUA + "[Minecentre] " + killed.getName() + ChatColor.YELLOW + " was killed by " + ChatColor.RED + killer.getName());
    12.  
    13. }


    Should I create a string and then round it like that?
    So i do
    String hearts - killer.getHealth()/2
    But how would i round that to nearest 0.5?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    AmShaegar

    Edit:
    I may have misunderstood your problem. I think your problem is, that you are dividing two integers. In Java 5/2 is 2. Because it will always ignore the remainder.

    What you need to do is to either cast to double or float or just devide by 2.0.
     
  3. killer.getHealth()/2.0?
     
  4. Offline

    JBoss925

    yes
     
  5. Offline

    jacob99200

    is there a way to heal a player once he is down to half a heart please i need to know
     
  6. Offline

    mythbusterma

  7. Offline

    BeefySticks

    edm wasrevilosmith1999

    Hmm, this is pretty simple. You just round it. Open wide, a spoonfeed is coming!

    Code:java
    1. int i = (int)Math.round(player.getKiller().getHealth());
    2. double health = i;
    3. if (health < 1.0D) {
    4. health = 1.0D;
    5. }
    6. player.sendMessage(ChatColor.RED + "Your Beefy killer was on " + health / 2.0D + " hearts :(");
     
    GrandmaJam and Skionz like this.
Thread Status:
Not open for further replies.

Share This Page