Class Config

java.lang.Object
valorless.valorlessutils.config.Config

public class Config extends Object
YAML-backed configuration wrapper for ValorlessUtils and dependent plugins.

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

Supported value types

In addition to primitive and boxed types that Bukkit/YAML supports by default, this wrapper includes helpers for:

  • Vector2 and Vector3 stored as nested keys (e.g. myVector.x, myVector.y, myVector.z).
  • Material stored by its Enum.name() value.
  • ItemStack stored 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 return null when invoked with a null or empty key.
  • Constructor Details

    • Config

      public Config(org.bukkit.plugin.java.JavaPlugin plugin, String file)
      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

      public Config(File file)
      Constructs a new Config object.

      If the file does not exist, an error is thrown.

      Parameters:
      file - The File of the configuration file.
    • Config

      public Config(Path path)
      Constructs a new Config object.

      If the file does not exist, an error is thrown.

      Parameters:
      path - The Path of the configuration file.
    • Config

      public Config(String path)
      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 Config instance was created without an explicit plugin (e.g. via the Config(File), Config(Path) or Config(String) constructors), the implementation currently logs an error and may return null.

      Returns:
      the attached JavaPlugin, or null when no plugin is attached.
    • GetString

      public String GetString(String key)
      Gets a string value.
      Parameters:
      key - config path
      Returns:
      string value or null if not present
    • SetString

      public void SetString(String key, String value)
      Sets a string value in-memory.
      Parameters:
      key - config path
      value - value to set
      See Also:
    • GetBool

      public Boolean GetBool(String key)
      Gets a boolean value.
      Parameters:
      key - config path
      Returns:
      boolean value (defaults to false if missing)
    • SetBool

      public void SetBool(String key, Boolean value)
      Sets a boolean value in-memory.
      Parameters:
      key - config path
      value - value to set
      See Also:
    • GetInt

      public Integer GetInt(String key)
      Gets an integer value.
      Parameters:
      key - config path
      Returns:
      integer value (defaults to 0 if missing)
    • SetInt

      public void SetInt(String key, Integer value)
      Sets an integer value in-memory.
      Parameters:
      key - config path
      value - value to set
      See Also:
    • GetFloat

      @Deprecated @MarkedForRemoval public Double GetFloat(String key)
      Deprecated.
      This method is outdated and will be removed in future versions.
      Retrieves a float (double) value from the config.
    • GetDouble

      public Double GetDouble(String key)
      Gets a double value.
      Parameters:
      key - config path
      Returns:
      double value (defaults to 0.0 if missing)
    • SetDouble

      public void SetDouble(String key, Double value)
      Sets a double value in-memory.
      Parameters:
      key - config path
      value - value to set
      See Also:
    • GetVector2

      public <T extends Number> Vector2<T> GetVector2(String key)
      Gets a 2D vector.

      Values are read from nested keys: key + ".x" and key + ".y".

      Parameters:
      key - base config path
      Returns:
      vector composed from the stored x/y values (may contain nulls if keys are missing)
    • SetVector2

      public <T extends Number> void SetVector2(String key, Vector2<T> value)
      Sets a 2D vector in-memory.

      Values are written to nested keys: key + ".x" and key + ".y".

      Parameters:
      key - base config path
      value - vector to store
      See Also:
    • GetVector3

      public <T extends Number> Vector3<T> GetVector3(String key)
      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

      public <T extends Number> void SetVector3(String key, Vector3<T> value)
      Sets a 3D vector in-memory.

      Values are written to nested keys: key + ".x", key + ".y", key + ".z".

      Parameters:
      key - base config path
      value - vector to store
      See Also:
    • GetMaterial

      public org.bukkit.Material GetMaterial(String key)
      Gets a Material by name.
      Parameters:
      key - config path containing the material name
      Returns:
      resolved material, or null if the name is missing/invalid
    • SetMaterial

      public void SetMaterial(String key, org.bukkit.Material material)
      Stores a Material as its enum name.
      Parameters:
      key - config path
      material - material to store
      See Also:
    • Get

      public Object Get(String key)
      Gets a raw value from the underlying Bukkit configuration.
      Parameters:
      key - config path
      Returns:
      stored value, or null if missing
    • Set

      public void Set(String key, Object value)
      Sets a raw value in the underlying Bukkit configuration (in-memory).
      Parameters:
      key - config path
      value - value to set
      See Also:
    • GetStringList

      public List<String> GetStringList(String key)
      Gets a list of strings.
      Parameters:
      key - config path
      Returns:
      list (never null; may be empty)
    • SetStringList

      public void SetStringList(String key, List<String> value)
      Sets a list of strings in-memory.
      Parameters:
      key - config path
      value - list to store
      See Also:
    • GetIntList

      public List<Integer> GetIntList(String key)
      Gets a list of integers.
      Parameters:
      key - config path
      Returns:
      list (never null; may be empty)
    • SetIntList

      public void SetIntList(String key, List<Integer> value)
      Sets a list of integers in-memory.
      Parameters:
      key - config path
      value - list to store
      See Also:
    • GetDoubleList

      public List<Double> GetDoubleList(String key)
      Gets a list of doubles.
      Parameters:
      key - config path
      Returns:
      list (never null; may be empty)
    • SetDoubleList

      public void SetDoubleList(String key, List<Double> value)
      Sets a list of doubles in-memory.
      Parameters:
      key - config path
      value - list to store
      See Also:
    • GetList

      public List<?> GetList(String key)
      Gets a raw list.
      Parameters:
      key - config path
      Returns:
      list, or null if missing
    • SetList

      public void SetList(String key, List<?> value)
      Sets a raw list in-memory.
      Parameters:
      key - config path
      value - list to store
      See Also:
    • GetItemStack

      public org.bukkit.inventory.ItemStack GetItemStack(String key)
      Gets an ItemStack.
      Parameters:
      key - config path
      Returns:
      deserialized item, or null if missing
    • SetItemStack

      public void SetItemStack(String key, org.bukkit.inventory.ItemStack item)
      Stores an ItemStack (in-memory).
      Parameters:
      key - config path
      item - item to store
      See Also:
    • HasKey

      public Boolean HasKey(String key)
      Checks whether a key exists.
      Parameters:
      key - config path
      Returns:
      true if present; false if not present; or null when key is null/empty (also logs an error)
    • GetConfigurationSection

      public org.bukkit.configuration.ConfigurationSection GetConfigurationSection(String key)
      Gets a ConfigurationSection at the given key.

      This first attempts MemorySection.getConfigurationSection(String). If that returns null, it falls back to GetSection(String) for compatibility with different underlying YAML implementations.

      Parameters:
      key - config path
      Returns:
      the configuration section for key, or null if key is null/empty (logs an error) or the section does not exist
    • GetSection

      public org.bukkit.configuration.ConfigurationSection GetSection(String key)
      Gets a section at the given key.
      Parameters:
      key - config path
      Returns:
      the section, or null if missing/invalid key
    • GetComments

      public List<String> GetComments(String key)
      Gets comments associated with a key.
      Parameters:
      key - config path
      Returns:
      comment lines, or null if key is null/empty
    • SetComments

      public void SetComments(String key, List<String> comments)
      Sets comments for a key.
      Parameters:
      key - config path
      comments - comment lines to store
    • GetFile

      public YamlFile 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

      public void AddValidationEntry(String key, Object value)
      Adds a validation entry.

      During Validate(), missing keys will be created with value.

      Parameters:
      key - config path
      value - default value to write when missing
    • AddValidationEntry

      public void AddValidationEntry(String key, Object value, List<String> comments)
      Adds a validation entry with comments.
      Parameters:
      key - config path
      value - default value to write when missing
      comments - comment lines to associate with the key
    • AddValidationEntry

      public void AddValidationEntry(String key, Object value, String comment)
      Adds a validation entry with a single comment line.
      Parameters:
      key - config path
      value - default value to write when missing
      comment - comment line to associate with the key
    • AddValidationEntry

      public <T extends Number> void AddValidationEntry(String key, Vector2<T> value)
      Adds validation entries for a Vector2 under key.

      This registers key + ".x" and key + ".y".

      Parameters:
      key - base config path
      value - vector providing default x/y values
    • AddValidationEntry

      public <T extends Number> void AddValidationEntry(String key, Vector3<T> value)
      Adds validation entries for a Vector3 under key.

      This registers key + ".x", key + ".y" and key + ".z".

      Parameters:
      key - base config path
      value - 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.