From 3308158a8229fc2c75d22372a89f694fd81a0b7d Mon Sep 17 00:00:00 2001 From: carm Date: Tue, 19 Dec 2023 20:26:13 +0800 Subject: [PATCH] feat(database): Dropped MineSQL with original easysql registry --- api/pom.xml | 17 ++++-- .../java/com/artformgames/core/ArtCore.java | 9 +++- .../com/artformgames/core/ArtCorePlugin.java | 6 ++- .../artformgames/core/conf/MessagesRoot.java | 47 ++++++++++++++++ plugin/pom.xml | 8 +-- .../main/java/com/artformgames/core/Main.java | 44 +++++++++++---- .../core/data/DBConfiguration.java | 34 ++++++++++++ .../artformgames/core/data/DataManager.java | 54 +++++++++++++++++++ .../artformgames/core/data/DataTables.java | 4 +- pom.xml | 5 +- 10 files changed, 200 insertions(+), 28 deletions(-) create mode 100644 api/src/main/java/com/artformgames/core/conf/MessagesRoot.java create mode 100644 plugin/src/main/java/com/artformgames/core/data/DBConfiguration.java create mode 100644 plugin/src/main/java/com/artformgames/core/data/DataManager.java diff --git a/api/pom.xml b/api/pom.xml index 5229b11..81a6daf 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -6,7 +6,7 @@ com.artformgames artcore-parent - 1.0-SNAPSHOT + 1.0.0 ${project.jdk.version} @@ -20,9 +20,16 @@ - cc.carm.plugin - minesql-api - ${deps.minesql.version} + cc.carm.lib + easysql-api + ${deps.easysql.version} + compile + + + + de.themoep + minedown + ${deps.minedown.version} compile @@ -123,7 +130,7 @@ 1.6 compile - + diff --git a/api/src/main/java/com/artformgames/core/ArtCore.java b/api/src/main/java/com/artformgames/core/ArtCore.java index 37544b7..57d855d 100644 --- a/api/src/main/java/com/artformgames/core/ArtCore.java +++ b/api/src/main/java/com/artformgames/core/ArtCore.java @@ -1,5 +1,6 @@ package com.artformgames.core; +import cc.carm.lib.easysql.api.SQLManager; import com.artformgames.core.user.manager.UserManager; import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi; import org.bukkit.Bukkit; @@ -24,12 +25,16 @@ private static ArtCorePlugin plugin() { return core; } + public static SQLManager getSQLManager() { + return plugin().getSQLManager(); + } + public static UserManager getUserManager() { - throw new UnsupportedOperationException("Not implemented"); + return plugin().getUserManager(); } public static BungeeChannelApi getBungeeAPI() { - throw new UnsupportedOperationException("Not implemented"); + return plugin().getBungeeAPI(); } diff --git a/api/src/main/java/com/artformgames/core/ArtCorePlugin.java b/api/src/main/java/com/artformgames/core/ArtCorePlugin.java index 457b29c..12679ee 100644 --- a/api/src/main/java/com/artformgames/core/ArtCorePlugin.java +++ b/api/src/main/java/com/artformgames/core/ArtCorePlugin.java @@ -1,13 +1,17 @@ package com.artformgames.core; +import cc.carm.lib.easysql.api.SQLManager; import com.artformgames.core.user.manager.UserManager; +import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi; import org.jetbrains.annotations.NotNull; interface ArtCorePlugin { - @NotNull UserManager getUserManager(); + @NotNull SQLManager getSQLManager(); + @NotNull UserManager getUserManager(); + @NotNull BungeeChannelApi getBungeeAPI(); } diff --git a/api/src/main/java/com/artformgames/core/conf/MessagesRoot.java b/api/src/main/java/com/artformgames/core/conf/MessagesRoot.java new file mode 100644 index 0000000..0bb5ced --- /dev/null +++ b/api/src/main/java/com/artformgames/core/conf/MessagesRoot.java @@ -0,0 +1,47 @@ +package com.artformgames.core.conf; + +import cc.carm.lib.configuration.core.ConfigurationRoot; +import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageListBuilder; +import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageValueBuilder; +import cc.carm.lib.mineconfiguration.bukkit.builder.title.TitleConfigBuilder; +import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage; +import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList; +import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle; +import de.themoep.minedown.MineDown; +import me.clip.placeholderapi.PlaceholderAPI; +import net.md_5.bungee.api.chat.BaseComponent; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.function.BiFunction; + +public abstract class MessagesRoot extends ConfigurationRoot { + + protected static @NotNull CraftMessageListBuilder list() { + return ConfiguredMessageList.create(getParser()) + .whenSend((sender, message) -> message.forEach(m -> sender.spigot().sendMessage(m))); + } + + protected static @NotNull CraftMessageValueBuilder value() { + return ConfiguredMessage.create(getParser()) + .whenSend((sender, message) -> sender.spigot().sendMessage(message)); + } + + protected static @NotNull TitleConfigBuilder title() { + return ConfiguredTitle.create(); + } + + private static @NotNull BiFunction getParser() { + return (sender, message) -> { + if (sender == null) return MineDown.parse(message); + if (sender instanceof Player player) { + return MineDown.parse(PlaceholderAPI.setPlaceholders(player, message)); + } else { + return MineDown.parse(message); + } + }; + } + + +} diff --git a/plugin/pom.xml b/plugin/pom.xml index 16797a8..8e48162 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -6,7 +6,7 @@ com.artformgames artcore-parent - 1.0-SNAPSHOT + 1.0.0 ${project.jdk.version} @@ -27,9 +27,9 @@ - cc.carm.plugin - minesql-api - ${deps.minesql.version} + cc.carm.lib + easysql-beecp + ${deps.easysql.version} provided diff --git a/plugin/src/main/java/com/artformgames/core/Main.java b/plugin/src/main/java/com/artformgames/core/Main.java index 2d2774c..6f52f04 100644 --- a/plugin/src/main/java/com/artformgames/core/Main.java +++ b/plugin/src/main/java/com/artformgames/core/Main.java @@ -4,15 +4,15 @@ import cc.carm.lib.easyplugin.gui.GUI; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.mineconfiguration.bukkit.MineConfiguration; -import cc.carm.plugin.minesql.MineSQL; import com.artformgames.core.conf.PluginConfig; import com.artformgames.core.conf.PluginMessages; -import com.artformgames.core.data.DataTables; +import com.artformgames.core.data.DataManager; import com.artformgames.core.function.settings.UserSettingsLoader; import com.artformgames.core.listener.PluginListener; import com.artformgames.core.listener.UserListener; import com.artformgames.core.user.BukkitUserManager; import com.artformgames.core.utils.GHUpdateChecker; +import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; @@ -26,7 +26,9 @@ public Main() { } protected MineConfiguration configuration; + protected DataManager dataManager; protected BukkitUserManager usersManager; + protected BungeeChannelApi bungeeChannel; @Override protected void load() { @@ -38,36 +40,40 @@ protected void load() { log("Loading database..."); - SQLManager sqlManager = MineSQL.getRegistry().get(PluginConfig.DATASOURCE_ID.getNotNull()); - if (sqlManager == null) { - error("Datasource not configured or exists! Please check the configuration!"); + this.dataManager = new DataManager(getLogger()); + if (!dataManager.initialize()) { + dataManager.shutdown(); setEnabled(false); return; } - DataTables.initializeTables(sqlManager); - log("Initialize users manager..."); + log("Initializing users manager..."); this.usersManager = new BukkitUserManager(getLogger()); if (!Bukkit.getOnlinePlayers().isEmpty()) { this.usersManager.loadAll(); } + + log("Initializing channels..."); + this.bungeeChannel = new BungeeChannelApi(this); + } @Override protected boolean initialize() { - log("Register listeners..."); + log("Registering listeners..."); GUI.initialize(this); registerListener(new UserListener()); registerListener(new PluginListener()); - log("Register commands..."); + log("Registering commands..."); - log("Enable user settings data..."); + log("Enabling user settings data..."); this.usersManager.registerHandler(new UserSettingsLoader(this)); + if (PluginConfig.METRICS.getNotNull()) { log("Initializing bStats..."); new Metrics(this, 20501); @@ -86,6 +92,9 @@ protected boolean initialize() { @Override protected void shutdown() { + log("Close channels..."); + this.bungeeChannel.unregister(); + log("Shutting down UserManager..."); try { this.usersManager.unloadAll(); @@ -93,13 +102,16 @@ protected void shutdown() { e.printStackTrace(); } + log("Shutting down DataManager..."); + this.dataManager.shutdown(); + } @Override public boolean isDebugging() { return PluginConfig.DEBUG.getNotNull(); } - + public static void info(String... messages) { getInstance().log(messages); } @@ -116,9 +128,19 @@ public static Main getInstance() { return instance; } + @Override + public @NotNull SQLManager getSQLManager() { + return this.dataManager.getSQLManager(); + } + @Override public @NotNull BukkitUserManager getUserManager() { return this.usersManager; } + @Override + public @NotNull BungeeChannelApi getBungeeAPI() { + return this.bungeeChannel; + } + } diff --git a/plugin/src/main/java/com/artformgames/core/data/DBConfiguration.java b/plugin/src/main/java/com/artformgames/core/data/DBConfiguration.java new file mode 100644 index 0000000..1d21aa3 --- /dev/null +++ b/plugin/src/main/java/com/artformgames/core/data/DBConfiguration.java @@ -0,0 +1,34 @@ +package com.artformgames.core.data; + +import cc.carm.lib.configuration.core.ConfigurationRoot; +import cc.carm.lib.configuration.core.annotation.ConfigPath; +import cc.carm.lib.configuration.core.annotation.HeaderComment; +import cc.carm.lib.configuration.core.value.ConfigValue; +import cc.carm.lib.configuration.core.value.type.ConfiguredValue; + +public class DBConfiguration extends ConfigurationRoot { + + @ConfigPath("driver") + @HeaderComment({ + "数据库驱动配置,请根据数据库类型设置。", + "默认为MySQL: com.mysql.cj.jdbc.Driver", + }) + protected static final ConfigValue DRIVER_NAME = ConfiguredValue.of( + String.class, "com.mysql.cj.jdbc.Driver" + ); + + protected static final ConfigValue HOST = ConfiguredValue.of(String.class, "127.0.0.1"); + protected static final ConfigValue PORT = ConfiguredValue.of(Integer.class, 3306); + protected static final ConfigValue DATABASE = ConfiguredValue.of(String.class, "minecraft"); + protected static final ConfigValue USERNAME = ConfiguredValue.of(String.class, "root"); + protected static final ConfigValue PASSWORD = ConfiguredValue.of(String.class, "password"); + protected static final ConfigValue EXTRA = ConfiguredValue.of(String.class, "?useSSL=false"); + + protected static String buildJDBC() { + return String.format("jdbc:mysql://%s:%s/%s%s", + HOST.getNotNull(), PORT.getNotNull(), DATABASE.getNotNull(), EXTRA.getNotNull() + ); + } + + +} diff --git a/plugin/src/main/java/com/artformgames/core/data/DataManager.java b/plugin/src/main/java/com/artformgames/core/data/DataManager.java new file mode 100644 index 0000000..bae1f84 --- /dev/null +++ b/plugin/src/main/java/com/artformgames/core/data/DataManager.java @@ -0,0 +1,54 @@ +package com.artformgames.core.data; + +import cc.carm.lib.easysql.EasySQL; +import cc.carm.lib.easysql.api.SQLManager; +import com.artformgames.core.Main; + +import java.util.logging.Logger; + + +public class DataManager { + + + private final Logger logger; + private SQLManager sqlManager; + + public DataManager(Logger logger) { + this.logger = logger; + } + + public Logger getLogger() { + return logger; + } + + public boolean initialize() { + try { + getLogger().info(" Connecting to database..."); + this.sqlManager = EasySQL.createManager( + DBConfiguration.DRIVER_NAME.getNotNull(), DBConfiguration.buildJDBC(), + DBConfiguration.USERNAME.getNotNull(), DBConfiguration.PASSWORD.getNotNull() + ); + this.sqlManager.setDebugMode(() -> Main.getInstance().isDebugging()); + } catch (Exception exception) { + getLogger().severe("Error connecting to database, please check the configuration."); + exception.printStackTrace(); + return false; + } + + getLogger().info(" Initializing tables..."); + DataTables.initializeTables(this.sqlManager); + + return true; + } + + public void shutdown() { + EasySQL.shutdownManager(getSQLManager()); + this.sqlManager = null; + } + + public SQLManager getSQLManager() { + return sqlManager; + } + + +} diff --git a/plugin/src/main/java/com/artformgames/core/data/DataTables.java b/plugin/src/main/java/com/artformgames/core/data/DataTables.java index 599ad51..9063b5e 100644 --- a/plugin/src/main/java/com/artformgames/core/data/DataTables.java +++ b/plugin/src/main/java/com/artformgames/core/data/DataTables.java @@ -16,7 +16,7 @@ public enum DataTables implements SQLTable { - USERS("users", (table) -> { + USERS("users", table -> { table.addAutoIncrementColumn(UserKey.KeyType.ID.getColumnName()); table.addColumn(UserKey.KeyType.UUID.getColumnName(), "CHAR(36) NOT NULL"); table.addColumn(UserKey.KeyType.NAME.getColumnName(), "VARCHAR(20)"); @@ -25,7 +25,7 @@ public enum DataTables implements SQLTable { table.setIndex(IndexType.UNIQUE_KEY, "idx_user_uuid", UserKey.KeyType.UUID.getColumnName()); }), - USER_SETTINGS("user_settings", (builder) -> { + USER_SETTINGS("user_settings", builder -> { builder.addColumn("uid", "INT UNSIGNED NOT NULL"); builder.addColumn("type", "INT(11) UNSIGNED NOT NULL"); builder.addColumn("value", "TEXT"); diff --git a/pom.xml b/pom.xml index 26b515e..026fa9d 100644 --- a/pom.xml +++ b/pom.xml @@ -17,8 +17,7 @@ ${project.groupId}.core - 1.4.1 - + 0.4.7 1.5.11 2.8.8 1.7.1-SNAPSHOT @@ -31,7 +30,7 @@ com.artformgames artcore-parent - 1.0-SNAPSHOT + 1.0.0 ArtCore