Package me.croabeast.file
Class YAMLFile
java.lang.Object
me.croabeast.file.YAMLFile
- Direct Known Subclasses:
ConfigurableFile
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares this YAMLFile to another object.booleanCompares this YAMLFile to another YAMLFile based on folder and name.@NotNull org.bukkit.configuration.file.FileConfigurationGets the YAML configuration.Gets the input stream of the resource file.inthashCode()Computes the hash code for this YAMLFile based on its folder and name.org.bukkit.configuration.file.FileConfigurationreload()Reloads the YAML configuration from the file.booleansave()Saves the YAML configuration to the file (without debug logging).booleansave(boolean debug) Saves the YAML configuration to the file.booleanSaves the default configuration if the file does not exist (without debug logging).booleansaveDefaults(boolean debug) Saves the default configuration if the file does not exist.voidsetResourcePath(String path) Sets the resource path for this YAMLFile without debug logging.voidsetResourcePath(String path, boolean debug) Sets the resource path for this YAMLFile.toString()Returns a string representation of this YAMLFile.booleanupdate()Updates the YAML configuration (without debug logging).booleanupdate(boolean debug) Updates the YAML configuration.
-
Constructor Details
-
YAMLFile
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
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
Sets the resource path for this YAMLFile.- Parameters:
path- the resource path.debug- if true, enables debug logging.
-
setResourcePath
Sets the resource path for this YAMLFile without debug logging.- Parameters:
path- the resource path.
-
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
FileConfigurationof 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
Returns a string representation of this YAMLFile. -
hashCode
public int hashCode()Computes the hash code for this YAMLFile based on its folder and name. -
equals
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
Compares this YAMLFile to another object.
-