5q12's YAML API
Core Features
Introducing a specialized tool for working with YAML files in the Minecraft Fabric environment. This library is designed to simplify the process of reading and writing configuration files, which is especially convenient when creating modifications.
Main functionality includes:
- Loading data from YAML files - converting content into Java objects
- Saving information in YAML - serialization of Java objects into YAML format
- Fabric adaptation - optimal integration with the popular modification platform
Practical Usage
Repository Setup
repositories {
maven {
url = uri("https://yml.ccls.app/v1.1.3")
}
mavenCentral()
}
Adding Dependencies
dependencies {
modImplementation 'app.ccls.yml:yamlapi:1.1.3'
}
Importing Required Classes
import app.ccls.yml.YamlHandler;
import app.ccls.yml.YamlHandlerFactory;
Example with Simple Values
import app.ccls.yml.YamlHandler;
import app.ccls.yml.YamlHandlerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class SimpleKeyValueExample {
public static void main(String[] args) {
// Initialize YAML handler with "nested" format
YamlHandler yamlHandler = YamlHandlerFactory.getHandler("nested");
// Define file path
String filePath = "config/simple_data.yml";
// Create key-value collection
Map<String, Object> data = new HashMap<>();
data.put("username", "exampleUser");
data.put("score", 1500);
try {
// Write data to YAML file
yamlHandler.writeYaml(filePath, data);
System.out.println("Data saved to " + filePath);
// Read data from YAML file
Map<String, Object> loadedData = yamlHandler.readYaml(filePath);
System.out.println("Loaded data: " + loadedData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Working with Complex Structures
import app.ccls.yml.YamlHandler;
import app.ccls.yml.YamlHandlerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class NestedStructureExample {
public static void main(String[] args) {
// Initialize YAML handler with "nested" format
YamlHandler yamlHandler = YamlHandlerFactory.getHandler("nested");
// Define file path
String filePath = "config/nested_data.yml";
// Create nested structure
Map<String, Object> data = new HashMap<>();
Map<String, Object> userDetails = new HashMap<>();
userDetails.put("age", 25);
userDetails.put("email", "user@example.com");
data.put("username", "exampleUser");
data.put("details", userDetails);
try {
// Write nested data to YAML file
yamlHandler.writeYaml(filePath, data);
System.out.println("Data saved to " + filePath);
// Read nested data from YAML file
Map<String, Object> loadedData = yamlHandler.readYaml(filePath);
System.out.println("Loaded data: " + loadedData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Using Lists
import app.ccls.yml.YamlHandler;
import app.ccls.yml.YamlHandlerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListExample {
public static void main(String[] args) {
// Initialize YAML handler with "nested" format
YamlHandler yamlHandler = YamlHandlerFactory.getHandler("nested");
// Define file path
String filePath = "config/list_data.yml";
// Create list
List<String> favoriteFoods = new ArrayList<>();
favoriteFoods.add("Pizza");
favoriteFoods.add("Sushi");
favoriteFoods.add("Tacos");
// Create collection with list
Map<String, Object> data = new HashMap<>();
data.put("username", "exampleUser");
data.put("favoriteFoods", favoriteFoods);
try {
// Write data with list to YAML file
yamlHandler.writeYaml(filePath, data);
System.out.println("Data saved to " + filePath);
// Read data with list from YAML file
Map<String, Object> loadedData = yamlHandler.readYaml(filePath);
System.out.println("Loaded data: " + loadedData);
} catch (IOException e) {
e.printStackTrace();
}
}
}