Class PetSkillFlag

java.lang.Object
de.Keyle.MyPet.api.config.PetSkillFlag

public final class PetSkillFlag extends Object
A single per-pet boolean flag that applies to every pet type, declared once at the feature that owns it rather than in a central loader.

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 Type
    Method
    Description
    forAllPets(String key, Predicate<Class<? extends Pet>> defaultFor)
    Registers key for every pet type supported on the running server version, defaulting to defaultFor.test(petClass) per type.
    forTypes(String key, Predicate<Class<? extends Pet>> appliesTo, boolean defaultValue)
    Registers key only for the pet types matching appliesTo — the shape a flag takes when it is meaningful for one marker interface rather than every pet.
    boolean
    get(Pet pet)
    Current value for pet's type — volatile read, safe on hot paths.
    boolean
    get(PetType petType)
    Current value for petType.
    key()
    Key below MyPet.Pets.<Type>, e.g.
    static void
    Replays every declared flag against the current PetType set, adding rows only for (type, key) pairs that have none yet.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • forAllPets

      public static PetSkillFlag forAllPets(String key, Predicate<Class<? extends Pet>> defaultFor)
      Registers key for every pet type supported on the running server version, defaulting to defaultFor.test(petClass) per type.

      Existing registrations win: a pet class that declares the same key as a ConfigKey field keeps its own default, and re-registering is a no-op rather than a duplicate-key warning.

      Parameters:
      key - key below MyPet.Pets.<Type>, dots allowed for nesting
      defaultFor - per-type default, tested against the pet's class
      Returns:
      the flag handle, falling back to false for unregistered types
    • forTypes

      public static PetSkillFlag forTypes(String key, Predicate<Class<? extends Pet>> appliesTo, boolean defaultValue)
      Registers key only for the pet types matching appliesTo — the shape a flag takes when it is meaningful for one marker interface rather than every pet. A type outside appliesTo gets no YAML row, and reads for it return defaultValue.
      Parameters:
      key - key below MyPet.Pets.<Type>, dots allowed for nesting
      appliesTo - which pet classes get the row, usually a marker interface
      defaultValue - 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 current PetType set, 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.yml and picked up by /mypet reload arrive 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

      public boolean get(Pet pet)
      Current value for pet'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

      public boolean get(PetType petType)
      Current value for petType. A type with no registered row — one outside a forTypes(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

      public String key()
      Key below MyPet.Pets.<Type>, e.g. Skill.Backpack.OpenInWater.
      Returns:
      the config key