From 4c10a9798f427739e1f696c2da2fa4066982123d Mon Sep 17 00:00:00 2001 From: carm Date: Sat, 18 Jun 2022 02:32:08 +0800 Subject: [PATCH] =?UTF-8?q?feat(storage):=20=E6=B7=BB=E5=8A=A0=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E6=8E=A5=E5=8F=A3=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E5=AD=98=E5=82=A8=E7=9B=B8=E5=85=B3=E7=9A=84=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/main/pom.xml | 1 - .../lib/easyplugin/utils/ColorParser.java | 2 +- base/storage/pom.xml | 27 +++++++++++ .../lib/easyplugin/storage/DataStorage.java | 40 ++++++++++++++++ .../lib/easyplugin/storage/StorageType.java | 19 ++++++++ .../storage/file/FileBasedStorage.java | 31 +++++++++++++ .../storage/file/FolderBasedStorage.java | 46 +++++++++++++++++++ .../storage/file/YAMLBasedStorage.java | 24 ++++++++++ .../storage/plugin/PluginBasedStorage.java | 31 +++++++++++++ extension/papi/pom.xml | 1 - extension/vault/pom.xml | 1 - pom.xml | 27 +++++++++++ 12 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 base/storage/pom.xml create mode 100644 base/storage/src/main/java/cc/carm/lib/easyplugin/storage/DataStorage.java create mode 100644 base/storage/src/main/java/cc/carm/lib/easyplugin/storage/StorageType.java create mode 100644 base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FileBasedStorage.java create mode 100644 base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FolderBasedStorage.java create mode 100644 base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/YAMLBasedStorage.java create mode 100644 base/storage/src/main/java/cc/carm/lib/easyplugin/storage/plugin/PluginBasedStorage.java diff --git a/base/main/pom.xml b/base/main/pom.xml index a10e580..3939c84 100644 --- a/base/main/pom.xml +++ b/base/main/pom.xml @@ -56,7 +56,6 @@ me.clip placeholderapi - 2.10.9 provided diff --git a/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java index ff8b6c0..7efcaf9 100644 --- a/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java +++ b/base/main/src/main/java/cc/carm/lib/easyplugin/utils/ColorParser.java @@ -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)); diff --git a/base/storage/pom.xml b/base/storage/pom.xml new file mode 100644 index 0000000..3cd5dc8 --- /dev/null +++ b/base/storage/pom.xml @@ -0,0 +1,27 @@ + + + + easyplugin-parent + cc.carm.lib + 1.4.7 + ../../pom.xml + + 4.0.0 + + ${project.jdk.version} + ${project.jdk.version} + UTF-8 + UTF-8 + + + easyplugin-storage + jar + + EasyPlugin-Storage + 轻松插件存储接口模块,包含存储相关的统一接口。 + https://github.com/CarmJos/EasyPlugin + + + \ No newline at end of file diff --git a/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/DataStorage.java b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/DataStorage.java new file mode 100644 index 0000000..17fbd80 --- /dev/null +++ b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/DataStorage.java @@ -0,0 +1,40 @@ +package cc.carm.lib.easyplugin.storage; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public interface DataStorage { + + /** + * 在插件加载存储源时执行。 + * + * @throws Exception 当出现任何错误时抛出 + */ + void initialize() throws Exception; + + /** + * 在插件被卸载时执行。 + */ + void shutdown(); + + /** + * 用于加载数据的方法。该方法应当异步运行! + *
+ *
若不存在对应数据,请返回 null 。 + *
若加载出现任何错误,请抛出异常。 + * + * @param key 数据主键 + * @throws Exception 当出现任何错误时抛出 + */ + @Nullable + T loadData(@NotNull K key) throws Exception; + + /** + * 用于保存数据的方法。 该方法应当被异步运行! + * + * @param data 数据 + * @throws Exception 当出现任何错误时抛出 + */ + void saveData(@NotNull T data) throws Exception; + +} diff --git a/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/StorageType.java b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/StorageType.java new file mode 100644 index 0000000..31dd031 --- /dev/null +++ b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/StorageType.java @@ -0,0 +1,19 @@ +package cc.carm.lib.easyplugin.storage; + +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public interface StorageType { + + int getID(); + + @NotNull List getAlias(); + + @NotNull Class> getStorageClass(); + + default @NotNull DataStorage createStorage() throws Exception { + return getStorageClass().newInstance(); + } + +} diff --git a/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FileBasedStorage.java b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FileBasedStorage.java new file mode 100644 index 0000000..0b64ba7 --- /dev/null +++ b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FileBasedStorage.java @@ -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 implements DataStorage { + + + 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; + } +} diff --git a/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FolderBasedStorage.java b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FolderBasedStorage.java new file mode 100644 index 0000000..d06d206 --- /dev/null +++ b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/FolderBasedStorage.java @@ -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 implements DataStorage { + + 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 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; + } + +} diff --git a/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/YAMLBasedStorage.java b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/YAMLBasedStorage.java new file mode 100644 index 0000000..2a0ffd6 --- /dev/null +++ b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/file/YAMLBasedStorage.java @@ -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 extends FileBasedStorage { + + 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; + } +} diff --git a/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/plugin/PluginBasedStorage.java b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/plugin/PluginBasedStorage.java new file mode 100644 index 0000000..fc5f029 --- /dev/null +++ b/base/storage/src/main/java/cc/carm/lib/easyplugin/storage/plugin/PluginBasedStorage.java @@ -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 implements DataStorage { + + 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; + } + + +} diff --git a/extension/papi/pom.xml b/extension/papi/pom.xml index fe71b1b..32e85c4 100644 --- a/extension/papi/pom.xml +++ b/extension/papi/pom.xml @@ -27,7 +27,6 @@ me.clip placeholderapi - 2.10.9 provided diff --git a/extension/vault/pom.xml b/extension/vault/pom.xml index 3f54b0c..97e8d46 100644 --- a/extension/vault/pom.xml +++ b/extension/vault/pom.xml @@ -27,7 +27,6 @@ com.github.MilkBowl VaultAPI - 1.7 provided diff --git a/pom.xml b/pom.xml index 88e869e..1db07a8 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,11 @@ ${project.jdk.version} UTF-8 UTF-8 + + 1.3.1 + 1.3.1 + + cc.carm.lib @@ -24,10 +29,12 @@ extension/papi extension/vault + extension/updater collection/all collection/bom collection/common + base/storage @@ -122,6 +129,26 @@ + + + + me.clip + placeholderapi + 2.10.9 + + + com.github.MilkBowl + VaultAPI + 1.7 + + + cc.carm.lib + githubreleases4j + 1.3.1 + + + +