-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/artillexstudios/axgraves/utils/ExperienceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.artillexstudios.axgraves.utils; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public final class ExperienceUtils { | ||
// credit: https://gist.githubusercontent.com/Jikoo/30ec040443a4701b8980/raw/0745ca25a8aaaf749ba2f2164a809e998f6a37c4/Experience.java | ||
|
||
public static int getExp(@NotNull Player player) { | ||
return getExpFromLevel(player.getLevel()) + Math.round(getExpToNext(player.getLevel()) * player.getExp()); | ||
} | ||
|
||
public static int getExpFromLevel(int level) { | ||
if (level > 30) { | ||
return (int) (4.5 * level * level - 162.5 * level + 2220); | ||
} | ||
if (level > 15) { | ||
return (int) (2.5 * level * level - 40.5 * level + 360); | ||
} | ||
return level * level + 6 * level; | ||
} | ||
|
||
public static double getLevelFromExp(long exp) { | ||
int level = getIntLevelFromExp(exp); | ||
|
||
// Get remaining exp progressing towards next level. Cast to float for next bit of math. | ||
float remainder = exp - (float) getExpFromLevel(level); | ||
|
||
// Get level progress with float precision. | ||
float progress = remainder / getExpToNext(level); | ||
|
||
// Slap both numbers together and call it a day. While it shouldn't be possible for progress | ||
// to be an invalid value (value < 0 || 1 <= value) | ||
return ((double) level) + progress; | ||
} | ||
|
||
public static int getIntLevelFromExp(long exp) { | ||
if (exp > 1395) { | ||
return (int) ((Math.sqrt(72 * exp - 54215D) + 325) / 18); | ||
} | ||
if (exp > 315) { | ||
return (int) (Math.sqrt(40 * exp - 7839D) / 10 + 8.1); | ||
} | ||
if (exp > 0) { | ||
return (int) (Math.sqrt(exp + 9D) - 3); | ||
} | ||
return 0; | ||
} | ||
|
||
private static int getExpToNext(int level) { | ||
if (level >= 30) { | ||
// Simplified formula. Internal: 112 + (level - 30) * 9 | ||
return level * 9 - 158; | ||
} | ||
if (level >= 15) { | ||
// Simplified formula. Internal: 37 + (level - 15) * 5 | ||
return level * 5 - 38; | ||
} | ||
// Internal: 7 + level * 2 | ||
return level * 2 + 7; | ||
} | ||
|
||
public static void changeExp(Player player, int exp) { | ||
exp += getExp(player); | ||
|
||
if (exp < 0) { | ||
exp = 0; | ||
} | ||
|
||
double levelAndExp = getLevelFromExp(exp); | ||
int level = (int) levelAndExp; | ||
player.setLevel(level); | ||
player.setExp((float) (levelAndExp - level)); | ||
} | ||
} |