From e20b9ef90a87b5caf7c0fc1aea65cde2744e5b3d Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Sat, 29 Jan 2022 23:22:42 +0800 Subject: [PATCH] sync with github --- .../carm/plugin/easysql/api/EasySQLAPI.java | 55 ++++++- .../easysql/api/EasySQLPluginPlatform.java | 4 + .../plugin/easysql/api/SQLConfiguration.java | 15 -- easysql-plugin-impl/pom.xml | 7 + .../java/cc/carm/plugin/easysql/EasySQL.java | 9 -- .../carm/plugin/easysql/EasySQLManager.java | 143 ++++++++++++++++++ .../src/main/resources/config.yml | 9 ++ platforms/easysql-plugin-bukkit/pom.xml | 8 - .../cc/carm/plugin/easysql/EasySQLBukkit.java | 97 +++++++++++- .../cc/carm/plugin/easysql/EasySQLBungee.java | 124 ++++++++++++++- .../carm/plugin/easysql/EasySQLVelocity.java | 13 +- pom.xml | 7 + 12 files changed, 450 insertions(+), 41 deletions(-) create mode 100644 easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLPluginPlatform.java delete mode 100644 easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/SQLConfiguration.java delete mode 100644 easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQL.java create mode 100644 easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQLManager.java create mode 100644 easysql-plugin-impl/src/main/resources/config.yml diff --git a/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLAPI.java b/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLAPI.java index bf16b2b..524384d 100644 --- a/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLAPI.java +++ b/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLAPI.java @@ -1,15 +1,62 @@ package cc.carm.plugin.easysql.api; import cc.carm.lib.easysql.api.SQLManager; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Map; + /** * 入口类 */ -public class EasySQLAPI { +public interface EasySQLAPI { + /** + * 获取指定名称的 SQLManager 实例 + * @param name 要获取的 SQLManager 实例名称 + * @return SQLManager 实例 + */ + @Nullable SQLManager getSQLManager(@Nullable String name); - public static @Nullable SQLManager getSQLManager(@Nullable String name) { - return null; - } + /** + * 获取所有 SQLManager 实例 + * @return SQLManager 实例集合 + */ + @NotNull Map getSQLManagers(); + + /** + * 获取指定名称的 SQLManager 实例的命名空间 + * @param name SQLManager 名称 + * @return 命名空间 + */ + @Nullable + String getNameSpace(@Nullable String name); + + /** + * 获取指定 SQLManager 的注册名 + * @param sqlManager SQLManager 实例 + * @return 注册名 + */ + @Nullable + String getName(@NotNull SQLManager sqlManager); + + /** + * 注册 SQLManager 实例 + * @param namespace 命名空间,通常为插件名称 + * @param name 实例名称 + * @param sqlManager SQLManager 实例 + */ + void registerSQLManager(@NotNull String namespace,@NotNull String name, @NotNull SQLManager sqlManager); + + /** + * 注销指定的 SQLManager 实例 + * @param name 实例名称 + */ + void unregisterSQLManager(@NotNull String name); + + /** + * 注销指定命名空间下的所有 SQLManager 实例 + * @param namespace 命名空间 + */ + void unregisterSQLManagerAll(@NotNull String namespace); } diff --git a/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLPluginPlatform.java b/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLPluginPlatform.java new file mode 100644 index 0000000..acc6f32 --- /dev/null +++ b/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/EasySQLPluginPlatform.java @@ -0,0 +1,4 @@ +package cc.carm.plugin.easysql.api; + +public interface EasySQLPluginPlatform { +} diff --git a/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/SQLConfiguration.java b/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/SQLConfiguration.java deleted file mode 100644 index 2ec65f8..0000000 --- a/easysql-plugin-api/src/main/java/cc/carm/plugin/easysql/api/SQLConfiguration.java +++ /dev/null @@ -1,15 +0,0 @@ -package cc.carm.plugin.easysql.api; - -public class SQLConfiguration { - - String driver; - - String address; - int port; - - String database; - - String username; - String password; - -} diff --git a/easysql-plugin-impl/pom.xml b/easysql-plugin-impl/pom.xml index 2dc64e6..0949e72 100644 --- a/easysql-plugin-impl/pom.xml +++ b/easysql-plugin-impl/pom.xml @@ -48,6 +48,13 @@ compile + + + com.google.guava + guava + compile + + diff --git a/easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQL.java b/easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQL.java deleted file mode 100644 index bc3f830..0000000 --- a/easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQL.java +++ /dev/null @@ -1,9 +0,0 @@ -package cc.carm.plugin.easysql; - -public class EasySQL { - - - - - -} diff --git a/easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQLManager.java b/easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQLManager.java new file mode 100644 index 0000000..f879265 --- /dev/null +++ b/easysql-plugin-impl/src/main/java/cc/carm/plugin/easysql/EasySQLManager.java @@ -0,0 +1,143 @@ +package cc.carm.plugin.easysql; + +import cc.carm.lib.easysql.api.SQLManager; +import cc.carm.plugin.easysql.api.EasySQLAPI; +import cc.carm.plugin.easysql.api.EasySQLPluginPlatform; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.ImmutableMap; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class EasySQLManager implements EasySQLAPI { + public static final String VERSION = "1.0.0"; + private final Lock LOCK =new ReentrantLock(); + private final BiMap> nameSpaceRegistry = HashBiMap.create(); + private final BiMap sqlManagerRegistry = HashBiMap.create(); + + // 这个主要是避免外部插件注册此 EasySQLManager + // 没有其他意义 + public EasySQLManager(@NotNull EasySQLPluginPlatform pluginPlatform){ + + } + + /** + * 获取指定名称的 SQLManager 实例 + * + * @param name 要获取的 SQLManager 实例名称 + * @return SQLManager 实例 + */ + @Override + public @Nullable SQLManager getSQLManager(@Nullable String name) { + LOCK.lock(); + SQLManager sqlManager = this.sqlManagerRegistry.get(name); + LOCK.unlock(); + return sqlManager; + } + + /** + * 获取所有 SQLManager 实例 + * + * @return SQLManager 实例集合 + */ + @Override + public @NotNull Map getSQLManagers() { + return ImmutableMap.copyOf(this.sqlManagerRegistry); + } + + /** + * 获取指定名称的 SQLManager 实例的命名空间 + * + * @param name SQLManager 名称 + * @return 命名空间 + */ + @Override + public @Nullable String getNameSpace(@Nullable String name) { + LOCK.lock(); + String namespace = null; + for (Map.Entry> stringSetEntry : this.nameSpaceRegistry.entrySet()) { + if (stringSetEntry.getValue().contains(name)) { + namespace = stringSetEntry.getKey(); + break; + } + } + LOCK.unlock(); + return namespace; + } + + /** + * 获取指定 SQLManager 的注册名 + * + * @param sqlManager SQLManager 实例 + * @return 注册名 + */ + @Override + public @Nullable String getName(@NotNull SQLManager sqlManager) { + LOCK.lock(); + String name = sqlManagerRegistry.inverse().get(sqlManager); + LOCK.unlock(); + return name; + } + + /** + * 注册 SQLManager 实例 + * + * @param namespace 命名空间,通常为插件名称 + * @param name 实例名称 + * @param sqlManager SQLManager 实例 + */ + @Override + public void registerSQLManager(@NotNull String namespace, @NotNull String name, @NotNull SQLManager sqlManager) { + LOCK.lock(); + // 注册命名空间 + this.sqlManagerRegistry.put(name, sqlManager); + // 注册名称 + this.nameSpaceRegistry.putIfAbsent(namespace, this.nameSpaceRegistry.getOrDefault(namespace, new HashSet<>())); + // 注册名称 -> 命名空间 + this.nameSpaceRegistry.get(namespace).add(name); + LOCK.unlock(); + } + + /** + * 注销指定的 SQLManager 实例 + * + * @param name 实例名称 + */ + @Override + public void unregisterSQLManager(@NotNull String name) { + LOCK.lock(); + // 先清除命名空间 + this.sqlManagerRegistry.remove(name); + // 再清除名称 + for (Map.Entry> stringSetEntry : this.nameSpaceRegistry.entrySet()) { + stringSetEntry.getValue().remove(name); + } + LOCK.unlock(); + } + + /** + * 注销指定命名空间下的所有 SQLManager 实例 + * + * @param namespace 命名空间 + */ + @Override + public void unregisterSQLManagerAll(@NotNull String namespace) { + LOCK.lock(); + Set names = this.nameSpaceRegistry.get(namespace); + if (names != null) { + // 删除所有的 SQLManager 实例 + for (String name : names) { + this.sqlManagerRegistry.remove(name); + } + } + // 删除命名空间 + this.nameSpaceRegistry.remove(namespace); + LOCK.unlock(); + } +} diff --git a/easysql-plugin-impl/src/main/resources/config.yml b/easysql-plugin-impl/src/main/resources/config.yml new file mode 100644 index 0000000..9de2f9f --- /dev/null +++ b/easysql-plugin-impl/src/main/resources/config.yml @@ -0,0 +1,9 @@ +instances: + name1: + url: jdbc://localhost:3306/name1 + username: mc + password: password + name2: + url: jdbc://localhost:3306/name2 + username: mc + password: password \ No newline at end of file diff --git a/platforms/easysql-plugin-bukkit/pom.xml b/platforms/easysql-plugin-bukkit/pom.xml index 5c78c2a..323624c 100644 --- a/platforms/easysql-plugin-bukkit/pom.xml +++ b/platforms/easysql-plugin-bukkit/pom.xml @@ -47,14 +47,6 @@ 2.2.1 compile - - - cc.carm.lib - easyplugin-main - true - compile - - diff --git a/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java b/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java index b90186f..f3b9492 100644 --- a/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java +++ b/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java @@ -1,4 +1,99 @@ package cc.carm.plugin.easysql; -public class EasySQLBukkit { +import cc.carm.lib.easysql.api.SQLManager; +import cc.carm.lib.easysql.manager.SQLManagerImpl; +import cc.carm.plugin.easysql.api.EasySQLAPI; +import cc.carm.plugin.easysql.api.EasySQLPluginPlatform; +import cn.beecp.BeeDataSource; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +public class EasySQLBukkit extends JavaPlugin implements EasySQLPluginPlatform, Listener { + private EasySQLAPI apiImpl; + + private boolean setup = false; + + public void setup() { + if (setup) return; + apiImpl = new EasySQLManager(this); + saveDefaultConfig(); + // 读取配置文件 - 预注册 instance + ConfigurationSection instancesConfig = getConfig().getConfigurationSection("instances"); + if (instancesConfig != null) { + for (String instanceName : instancesConfig.getKeys(false)) { + SQLManager sqlManager = new SQLManagerImpl( + new BeeDataSource("mysql" + , instancesConfig.getString("url") + , instancesConfig.getString("username") + , instancesConfig.getString("password") + ) + ); + this.register(this, instanceName, sqlManager); + } + } + setup = true; + } + + @Override + public void onLoad() { + setup(); // 尽可能早的初始化预注册的 SQLManager + } + + @Override + public void onEnable() { + setup(); // 在特定情况下 Bukkit 可能不会调用 onLoad 函数 + } + + @Override + public void onDisable() { + getLogger().info("Shutting down..."); + this.unregister(this); + apiImpl.getSQLManagers().keySet().forEach(apiImpl::unregisterSQLManager); + super.onDisable(); + } + // =========== 平台注册方法 =========== + + /** + * 注册 SQLManager 实例 + * @param plugin 插件实例 + * @param name SQLManager 映射名称 + * @param sqlManager SQLManager 实例 + */ + public void register(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull SQLManager sqlManager){ + if(this.apiImpl.getSQLManagers().containsKey(name)){ + // 名称冲突处理 + throw new IllegalArgumentException("Instance name conflict: " + name+", Already registered by "+this.apiImpl.getNameSpace(name)); + } + this.apiImpl.registerSQLManager(plugin.getDescription().getName(), name, sqlManager); + } + + /** + * 注销指定 SQLManager 实例 + * @param sqlManager SQLManager 实例 + */ + public void unregister(@NotNull SQLManager sqlManager){ + String name = this.apiImpl.getName(sqlManager); + if(name != null){ + this.apiImpl.unregisterSQLManager(name); + } + } + + /** + * 注销指定名称的 SQLManager + * @param name SQLManager 名称 + */ + public void unregister(@NotNull String name){ + this.apiImpl.unregisterSQLManager(name); + } + + /** + * 注销指定插件注册的所有 SQLManager 实例 + * @param plugin 插件 + */ + public void unregister(@NotNull JavaPlugin plugin){ + this.apiImpl.unregisterSQLManagerAll(plugin.getName()); + } + } diff --git a/platforms/easysql-plugin-bungee/src/main/java/cc/carm/plugin/easysql/EasySQLBungee.java b/platforms/easysql-plugin-bungee/src/main/java/cc/carm/plugin/easysql/EasySQLBungee.java index 832ab02..11ee31a 100644 --- a/platforms/easysql-plugin-bungee/src/main/java/cc/carm/plugin/easysql/EasySQLBungee.java +++ b/platforms/easysql-plugin-bungee/src/main/java/cc/carm/plugin/easysql/EasySQLBungee.java @@ -1,4 +1,126 @@ package cc.carm.plugin.easysql; -public class EasySQLBungee { +import cc.carm.lib.easysql.api.SQLManager; +import cc.carm.lib.easysql.manager.SQLManagerImpl; +import cc.carm.plugin.easysql.api.EasySQLAPI; +import cc.carm.plugin.easysql.api.EasySQLPluginPlatform; +import cn.beecp.BeeDataSource; +import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; + +public class EasySQLBungee extends Plugin implements EasySQLPluginPlatform { + private EasySQLAPI apiImpl; + + private boolean setup = false; + + private void saveDefaultConfig(){ + if (!getDataFolder().exists()) + getDataFolder().mkdir(); + + File file = new File(getDataFolder(), "config.yml"); + + + if (!file.exists()) { + try (InputStream in = getResourceAsStream("config.yml")) { + Files.copy(in, file.toPath()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public void setup() { + if (setup) return; + apiImpl = new EasySQLManager(this); + saveDefaultConfig(); + // 读取配置文件 - 预注册 instance + Configuration configuration = null; + try { + configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(getDataFolder(), "config.yml")); + } catch (IOException e) { + e.printStackTrace(); + return; + } + Configuration instancesConfig = configuration.getSection("instances"); + if (instancesConfig != null) { + for (String instanceName : instancesConfig.getKeys()) { + SQLManager sqlManager = new SQLManagerImpl( + new BeeDataSource("mysql" + , instancesConfig.getString("url") + , instancesConfig.getString("username") + , instancesConfig.getString("password") + ) + ); + this.register(this, instanceName, sqlManager); + } + } + setup = true; + } + + @Override + public void onLoad() { + setup(); + } + + @Override + public void onEnable() { + setup(); + } + @Override + public void onDisable() { + getLogger().info("Shutting down..."); + this.unregister(this); + apiImpl.getSQLManagers().keySet().forEach(apiImpl::unregisterSQLManager); + super.onDisable(); + } + // =========== 平台注册方法 =========== + + /** + * 注册 SQLManager 实例 + * @param plugin 插件实例 + * @param name SQLManager 映射名称 + * @param sqlManager SQLManager 实例 + */ + public void register(@NotNull Plugin plugin, @NotNull String name, @NotNull SQLManager sqlManager){ + if(this.apiImpl.getSQLManagers().containsKey(name)){ + // 名称冲突处理 + throw new IllegalArgumentException("Instance name conflict: " + name+", Already registered by "+this.apiImpl.getNameSpace(name)); + } + this.apiImpl.registerSQLManager(plugin.getDescription().getName(), name, sqlManager); + } + + /** + * 注销指定 SQLManager 实例 + * @param sqlManager SQLManager 实例 + */ + public void unregister(@NotNull SQLManager sqlManager){ + String name = this.apiImpl.getName(sqlManager); + if(name != null){ + this.apiImpl.unregisterSQLManager(name); + } + } + + /** + * 注销指定名称的 SQLManager + * @param name SQLManager 名称 + */ + public void unregister(@NotNull String name){ + this.apiImpl.unregisterSQLManager(name); + } + + /** + * 注销指定插件注册的所有 SQLManager 实例 + * @param plugin 插件 + */ + public void unregister(@NotNull Plugin plugin){ + this.apiImpl.unregisterSQLManagerAll(plugin.getDescription().getName()); + } } diff --git a/platforms/easysql-plugin-velocity/src/main/java/cc/carm/plugin/easysql/EasySQLVelocity.java b/platforms/easysql-plugin-velocity/src/main/java/cc/carm/plugin/easysql/EasySQLVelocity.java index ab086f8..f4f7815 100644 --- a/platforms/easysql-plugin-velocity/src/main/java/cc/carm/plugin/easysql/EasySQLVelocity.java +++ b/platforms/easysql-plugin-velocity/src/main/java/cc/carm/plugin/easysql/EasySQLVelocity.java @@ -2,16 +2,19 @@ package cc.carm.plugin.easysql; import com.google.inject.Inject; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.proxy.ProxyServer; import java.util.logging.Logger; + @Plugin(id = "easysql-plugin", name = "EasySQL Plugin For Velocity", version = "1.0.0", description = "", url = "https://github.com/CarmJos/EasySQL-Plugin", authors = "CarmJos" ) -public class EasySQLVelocity { +public class EasySQLVelocity { private static EasySQLVelocity instance; @@ -23,10 +26,10 @@ public class EasySQLVelocity { instance = this; this.server = server; this.logger = logger; - + // register listeners + server.getEventManager().register(this,this); } - public static EasySQLVelocity getInstance() { return instance; } @@ -39,5 +42,9 @@ public class EasySQLVelocity { return logger; } + @Subscribe + public void onInitialize(ProxyInitializeEvent event) { + + } } diff --git a/pom.xml b/pom.xml index 533fa97..ca18bdf 100644 --- a/pom.xml +++ b/pom.xml @@ -157,6 +157,13 @@ ${easysql.version} + + + com.google.guava + guava + 31.0.1-jre + +