diff --git a/index.d.ts b/index.d.ts index 1b83acbc7..ed0c3f0a7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { diff --git a/lib/plugins/experience.js b/lib/plugins/experience.js index 1c8334ac3..b78bef504 100644 --- a/lib/plugins/experience.js +++ b/lib/plugins/experience.js @@ -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)); + } + }