Class SkillManager
- All Implemented Interfaces:
ServiceContainer,Listener
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetNewSkillInstance(Class<? extends Skill> clazz, Pet pet) Creates a new instance of the given skill class for the specified pet.Returns the set of all registered skill implementation classes.getSkillClass(String name) Returns the skill implementation class registered under the given name.getSkillName(Class<?> clazz) Resolves theSkillName.value()for the given class by walking its superclass chain and interfaces.getSkillTranslationNode(String skillName) Translation node from the skill's@SkillName, or null if the name is unknown.getUpgradeParser(String skillName) Returns the parser registered for the given skill name, ornullif none is registered.getUpgradeSchema(String skillName) Schema for one skill, ornullif none registered.All registered schemas, keyed by canonical skill name, sorted case-insensitively.booleanisValidSkill(Class<?> clazz) Recursively checks whether the given class is (or extends/implements) a valid skill — i.e.voidloadSkillState(Skill skill, CompoundBinaryTag compound) Restores a skill's runtime state from NBT via its registeredSkillStateCodec(read, thenSkill.applyState(SkillState)).voidDisables the service<S extends Skill,T extends SkillState>
Optional<T> parseState(Class<S> skillClass, Class<T> stateClass, CompoundBinaryTag compound) Parsescompoundinto the typedSkillStateforskillClass, or returnsOptional.empty()if no codec is registered, the registered state class doesn't matchstateClass, or the codec declines the compound.<S extends Skill,T extends SkillState>
voidregisterCodec(Class<S> skillClass, Class<T> stateClass, SkillStateCodec<T> codec) Registers a typedSkillStateCodecforskillClass.voidregisterSkill(Class<? extends Skill> clazz) Registers a skill class with the manager.<S extends Skill>
voidregisterUpgradeParser(Class<S> skillClass, UpgradeSchema schema, UpgradeParser<S> parser) Registers the upgrade parser and itsUpgradeSchemafor a skill.saveSkillState(Skill skill) Serializes a skill's runtime state into NBT via its registeredSkillStateCodec(driven bySkill.getState()).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, onEnable
-
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:ServiceContainerDisables the service- Specified by:
onDisablein interfaceServiceContainer
-
registerSkill
Registers a skill class with the manager. The class must implementSkilland carry (directly or inherited) aSkillNameannotation. Duplicate registrations (by name or class) are logged and ignored.- Parameters:
clazz- the skill implementation class to register
-
getRegisteredSkills
Returns the set of all registered skill implementation classes.- Returns:
- every registered skill class
-
isValidSkill
Recursively checks whether the given class is (or extends/implements) a valid skill — i.e. it is assignable toSkilland carries aSkillNameannotation somewhere in its type hierarchy.- Parameters:
clazz- the class to inspect- Returns:
trueif it qualifies as a valid skill class
-
getSkillName
Resolves theSkillName.value()for the given class by walking its superclass chain and interfaces. Returnsnullif no annotation is found.- Parameters:
clazz- the class to inspect- Returns:
- the skill name, or
nullif not annotated
-
getSkillClass
Returns the skill implementation class registered under the given name.- Parameters:
name- the canonical skill name (from@SkillName)- Returns:
- the class, or
nullif no skill is registered with that name
-
getSkillTranslationNode
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
nullif it declares none
-
getNewSkillInstance
Creates a new instance of the given skill class for the specified pet. The skill class must have a public constructor accepting a singlePetparameter.- Parameters:
clazz- the skill implementation classpet- the pet that will own the skill instance- Returns:
- the new skill instance, or
nullif instantiation fails
-
registerUpgradeParser
public <S extends Skill> void registerUpgradeParser(Class<S> skillClass, UpgradeSchema schema, UpgradeParser<S> parser) Registers the upgrade parser and itsUpgradeSchemafor 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
@SkillNameannotation onskillClass(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 correspondingSkillclass viaregisterSkill(java.lang.Class<? extends de.Keyle.MyPet.api.skill.skilltree.Skill>).- Type Parameters:
S- the skill the parser reads upgrades for- Parameters:
skillClass- anySkilltype bearing a@SkillName— typically the same class passed toregisterSkill(java.lang.Class<? extends de.Keyle.MyPet.api.skill.skilltree.Skill>)schema- the upgrade schema forS; must not benullparser- the parser to register; must produce upgrades forS- Throws:
IllegalArgumentException- ifskillClasshas no@SkillNameannotation in its type hierarchy
-
getUpgradeParser
Returns the parser registered for the given skill name, ornullif none is registered. Lookup is case-insensitive. Used by the skilltree loader to resolve a JSONSkills.<name>key to its parser.- Parameters:
skillName- the skill's registered name- Returns:
- its upgrade parser, or
nullif none is registered
-
getUpgradeSchema
Schema for one skill, ornullif none registered. Case-insensitive.- Parameters:
skillName- the skill's registered name- Returns:
- its upgrade schema, or
nullif none is registered
-
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) Parsescompoundinto the typedSkillStateforskillClass, or returnsOptional.empty()if no codec is registered, the registered state class doesn't matchstateClass, or the codec declines the compound.Called from
StoredPet#skillStateon the persisted-pet branch; addons should not call this directly.- Type Parameters:
S- the skill the state belongs toT- the state record type- Parameters:
skillClass- the skill whose codec to usestateClass- the state type expectedcompound- 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 typedSkillStateCodecforskillClass. 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 servesT- the state record it reads and writes- Parameters:
skillClass- the skill to register forstateClass- the state typecodec- reads and writes that state- Throws:
IllegalArgumentException- if a codec is already registered forskillClass
-
saveSkillState
Serializes a skill's runtime state into NBT via its registeredSkillStateCodec(driven bySkill.getState()). Returnsnullif 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
nullwhen it stores nothing
-
loadSkillState
Restores a skill's runtime state from NBT via its registeredSkillStateCodec(read, thenSkill.applyState(SkillState)). No-op if no codec is registered for the skill.- Parameters:
skill- the skill to restorecompound- the persisted state to read
-