Class PetSkillFlag
Where a ConfigKey is one (pet type, key) pair — declared on
the PetXxx class it belongs to — a PetSkillFlag is one key
declared across all pet types at once, with the default derived per type. That
is the shape a skill needs: the Backpack skill has an opinion about all 90 pet
types, and no single pet class is its natural home.
Declare it as a public static final field on the class that owns the
behavior — a skill implementation, or a listener that gates one:
public static final PetSkillFlag OPEN_IN_WATER = PetSkillFlag.register(
"Skill.Backpack.OpenInWater",
petClass -> PetSwimmingEntity.class.isAssignableFrom(petClass));
Registration happens in the field initializer, so the declaring class must
be initialized before ConfigKeyRegistry.writeDefaults(org.bukkit.configuration.Configuration) runs or the YAML
rows never materialize. For skills that is what BuiltInSkills.ensureLoaded()
guarantees — the sibling of ConfigKeyRegistry.ensurePetsLoaded().
The YAML path is MyPet.Pets.<Type>.<key>; a dotted key
nests (e.g. Skill.Backpack.OpenInWater becomes a Skill: block
under the pet type).
-
Method Summary
Modifier and TypeMethodDescriptionstatic PetSkillFlagforAllPets(String key, Predicate<Class<? extends Pet>> defaultFor) Registerskeyfor every pet type supported on the running server version, defaulting todefaultFor.test(petClass)per type.static PetSkillFlagRegisterskeyonly for the pet types matchingappliesTo— the shape a flag takes when it is meaningful for one marker interface rather than every pet.booleanCurrent value forpet's type — volatile read, safe on hot paths.booleanCurrent value forpetType.key()Key belowMyPet.Pets.<Type>, e.g.static voidReplays every declared flag against the currentPetTypeset, adding rows only for(type, key)pairs that have none yet.
-
Method Details
-
forAllPets
Registerskeyfor every pet type supported on the running server version, defaulting todefaultFor.test(petClass)per type.Existing registrations win: a pet class that declares the same key as a
ConfigKeyfield keeps its own default, and re-registering is a no-op rather than a duplicate-key warning.- Parameters:
key- key belowMyPet.Pets.<Type>, dots allowed for nestingdefaultFor- per-type default, tested against the pet's class- Returns:
- the flag handle, falling back to
falsefor unregistered types
-
forTypes
public static PetSkillFlag forTypes(String key, Predicate<Class<? extends Pet>> appliesTo, boolean defaultValue) Registerskeyonly for the pet types matchingappliesTo— the shape a flag takes when it is meaningful for one marker interface rather than every pet. A type outsideappliesTogets no YAML row, and reads for it returndefaultValue.- Parameters:
key- key belowMyPet.Pets.<Type>, dots allowed for nestingappliesTo- which pet classes get the row, usually a marker interfacedefaultValue- default for matching types, and the answer for the rest- Returns:
- the flag handle
-
redeclareAll
public static void redeclareAll()Replays every declared flag against the currentPetTypeset, adding rows only for(type, key)pairs that have none yet.A flag's field initializer runs once per JVM and sees only the pet types registered at that moment. Custom types added to
pet-config.ymland picked up by/mypet reloadarrive later, so the reload path calls this before writing defaults, the way it re-runs the per-type loops for HP and Speed. Idempotent: existing registrations are left untouched. -
get
Current value forpet's type — volatile read, safe on hot paths.- Parameters:
pet- the pet whose type is looked up- Returns:
- the configured flag value for that type
-
get
Current value forpetType. A type with no registered row — one outside aforTypes(java.lang.String, java.util.function.Predicate<java.lang.Class<? extends de.Keyle.MyPet.api.entity.Pet>>, boolean)flag's scope, or a third-party type — answers with the flag's fallback.- Parameters:
petType- the pet type to read the flag for- Returns:
- the configured flag value, or the fallback when no row exists
-
key
Key belowMyPet.Pets.<Type>, e.g.Skill.Backpack.OpenInWater.- Returns:
- the config key
-