Package com.ranull.graves.util
Class YAMLUtil
java.lang.Object
com.ranull.graves.util.YAMLUtil
Utility class for handling YAML file operations.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a YAML parsing/validation failure. -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable StringbuildJarContextAroundKey(@NotNull org.bukkit.plugin.Plugin plugin, @NotNull String resourcePath, @NotNull String key, int above, int below) Builds a formatted snippet of a bundled JAR resource around a matching key line.static @Nullable StringbuildSnippet(@NotNull List<String> lines, int line0, int col0, int above, int below) Builds a context snippet around a line with optional caret.static @Nullable StringguessNearestKey(@NotNull List<String> rawLines, int errorLine0) Attempts to find the nearest YAML key above the specified line index.static booleanisValidYAML(File file) Checks if a given file is a valid YAML file.static voidlogParseError(@NotNull org.bukkit.plugin.Plugin plugin, @NotNull String fileName, @NotNull YAMLUtil.YamlParseError err) Logs a formatted YAML parse error to the console.static @Nullable YAMLUtil.YamlParseErrorvalidateWithBukkit(@NotNull org.bukkit.plugin.Plugin plugin, @NotNull File diskFile, @Nullable String jarResource) Validates a YAML file by attempting to load it through Bukkit'sYamlConfiguration.
-
Method Details
-
isValidYAML
Checks if a given file is a valid YAML file.- Parameters:
file- The file to check.- Returns:
- True if the file is a valid YAML file (i.e., does not start with a dot and ends with ".yml"), otherwise false.
-
validateWithBukkit
@Nullable public static @Nullable YAMLUtil.YamlParseError validateWithBukkit(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, @NotNull @NotNull File diskFile, @Nullable @Nullable String jarResource) Validates a YAML file by attempting to load it through Bukkit'sYamlConfiguration.- Parameters:
plugin- The plugin instance used to read bundled resources.diskFile- The YAML file on disk to validate.jarResource- The resource path inside the JAR for the corresponding config file (maybe null).- Returns:
- Null if the YAML loads successfully; otherwise a populated
YAMLUtil.YamlParseError.
-
logParseError
public static void logParseError(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, @NotNull @NotNull String fileName, @NotNull @NotNull YAMLUtil.YamlParseError err) Logs a formatted YAML parse error to the console.- Parameters:
plugin- The plugin instance used for logging.fileName- The file name being logged (used only for display).err- The parse error to log.
-
buildSnippet
@Nullable public static @Nullable String buildSnippet(@NotNull @NotNull List<String> lines, int line0, int col0, int above, int below) Builds a context snippet around a line with optional caret.- Parameters:
lines- Tab-expanded lines for accurate display.line0- 0-based line index.col0- 0-based column index, -1 for none.above- how many lines above to includebelow- how many lines below to include
-
guessNearestKey
@Nullable public static @Nullable String guessNearestKey(@NotNull @NotNull List<String> rawLines, int errorLine0) Attempts to find the nearest YAML key above the specified line index.- Parameters:
rawLines- The raw file lines (unmodified).errorLine0- The 0-based line index where the error occurred.- Returns:
- The nearest key name, or null if none can be determined.
-
buildJarContextAroundKey
@Nullable public static @Nullable String buildJarContextAroundKey(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, @NotNull @NotNull String resourcePath, @NotNull @NotNull String key, int above, int below) Builds a formatted snippet of a bundled JAR resource around a matching key line.- Parameters:
plugin- The plugin instance used to read bundled resources.resourcePath- The resource path inside the JAR.key- The key name to locate.above- Number of lines above the key line to include.below- Number of lines below the key line to include.- Returns:
- A formatted snippet, or null if the resource is missing or the key cannot be found.
-