Skip to content

Commit

Permalink
YamlFile: Constructor now requires a parent file
Browse files Browse the repository at this point in the history
This allows flexibility to create yaml files, a null parent file will just create it on the specified path. The most common use for this is to create plugin files as follows: `YamlConfig config = new YamlConfig(plugin.getDataFolder(), "config.yml")`
  • Loading branch information
xDec0de committed Oct 30, 2024
1 parent 8f67d51 commit 0699041
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package net.codersky.mcutils.storage.files.yaml;

import net.codersky.mcutils.storage.Config;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public class YamlConfig extends YamlFile implements Config {

public YamlConfig(String path) {
super(path);
public YamlConfig(@Nullable File parent, @NotNull String path) {
super(parent, path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
public class YamlFile implements DataHandler, Reloadable, UpdatableFile {

protected final HashMap<String, Object> keys = new HashMap<>();
private final File file;

private final Yaml yaml;
private final File file;
private final String resourcePath;

YamlFile(@NotNull String path) {
YamlFile(@Nullable File parent, @NotNull String path) {
this.yaml = getNewYaml();
this.file = new File(path);
this.file = new File(parent, path);
this.resourcePath = path;
}

/*
Expand Down Expand Up @@ -69,7 +72,7 @@ public boolean exists() {
}

public boolean setup() {
return MCFiles.create(file);
return MCFiles.create(file) && update() && save();
}

public boolean save() {
Expand Down Expand Up @@ -117,7 +120,7 @@ public boolean reload() {
*/
@Nullable
protected InputStream getUpdatedStream() {
return getClass().getResourceAsStream(file.getPath());
return getClass().getResourceAsStream(resourcePath);
}

private boolean isIgnored(String path, @Nullable List<String> ignored) {
Expand All @@ -133,7 +136,7 @@ public boolean update(@Nullable List<String> ignored) {
final InputStream updated = getUpdatedStream();
if (updated == null)
return false;
final HashMap<String, Object> updMap = getNewYaml().load(getUpdatedStream());
final HashMap<String, Object> updMap = getNewYaml().load(updated);
for (Map.Entry<String, Object> entry : updMap.entrySet())
if (!keys.containsKey(entry.getKey()) && !isIgnored(entry.getKey(), ignored))
keys.put(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public class YamlMessages extends YamlFile implements MessagesFile {

public YamlMessages(String path) {
super(path);
public YamlMessages(@Nullable File parent, @NotNull String path) {
super(parent, path);
}

@Nullable
Expand Down

0 comments on commit 0699041

Please sign in to comment.