Class LevelManager
java.lang.Object
tfagaming.projects.minecraft.homestead.managers.LevelManager
A utility class that manages
Level progression for regions.-
Method Summary
Modifier and TypeMethodDescriptionstatic longaddRandomXp(long regionId, double min, double max) Add random XP between min and max (inclusive).static longaddRandomXp(long regionId, long min, long max) Add random XP with integer bounds.static voidaddXp(long regionId, long amount) Add fixed XP to a region.static voidaddXpPercentage(long regionId, double percentage) Add XP as a percentage of the XP required for the next level.static intRemoves all level entries with invalid references:
- Regions that no longer existstatic LevelcreateLevel(long regionId) Create a new level entry for a region.static voiddeleteLevel(long id) Permanently deletes the specified level.static voiddeleteLevelByRegion(long regionId) Delete level by region ID.static voiddelevel(long regionId, int levels) Removes levels from a region without going below 0.static LevelfindLevel(long id) Retrieves the level with the exact ID, or null if none exists.getAll()Returns an immutable view of every loaded level.Returns all region IDs that have a level entry.static doubleReturns the server-wide average region level.static LevelFinds the region with the highest level.static longFinds the region ID with the highest level.static longgetLevelAge(long regionId) Returns how many milliseconds ago the level entry was created.static LevelgetLevelByRegion(long regionId) Get level by region ID.static intReturns the number of levels in the server.static intgetLevelDifference(long regionIdA, long regionIdB) Calculates the level difference between two regions.Returns a histogram of level distribution.static doublegetLevelProgressPercentage(long regionId) Returns the current progress percentage toward the next level.static LevelgetOrCreateLevel(long regionId) Get or create level for a region.static intgetRank(long regionId) Get region's rank on leaderboard.getRegionsAboveLevel(int minLevel) Returns all region IDs above the specified level threshold.getRegionsAtLevel(int level) Returns all region IDs at exactly the specified level.getTopLevels(int limit) Get top levels sorted by level (desc), then by XP (desc).static longgetTotalXpForLevel(int targetLevel) Returns the XP required to reach a specific level from level 0.static longgetTotalXpOfRegion(long regionId) Returns the total accumulated XP (including spent on levels) for a region.static longgetXpUntilNextLevel(long regionId) Returns the remaining XP needed to reach the next level.static voidgrantLevels(long regionId, int levels) Grants multiple levels at once.static booleanisMaxLevel(long regionId, int maxLevel) Checks if a region has reached or exceeded a maximum level cap.static voidmultiplyXp(long regionId, double factor) Multiplies the current XP progress by a factor (useful for boosters).static voidremoveXp(long regionId, long amount) Remove XP from a region (won't decrease level, only progress).static intResets every level in the cache.static voidresetLevel(long regionId) Reset level to 0.static voidsetLevel(long regionId, int level) Set exact level (resets progress to 0).static voidsetXp(long regionId, long experience) Set exact XP amount (triggers level re-calculation).
-
Method Details
-
createLevel
Create a new level entry for a region.- Parameters:
regionId- The region ID- Returns:
- The created Level, or
nullif one already exists.
-
getAll
Returns an immutable view of every loaded level.- Returns:
- List of all levels.
-
getAllRegions
Returns all region IDs that have a level entry.- Returns:
- List of region IDs.
-
getLevelByRegion
Get level by region ID.- Parameters:
regionId- The region ID- Returns:
- The Level, or
nullif not found.
-
findLevel
Retrieves the level with the exact ID, or null if none exists.- Parameters:
id- The level ID- Returns:
- The Level, or
null.
-
getOrCreateLevel
Get or create level for a region.- Parameters:
regionId- The region ID- Returns:
- The existing or newly created Level.
-
getLevelCount
public static int getLevelCount()Returns the number of levels in the server.- Returns:
- The level count.
-
deleteLevel
public static void deleteLevel(long id) Permanently deletes the specified level.- Parameters:
id- The level ID
-
deleteLevelByRegion
public static void deleteLevelByRegion(long regionId) Delete level by region ID.- Parameters:
regionId- The region ID
-
addXp
public static void addXp(long regionId, long amount) Add fixed XP to a region.- Parameters:
regionId- The region IDamount- Amount of XP to add
-
addXpPercentage
public static void addXpPercentage(long regionId, double percentage) Add XP as a percentage of the XP required for the next level.- Parameters:
regionId- The region IDpercentage- Percentage of next level XP (0.0–100.0)
-
multiplyXp
public static void multiplyXp(long regionId, double factor) Multiplies the current XP progress by a factor (useful for boosters).- Parameters:
regionId- The region IDfactor- The multiplier (e.g., 2.0 for double)
-
addRandomXp
public static long addRandomXp(long regionId, double min, double max) Add random XP between min and max (inclusive).- Parameters:
regionId- The region IDmin- Minimum XP (can be double, will be floored)max- Maximum XP (can be double, will be floored)- Returns:
- The actual amount of XP added.
-
addRandomXp
public static long addRandomXp(long regionId, long min, long max) Add random XP with integer bounds.- Parameters:
regionId- The region IDmin- Minimum XPmax- Maximum XP- Returns:
- The actual amount of XP added.
-
removeXp
public static void removeXp(long regionId, long amount) Remove XP from a region (won't decrease level, only progress).- Parameters:
regionId- The region IDamount- Amount of XP to remove
-
setXp
public static void setXp(long regionId, long experience) Set exact XP amount (triggers level re-calculation).- Parameters:
regionId- The region IDexperience- XP amount
-
setLevel
public static void setLevel(long regionId, int level) Set exact level (resets progress to 0).- Parameters:
regionId- The region IDlevel- Level to set
-
grantLevels
public static void grantLevels(long regionId, int levels) Grants multiple levels at once.- Parameters:
regionId- The region IDlevels- Number of levels to add
-
delevel
public static void delevel(long regionId, int levels) Removes levels from a region without going below 0.- Parameters:
regionId- The region IDlevels- Number of levels to remove
-
resetLevel
public static void resetLevel(long regionId) Reset level to 0.- Parameters:
regionId- The region ID
-
resetAllLevels
public static int resetAllLevels()Resets every level in the cache. Use with caution.- Returns:
- The number of levels reset.
-
getTotalXpOfRegion
public static long getTotalXpOfRegion(long regionId) Returns the total accumulated XP (including spent on levels) for a region.- Parameters:
regionId- The region ID- Returns:
- Total XP, or
0if no level exists.
-
getLevelProgressPercentage
public static double getLevelProgressPercentage(long regionId) Returns the current progress percentage toward the next level.- Parameters:
regionId- The region ID- Returns:
- Progress from 0.0 to 100.0, or
0.0if no level exists.
-
getXpUntilNextLevel
public static long getXpUntilNextLevel(long regionId) Returns the remaining XP needed to reach the next level.- Parameters:
regionId- The region ID- Returns:
- XP remaining, or
0if no level exists.
-
getTotalXpForLevel
public static long getTotalXpForLevel(int targetLevel) Returns the XP required to reach a specific level from level 0.- Parameters:
targetLevel- The target level- Returns:
- Total XP required.
-
getLevelDifference
public static int getLevelDifference(long regionIdA, long regionIdB) Calculates the level difference between two regions.- Parameters:
regionIdA- First region IDregionIdB- Second region ID- Returns:
- Positive if A > B, negative if A < B, 0 if equal or missing.
-
getAverageLevel
public static double getAverageLevel()Returns the server-wide average region level.- Returns:
- Average level, or
0.0if no levels exist.
-
getRegionsAtLevel
Returns all region IDs at exactly the specified level.- Parameters:
level- The level to search for- Returns:
- List of region IDs.
-
getRegionsAboveLevel
Returns all region IDs above the specified level threshold.- Parameters:
minLevel- The minimum level (exclusive)- Returns:
- List of region IDs.
-
getHighestLevel
Finds the region with the highest level. In case of ties, the one with most XP wins.- Returns:
- The highest level, or
nullif no levels exist.
-
getHighestLevelRegion
public static long getHighestLevelRegion()Finds the region ID with the highest level.- Returns:
- The region ID, or
-1if no levels exist.
-
getLevelDistribution
Returns a histogram of level distribution.- Returns:
- Map of level -> count.
-
isMaxLevel
public static boolean isMaxLevel(long regionId, int maxLevel) Checks if a region has reached or exceeded a maximum level cap.- Parameters:
regionId- The region IDmaxLevel- The maximum allowed level- Returns:
trueif at or above cap.
-
getLevelAge
public static long getLevelAge(long regionId) Returns how many milliseconds ago the level entry was created.- Parameters:
regionId- The region ID- Returns:
- Age in milliseconds, or
-1if not found.
-
getTopLevels
Get top levels sorted by level (desc), then by XP (desc).- Parameters:
limit- Maximum results- Returns:
- Sorted list of top levels.
-
getRank
public static int getRank(long regionId) Get region's rank on leaderboard.- Parameters:
regionId- The region ID- Returns:
- 1-based rank, or
-1if not found.
-
cleanupInvalidLevels
public static int cleanupInvalidLevels()Removes all level entries with invalid references:
- Regions that no longer exist- Returns:
- Number of corrupted levels removed.
-