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