Guest User

Bukkit XP Level Calculation

a guest
Nov 24th, 2016
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4. // These values were derived from http://minecraft.gamepedia.com/Experience#leveling_up
  5.  
  6. System.out.println(getMaxLevelForGivenXP(160)); // Should be 10
  7. System.out.println(getMaxLevelForGivenXP(550)); // Should be 20
  8. System.out.println(getMaxLevelForGivenXP(1395)); // Should be 30
  9. System.out.println(getMaxLevelForGivenXP(2920)); // Should be 40
  10. }
  11.  
  12. public static int getMaxLevelForGivenXP(int xp) {
  13. int currentLevel = 0; // Keeps track of what XP level we are at
  14. int remainingXP = xp; // How much XP we have left to spend
  15.  
  16. while (true) { // Infinite loop that will always break when finished
  17. int requiredForNextLevel; // This is based on what the current level is
  18. if (currentLevel <= 16) { // 0-16
  19. requiredForNextLevel = (2 * currentLevel) + 7;
  20. } else if (currentLevel >= 17 && currentLevel <= 31) { // 17-31
  21. requiredForNextLevel = (5 * currentLevel) - 38;
  22. } else { // 32+
  23. requiredForNextLevel = (9 * currentLevel) - 158;
  24. }
  25.  
  26. if (remainingXP >= requiredForNextLevel) { // Check if we have enough XP for the next level
  27. remainingXP -= requiredForNextLevel; // We have enough, subtract the amount required from the remaining
  28. currentLevel++; // Add another level
  29. } else break; // We didn't have enough, we are at the max level available with the given XP
  30. }
  31.  
  32. return currentLevel;
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment