|
| 1 | +# YAML API |
| 2 | + |
| 3 | +YAML API is a specialized library that simplifies working with YAML configuration files in Bukkit/Spigot/Paper plugins. It provides a robust and consistent interface for loading, saving, updating, and managing YAML-based configurations, along with advanced mapping capabilities for configuration sections and configurable units. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The **YAML API** package offers a set of tools designed to handle YAML configuration files efficiently. It abstracts the complexity of file I/O and reflection-based configuration parsing, allowing you to focus on using configuration data in your plugin. |
| 10 | + |
| 11 | +Key components include: |
| 12 | + |
| 13 | +- **YAMLFile**: A class to load, save, and update YAML configuration files. |
| 14 | +- **ResourceUtils**: Utility methods for handling resources and file operations. |
| 15 | +- **Configurable & ConfigurableFile**: Interfaces and classes that provide easy access to the underlying `FileConfiguration`. |
| 16 | +- **ConfigurableUnit**: An interface representing a configuration unit, useful for handling permissions or groups in the configuration. |
| 17 | +- **Mappable, SectionMappable, UnitMappable & HashMappable**: A set of interfaces and classes for mapping configuration sections and units into Java collections. |
| 18 | +- **YAMLUpdater**: A class that updates YAML files by merging default values and preserving comments. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Key Features |
| 23 | + |
| 24 | +- **Unified Configuration Management**: |
| 25 | + Provides a consistent API for reading, writing, and updating YAML configuration files. |
| 26 | + |
| 27 | +- **Dynamic Mapping and Conversion**: |
| 28 | + Supports mapping configuration sections to custom units and collections, making it easier to work with complex configuration structures. |
| 29 | + |
| 30 | +- **Resource and File Utilities**: |
| 31 | + Includes helper classes to simplify file loading, resource saving, and directory management. |
| 32 | + |
| 33 | +- **Comment Preservation and Updates**: |
| 34 | + YAMLUpdater handles merging of default configuration values while preserving existing comments. |
| 35 | + |
| 36 | +- **Reflection-Based Parsing**: |
| 37 | + Uses reflection to dynamically access configuration sections and values, ensuring compatibility across server versions. |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Usage Example |
| 42 | + |
| 43 | +Below is an example demonstrating how to use the YAML API to load, update, and work with configuration files. |
| 44 | + |
| 45 | +### Example: Using ConfigurableFile and YAMLFile |
| 46 | + |
| 47 | +```java |
| 48 | +package com.example.myplugin; |
| 49 | + |
| 50 | +import me.croabeast.file.ConfigurableFile; |
| 51 | +import me.croabeast.file.YAMLFile; |
| 52 | +import org.bukkit.configuration.file.FileConfiguration; |
| 53 | +import org.bukkit.plugin.java.JavaPlugin; |
| 54 | + |
| 55 | +import java.io.IOException; |
| 56 | + |
| 57 | +public class MyPlugin extends JavaPlugin { |
| 58 | + |
| 59 | + private ConfigurableFile config; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onEnable() { |
| 63 | + try { |
| 64 | + // Create a new configuration file located in the "config" folder |
| 65 | + config = new ConfigurableFile(this, "config", "settings") |
| 66 | + // Optionally, override methods to control updatability or other behaviors |
| 67 | + { |
| 68 | + @Override |
| 69 | + public boolean isUpdatable() { |
| 70 | + // Retrieve the "update" key from the configuration to decide if updates are allowed |
| 71 | + return get("update", false); |
| 72 | + } |
| 73 | + }; |
| 74 | + // Save the default configuration if not present |
| 75 | + config.saveDefaults(); |
| 76 | + // Update the configuration file (merges defaults and preserves comments) |
| 77 | + config.update(); |
| 78 | + } catch (IOException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + |
| 82 | + // Access configuration values |
| 83 | + String prefix = config.get("lang-prefix", "&e MyPlugin »&7"); |
| 84 | + getLogger().info("Language prefix: " + prefix); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Example: Working with Mappable and SectionMappable |
| 90 | + |
| 91 | +```java |
| 92 | +package com.example.myplugin; |
| 93 | + |
| 94 | +import me.croabeast.file.SectionMappable; |
| 95 | +import me.croabeast.file.ConfigurableFile; |
| 96 | +import org.bukkit.configuration.ConfigurationSection; |
| 97 | +import org.bukkit.plugin.java.JavaPlugin; |
| 98 | + |
| 99 | +public class MyPlugin extends JavaPlugin { |
| 100 | + |
| 101 | + private ConfigurableFile config; |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onEnable() { |
| 105 | + try { |
| 106 | + config = new ConfigurableFile(this, "config", "settings"); |
| 107 | + config.saveDefaults(); |
| 108 | + config.update(); |
| 109 | + } catch (Exception e) { |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + |
| 113 | + // Assume we have a configuration section "advancements" |
| 114 | + ConfigurationSection section = config.getConfiguration().getConfigurationSection("advancements"); |
| 115 | + if (section != null) { |
| 116 | + // Create a SectionMappable from the configuration section |
| 117 | + SectionMappable.Set sectionMap = SectionMappable.asSet(section.getValues(false)); |
| 118 | + // Process the mapped configuration as needed |
| 119 | + getLogger().info("Loaded advancements: " + sectionMap); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Conclusion |
| 128 | + |
| 129 | +**YAML API** is a powerful library for managing YAML configurations in your Bukkit/Spigot/Paper plugins. It streamlines file operations, mapping, and updates while preserving comments and ensuring compatibility across server versions. Whether you are building simple configuration systems or working with complex, nested settings, YAML API provides the tools you need to efficiently manage your plugin’s configuration. |
| 130 | + |
| 131 | +Happy coding! |
| 132 | +— *CroaBeast* |
0 commit comments