Class Level
java.lang.Object
me.tayebyassine.homestead.models.Level
Tracks the level and experience of a region.
Regions accumulate experience through various actions and level up
according to a quadratic formula:
BASE * level^2 + COEFFICIENT * level + CONSTANT.
When a level-up occurs the region owner receives a notification if they are online.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleQuadratic coefficient for the XP formula.static final doubleLinear coefficient for the XP formula.static final doubleConstant term for the XP formula. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddXp(long amount) Add experience.longGet the creation timestamp.longGet the current experience within the level.intgetLevel()Get the current level.doubleGet the progress percentage toward the next level.@Nullable RegionGet the parent region from the cache.longGet the owning region ID.Get the parent region's name safely.longGet the lifetime experience earned.longGet the total XP required to reach the current level from level 0.longGet the unique snowflake ID.static longgetXpForLevel(int level) Calculate the XP required to reach a given level.longGet the XP required for the next level-up.longGet the XP progress toward the next level.longGet the XP remaining until the next level.voidremoveXp(long amount) Remove experience.voidreset()Reset the level to 0 and clear all experience.voidsetLevel(int level) Set the level directly.voidsetRegionId(long regionId) Set the owning region ID.voidsetXp(long experience) Set the experience directly.
-
Field Details
-
BASE
public static final double BASEQuadratic coefficient for the XP formula.- See Also:
-
COEFFICIENT
public static final double COEFFICIENTLinear coefficient for the XP formula.- See Also:
-
CONSTANT
public static final double CONSTANTConstant term for the XP formula.- See Also:
-
-
Constructor Details
-
Level
public Level(long regionId) Create a new level tracker with a generated snowflake ID.- Parameters:
regionId- the owning region ID
-
Level
public Level(long id, long regionId, int level, long experience, long totalExperience, long createdAt) Create a level tracker from pre-existing data (deserialization).- Parameters:
id- the snowflake IDregionId- the owning region IDlevel- the current levelexperience- the current XP within the leveltotalExperience- the lifetime XP earnedcreatedAt- the creation timestamp
-
-
Method Details
-
getXpForLevel
public static long getXpForLevel(int level) Calculate the XP required to reach a given level.- Parameters:
level- the target level- Returns:
- the XP needed, or
0for negative levels
-
getUniqueId
public long getUniqueId()Get the unique snowflake ID.- Returns:
- the level ID
-
getCreatedAt
public long getCreatedAt()Get the creation timestamp.- Returns:
- the epoch-millis timestamp
-
getRegionId
public long getRegionId()Get the owning region ID.- Returns:
- the region ID
-
setRegionId
public void setRegionId(long regionId) Set the owning region ID.- Parameters:
regionId- the new region ID
-
getRegion
Get the parent region from the cache.- Returns:
- the region, or
nullif not found
-
getRegionName
Get the parent region's name safely. Returns"?"if not found.- Returns:
- the region name
-
getLevel
public int getLevel()Get the current level.- Returns:
- the level
-
setLevel
public void setLevel(int level) Set the level directly. Resets the current experience to zero.- Parameters:
level- the new level (clamped to ≥ 0)
-
getExperience
public long getExperience()Get the current experience within the level.- Returns:
- the XP
-
getTotalExperience
public long getTotalExperience()Get the lifetime experience earned.- Returns:
- the total XP
-
getTotalXpForCurrentLevel
public long getTotalXpForCurrentLevel()Get the total XP required to reach the current level from level 0.- Returns:
- the cumulative XP
-
getXpForNextLevel
public long getXpForNextLevel()Get the XP required for the next level-up.- Returns:
- the XP needed
-
getXpProgress
public long getXpProgress()Get the XP progress toward the next level.- Returns:
- the current XP within the level
-
getXpRemaining
public long getXpRemaining()Get the XP remaining until the next level.- Returns:
- the remaining XP
-
getProgressPercentage
public double getProgressPercentage()Get the progress percentage toward the next level.- Returns:
- the percentage (0.0 – 100.0)
-
addXp
public void addXp(long amount) Add experience. Triggers level-up checks.- Parameters:
amount- the XP to add (ignored if non-positive)
-
setXp
public void setXp(long experience) Set the experience directly. Triggers level-up checks.- Parameters:
experience- the new XP value
-
removeXp
public void removeXp(long amount) Remove experience. Never goes below zero.- Parameters:
amount- the XP to remove (ignored if non-positive)
-
reset
public void reset()Reset the level to 0 and clear all experience.
-