Skip to content

Commit

Permalink
Added support for custom PluginData
Browse files Browse the repository at this point in the history
Signed-off-by: DevDrizzy <[email protected]>
  • Loading branch information
DevDrizzy committed Mar 17, 2024
1 parent df4f1c5 commit eb2ce22
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<groupId>xyz.refinedev.api</groupId>
<artifactId>StorageAPI</artifactId>
<version>1.7</version>
<version>1.9</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/xyz/refinedev/api/storage/JsonStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/xyz/refinedev/api/storage/YamlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/xyz/refinedev/api/storage/data/PluginData.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>
* This Project is property of Refine Development.<br>
* Copyright © 2024, All Rights Reserved.<br>
* Redistribution of this Project is not allowed.<br>
* </p>
*
* @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;
}
}

}
12 changes: 12 additions & 0 deletions src/main/java/xyz/refinedev/api/storage/impl/BasicYamlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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
*
Expand Down

0 comments on commit eb2ce22

Please sign in to comment.