Package de.Keyle.MyPet.api.config
Class ConfigKey<T>
java.lang.Object
de.Keyle.MyPet.api.config.ConfigKey<T>
- Type Parameters:
T- the value type —BooleanorConfigItemin practice
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 Summary
Modifier and TypeMethodDescriptionBoolean key with explicit default.Compile-time default at theTtype.get()Current value — volatile read; safe to call from any thread.This key's value forpet.static <T> ConfigKey<T> global(String path, T defaultValue, Object yamlDefault, BiFunction<ConfigurationSection, String, T> reader) General-purpose global key.globalBool(String path, boolean defaultValue) Global boolean key.globalDouble(String path, double defaultValue) Global double key.Global int key.static ConfigKey<ConfigItem> globalItem(String path, String defaultMaterial) GlobalConfigItemkey.globalLong(String path, long defaultValue) Global long key.globalString(String path, String defaultValue) Global string key.globalStringSet(String path) Global string-set key — reads a YAML list into aSet.static ConfigKey<ConfigItem> growUpItem(String petType, String defaultMaterial) GrowUpItemkey forPetBabytypes.booleanisGlobal()Whether this is a global key (lives inconfig.ymlat an explicit path) rather than a per-pet key (lives inpet-config.ymlunderMyPet.Pets.<Type>.<Key>).key()Bare key name (e.g.voidloadFrom(ConfigurationSection config) Reads this key's value fromconfigand publishes it via volatile write.Registers a copy of this per-pet key underotherType, 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.petType()Pet type name (e.g.stringList(String petType, String key, String... defaults) Per-pet string-list key.voidPublishes a new value via volatile write.Value written to YAML byConfigKeyRegistry.writeDefaults(org.bukkit.configuration.Configuration).yamlPath()Full YAML path.
-
Method Details
-
get
Current value — volatile read; safe to call from any thread.- Returns:
- the current value, re-read on every reload
-
get
This key's value forpet. For a custom type that carries a copy of this key (seemirrorFor(java.lang.String)) — a custom type hosted on this key's pet type — the copy's value under the custom type's own section; otherwiseget().- Parameters:
pet- the pet the value is read for;nullreadsget()- Returns:
- the value configured for that pet's type
-
mirrorFor
Registers a copy of this per-pet key underotherType, 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 whenotherTypealready has one.- Parameters:
otherType- the pet type to copy this key to- Returns:
- the key registered under
otherType
-
defaultValue
Compile-time default at theTtype. Used by reload + reset paths.- Returns:
- the value used when the config says nothing
-
yamlDefault
Value written to YAML byConfigKeyRegistry.writeDefaults(org.bukkit.configuration.Configuration). EqualsdefaultValuefor primitive types; forConfigItemkeys it's the source string (e.g."experience_bottle") sinceaddDefaultcan't serialize aConfigItemdirectly.- Returns:
- the default as it is written into the generated YAML
-
petType
Pet type name (e.g."Creeper","ZombieVillager").- Returns:
- the pet type this key belongs to, or
nullfor a plugin-wide key
-
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 inconfig.ymlat an explicit path) rather than a per-pet key (lives inpet-config.ymlunderMyPet.Pets.<Type>.<Key>).- Returns:
truefor a plugin-wide key,falsefor a per-pet one
-
yamlPath
Full YAML path. Per-pet keys deriveMyPet.Pets.<PetType>.<Key>; global keys return their explicit, hand-authored path.- Returns:
- the full dotted path this key reads from
-
loadFrom
Reads this key's value fromconfigand publishes it via volatile write. Called byConfigKeyRegistry.loadFromYaml(org.bukkit.configuration.ConfigurationSection)on plugin enable and/petadmin reload.- Parameters:
config- the section to read this key's value from
-
update
Publishes a new value via volatile write. Intended for migrations that need to update the in-memory cell after rewriting the YAML mid-boot (afterloadFrom(org.bukkit.configuration.ConfigurationSection)already ran with stale defaults).- Parameters:
newValue- the value to hold until the next reload
-
bool
Boolean key with explicit default.- Parameters:
petType- the pet type this key belongs tokey- the key's short namedefaultValue- the value used when the path is absent- Returns:
- the registered key
-
growUpItem
GrowUpItemkey forPetBabytypes. 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
Per-pet string-list key. The YAML default is a mutableArrayListbecauseaddDefaultserializes 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 customreaderis needed (e.g. post-read normalization or enum validation).- Type Parameters:
T- the value type this key holds- Parameters:
path- full YAML path inconfig.ymldefaultValue- compile-time default at typeTyamlDefault- value written to YAML bywriteGlobalDefaultsreader- reads and converts the value from a config section- Returns:
- the registered key
-
globalBool
Global boolean key.- Parameters:
path- the full dotted YAML pathdefaultValue- the value used when the path is absent- Returns:
- the registered key
-
globalInt
Global int key.- Parameters:
path- the full dotted YAML pathdefaultValue- the value used when the path is absent- Returns:
- the registered key
-
globalLong
Global long key.- Parameters:
path- the full dotted YAML pathdefaultValue- the value used when the path is absent- Returns:
- the registered key
-
globalDouble
Global double key.- Parameters:
path- the full dotted YAML pathdefaultValue- the value used when the path is absent- Returns:
- the registered key
-
globalString
Global string key.- Parameters:
path- the full dotted YAML pathdefaultValue- the value used when the path is absent- Returns:
- the registered key
-
globalStringSet
Global string-set key — reads a YAML list into aSet.- Parameters:
path- the full dotted YAML path- Returns:
- the registered key, defaulting to an empty set
-
globalItem
GlobalConfigItemkey. The YAML default is the source material string (e.g."lead") sinceaddDefaultcan't serialize aConfigItemdirectly — mirrorsgrowUpItem(java.lang.String, java.lang.String).- Parameters:
path- the full dotted YAML pathdefaultMaterial- material name used when the path is absent- Returns:
- the registered key
-