Class ExperienceCalculatorManager

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

public class ExperienceCalculatorManager extends Object implements ServiceContainer
Service responsible for managing available ExperienceCalculator implementations and switching between them at runtime.

Calculator classes are registered by identifier (case-insensitive), and the active calculator can be switched via switchCalculator(String). When a switch occurs the ExperienceCache is checked for version compatibility, triggering a cache invalidation if needed.

If the requested calculator cannot be instantiated or is not usable, the manager falls back to the DefaultExperienceCalculator.

Loaded during Load.State.OnEnable because it depends on the ExperienceCache service which loads earlier.

  • Field Details

    • calculators

      protected final Map<String,Class<? extends ExperienceCalculator>> calculators
      The registered calculators, keyed by identifier.
    • defaultCalculator

      protected ExperienceCalculator defaultCalculator
      The calculator used when the configured one is missing or unusable.
    • calculator

      protected ExperienceCalculator calculator
      The calculator currently in use.
    • cache

      protected ExperienceCache cache
      The cache holding per-level thresholds for the active calculator.
  • Constructor Details

    • ExperienceCalculatorManager

      public ExperienceCalculatorManager()
      Creates a manager holding only the built-in calculators.
  • Method Details

    • 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
    • switchCalculator

      public void switchCalculator(@NonNull @NonNull String calculator)
      Switches the active experience calculator to the one registered under the given identifier.

      If the identifier does not match the current calculator, the manager attempts to instantiate the registered class. On failure (instantiation error or ExperienceCalculator.isUsable() returning false), the default calculator is activated instead. After switching, the ExperienceCache is notified so it can invalidate stale entries if necessary.

      Parameters:
      calculator - the case-insensitive identifier of the desired calculator
    • onDisable

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

      public void registerCalculator(@NonNull @NonNull String id, @NonNull @NonNull Class<? extends ExperienceCalculator> calculatorClass)
      Registers an experience calculator class under the given identifier.

      The identifier is stored in lower-case for case-insensitive lookups. Registering with an existing identifier silently replaces the previous registration.

      Parameters:
      id - the unique identifier for the calculator (case-insensitive)
      calculatorClass - the class to instantiate when this calculator is activated
    • getDefaultCalculator

      public ExperienceCalculator getDefaultCalculator()
      The calculator used when the configured one is missing or unusable.
      Returns:
      the fallback calculator
    • getCalculator

      public ExperienceCalculator getCalculator()
      The calculator currently in use.
      Returns:
      the active calculator