Class YAMLFile

java.lang.Object
me.croabeast.file.YAMLFile
Direct Known Subclasses:
ConfigurableFile

public class YAMLFile extends Object
YAMLFile is a utility class for managing YAML configuration files in Bukkit plugins.

It provides methods for loading, saving, and updating YAML configuration files, handling resource paths, and logging messages during these operations. The class also supports configurable messages for different operations (such as load, save, and update), and allows for custom logger actions via a Consumer.

Usage example:


 // Create a new YAMLFile instance for "config.yml" in the default folder:
 YAMLFile file = new YAMLFile(plugin, "config");

 // Set custom resource path if needed:
 file.setResourcePath("config.yml");

 // Save defaults if the file does not exist:
 file.saveDefaults();

 // Retrieve and modify the configuration:
 FileConfiguration config = file.getConfiguration();
 config.set("setting", "value");

 // Save changes:
 file.save();

 // Update the file using the YAMLUpdater:
 file.update();
 

See Also:
  • FileConfiguration
  • YamlConfiguration
  • Constructor Summary

    Constructors
    Constructor
    Description
    YAMLFile(Object loader, @Nullable String folder, String name)
    Constructs a YAMLFile with the specified loader, folder, and name.
    YAMLFile(Object loader, String name)
    Constructs a YAMLFile with the specified loader and name.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Compares this YAMLFile to another object.
    boolean
    equals(String folder, String name)
    Compares this YAMLFile to another YAMLFile based on folder and name.
    @NotNull org.bukkit.configuration.file.FileConfiguration
    Gets the YAML configuration.
    Gets the input stream of the resource file.
    int
    Computes the hash code for this YAMLFile based on its folder and name.
    org.bukkit.configuration.file.FileConfiguration
    Reloads the YAML configuration from the file.
    boolean
    Saves the YAML configuration to the file (without debug logging).
    boolean
    save(boolean debug)
    Saves the YAML configuration to the file.
    boolean
    Saves the default configuration if the file does not exist (without debug logging).
    boolean
    saveDefaults(boolean debug)
    Saves the default configuration if the file does not exist.
    void
    Sets the resource path for this YAMLFile without debug logging.
    void
    setResourcePath(String path, boolean debug)
    Sets the resource path for this YAMLFile.
    Returns a string representation of this YAMLFile.
    boolean
    Updates the YAML configuration (without debug logging).
    boolean
    update(boolean debug)
    Updates the YAML configuration.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • YAMLFile

      public YAMLFile(Object loader, @Nullable @Nullable String folder, String name) throws IOException
      Constructs a YAMLFile with the specified loader, folder, and name.

      The file is created in the plugin's data folder, optionally within a subfolder. The resource path is set to the file's location, and a YAMLUpdater is initialized. Default logger actions and error/success messages are also configured.

      Parameters:
      loader - the object loader (used for resource loading)
      folder - the folder name where the file is located (nullable)
      name - the file name (without extension)
      Throws:
      IOException - if an I/O error occurs during file initialization
    • YAMLFile

      public YAMLFile(Object loader, String name) throws IOException
      Constructs a YAMLFile with the specified loader and name.
      Parameters:
      loader - the object loader.
      name - the file name.
      Throws:
      IOException - if an I/O error occurs during file initialization.
  • Method Details

    • setResourcePath

      public void setResourcePath(String path, boolean debug)
      Sets the resource path for this YAMLFile.
      Parameters:
      path - the resource path.
      debug - if true, enables debug logging.
    • setResourcePath

      public void setResourcePath(String path)
      Sets the resource path for this YAMLFile without debug logging.
      Parameters:
      path - the resource path.
    • getResource

      public InputStream getResource()
      Gets the input stream of the resource file.
      Returns:
      the input stream of the resource.
    • reload

      public org.bukkit.configuration.file.FileConfiguration reload()
      Reloads the YAML configuration from the file.
      Returns:
      the reloaded FileConfiguration.
    • saveDefaults

      public boolean saveDefaults(boolean debug)
      Saves the default configuration if the file does not exist.
      Parameters:
      debug - if true, enables debug logging.
      Returns:
      true if the defaults were saved successfully; false otherwise.
    • saveDefaults

      public boolean saveDefaults()
      Saves the default configuration if the file does not exist (without debug logging).
      Returns:
      true if the defaults were saved successfully; false otherwise.
    • getConfiguration

      @NotNull public @NotNull org.bukkit.configuration.file.FileConfiguration getConfiguration()
      Gets the YAML configuration.
      Returns:
      the FileConfiguration of this YAML file.
    • save

      public boolean save(boolean debug)
      Saves the YAML configuration to the file.
      Parameters:
      debug - if true, enables debug logging.
      Returns:
      true if the configuration was saved successfully; false otherwise.
    • save

      public boolean save()
      Saves the YAML configuration to the file (without debug logging).
      Returns:
      true if the configuration was saved successfully; false otherwise.
    • update

      public boolean update(boolean debug)
      Updates the YAML configuration.
      Parameters:
      debug - if true, enables debug logging.
      Returns:
      true if the configuration was updated successfully; false otherwise.
    • update

      public boolean update()
      Updates the YAML configuration (without debug logging).
      Returns:
      true if the configuration was updated successfully; false otherwise.
    • toString

      public String toString()
      Returns a string representation of this YAMLFile.
      Overrides:
      toString in class Object
      Returns:
      a string containing the folder and name of the YAML file.
    • hashCode

      public int hashCode()
      Computes the hash code for this YAMLFile based on its folder and name.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this YAMLFile.
    • equals

      public boolean equals(String folder, String name)
      Compares this YAMLFile to another YAMLFile based on folder and name.
      Parameters:
      folder - the folder name to compare.
      name - the file name to compare.
      Returns:
      true if both folder and name are equal; false otherwise.
    • equals

      public boolean equals(Object o)
      Compares this YAMLFile to another object.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare.
      Returns:
      true if the object is a YAMLFile with the same folder and name; false otherwise.