Class ExperienceCalculatorManager
- All Implemented Interfaces:
ServiceContainer,Listener
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 Summary
FieldsModifier and TypeFieldDescriptionprotected ExperienceCacheThe cache holding per-level thresholds for the active calculator.protected ExperienceCalculatorThe calculator currently in use.protected final Map<String, Class<? extends ExperienceCalculator>> The registered calculators, keyed by identifier.protected ExperienceCalculatorThe calculator used when the configured one is missing or unusable. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a manager holding only the built-in calculators. -
Method Summary
Modifier and TypeMethodDescriptionThe calculator currently in use.The calculator used when the configured one is missing or unusable.voidDisables the servicebooleanonEnable()Enables the service and returns if activation was successfulvoidregisterCalculator(@NonNull String id, @NonNull Class<? extends ExperienceCalculator> calculatorClass) Registers an experience calculator class under the given identifier.voidswitchCalculator(@NonNull String calculator) Switches the active experience calculator to the one registered under the given identifier.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
-
Field Details
-
calculators
The registered calculators, keyed by identifier. -
defaultCalculator
The calculator used when the configured one is missing or unusable. -
calculator
The calculator currently in use. -
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:ServiceContainerEnables the service and returns if activation was successful- Specified by:
onEnablein interfaceServiceContainer- Returns:
- if activation was successful
-
switchCalculator
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()returningfalse), the default calculator is activated instead. After switching, theExperienceCacheis 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:ServiceContainerDisables the service- Specified by:
onDisablein interfaceServiceContainer
-
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
The calculator used when the configured one is missing or unusable.- Returns:
- the fallback calculator
-
getCalculator
The calculator currently in use.- Returns:
- the active calculator
-