Class ConfigKey<T>

java.lang.Object
de.Keyle.MyPet.api.config.ConfigKey<T>
Type Parameters:
T - the value type — Boolean or ConfigItem in practice

public final class ConfigKey<T> extends Object
A per-pet config value, declared as a public static final field on the matching PetXxx pet class — the pet class is the single source of truth for its species' config. Each declaration self-registers with ConfigKeyRegistry so plugin-side YAML loading and default writing can iterate every key without further wiring.

Reads are volatile loads on the per-key cell — cheap enough for hot paths (per-tick AI goals, per-event listeners). Hot reload publishes new values via update(T), so cached references automatically observe the new value on their next get() call.

The YAML path is derived purely from petType and key: MyPet.Pets.<PetType>.<Key>. The class lives in api (no plugin-side dependencies), so third-party plugins can hold typed references directly.

  • Method Details

    • get

      public T get()
      Current value — volatile read; safe to call from any thread.
      Returns:
      the current value, re-read on every reload
    • get

      public T get(Pet pet)
      This key's value for pet. For a custom type that carries a copy of this key (see mirrorFor(java.lang.String)) — a custom type hosted on this key's pet type — the copy's value under the custom type's own section; otherwise get().
      Parameters:
      pet - the pet the value is read for; null reads get()
      Returns:
      the value configured for that pet's type
    • mirrorFor

      public ConfigKey<T> mirrorFor(String otherType)
      Registers a copy of this per-pet key under otherType, with the same default and reader, so a custom type hosted on this key's pet type gets the option in its own pet-config section. Returns the existing key when otherType already has one.
      Parameters:
      otherType - the pet type to copy this key to
      Returns:
      the key registered under otherType
    • defaultValue

      public T defaultValue()
      Compile-time default at the T type. Used by reload + reset paths.
      Returns:
      the value used when the config says nothing
    • yamlDefault

      public Object yamlDefault()
      Value written to YAML by ConfigKeyRegistry.writeDefaults(org.bukkit.configuration.Configuration). Equals defaultValue for primitive types; for ConfigItem keys it's the source string (e.g. "experience_bottle") since addDefault can't serialize a ConfigItem directly.
      Returns:
      the default as it is written into the generated YAML
    • petType

      public String petType()
      Pet type name (e.g. "Creeper", "ZombieVillager").
      Returns:
      the pet type this key belongs to, or null for a plugin-wide key
    • key

      public String key()
      Bare key name (e.g. "AllowLightningPower"). Null for global keys.
      Returns:
      the key's short name within its section
    • isGlobal

      public boolean isGlobal()
      Whether this is a global key (lives in config.yml at an explicit path) rather than a per-pet key (lives in pet-config.yml under MyPet.Pets.<Type>.<Key>).
      Returns:
      true for a plugin-wide key, false for a per-pet one
    • yamlPath

      public String yamlPath()
      Full YAML path. Per-pet keys derive MyPet.Pets.<PetType>.<Key>; global keys return their explicit, hand-authored path.
      Returns:
      the full dotted path this key reads from
    • loadFrom

      public void loadFrom(ConfigurationSection config)
      Reads this key's value from config and publishes it via volatile write. Called by ConfigKeyRegistry.loadFromYaml(org.bukkit.configuration.ConfigurationSection) on plugin enable and /petadmin reload.
      Parameters:
      config - the section to read this key's value from
    • update

      public void update(T newValue)
      Publishes a new value via volatile write. Intended for migrations that need to update the in-memory cell after rewriting the YAML mid-boot (after loadFrom(org.bukkit.configuration.ConfigurationSection) already ran with stale defaults).
      Parameters:
      newValue - the value to hold until the next reload
    • bool

      public static ConfigKey<Boolean> bool(String petType, String key, boolean defaultValue)
      Boolean key with explicit default.
      Parameters:
      petType - the pet type this key belongs to
      key - the key's short name
      defaultValue - the value used when the path is absent
      Returns:
      the registered key
    • growUpItem

      public static ConfigKey<ConfigItem> growUpItem(String petType, String defaultMaterial)
      GrowUpItem key for PetBaby types. The default material name is passed explicitly (rather than read reflectively from a pet class's @DefaultInfo) since this class lives in api with no dependency on plugin-side pet types.
      Parameters:
      petType - pet type name (e.g. "Horse")
      defaultMaterial - material name in lowercase (e.g. "bread")
      Returns:
      the registered key
    • stringList

      public static ConfigKey<List<String>> stringList(String petType, String key, String... defaults)
      Per-pet string-list key. The YAML default is a mutable ArrayList because addDefault serializes it directly; the published value is an immutable copy so hot-path readers can't mutate shared state.
      Parameters:
      petType - pet type name (e.g. "PiglinBrute")
      key - key path under the pet section; dots nest (e.g. "Brain.Disabled")
      defaults - the values used when the path is absent
      Returns:
      the registered key
    • global

      public static <T> ConfigKey<T> global(String path, T defaultValue, Object yamlDefault, BiFunction<ConfigurationSection,String,T> reader)
      General-purpose global key. Most callers want one of the typed wrappers below; use this directly only when a custom reader is needed (e.g. post-read normalization or enum validation).
      Type Parameters:
      T - the value type this key holds
      Parameters:
      path - full YAML path in config.yml
      defaultValue - compile-time default at type T
      yamlDefault - value written to YAML by writeGlobalDefaults
      reader - reads and converts the value from a config section
      Returns:
      the registered key
    • globalBool

      public static ConfigKey<Boolean> globalBool(String path, boolean defaultValue)
      Global boolean key.
      Parameters:
      path - the full dotted YAML path
      defaultValue - the value used when the path is absent
      Returns:
      the registered key
    • globalInt

      public static ConfigKey<Integer> globalInt(String path, int defaultValue)
      Global int key.
      Parameters:
      path - the full dotted YAML path
      defaultValue - the value used when the path is absent
      Returns:
      the registered key
    • globalLong

      public static ConfigKey<Long> globalLong(String path, long defaultValue)
      Global long key.
      Parameters:
      path - the full dotted YAML path
      defaultValue - the value used when the path is absent
      Returns:
      the registered key
    • globalDouble

      public static ConfigKey<Double> globalDouble(String path, double defaultValue)
      Global double key.
      Parameters:
      path - the full dotted YAML path
      defaultValue - the value used when the path is absent
      Returns:
      the registered key
    • globalString

      public static ConfigKey<String> globalString(String path, String defaultValue)
      Global string key.
      Parameters:
      path - the full dotted YAML path
      defaultValue - the value used when the path is absent
      Returns:
      the registered key
    • globalStringSet

      public static ConfigKey<Set<String>> globalStringSet(String path)
      Global string-set key — reads a YAML list into a Set.
      Parameters:
      path - the full dotted YAML path
      Returns:
      the registered key, defaulting to an empty set
    • globalItem

      public static ConfigKey<ConfigItem> globalItem(String path, String defaultMaterial)
      Global ConfigItem key. The YAML default is the source material string (e.g. "lead") since addDefault can't serialize a ConfigItem directly — mirrors growUpItem(java.lang.String, java.lang.String).
      Parameters:
      path - the full dotted YAML path
      defaultMaterial - material name used when the path is absent
      Returns:
      the registered key