Solved Convert XP to Level

Discussion in 'Plugin Development' started by padili, Jun 16, 2014.

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

    padili

    Hey Guys, what's the formula to convert the given xp to the minecraft level?
     
  2. Offline

    AdamQpzm

    padili This isn't Google. I'll bet there aren't many who've memorised the formula, and how do you think we're going to get to it? Or even those who do know it off by heart - how do you think they learned it?
     
  3. Offline

    padili

    Found it on Minecraft Wiki it works now:
    Code:java
    1. public static int getXPForLevel(int lvl) {
    2. if (lvl <= 15) {
    3. return lvl * 17;
    4. }else if (lvl > 16 && lvl < 31) {
    5. return (int) (1.5 * Math.pow(lvl, 2) - 29.5 * lvl + 360);
    6. }else if (lvl > 30) {
    7. return (int) (3.5 * Math.pow(lvl, 2) - 151.5 * lvl + 2220);
    8. }
    9. return 0;
    10. }

    But I can't convert it back!
     
  4. Offline

    AdamQpzm

    padili What do you mean "convert it back"?
     
  5. Offline

    padili

    Get Level for xp, the opposite
     
  6. Offline

    AdamQpzm

    padili Surely you can figure that out by reading the wiki page you found? They've even got a lovely little table there and everything. :)
     
  7. Offline

    Necrodoom

    padili Since you already have level to XP, simply reverse the math operations to get XP to level.
     
    Konkz and AdamQpzm like this.
  8. Offline

    AdamQpzm

    Necrodoom Who ever said that basic algebra wasn't useful?
     
  9. Offline

    padili

    Got it working:
    Code:java
    1. public static int getXPForLevel(int lvl) {
    2. if (lvl <= 15) {
    3. return lvl * 17;
    4. }else if (lvl > 16 && lvl < 31) {
    5. return (int) (1.5 * Math.pow(lvl, 2) - 29.5 * lvl + 360);
    6. }else if (lvl > 30) {
    7. return (int) (3.5 * Math.pow(lvl, 2) - 151.5 * lvl + 2220);
    8. }
    9. return 0;
    10. }
    11. public static int getLvlForXP(int xp) {
    12. if (xp <= 255) {
    13. return xp / 17;
    14. }else if (xp > 272 && xp < 887) {
    15. return (int) ((Math.sqrt(24 * xp - 5159) + 59) / 6);
    16. }else if (xp > 825) {
    17. return (int) ((Math.sqrt(56 * xp - 32511) + 303) / 14);
    18. }
    19. return 0;
    20. }
     
Thread Status:
Not open for further replies.

Share This Page