feat: 允许单独配置 redis-url
All checks were successful
Publish Project / build (push) Successful in 4m23s

This commit is contained in:
2025-07-10 20:40:36 +08:00
parent 1e0417c814
commit de10bd7feb
5 changed files with 27 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
@@ -60,6 +61,8 @@ public abstract class BallAPI {
@NotNull
private final DataSource datasource;
@NotNull
private final RedisClient redisClient;
@NotNull
private final BallServerInfo localServerInfo;
@NotNull
@@ -81,6 +84,11 @@ public abstract class BallAPI {
throw new IllegalArgumentException("配置文件中未找到 server-info 节点");
}
localServerInfo = new BallServerInfo(serverInfoConfig, type);
if (config.hasKey("redis-url")) {
redisClient = RedisClient.create(config.getString("redis-url"));
} else {
redisClient = CoreAPI.getInstance().getRedisClient();
}
ConfigSection section = config.getSection("datasource");
if (section != null) {
getLogger().info("启用仓鼠球自定义数据库连接池");
@@ -94,7 +102,7 @@ public abstract class BallAPI {
eventBus.register(BallCommonListener.INSTANCE);
allServerInfo = new ConcurrentHashMap<>();
allPlayerInfo = new ConcurrentHashMap<>();
redisPubSub = CoreAPI.getInstance().getRedisClient().connectPubSub();
redisPubSub = getRedisClient().connectPubSub();
getLogger().info("频道前缀: " + ballConfig.getChannelPrefix());
getLogger().info("启用子服更新玩家状态: " + ballConfig.isGameServerUpdatePlayerInfo());
if (ballConfig.isGameServerUpdatePlayerInfo()) {
@@ -109,7 +117,7 @@ public abstract class BallAPI {
}
protected void enable() throws SQLException, InterruptedException {
try (StatefulRedisConnection<String, String> connect = CoreAPI.getInstance().getRedisClient().connect()) {
try (StatefulRedisConnection<String, String> connect = getRedisClient().connect()) {
RedisCommands<String, String> redis = connect.sync();
String key = "HamsterBall:ServerInfo:" + localServerInfo.getId();
if (redis.exists(key) > 0 && ballConfig.isSingletonServerID()) {
@@ -196,7 +204,7 @@ public abstract class BallAPI {
if (lockUpdater != null) {
lockUpdater.cancel(true);
lockUpdater = null;
try (StatefulRedisConnection<String, String> connect = CoreAPI.getInstance().getRedisClient().connect()) {
try (StatefulRedisConnection<String, String> connect = getRedisClient().connect()) {
RedisCommands<String, String> redis = connect.sync();
String key = "HamsterBall:ServerInfo:" + localServerInfo.getId();
redis.del(key);

View File

@@ -1,7 +1,6 @@
package cn.hamster3.mc.plugin.ball.common.thread;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
@@ -14,7 +13,7 @@ public class LockUpdateThread implements Runnable {
@Override
public void run() {
String key = "HamsterBall:ServerInfo:" + BallAPI.getInstance().getLocalServerInfo().getId();
try (StatefulRedisConnection<String, String> connect = CoreAPI.getInstance().getRedisClient().connect()) {
try (StatefulRedisConnection<String, String> connect = BallAPI.getInstance().getRedisClient().connect()) {
RedisCommands<String, String> redis = connect.sync();
redis.expire(key, 180);
}