perf: 简化代码

This commit is contained in:
2024-03-17 01:13:45 +08:00
parent 0d2f5f0468
commit ade9a1094a
12 changed files with 98 additions and 175 deletions

View File

@@ -1,7 +1,7 @@
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
dependencies {
compileOnly("cn.hamster3.mc.plugin:core-common:1.3.0")
compileOnly("cn.hamster3.mc.plugin:core-common:+")
compileOnly("com.google.code.gson:gson:2.8.0")
compileOnly("com.google.guava:guava:31.0-jre")

View File

@@ -14,7 +14,9 @@ import cn.hamster3.mc.plugin.ball.common.listener.BallCommonListener;
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
import cn.hamster3.mc.plugin.ball.common.listener.BallRedisListener;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
import cn.hamster3.mc.plugin.core.lib.redis.clients.jedis.Jedis;
import com.google.common.eventbus.AsyncEventBus;
@@ -40,26 +42,62 @@ public abstract class BallAPI {
* API 使用的玩家信息更新通信频道
*/
public static final String PLAYER_INFO_CHANNEL = "HamsterBall:PlayerInfo";
/**
* API 实例
*/
@Getter
protected static BallAPI instance;
@NotNull
private final BallConfig ballConfig;
@NotNull
private final DataSource datasource;
@NotNull
private final BallServerInfo serverInfo;
@NotNull
private final EventBus eventBus;
@NotNull
private final Map<String, BallServerInfo> allServerInfo;
@NotNull
private final Map<UUID, BallPlayerInfo> allPlayerInfo;
@NotNull
private final Jedis redisSub;
@NotNull
private final Jedis redisPub;
public BallAPI(@NotNull BallConfig ballConfig) {
this.ballConfig = ballConfig;
public BallAPI(@NotNull ConfigSection config) {
Map<String, String> env = System.getenv();
ConfigSection serverInfoConfig = config.getSection("server-info");
if (serverInfoConfig == null) {
throw new IllegalArgumentException("配置文件中未找到 server-info 节点");
}
serverInfo = new BallServerInfo(
env.getOrDefault("BALL_LOCAL_SERVER_INFO_ID", serverInfoConfig.getString("id")),
env.getOrDefault("BALL_LOCAL_SERVER_INFO_NAME", serverInfoConfig.getString("name")),
BallServerType.GAME,
env.getOrDefault("BALL_LOCAL_SERVER_IP", serverInfoConfig.getString("host")),
Integer.parseInt(
env.getOrDefault("BALL_LOCAL_SERVER_PORT", String.valueOf(serverInfoConfig.getInt("port")))
)
);
ConfigSection section = config.getSection("datasource");
if (section != null) {
getLogger().info("启用仓鼠球自定义数据库连接池");
datasource = CoreUtils.getDataSource(section);
} else {
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<>();
@@ -526,12 +564,12 @@ public abstract class BallAPI {
*/
@NotNull
public BallServerInfo getLocalServerInfo() {
return ballConfig.getServerInfo();
return serverInfo;
}
@NotNull
public String getLocalServerId() {
return ballConfig.getServerInfo().getId();
return serverInfo.getId();
}
/**
@@ -651,6 +689,6 @@ public abstract class BallAPI {
@NotNull
public DataSource getDatasource() {
return ballConfig.getDatasource() == null ? CoreAPI.getInstance().getDataSource() : ballConfig.getDatasource();
return datasource;
}
}

View File

@@ -1,12 +1,9 @@
package cn.hamster3.mc.plugin.ball.common.config;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.sql.DataSource;
import java.util.List;
@Getter
@@ -17,8 +14,4 @@ public class BallConfig {
private String channelPrefix;
private boolean gameServerUpdatePlayerInfo;
private List<String> loadPlayerInfoFilter;
@NotNull
private BallServerInfo serverInfo;
@Nullable
private DataSource datasource;
}