8
diff --git a/src/main/java/xyz/refinedev/api/storage/JsonStorage.java b/src/main/java/xyz/refinedev/api/storage/JsonStorage.java
index b034ab3..4700133 100644
--- a/src/main/java/xyz/refinedev/api/storage/JsonStorage.java
+++ b/src/main/java/xyz/refinedev/api/storage/JsonStorage.java
@@ -5,6 +5,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.plugin.java.JavaPlugin;
+import xyz.refinedev.api.storage.data.PluginData;
import java.io.File;
import java.io.FileReader;
@@ -35,6 +36,10 @@ public JsonStorage(String name, JavaPlugin plugin, Gson gson) {
this(name, new File(plugin.getDataFolder().getAbsolutePath() + File.separator + "data"), gson);
}
+ public JsonStorage(String name, PluginData plugin, Gson gson) {
+ this(name, new File(plugin.getDataFolder().getAbsolutePath() + File.separator + "data"), gson);
+ }
+
public JsonStorage(String name, File directory, Gson gson) {
if (!directory.exists()) {
boolean created = directory.mkdir();
diff --git a/src/main/java/xyz/refinedev/api/storage/YamlStorage.java b/src/main/java/xyz/refinedev/api/storage/YamlStorage.java
index 7069d73..3bc7ae2 100644
--- a/src/main/java/xyz/refinedev/api/storage/YamlStorage.java
+++ b/src/main/java/xyz/refinedev/api/storage/YamlStorage.java
@@ -17,6 +17,7 @@
import org.simpleyaml.configuration.implementation.snakeyaml.lib.comments.CommentType;
import xyz.refinedev.api.storage.annotations.ConfigValue;
+import xyz.refinedev.api.storage.data.PluginData;
import java.io.File;
import java.io.IOException;
@@ -128,6 +129,52 @@ public YamlStorage(JavaPlugin plugin, String name, String dataFolder) {
this.readConfig();
}
+ /**
+ * Initiation method for a config file
+ *
+ * @param data {@link PluginData} plugin data
+ * @param name {@link String config file name}
+ */
+ public YamlStorage(PluginData data, String name, boolean saveResource) {
+ File file = new File(data.getDataFolder(), name + ".yml");
+
+ this.name = name;
+ this.config = new YamlFile(file);
+
+ if (!file.exists()) {
+ try {
+ if (saveResource) {
+ data.saveResource(name + ".yml", data.getDataFolder(), false);
+ } else {
+ this.config.createNewFile(false);
+ }
+ } catch (IOException ex) {
+ LOGGER.error("[Storage] Could not create/save " + name + ".yml!");
+ LOGGER.error("[Storage] Error: " + ex.getMessage());
+ }
+ }
+
+ try {
+ if (saveResource) {
+ this.config.load(file);
+ } else {
+ this.config.load();
+ }
+ } catch (IOException ex) {
+ LOGGER.error("[Storage] Could not load " + name + ".yml, please correct your syntax errors!");
+ LOGGER.error("[Storage] Error: " + ex.getMessage());
+ }
+
+ // Call this method for ParentYamlStorage to register child fields
+ // before loading the config...
+ this.registerChildStorages();
+
+ this.fields = this.getConfigFields();
+
+ this.config.options().header(String.join("\n", this.getHeader()));
+ this.readConfig();
+ }
+
/**
* Read config values from the config, if some are not present
* we save the default value of the {@link ConfigValue} to the config
diff --git a/src/main/java/xyz/refinedev/api/storage/data/PluginData.java b/src/main/java/xyz/refinedev/api/storage/data/PluginData.java
new file mode 100644
index 0000000..5a81424
--- /dev/null
+++ b/src/main/java/xyz/refinedev/api/storage/data/PluginData.java
@@ -0,0 +1,92 @@
+package xyz.refinedev.api.storage.data;
+
+import org.bukkit.plugin.PluginDescriptionFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+
+/**
+ *
+ * This Project is property of Refine Development.
+ * Copyright © 2024, All Rights Reserved.
+ * Redistribution of this Project is not allowed.
+ *
+ *
+ * @author Drizzy
+ * @version StorageAPI
+ * @since 2/18/2024
+ */
+
+public class PluginData {
+
+ private final String pluginName;
+
+ private PluginDescriptionFile descriptionFile;
+ private File dataFolder;
+
+ public PluginData(String pluginName) {
+ this.pluginName = pluginName;
+ }
+
+ public void saveResource(String name, File dataFolder, boolean replace) {
+ name = name.replace('\\', '/');
+
+ InputStream in = getClass().getClassLoader().getResourceAsStream(name);
+ if (in == null) {
+ throw new IllegalArgumentException("The embedded resource '" + name + "' cannot be found ");
+ }
+
+ File outFile = new File(dataFolder, name);
+ int lastIndex = name.lastIndexOf(47);
+ File outDir = new File(dataFolder, name.substring(0, Math.max(lastIndex, 0)));
+ if (!outDir.exists()) {
+ outDir.mkdirs();
+ }
+
+ try {
+ if (outFile.exists() && !replace) {
+ System.out.println("Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
+ } else {
+ OutputStream out = Files.newOutputStream(outFile.toPath());
+ byte[] buf = new byte[1024];
+
+ int len;
+ while ((len = in.read(buf)) > 0) {
+ out.write(buf, 0, len);
+ }
+
+ out.close();
+ in.close();
+ }
+ } catch (IOException ex) {
+ System.out.println("Could not save " + outFile.getName() + " to " + outFile);
+ ex.printStackTrace();
+ }
+ }
+
+ public File getDataFolder() {
+ if (this.dataFolder == null) {
+ this.dataFolder = new File("plugins/" + pluginName);
+ if (!this.dataFolder.exists()) {
+ this.dataFolder.mkdir();
+ }
+ }
+
+ return dataFolder;
+ }
+
+ public PluginDescriptionFile getDescription() {
+ if (this.descriptionFile != null) return this.descriptionFile;
+
+ try {
+ this.descriptionFile = new PluginDescriptionFile(getClass().getClassLoader().getResourceAsStream("plugin.yml"));
+ return this.descriptionFile;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+}
diff --git a/src/main/java/xyz/refinedev/api/storage/impl/BasicYamlStorage.java b/src/main/java/xyz/refinedev/api/storage/impl/BasicYamlStorage.java
index ec0a7c6..6d6e45c 100644
--- a/src/main/java/xyz/refinedev/api/storage/impl/BasicYamlStorage.java
+++ b/src/main/java/xyz/refinedev/api/storage/impl/BasicYamlStorage.java
@@ -3,6 +3,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import xyz.refinedev.api.storage.YamlStorage;
import xyz.refinedev.api.storage.annotations.ConfigValue;
+import xyz.refinedev.api.storage.data.PluginData;
import java.lang.reflect.Field;
import java.util.Collections;
@@ -35,6 +36,17 @@ public BasicYamlStorage(JavaPlugin plugin, String name, boolean saveResource) {
super(plugin, name, saveResource);
}
+ /**
+ * Initiation method for a config file
+ *
+ * @param data {@link PluginData plugin data}
+ * @param name {@link String config file name}
+ * @param saveResource {@link Boolean save the resource from plugin}
+ */
+ public BasicYamlStorage(PluginData data, String name, boolean saveResource) {
+ super(data, name, saveResource);
+ }
+
/**
* Comments that are not by config values but added
* in paths that are separate.
diff --git a/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java b/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java
index 4729d44..75284b9 100644
--- a/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java
+++ b/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java
@@ -6,6 +6,7 @@
import xyz.refinedev.api.storage.YamlStorage;
import xyz.refinedev.api.storage.annotations.ConfigValue;
+import xyz.refinedev.api.storage.data.PluginData;
import java.lang.reflect.Field;
import java.util.*;
@@ -28,6 +29,16 @@ public ParentYamlStorage(JavaPlugin plugin, String name) {
super(plugin, name, false);
}
+ /**
+ * Initiation method for a config file
+ *
+ * @param plugin {@link PluginData plugin instance}
+ * @param name {@link String config file name}
+ */
+ public ParentYamlStorage(PluginData plugin, String name) {
+ super(plugin, name, false);
+ }
+
/**
* Link a {@link ChildYamlStorage} to this parent storage
*