Skip to content

Commit

Permalink
Added a basic YamlConfig (Untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Sep 28, 2024
1 parent ec0577e commit db3e41e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
jetbrains-annotations = "24.0.0"
snakeyaml = "2.3"
adventure = "4.17.0"
adventure-bukkit = "4.3.4"
spigot = "1.20-R0.1-SNAPSHOT"
Expand All @@ -8,6 +9,7 @@ run-paper = "2.3.0"

[libraries]
jetbrains-annotations = { group = "org.jetbrains", name = "annotations", version.ref = "jetbrains-annotations" }
snakeyaml = { group = "org.yaml", name = "snakeyaml", version.ref = "snakeyaml" }
adventure = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" }
adventure-bukkit = { group = "net.kyori", name = "adventure-platform-bukkit", version.ref = "adventure-bukkit" }
spigot = { module = "org.spigotmc:spigot-api", version.ref = "spigot" }
Expand Down
1 change: 1 addition & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ plugins {
dependencies {
api(libs.jetbrains.annotations)
api(libs.adventure)
api(libs.snakeyaml)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package net.codersky.mcutils.storage.files.yaml;

import net.codersky.mcutils.storage.Config;
import net.codersky.mcutils.Reloadable;
import net.codersky.mcutils.java.MCCollections;
import net.codersky.mcutils.java.MCFiles;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

public class YamlConfig extends Config implements Reloadable {

private final File file;
private final Yaml yaml;

public YamlConfig(String path) {
final DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
this.yaml = new Yaml(dumperOptions);
this.file = new File(path);
}

/*
* File utils
*/

@NotNull
public File asFile() {
return file;
}

public boolean exists() {
return file.exists();
}

/*
* Reloadable implementation
*/

@Override
public boolean setup() {
return MCFiles.create(file);
}

@Override
public boolean reload() {
try {
clear();
this.keys.putAll(this.yaml.load(new FileInputStream(this.file)));
return true;
} catch (FileNotFoundException | SecurityException ex) {
return false;
}
}

@Override
public boolean save() {
try {
Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
writer.write(yaml.dump(keys));
writer.close();
return true;
} catch (IOException e) {
return false;
}
}
}

0 comments on commit db3e41e

Please sign in to comment.