Java hates me today, Math not working

Discussion in 'Bukkit Discussion' started by Plo124, Apr 17, 2014.

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

    Plo124

    I am working on an RPG server, and so I have rewritten the health system, all works well except for when I go to set the health to a % of the health out of the max health.

    Whenever I take damage, it instantly kills me, nomatter what.
    I have identified this to be to Maths, this is my code
    Code:java
    1. float newHealth = (health / maxhealth) * 20;

    This should work, lets substitute values:
    health is 25
    maxhealth is 50
    so health divided by maxhealth would be 0.5.
    0.5 times 20 = 10, which would mean 5 hearts left.

    What Java is doing is returning 0, being an int, when I do health/maxhealth.
    Why is this so?
     
  2. Offline

    DoingItWell

    Perhaps (health * 20) / maxhealth.
     
    asofold likes this.
  3. I'd even make it health * 20f / maxhealth , variable are floats and constants are floats - less confusion potential,
     
  4. Offline

    Plo124

    DoingItWell
    Ok now Im doing this with Mana, which is out of 100. i made it take 10% exp bar, but it sets it to 0 now.

    Also I want to know why it uses ints now but it didnt used to
     
  5. Offline

    Necrodoom

    Paste the code so we can see what type are your variables.
    Also, in future, know this kind of questions belong to 'Plugin Development' section, you will get more responses there.
     
  6. Offline

    Qwahchees

    Hey Plo,

    I've bumped into this before. Divide using 20f (20 in float, not int). I don't know why Java does that either but this works.

    Cheers.
     
Thread Status:
Not open for further replies.

Share This Page