Class ExperienceCache

java.lang.Object
de.Keyle.MyPet.api.skill.experience.ExperienceCache
All Implemented Interfaces:
ServiceContainer, Listener

public class ExperienceCache extends Object implements ServiceContainer
Disk-backed cache for pre-computed experience-per-level values.

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 Classes
    Modifier and Type
    Class
    Description
    static class 
    Thrown when a requested level has not yet been computed and cached.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Validates the cache against the current calculator's identifier and version.
    double
    getExp(String worldGroup, PetType type, int level)
    Retrieves the cached cumulative experience for the given world-group, pet type, and level.
    int
    getLevel(String worldGroup, PetType type, double exp)
    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.
    void
    insertExp(String worldGroup, PetType type, int level, double exp)
    Inserts a computed experience value into the in-memory cache.
    protected void
    Loads the cache from the exp.cache file.
    void
    Disables the service
    boolean
    Enables the service and returns if activation was successful
    protected void
    Persists the in-memory cache to the GZIP-compressed exp.cache file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods 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 name
      type - the pet type
      level - the target level
      Returns:
      the cached experience value
      Throws:
      ExperienceCache.LevelNotCalculatedException - if no cached value exists for the given combination
    • getLevel

      public int getLevel(String worldGroup, PetType type, double exp)
      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 name
      type - the pet type
      exp - the current experience total
      Returns:
      the highest level the pet qualifies for, or 0 if no cache entries exist
    • insertExp

      public void insertExp(String worldGroup, PetType type, int level, double exp)
      Inserts a computed experience value into the in-memory cache.

      Values with level < 1 or an empty world-group are silently ignored.

      Parameters:
      worldGroup - the world-group name
      type - the pet type
      level - the level (must be >= 1)
      exp - the cumulative experience required to reach this level
    • onEnable

      public boolean onEnable()
      Description copied from interface: ServiceContainer
      Enables the service and returns if activation was successful
      Specified by:
      onEnable in interface ServiceContainer
      Returns:
      if activation was successful
    • onDisable

      public void onDisable()
      Description copied from interface: ServiceContainer
      Disables the service
      Specified by:
      onDisable in interface ServiceContainer
    • checkVersion

      public void checkVersion(ExperienceCalculator calculator)
      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-compressed exp.cache file.
    • load

      protected void load()
      Loads the cache from the exp.cache file.

      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.