feat: 允许数据库独立于core配置
This commit is contained in:
@@ -7,15 +7,20 @@ import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
|
||||
import cn.hamster3.mc.plugin.core.bungee.HamsterBallPlugin;
|
||||
import cn.hamster3.mc.plugin.core.bungee.listener.BallBungeeCordListener;
|
||||
import cn.hamster3.mc.plugin.core.bungee.util.BallBungeeCordUtils;
|
||||
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class BallBungeeCordAPI extends BallAPI {
|
||||
private DataSource datasource;
|
||||
|
||||
public BallBungeeCordAPI(@NotNull BallConfig config) {
|
||||
super(config);
|
||||
}
|
||||
@@ -56,12 +61,14 @@ public class BallBungeeCordAPI extends BallAPI {
|
||||
String.valueOf(pluginConfig.getInt("ball-server.event-loop-thread", 2))));
|
||||
BallConfig config = new BallConfig(serverInfo, serverHost, serverPort, eventLoopThread);
|
||||
|
||||
instance = new BallBungeeCordAPI(config);
|
||||
BallBungeeCordAPI apiInstance = new BallBungeeCordAPI(config);
|
||||
apiInstance.datasource = BallBungeeCordUtils.getDataSource(pluginConfig.getSection("datasource"));
|
||||
|
||||
instance.addListener(BallBungeeCordListener.INSTANCE);
|
||||
apiInstance.addListener(BallBungeeCordListener.INSTANCE);
|
||||
if (pluginConfig.getBoolean("debug", false)) {
|
||||
instance.addListener(BallDebugListener.INSTANCE);
|
||||
apiInstance.addListener(BallDebugListener.INSTANCE);
|
||||
}
|
||||
instance = apiInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,4 +85,9 @@ public class BallBungeeCordAPI extends BallAPI {
|
||||
public @NotNull Logger getLogger() {
|
||||
return HamsterBallPlugin.getInstance().getLogger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull DataSource getDatasource() {
|
||||
return datasource == null ? CoreAPI.getInstance().getDataSource() : datasource;
|
||||
}
|
||||
}
|
||||
|
@@ -3,12 +3,16 @@ package cn.hamster3.mc.plugin.core.bungee.util;
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerInfoUpdateEvent;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import cn.hamster3.mc.plugin.core.lib.com.zaxxer.hikari.HikariConfig;
|
||||
import cn.hamster3.mc.plugin.core.lib.com.zaxxer.hikari.HikariDataSource;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.connection.Server;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
@@ -31,15 +35,17 @@ public final class BallBungeeCordUtils {
|
||||
|
||||
public static void uploadPlayerInfo(@NotNull BallPlayerInfo playerInfo) {
|
||||
CoreUtils.WORKER_EXECUTOR.execute(() -> {
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
PreparedStatement statement = connection.prepareStatement("REPLACE INTO `hamster_ball_player_info` VALUES(?, ?, ?, ?, ?);");
|
||||
statement.setString(1, playerInfo.getUuid().toString());
|
||||
statement.setString(2, playerInfo.getName());
|
||||
statement.setString(3, playerInfo.getGameServer());
|
||||
statement.setString(4, playerInfo.getProxyServer());
|
||||
statement.setBoolean(5, playerInfo.isOnline());
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"REPLACE INTO `hamster_ball_player_info` VALUES(?, ?, ?, ?, ?);"
|
||||
)) {
|
||||
statement.setString(1, playerInfo.getUuid().toString());
|
||||
statement.setString(2, playerInfo.getName());
|
||||
statement.setString(3, playerInfo.getGameServer());
|
||||
statement.setString(4, playerInfo.getProxyServer());
|
||||
statement.setBoolean(5, playerInfo.isOnline());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -50,4 +56,32 @@ public final class BallBungeeCordUtils {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static DataSource getDataSource(@Nullable Configuration datasourceConfig) {
|
||||
if (datasourceConfig == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
HikariConfig hikariConfig = new HikariConfig();
|
||||
String driver = datasourceConfig.getString("driver");
|
||||
hikariConfig.setDriverClassName(driver);
|
||||
hikariConfig.setJdbcUrl(datasourceConfig.getString("url"));
|
||||
hikariConfig.setUsername(datasourceConfig.getString("username"));
|
||||
hikariConfig.setPassword(datasourceConfig.getString("password"));
|
||||
hikariConfig.setMaximumPoolSize(datasourceConfig.getInt("maximum-pool-size", 3));
|
||||
hikariConfig.setMinimumIdle(datasourceConfig.getInt("minimum-idle", 1));
|
||||
long keepAliveTime = datasourceConfig.getLong("keep-alive-time", 0);
|
||||
if (keepAliveTime > 5000) {
|
||||
hikariConfig.setKeepaliveTime(keepAliveTime);
|
||||
}
|
||||
hikariConfig.setIdleTimeout(datasourceConfig.getLong("idle-timeout", 10 * 60 * 1000));
|
||||
hikariConfig.setMaxLifetime(datasourceConfig.getLong("max-lifetime", 30 * 60 * 1000));
|
||||
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
||||
hikariConfig.setPoolName("HamsterBall-Pool");
|
||||
return new HikariDataSource(hikariConfig);
|
||||
} catch (Exception | Error e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -13,3 +13,38 @@ server-info:
|
||||
name: "代理端"
|
||||
host: 0.0.0.0
|
||||
port: 25577
|
||||
|
||||
# 数据库连接池配置
|
||||
# 如果注释该选项则默认使用 HamsterCore 中的连接池配置
|
||||
#datasource:
|
||||
# # 数据库链接驱动地址
|
||||
# driver: "com.mysql.jdbc.Driver"
|
||||
# # 数据库链接填写格式:
|
||||
# # jdbc:mysql://{数据库地址}:{数据库端口}/{使用的库名}?参数
|
||||
# # 除非你知道自己在做什么,否则不建议随意更改参数
|
||||
# url: "jdbc:mysql://sql.hamster3.cn:3306/Test1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true"
|
||||
# # 用户名
|
||||
# username: "Test"
|
||||
# # 密码
|
||||
# password: "Test123.."
|
||||
# # 最小闲置链接数
|
||||
# # 推荐值:1~3
|
||||
# minimum-idle: 0
|
||||
# # 最大链接数
|
||||
# # 推荐值:不低于3
|
||||
# maximum-pool-size: 3
|
||||
# # 保持连接池可用的间隔
|
||||
# # 除非你的服务器数据库连接经常断开,否则不建议启用该选项
|
||||
# # 单位:毫秒
|
||||
# # 默认值为0(禁用)
|
||||
# keep-alive-time: 0
|
||||
# # 连接闲置回收时间
|
||||
# # 单位:毫秒
|
||||
# # 推荐值:600000(10分钟)
|
||||
# idle-timeout: 600000
|
||||
# # 链接最长存活时间
|
||||
# # 单位:毫秒
|
||||
# max-lifetime: 1800000
|
||||
# # 验证连接存活的超时时间
|
||||
# # 单位:毫秒
|
||||
# validation-timeout: 5000
|
||||
|
Reference in New Issue
Block a user