Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added converter functions for Experience points and levels #3121

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ export interface Experience {
level: number
points: number
progress: number
convertLevelsToXpPoints: (level: number) => number | null
convertXpPointsToLevels: (xpPoints: number) => number | null
}

export interface PhysicsOptions {
Expand Down
23 changes: 23 additions & 0 deletions lib/plugins/experience.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,27 @@ function inject (bot) {
bot.experience.progress = packet.experienceBar
bot.emit('experience')
})

// SOURCE: https://minecraft.fandom.com/wiki/Experience#Leveling_up
bot.experience.convertLevelsToXpPoints = function (level) {
if (level < 0) return null;

if (level >= 0 && level <= 16) return level ** 2 + 6 * level;

if (level >= 17 && level <= 31) return 2.5 * level ** 2 - 40.5 * level + 360;

if (level >= 32) return 4.5 * level ** 2 - 162.5 * level + 2220;
}

// SOURCE: https://minecraft.fandom.com/wiki/Experience#Leveling_up
bot.experience.convertXpPointsToLevels = function(xpPoints) {
if (xpPoints < 0) return null;

if (xpPoints >= 0 && xpPoints <= 352) return Math.sqrt(xpPoints + 9) - 3;

if (xpPoints >= 353 && xpPoints <= 1507) return 81 / 10 + Math.sqrt((2 / 5) * (xpPoints - 7839 / 40));

if (xpPoints >= 1508) return 325 / 18 + Math.sqrt((2 / 9) * (xpPoints - 54215 / 72));
}

}