1
mirror of https://github.com/CarmJos/MineSQL.git synced 2026-06-05 00:48:16 +08:00

fix(load): 修复插件加载时出现的问题

This commit is contained in:
2022-12-18 03:58:46 +08:00
parent b554f799ef
commit 708d3f8f3f
17 changed files with 132 additions and 57 deletions
@@ -6,8 +6,8 @@ import cc.carm.lib.easyplugin.utils.JarResourceUtils;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.githubreleases4j.GithubReleases4J;
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
import cc.carm.plugin.minesql.command.EasySQLCommand;
import cc.carm.plugin.minesql.command.EasySQLHelpFormatter;
import cc.carm.plugin.minesql.command.MineSQLCommand;
import cc.carm.plugin.minesql.command.MineSQLHelpFormatter;
import cc.carm.plugin.minesql.conf.PluginConfiguration;
import cc.carm.plugin.minesql.conf.SQLSourceGroup;
import cc.carm.plugin.minesql.util.DBPropertiesUtil;
@@ -43,6 +43,9 @@ public class MineSQLCore implements IMineSQL {
this.config = new PluginConfiguration();
this.configProvider.initialize(this.config);
getLogger().info("初始化MineSQL API...");
MineSQL.initializeAPI(this);
getLogger().info("初始化注册池...");
this.registry = new MineSQLRegistry(this);
@@ -78,7 +81,9 @@ public class MineSQLCore implements IMineSQL {
public @NotNull Map<String, SQLSourceConfig> readConfigurations() {
SQLSourceGroup group = getConfig().SOURCES.getNotNull();
Map<String, SQLSourceConfig> sources = new LinkedHashMap<>();
group.getSources().forEach((k, v) -> sources.put(k, v.createSource()));
group.getSources().entrySet().stream()
.filter(entry -> !entry.getKey().startsWith("example-"))
.forEach(entry -> sources.put(entry.getKey(), entry.getValue().createSource()));
return sources;
}
@@ -90,12 +95,17 @@ public class MineSQLCore implements IMineSQL {
File file = new File(getPluginFolder(), propertiesFolder);
if (!file.exists() || !file.isDirectory()) {
try {
JarResourceUtils.copyFolderFromJar(
"db-properties", file, JarResourceUtils.CopyOption.COPY_IF_NOT_EXIST
);
} catch (Exception ex) {
getLogger().severe("初始化properties示例文件失败:" + ex.getMessage());
if ((propertiesFolder.equals("db-properties/") || propertiesFolder.equals("db-properties"))) {
try {
JarResourceUtils.copyFolderFromJar(
"db-properties", getPluginFolder(),
JarResourceUtils.CopyOption.COPY_IF_NOT_EXIST
);
} catch (Exception ex) {
getLogger().severe("初始化properties示例文件失败:" + ex.getMessage());
}
} else {
file.mkdirs();
}
}
@@ -105,7 +115,7 @@ public class MineSQLCore implements IMineSQL {
@SuppressWarnings("deprecation")
protected void initializeCommands(CommandManager<?, ?, ?, ?, ?, ?> commandManager) {
commandManager.enableUnstableAPI("help");
commandManager.setHelpFormatter(new EasySQLHelpFormatter(commandManager));
commandManager.setHelpFormatter(new MineSQLHelpFormatter(commandManager));
commandManager.getLocales().setDefaultLocale(Locales.SIMPLIFIED_CHINESE);
commandManager.getCommandContexts().registerContext(SQLManager.class, c -> {
String name = c.popFirstArg();
@@ -119,7 +129,7 @@ public class MineSQLCore implements IMineSQL {
if (c.getIssuer().isPlayer()) return ImmutableList.of();
else return ImmutableList.copyOf(getRegistry().list().keySet());
});
commandManager.registerCommand(new EasySQLCommand(this));
commandManager.registerCommand(new MineSQLCommand(this));
}
public void checkUpdate(String currentVersion) {
@@ -45,21 +45,25 @@ public class MineSQLRegistry implements SQLRegistry {
dbProperties.forEach((id, properties) -> {
try {
core.getLogger().info("正在初始化数据库 #" + id + " ...");
SQLManagerImpl sqlManager = create(id, properties);
this.managers.put(id, sqlManager);
} catch (Exception exception) {
core.getLogger().warning("初始化SQLManager(#" + id + ") 出错,请检查配置文件.");
exception.printStackTrace();
core.getLogger().info("完成成初始化数据库 #" + id + "");
} catch (Exception ex) {
core.getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage());
ex.printStackTrace();
}
});
dbConfigurations.forEach((id, configuration) -> {
try {
core.getLogger().info("正在初始化数据库 #" + id + " ...");
SQLManagerImpl sqlManager = create(id, configuration);
this.managers.put(id, sqlManager);
} catch (Exception exception) {
core.getLogger().warning("初始化SQLManager(#" + id + ") 出错,请检查配置文件.");
exception.printStackTrace();
core.getLogger().info("完成初始化数据库 #" + id + "");
} catch (Exception ex) {
core.getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage());
ex.printStackTrace();
}
});
@@ -69,7 +73,8 @@ public class MineSQLRegistry implements SQLRegistry {
this.managers.forEach((k, manager) -> {
getCore().getLogger().info(" 正在关闭数据库 " + k + "...");
shutdown(manager, activeQueries -> {
getCore().getLogger().info(" 数据库 " + k + " 仍有有 " + activeQueries + " 条活动查询");
if (activeQueries.isEmpty()) return;
getCore().getLogger().info(" 数据库 " + k + " 仍有 " + activeQueries.size() + " 条活动查询");
if (manager.getDataSource() instanceof BeeDataSource
&& this.core.getConfig().SETTINGS.FORCE_CLOSE.getNotNull()) {
getCore().getLogger().info(" 将强制关闭全部活跃链接...");
@@ -15,13 +15,13 @@ import java.util.UUID;
@SuppressWarnings("unused")
@CommandAlias("EasySQL")
@CommandAlias("MineSQL")
@Description("MineSQL的主指令,用于开发者进行调试,只允许后台执行。")
public class EasySQLCommand extends BaseCommand {
public class MineSQLCommand extends BaseCommand {
protected final MineSQLCore core;
public EasySQLCommand(MineSQLCore core) {
public MineSQLCommand(MineSQLCore core) {
this.core = core;
}
@@ -2,9 +2,9 @@ package cc.carm.plugin.minesql.command;
import co.aikar.commands.*;
public class EasySQLHelpFormatter extends CommandHelpFormatter {
public class MineSQLHelpFormatter extends CommandHelpFormatter {
public EasySQLHelpFormatter(CommandManager manager) {
public MineSQLHelpFormatter(CommandManager manager) {
super(manager);
}
@@ -42,6 +42,7 @@ public class PluginConfiguration extends ConfigurationRoot {
.asValue(SQLSourceGroup.class).fromSection()
.parseValue((w, d) -> SQLSourceGroup.parse(w))
.serializeValue(SQLSourceGroup::serialize)
.defaults(SQLSourceGroup.defaults())
.build();
public static class PropertiesConfig extends ConfigurationRoot {
@@ -32,6 +32,18 @@ public class SQLSourceGroup {
return data;
}
public static @NotNull SQLSourceGroup defaults() {
LinkedHashMap<String, SQLDriverConfig> configs = new LinkedHashMap<>();
configs.put("example-mysql", new RemoteAuthConfig(
SQLDriverType.MARIADB, "127.0.0.1", 3306,
"minecraft", "minecraft", "minecraft",
"?sslMode=false"
));
configs.put("example-h2-file", new FileBasedConfig(SQLDriverType.H2_FILE, "test"));
configs.put("example-h2-mem", new H2MemConfig("temp"));
return new SQLSourceGroup(configs);
}
public static @NotNull SQLSourceGroup parse(ConfigurationWrapper<?> rootSection) {
LinkedHashMap<String, SQLDriverConfig> configs = new LinkedHashMap<>();
for (String name : rootSection.getKeys(false)) {