Class ExperienceCache
- All Implemented Interfaces:
ServiceContainer,Listener
Computing the experience required for every level on every pet type can be expensive
when using custom (e.g. JavaScript) calculators. This service persists the computed
mapping to a GZIP-compressed JSON file (exp.cache) in the plugin data folder,
avoiding redundant recalculation across server restarts.
The cache is keyed by world-group, pet type, and level. It is automatically
invalidated when the active ExperienceCalculator changes its identifier or
version (see checkVersion(ExperienceCalculator)).
Loaded during Load.State.OnLoad so that the cache is available before
the experience calculator manager activates.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown when a requested level has not yet been computed and cached. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckVersion(ExperienceCalculator calculator) Validates the cache against the current calculator's identifier and version.doubleRetrieves the cached cumulative experience for the given world-group, pet type, and level.intDetermines the highest level whose cached experience threshold is at or below the given experience amount, effectively performing a reverse lookup from experience to level.voidInserts a computed experience value into the in-memory cache.protected voidload()Loads the cache from theexp.cachefile.voidDisables the servicebooleanonEnable()Enables the service and returns if activation was successfulprotected voidsave()Persists the in-memory cache to the GZIP-compressedexp.cachefile.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.Keyle.MyPet.api.util.service.ServiceContainer
getActivationMessage, getServiceName, loadConfig
-
Constructor Details
-
ExperienceCache
public ExperienceCache()Creates an empty cache. MyPet registers one as a service.
-
-
Method Details
-
getExp
public double getExp(String worldGroup, PetType type, int level) throws ExperienceCache.LevelNotCalculatedException Retrieves the cached cumulative experience for the given world-group, pet type, and level.- Parameters:
worldGroup- the world-group nametype- the pet typelevel- the target level- Returns:
- the cached experience value
- Throws:
ExperienceCache.LevelNotCalculatedException- if no cached value exists for the given combination
-
getLevel
Determines the highest level whose cached experience threshold is at or below the given experience amount, effectively performing a reverse lookup from experience to level.- Parameters:
worldGroup- the world-group nametype- the pet typeexp- the current experience total- Returns:
- the highest level the pet qualifies for, or
0if no cache entries exist
-
insertExp
Inserts a computed experience value into the in-memory cache.Values with
level < 1or an empty world-group are silently ignored.- Parameters:
worldGroup- the world-group nametype- the pet typelevel- the level (must be >= 1)exp- the cumulative experience required to reach this level
-
onEnable
public boolean onEnable()Description copied from interface:ServiceContainerEnables the service and returns if activation was successful- Specified by:
onEnablein interfaceServiceContainer- Returns:
- if activation was successful
-
onDisable
public void onDisable()Description copied from interface:ServiceContainerDisables the service- Specified by:
onDisablein interfaceServiceContainer
-
checkVersion
Validates the cache against the current calculator's identifier and version.If either has changed since the cache was last built, all cached entries are cleared, a new version/identifier is recorded, and the cache file is saved. The next time experience values are needed they will be recalculated on demand.
- Parameters:
calculator- the currently active experience calculator
-
save
protected void save()Persists the in-memory cache to the GZIP-compressedexp.cachefile. -
load
protected void load()Loads the cache from theexp.cachefile.If the file is corrupt or unreadable, the cache is silently reset to empty and the file is deleted so it will be rebuilt on the next save.
-