Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Save this in ILeveling.js ```// Converted From: https://github.com/HypixelDev/PublicAPI/blob/master/Java/src/main/java/net/hypixel/api/util/ILeveling.java
- const BASE = 10000;
- const GROWTH = 2500;
- /* Constants to generate the total amount of XP to complete a level */
- const HALF_GROWTH = 0.5 * GROWTH;
- /* Constants to look up the level from the total amount of XP */
- const REVERSE_PQ_PREFIX = -(BASE - 0.5 * GROWTH) / GROWTH;
- const REVERSE_CONST = REVERSE_PQ_PREFIX * REVERSE_PQ_PREFIX;
- const GROWTH_DIVIDES_2 = 2 / GROWTH;
- /**
- * This method returns players network level. Uses direct values from the API.
- */
- function getTrueLevel(networkExp, networkLevel) {
- return getLevel(networkExp + getTotalExpToLevel(networkLevel + 1))
- }
- /**
- * This method returns the level of a player calculated by the current experience gathered. The result is
- * a precise level of the player The value is not zero-indexed and represents the absolute visible level
- * for the player.
- * The result can't be smaller than 1 and negative experience results in level 1.
- * <p>
- * Examples:
- * - 0 XP = 1.0
- * - 5000 XP = 1.0
- * - 10000 XP = 2.0
- * - 50000 XP = 4.0
- * - 79342431 XP = 249.0
- *
- * @param exp Total experience gathered by the player.
- * @return number level of player (Smallest value is 1.0)
- */
- function getLevel(exp) {
- return exp <= 1 ? 1 : Math.floor(1 + REVERSE_PQ_PREFIX + Math.sqrt(REVERSE_CONST + GROWTH_DIVIDES_2 * exp));
- }
- /**
- * This method returns the level of a player calculated by the current experience gathered. The result is
- * a precise level of the player The value is not zero-indexed and represents the visible level
- * for the player.
- * The result can't be smaller than 1 and negative experience results in level 1.
- * <p>
- * Examples:
- * - 0 XP = 1.0
- * - 5000 XP = 1.5
- * - 10000 XP = 2.0
- * - 50000 XP = 4.71...
- * - 79342431 XP = 249.46...
- *
- * @param exp Total experience gathered by the player.
- * @return Exact level of player (Smallest value is 1.0)
- */
- function getExactLevel(exp) {
- return getLevel(exp) + getPercentageToNextLevel(exp);
- }
- /**
- * This method returns the amount of experience that is needed to progress from level to level + 1. (5 to 6)
- * The levels passed must absolute levels with the smallest level being 1. Smaller values always return
- * the BASE constant. The calculation is precise and if a decimal is passed it returns the XP from the
- * progress of this level to the next level with the same progress. (5.5 to 6.5)
- * <p>
- * Examples:
- * - 1 (to 2) = 10000.0 XP
- * - 2 (to 3) = 12500.0 XP
- * - 3 (to 4) = 15000.0 XP
- * - 5 (to 6) = 20000.0 XP
- * - 5.5 (to 6.5) = 21250.0 XP
- * - 130 (to 131) = 332500.0 XP
- * - 250 (to 251) = 632500.0 XP
- *
- * @param level Level from which you want to get the next level with the same level progress
- * @return number to reach the next level with same progress
- */
- function getExpFromLevelToNext(level) {
- return level < 1 ? BASE : GROWTH * (level - 1) + BASE;
- }
- /**
- * This method returns the experience it needs to reach that level. If you want to reach the given level
- * you have to gather the amount of experience returned by this method. This method is precise, that means
- * you can pass any progress of a level to receive the experience to reach that progress. (5.764 to get
- * the experience to reach level 5 with 76.4% of level 6.
- * <p>
- * Examples:
- * - 1.0 = 0.0 XP
- * - 2.0 = 10000.0 XP
- * - 3.0 = 22500.0 XP
- * - 5.0 = 55000.0 XP
- * - 5.764 = 70280.0 XP
- * - 130.0 = 21930000.0 XP
- * - 250.43 = 79951975.0 XP
- *
- * @param level The level and progress of the level to reach
- * @return The experience required to reach that level and progress
- */
- function getTotalExpToLevel(level) {
- const lv = Math.floor(level), x0 = getTotalExpToFullLevel(lv);
- if (level === lv) return x0;
- return (getTotalExpToFullLevel(lv + 1) - x0) * (level % 1) + x0;
- }
- /**
- * Helper method that may only be called by full levels and has the same functionality as getTotalExpToLevel()
- * but doesn't support progress and returns wrong values for progress due to perfect curve shape.
- *
- * @param level Level to receive the amount of experience to
- * @return Experience to reach the given level
- */
- function getTotalExpToFullLevel(level) {
- return (HALF_GROWTH * (level - 2) + BASE) * (level - 1);
- }
- /**
- * This method returns the current progress of this level to reach the next level. This method is as
- * precise as possible due to rounding errors on the mantissa. The first 10 decimals are totally
- * accurate.
- * <p>
- * Examples:
- * - 5000.0 XP (Lv. 1) = 0.5 (50 %)
- * - 22499.0 XP (Lv. 2) = 0.99992 (99.992 %)
- * - 5324224.0 XP (Lv. 62) = 0.856763076923077 (85.6763076923077 %)
- * - 23422443.0 XP (Lv. 134) = 0.4304905109489051 (43.04905109489051 %)
- *
- * @param exp Current experience gathered by the player
- * @return Current progress to the next level
- */
- function getPercentageToNextLevel(exp) {
- const lv = getLevel(exp), x0 = getTotalExpToLevel(lv);
- return (exp - x0) / (getTotalExpToLevel(lv + 1) - x0);
- }
- //xp - getTotalExpToLevel(ILeveling.getLevel(XP))
- module.exports = {
- getTrueLevel,
- getLevel,
- getExactLevel,
- getExpFromLevelToNext,
- getTotalExpToLevel,
- getTotalExpToFullLevel,
- getPercentageToNextLevel
- };```
- 2. Load the file in your bot `const ILeveling = require("./ILeveling")`
- 3. Call the function `const level = ILeveling.getTrueLevel(networkExp || 0, networkLevel || 0)`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement