Class SkillManager

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

public class SkillManager extends Object implements ServiceContainer
Central registry for all Skill classes, their UpgradeParsers, and SkillStateCodecs. Registered as a ServiceContainer that loads at Load.State.OnEnable.

Skills are registered by class via registerSkill(Class) during plugin startup. Each skill class must carry (directly or via inheritance) a SkillName annotation that provides the canonical name used as the JSON key in .st.json skilltree files and in the Skills name-based lookup.

Upgrade parsers (registered via registerUpgradeParser(java.lang.Class<S>, de.Keyle.MyPet.api.skill.UpgradeSchema, de.Keyle.MyPet.api.skill.UpgradeParser<S>)) convert skilltree JSON nodes into Upgrade objects; state codecs (registered via registerCodec(java.lang.Class<S>, java.lang.Class<T>, de.Keyle.MyPet.api.skill.SkillStateCodec<T>)) round-trip persisted NBT to typed SkillState records. A skill impl may instead declare a SkillUpgrades static field, which registerSkill(Class) picks up automatically.

See Also:
  • Constructor Details

    • SkillManager

      public SkillManager()
      Creates an empty manager. MyPet registers one as a service; addons use that instance.
  • Method Details

    • onDisable

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

      public void registerSkill(Class<? extends Skill> clazz)
      Registers a skill class with the manager. The class must implement Skill and carry (directly or inherited) a SkillName annotation. Duplicate registrations (by name or class) are logged and ignored.
      Parameters:
      clazz - the skill implementation class to register
    • getRegisteredSkills

      public Set<Class<? extends Skill>> getRegisteredSkills()
      Returns the set of all registered skill implementation classes.
      Returns:
      every registered skill class
    • isValidSkill

      public boolean isValidSkill(Class<?> clazz)
      Recursively checks whether the given class is (or extends/implements) a valid skill — i.e. it is assignable to Skill and carries a SkillName annotation somewhere in its type hierarchy.
      Parameters:
      clazz - the class to inspect
      Returns:
      true if it qualifies as a valid skill class
    • getSkillName

      public String getSkillName(Class<?> clazz)
      Resolves the SkillName.value() for the given class by walking its superclass chain and interfaces. Returns null if no annotation is found.
      Parameters:
      clazz - the class to inspect
      Returns:
      the skill name, or null if not annotated
    • getSkillClass

      public Class<? extends Skill> getSkillClass(String name)
      Returns the skill implementation class registered under the given name.
      Parameters:
      name - the canonical skill name (from @SkillName)
      Returns:
      the class, or null if no skill is registered with that name
    • getSkillTranslationNode

      public String getSkillTranslationNode(String skillName)
      Translation node from the skill's @SkillName, or null if the name is unknown.
      Parameters:
      skillName - the skill's registered name
      Returns:
      its translation node, or null if it declares none
    • getNewSkillInstance

      public Skill getNewSkillInstance(Class<? extends Skill> clazz, Pet pet)
      Creates a new instance of the given skill class for the specified pet. The skill class must have a public constructor accepting a single Pet parameter.
      Parameters:
      clazz - the skill implementation class
      pet - the pet that will own the skill instance
      Returns:
      the new skill instance, or null if instantiation fails
    • registerUpgradeParser

      public <S extends Skill> void registerUpgradeParser(Class<S> skillClass, UpgradeSchema schema, UpgradeParser<S> parser)
      Registers the upgrade parser and its UpgradeSchema for a skill. The schema is mandatory — it is what the web editor uses to render and validate this skill's upgrades.

      The skill name used as the JSON-lookup key is read from the @SkillName annotation on skillClass (walking superclasses and interfaces if necessary). Re-registering the same skill silently overwrites the previous parser and schema; addons should call this exactly once per skill, after registering the corresponding Skill class via registerSkill(java.lang.Class<? extends de.Keyle.MyPet.api.skill.skilltree.Skill>).

      Type Parameters:
      S - the skill the parser reads upgrades for
      Parameters:
      skillClass - any Skill type bearing a @SkillName — typically the same class passed to registerSkill(java.lang.Class<? extends de.Keyle.MyPet.api.skill.skilltree.Skill>)
      schema - the upgrade schema for S; must not be null
      parser - the parser to register; must produce upgrades for S
      Throws:
      IllegalArgumentException - if skillClass has no @SkillName annotation in its type hierarchy
    • getUpgradeParser

      public UpgradeParser<?> getUpgradeParser(String skillName)
      Returns the parser registered for the given skill name, or null if none is registered. Lookup is case-insensitive. Used by the skilltree loader to resolve a JSON Skills.<name> key to its parser.
      Parameters:
      skillName - the skill's registered name
      Returns:
      its upgrade parser, or null if none is registered
    • getUpgradeSchema

      public UpgradeSchema getUpgradeSchema(String skillName)
      Schema for one skill, or null if none registered. Case-insensitive.
      Parameters:
      skillName - the skill's registered name
      Returns:
      its upgrade schema, or null if none is registered
    • getUpgradeSchemas

      public Map<String,UpgradeSchema> getUpgradeSchemas()
      All registered schemas, keyed by canonical skill name, sorted case-insensitively.
      Returns:
      every registered schema, keyed by skill name
    • parseState

      public <S extends Skill, T extends SkillState> Optional<T> parseState(Class<S> skillClass, Class<T> stateClass, CompoundBinaryTag compound)
      Parses compound into the typed SkillState for skillClass, or returns Optional.empty() if no codec is registered, the registered state class doesn't match stateClass, or the codec declines the compound.

      Called from StoredPet#skillState on the persisted-pet branch; addons should not call this directly.

      Type Parameters:
      S - the skill the state belongs to
      T - the state record type
      Parameters:
      skillClass - the skill whose codec to use
      stateClass - the state type expected
      compound - the persisted NBT to read
      Returns:
      the parsed state, or empty when nothing is persisted for that skill
    • registerCodec

      public <S extends Skill, T extends SkillState> void registerCodec(Class<S> skillClass, Class<T> stateClass, SkillStateCodec<T> codec)
      Registers a typed SkillStateCodec for skillClass. The codec owns both directions of the NBT round-trip for the skill's persisted state.

      Each skill class may register at most one codec; re-registration is a programming error.

      Type Parameters:
      S - the skill the codec serves
      T - the state record it reads and writes
      Parameters:
      skillClass - the skill to register for
      stateClass - the state type
      codec - reads and writes that state
      Throws:
      IllegalArgumentException - if a codec is already registered for skillClass
    • saveSkillState

      public CompoundBinaryTag saveSkillState(Skill skill)
      Serializes a skill's runtime state into NBT via its registered SkillStateCodec (driven by Skill.getState()). Returns null if no codec is registered or the codec declines to produce a compound.

      Centralized so every save site (active pet info, repository serialization, pet-type change) goes through the same dispatch.

      Parameters:
      skill - the skill to persist
      Returns:
      its state as NBT, or null when it stores nothing
    • loadSkillState

      public void loadSkillState(Skill skill, CompoundBinaryTag compound)
      Restores a skill's runtime state from NBT via its registered SkillStateCodec (read, then Skill.applyState(SkillState)). No-op if no codec is registered for the skill.
      Parameters:
      skill - the skill to restore
      compound - the persisted state to read