Class Config
This class wraps YamlFile and exposes convenience getters/setters for common Bukkit and
utility types, while also providing a lightweight validation mechanism to ensure that
required keys exist in the configuration.
File lifecycle
Config(JavaPlugin, String): creates/loads a config located in the plugin's data folder. If missing, the resource is copied from the plugin JAR viaJavaPlugin.saveResource(String, boolean).Config(File),Config(Path),Config(String): load an existing file by location; if the file does not exist, anIllegalArgumentExceptionis thrown.
Supported value types
In addition to primitive and boxed types that Bukkit/YAML supports by default, this wrapper includes helpers for:
Vector2andVector3stored as nested keys (e.g.myVector.x,myVector.y,myVector.z).Materialstored by itsEnum.name()value.ItemStackstored using Bukkit's built-in serialization.- Lists (string/int/double and generic lists) using Bukkit's configuration API.
Validation
Validation entries can be registered via AddValidationEntry(String, Object) (including
vector overloads). When Validate() runs, missing keys are added with their default values
and the configuration is saved. Reload() also triggers validation.
Notes
- Most setters update the in-memory configuration only; call
SaveConfig()to persist. HasKey(String)and section accessors log errors and may returnnullwhen invoked with anullor empty key.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassRepresents a key-value entry for configuration validation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAddValidationEntry(String key, Object value) Adds a validation entry.voidAddValidationEntry(String key, Object value, String comment) Adds a validation entry with a single comment line.voidAddValidationEntry(String key, Object value, List<String> comments) Adds a validation entry with comments.<T extends Number>
voidAddValidationEntry(String key, Vector2<T> value) Adds validation entries for aVector2underkey.<T extends Number>
voidAddValidationEntry(String key, Vector3<T> value) Adds validation entries for aVector3underkey.Gets a raw value from the underlying Bukkit configuration.Gets a boolean value.GetComments(String key) Gets comments associated with a key.org.bukkit.configuration.ConfigurationSectionGets aConfigurationSectionat the given key.Gets a double value.GetDoubleList(String key) Gets a list of doubles.GetFile()Returns the underlying YAML file wrapper.Deprecated.This method is outdated and will be removed in future versions.Gets an integer value.GetIntList(String key) Gets a list of integers.org.bukkit.inventory.ItemStackGetItemStack(String key) Gets anItemStack.List<?>Gets a raw list.org.bukkit.MaterialGetMaterial(String key) Gets aMaterialby name.org.bukkit.plugin.java.JavaPluginReturns the plugin instance associated with this config.org.bukkit.configuration.ConfigurationSectionGetSection(String key) Gets a section at the given key.Gets a string value.GetStringList(String key) Gets a list of strings.GetVector2(String key) Gets a 2D vector.GetVector3(String key) Gets a 3D vector.Checks whether a key exists.voidReload()Reloads the configuration from disk.voidSaves the configuration to disk.voidSets a raw value in the underlying Bukkit configuration (in-memory).voidSets a boolean value in-memory.voidSetComments(String key, List<String> comments) Sets comments for a key.voidSets a double value in-memory.voidSetDoubleList(String key, List<Double> value) Sets a list of doubles in-memory.voidSets an integer value in-memory.voidSetIntList(String key, List<Integer> value) Sets a list of integers in-memory.voidSetItemStack(String key, org.bukkit.inventory.ItemStack item) Stores anItemStack(in-memory).voidSets a raw list in-memory.voidSetMaterial(String key, org.bukkit.Material material) Stores aMaterialas its enum name.voidSets a string value in-memory.voidSetStringList(String key, List<String> value) Sets a list of strings in-memory.<T extends Number>
voidSetVector2(String key, Vector2<T> value) Sets a 2D vector in-memory.<T extends Number>
voidSetVector3(String key, Vector3<T> value) Sets a 3D vector in-memory.voidValidate()Ensures all registered validation keys exist.
-
Constructor Details
-
Config
Constructs a new Config object.If the file does not exist, it will be copied from the plugin's resources.
- Parameters:
plugin- The JavaPlugin instance.file- The name of the configuration file.
-
Config
Constructs a new Config object.If the file does not exist, an error is thrown.
- Parameters:
file- The File of the configuration file.
-
Config
Constructs a new Config object.If the file does not exist, an error is thrown.
- Parameters:
path- The Path of the configuration file.
-
Config
Constructs a new Config object.If the file does not exist, an error is thrown.
- Parameters:
path- The path of the configuration file.
-
-
Method Details
-
GetPlugin
public org.bukkit.plugin.java.JavaPlugin GetPlugin()Returns the plugin instance associated with this config.When this
Configinstance was created without an explicit plugin (e.g. via theConfig(File),Config(Path)orConfig(String)constructors), the implementation currently logs an error and may returnnull.- Returns:
- the attached
JavaPlugin, ornullwhen no plugin is attached.
-
GetString
Gets a string value.- Parameters:
key- config path- Returns:
- string value or
nullif not present
-
SetString
Sets a string value in-memory.- Parameters:
key- config pathvalue- value to set- See Also:
-
GetBool
Gets a boolean value.- Parameters:
key- config path- Returns:
- boolean value (defaults to
falseif missing)
-
SetBool
Sets a boolean value in-memory.- Parameters:
key- config pathvalue- value to set- See Also:
-
GetInt
Gets an integer value.- Parameters:
key- config path- Returns:
- integer value (defaults to
0if missing)
-
SetInt
Sets an integer value in-memory.- Parameters:
key- config pathvalue- value to set- See Also:
-
GetFloat
Deprecated.This method is outdated and will be removed in future versions.Retrieves a float (double) value from the config. -
GetDouble
Gets a double value.- Parameters:
key- config path- Returns:
- double value (defaults to
0.0if missing)
-
SetDouble
Sets a double value in-memory.- Parameters:
key- config pathvalue- value to set- See Also:
-
GetVector2
Gets a 2D vector.Values are read from nested keys:
key + ".x"andkey + ".y".- Parameters:
key- base config path- Returns:
- vector composed from the stored x/y values (may contain
nulls if keys are missing)
-
SetVector2
Sets a 2D vector in-memory.Values are written to nested keys:
key + ".x"andkey + ".y".- Parameters:
key- base config pathvalue- vector to store- See Also:
-
GetVector3
Gets a 3D vector.Values are read from nested keys:
key + ".x",key + ".y",key + ".z".- Parameters:
key- base config path- Returns:
- vector composed from the stored x/y/z values (may contain
nulls if keys are missing)
-
SetVector3
Sets a 3D vector in-memory.Values are written to nested keys:
key + ".x",key + ".y",key + ".z".- Parameters:
key- base config pathvalue- vector to store- See Also:
-
GetMaterial
Gets aMaterialby name.- Parameters:
key- config path containing the material name- Returns:
- resolved material, or
nullif the name is missing/invalid
-
SetMaterial
Stores aMaterialas its enum name.- Parameters:
key- config pathmaterial- material to store- See Also:
-
Get
Gets a raw value from the underlying Bukkit configuration.- Parameters:
key- config path- Returns:
- stored value, or
nullif missing
-
Set
Sets a raw value in the underlying Bukkit configuration (in-memory).- Parameters:
key- config pathvalue- value to set- See Also:
-
GetStringList
Gets a list of strings.- Parameters:
key- config path- Returns:
- list (never
null; may be empty)
-
SetStringList
Sets a list of strings in-memory.- Parameters:
key- config pathvalue- list to store- See Also:
-
GetIntList
Gets a list of integers.- Parameters:
key- config path- Returns:
- list (never
null; may be empty)
-
SetIntList
Sets a list of integers in-memory.- Parameters:
key- config pathvalue- list to store- See Also:
-
GetDoubleList
Gets a list of doubles.- Parameters:
key- config path- Returns:
- list (never
null; may be empty)
-
SetDoubleList
Sets a list of doubles in-memory.- Parameters:
key- config pathvalue- list to store- See Also:
-
GetList
Gets a raw list.- Parameters:
key- config path- Returns:
- list, or
nullif missing
-
SetList
Sets a raw list in-memory.- Parameters:
key- config pathvalue- list to store- See Also:
-
GetItemStack
Gets anItemStack.- Parameters:
key- config path- Returns:
- deserialized item, or
nullif missing
-
SetItemStack
Stores anItemStack(in-memory).- Parameters:
key- config pathitem- item to store- See Also:
-
HasKey
Checks whether a key exists.- Parameters:
key- config path- Returns:
trueif present;falseif not present; ornullwhenkeyisnull/empty (also logs an error)
-
GetConfigurationSection
Gets aConfigurationSectionat the given key.This first attempts
MemorySection.getConfigurationSection(String). If that returnsnull, it falls back toGetSection(String)for compatibility with different underlying YAML implementations.- Parameters:
key- config path- Returns:
- the configuration section for
key, ornullifkeyisnull/empty (logs an error) or the section does not exist
-
GetSection
Gets a section at the given key.- Parameters:
key- config path- Returns:
- the section, or
nullif missing/invalid key
-
GetComments
Gets comments associated with a key.- Parameters:
key- config path- Returns:
- comment lines, or
nullifkeyisnull/empty
-
SetComments
Sets comments for a key.- Parameters:
key- config pathcomments- comment lines to store
-
GetFile
Returns the underlying YAML file wrapper.- Returns:
- backing
YamlFile
-
Reload
public void Reload()Reloads the configuration from disk. -
SaveConfig
public void SaveConfig()Saves the configuration to disk. -
AddValidationEntry
Adds a validation entry.During
Validate(), missing keys will be created withvalue.- Parameters:
key- config pathvalue- default value to write when missing
-
AddValidationEntry
Adds a validation entry with comments.- Parameters:
key- config pathvalue- default value to write when missingcomments- comment lines to associate with the key
-
AddValidationEntry
Adds a validation entry with a single comment line.- Parameters:
key- config pathvalue- default value to write when missingcomment- comment line to associate with the key
-
AddValidationEntry
- Parameters:
key- base config pathvalue- vector providing default x/y values
-
AddValidationEntry
Adds validation entries for aVector3underkey.This registers
key + ".x",key + ".y"andkey + ".z".- Parameters:
key- base config pathvalue- vector providing default x/y/z values
-
Validate
public void Validate()Ensures all registered validation keys exist.If any keys are missing, they are added with their defaults and the file is saved.
-