perf: 简化代码
This commit is contained in:
@@ -54,7 +54,7 @@ public abstract class BallAPI {
|
||||
@NotNull
|
||||
private final DataSource datasource;
|
||||
@NotNull
|
||||
private final BallServerInfo serverInfo;
|
||||
private final BallServerInfo localServerInfo;
|
||||
|
||||
@NotNull
|
||||
private final EventBus eventBus;
|
||||
@@ -74,7 +74,7 @@ public abstract class BallAPI {
|
||||
if (serverInfoConfig == null) {
|
||||
throw new IllegalArgumentException("配置文件中未找到 server-info 节点");
|
||||
}
|
||||
serverInfo = new BallServerInfo(serverInfoConfig, type);
|
||||
localServerInfo = new BallServerInfo(serverInfoConfig, type);
|
||||
ConfigSection section = config.getSection("datasource");
|
||||
if (section != null) {
|
||||
getLogger().info("启用仓鼠球自定义数据库连接池");
|
||||
@@ -83,18 +83,13 @@ public abstract class BallAPI {
|
||||
getLogger().info("复用 HamsterCore 的数据库连接池");
|
||||
datasource = CoreAPI.getInstance().getDataSource();
|
||||
}
|
||||
ballConfig = new BallConfig(
|
||||
config.getBoolean("debug", false),
|
||||
config.getString("channel-prefix", "") + ":",
|
||||
config.getBoolean("game-server-update-player-info", false),
|
||||
config.getStringList("load-player-info-filter")
|
||||
);
|
||||
redisSub = CoreAPI.getInstance().getJedisPool().getResource();
|
||||
redisPub = CoreAPI.getInstance().getJedisPool().getResource();
|
||||
allServerInfo = new ConcurrentHashMap<>();
|
||||
allPlayerInfo = new ConcurrentHashMap<>();
|
||||
ballConfig = new BallConfig(config);
|
||||
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
|
||||
eventBus.register(BallCommonListener.INSTANCE);
|
||||
allServerInfo = new ConcurrentHashMap<>();
|
||||
allPlayerInfo = new ConcurrentHashMap<>();
|
||||
redisSub = CoreAPI.getInstance().getJedisPool().getResource();
|
||||
redisPub = CoreAPI.getInstance().getJedisPool().getResource();
|
||||
getLogger().info("频道前缀: " + ballConfig.getChannelPrefix());
|
||||
getLogger().info("启用子服更新玩家状态: " + ballConfig.isGameServerUpdatePlayerInfo());
|
||||
if (ballConfig.isGameServerUpdatePlayerInfo()) {
|
||||
@@ -107,8 +102,6 @@ public abstract class BallAPI {
|
||||
}
|
||||
|
||||
protected void enable() throws SQLException, InterruptedException {
|
||||
BallServerInfo localInfo = getLocalServerInfo();
|
||||
|
||||
try (Connection connection = getDatasource().getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_player_info`(" +
|
||||
@@ -133,11 +126,11 @@ public abstract class BallAPI {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"REPLACE INTO `hamster_ball_server_info` VALUES(?, ?, ?, ?, ?);"
|
||||
)) {
|
||||
statement.setString(1, localInfo.getId());
|
||||
statement.setString(2, localInfo.getName());
|
||||
statement.setString(3, localInfo.getType().name());
|
||||
statement.setString(4, localInfo.getHost());
|
||||
statement.setInt(5, localInfo.getPort());
|
||||
statement.setString(1, localServerInfo.getId());
|
||||
statement.setString(2, localServerInfo.getName());
|
||||
statement.setString(3, localServerInfo.getType().name());
|
||||
statement.setString(4, localServerInfo.getHost());
|
||||
statement.setInt(5, localServerInfo.getPort());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
@@ -549,18 +542,40 @@ public abstract class BallAPI {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本地服务器ID
|
||||
* 取消订阅 redis 消息频道
|
||||
* <p>
|
||||
* 会自动加上 config 中设置的频道前缀
|
||||
*
|
||||
* @return 服务器ID
|
||||
* @param channel 频道名称
|
||||
*/
|
||||
@NotNull
|
||||
public BallServerInfo getLocalServerInfo() {
|
||||
return serverInfo;
|
||||
public void unsubscribe(@NotNull String... channel) {
|
||||
for (int i = 0; i < channel.length; i++) {
|
||||
channel[i] = ballConfig.getChannelPrefix() + channel[i];
|
||||
}
|
||||
BallRedisListener.INSTANCE.unsubscribe(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略频道前缀配置,取消订阅 redis 消息频道
|
||||
*
|
||||
* @param channel 频道名称
|
||||
*/
|
||||
public void unsubscribeIgnorePrefix(@NotNull String... channel) {
|
||||
BallRedisListener.INSTANCE.unsubscribe(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订阅 redis 消息频道(正则)
|
||||
*
|
||||
* @param patterns 频道名称正则表达式
|
||||
*/
|
||||
public void unsubscribePatterns(@NotNull String patterns) {
|
||||
BallRedisListener.INSTANCE.punsubscribe(patterns);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getLocalServerId() {
|
||||
return serverInfo.getId();
|
||||
return localServerInfo.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.config;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -14,4 +15,12 @@ public class BallConfig {
|
||||
private String channelPrefix;
|
||||
private boolean gameServerUpdatePlayerInfo;
|
||||
private List<String> loadPlayerInfoFilter;
|
||||
|
||||
public BallConfig(@NotNull ConfigSection config) {
|
||||
debug = config.getBoolean("debug", false);
|
||||
channelPrefix = config.getString("channel-prefix", "");
|
||||
channelPrefix = channelPrefix.isEmpty() ? channelPrefix : channelPrefix + ":";
|
||||
gameServerUpdatePlayerInfo = config.getBoolean("game-server-update-player-info", false);
|
||||
loadPlayerInfoFilter = config.getStringList("load-player-info-filter");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user