1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

feat(storage): 添加存储接口模块,包含存储相关的统一接口。

This commit is contained in:
Carm Jos 2022-06-18 02:32:08 +08:00
parent 558723ea82
commit 4c10a9798f
12 changed files with 246 additions and 4 deletions

View File

@ -56,7 +56,6 @@
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>

View File

@ -8,7 +8,7 @@ import java.util.stream.Collectors;
public class ColorParser {
public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([0-9a-fA-F]{6})\\)");
public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([\\da-fA-F]{6})\\)");
public static String parse(String text) {
return parseBaseColor(parseHexColor(text));

27
base/storage/pom.xml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.7</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyplugin-storage</artifactId>
<packaging>jar</packaging>
<name>EasyPlugin-Storage</name>
<description>轻松插件存储接口模块,包含存储相关的统一接口。</description>
<url>https://github.com/CarmJos/EasyPlugin</url>
</project>

View File

@ -0,0 +1,40 @@
package cc.carm.lib.easyplugin.storage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface DataStorage<K, T> {
/**
* 在插件加载存储源时执行
*
* @throws Exception 当出现任何错误时抛出
*/
void initialize() throws Exception;
/**
* 在插件被卸载时执行
*/
void shutdown();
/**
* 用于加载数据的方法<b>该方法应当异步运行</b>
* <br>
* <br>若不存在对应数据请返回 null
* <br>若加载出现任何错误请抛出异常
*
* @param key 数据主键
* @throws Exception 当出现任何错误时抛出
*/
@Nullable
T loadData(@NotNull K key) throws Exception;
/**
* 用于保存数据的方法 <b>该方法应当被异步运行</b>
*
* @param data 数据
* @throws Exception 当出现任何错误时抛出
*/
void saveData(@NotNull T data) throws Exception;
}

View File

@ -0,0 +1,19 @@
package cc.carm.lib.easyplugin.storage;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public interface StorageType<K, V> {
int getID();
@NotNull List<String> getAlias();
@NotNull Class<? extends DataStorage<K, V>> getStorageClass();
default @NotNull DataStorage<K, V> createStorage() throws Exception {
return getStorageClass().newInstance();
}
}

View File

@ -0,0 +1,31 @@
package cc.carm.lib.easyplugin.storage.file;
import cc.carm.lib.easyplugin.storage.DataStorage;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public abstract class FileBasedStorage<K, T> implements DataStorage<K, T> {
protected final String fileName;
protected File dataFile;
public FileBasedStorage(String fileName) {
this.fileName = fileName;
}
protected @NotNull File initializeFile(@NotNull File parentFolder) throws Exception {
this.dataFile = new File(parentFolder, fileName);
if (!dataFile.exists()) {
if (!dataFile.createNewFile()) throw new Exception("无法创建数据文件!");
} else if (dataFile.isDirectory()) {
throw new Exception("文件路径对应的不是一个文件!");
}
return dataFile;
}
public File getDataFile() {
return dataFile;
}
}

View File

@ -0,0 +1,46 @@
package cc.carm.lib.easyplugin.storage.file;
import cc.carm.lib.easyplugin.storage.DataStorage;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public abstract class FolderBasedStorage<K, T> implements DataStorage<K, T> {
protected final @NotNull String folderPath;
protected File dataFolder;
public FolderBasedStorage(@NotNull String folderPath) {
this.folderPath = folderPath;
}
protected @NotNull File initializeFolder(@NotNull File parentFolder) throws Exception {
this.dataFolder = new File(parentFolder, folderPath);
if (!dataFolder.exists()) {
if (!dataFolder.mkdir()) {
throw new Exception("无法创建数据文件夹!");
}
} else if (!dataFolder.isDirectory()) {
throw new Exception("数据文件夹路径对应的不是一个文件夹!");
}
return dataFolder;
}
protected @NotNull List<File> listFiles() {
if (this.dataFolder == null) return Collections.emptyList();
if (!this.dataFolder.isDirectory()) return Collections.emptyList();
File[] files = this.dataFolder.listFiles();
if (files == null) return Collections.emptyList();
return Arrays.asList(files);
}
public File getDataFolder() {
return dataFolder;
}
}

View File

@ -0,0 +1,24 @@
package cc.carm.lib.easyplugin.storage.file;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public abstract class YAMLBasedStorage<K, T> extends FileBasedStorage<K, T> {
protected FileConfiguration configuration;
public YAMLBasedStorage(String fileName) {
super(fileName);
}
protected @NotNull FileConfiguration initializeConfiguration(@NotNull File parentFolder) throws Exception {
return this.configuration = YamlConfiguration.loadConfiguration(initializeFile(parentFolder));
}
public FileConfiguration getConfiguration() {
return configuration;
}
}

View File

@ -0,0 +1,31 @@
package cc.carm.lib.easyplugin.storage.plugin;
import cc.carm.lib.easyplugin.storage.DataStorage;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
public abstract class PluginBasedStorage<K, T> implements DataStorage<K, T> {
protected Plugin dependPlugin;
public PluginBasedStorage(String dependPluginName) {
this(Bukkit.getPluginManager().getPlugin(dependPluginName));
}
public PluginBasedStorage(Plugin dependPlugin) {
this.dependPlugin = dependPlugin;
}
@Override
public void initialize() throws NullPointerException {
if (dependPlugin == null) {
throw new NullPointerException("该存储类型依赖的插件不存在,请检查配置文件");
}
}
public Plugin getDependPlugin() {
return dependPlugin;
}
}

View File

@ -27,7 +27,6 @@
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>

View File

@ -27,7 +27,6 @@
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>

27
pom.xml
View File

@ -10,6 +10,11 @@
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<dep.spigot>1.3.1</dep.spigot>
<dep.githubreleases4j>1.3.1</dep.githubreleases4j>
</properties>
<groupId>cc.carm.lib</groupId>
@ -24,10 +29,12 @@
<module>extension/papi</module>
<module>extension/vault</module>
<module>extension/updater</module>
<module>collection/all</module>
<module>collection/bom</module>
<module>collection/common</module>
<module>base/storage</module>
</modules>
@ -122,6 +129,26 @@
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>githubreleases4j</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>