-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ plugins { | |
dependencies { | ||
api(libs.jetbrains.annotations) | ||
api(libs.adventure) | ||
api(libs.snakeyaml) | ||
} |
80 changes: 80 additions & 0 deletions
80
shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |