refactor: 将 lettuce 转至 HamsterCore 里

This commit is contained in:
2024-01-13 18:49:22 +08:00
parent 0566941e85
commit b7b7f89267
13 changed files with 47 additions and 93 deletions

View File

@@ -131,7 +131,7 @@ dependencies {
### 示例 ### 示例
在下面这个例子中我将实现一个业务功能:**跨服聊天** 在下面这个例子中我将实现一个功能:**跨服聊天**
插件名称为:`GlobalChat` 插件名称为:`GlobalChat`
<details> <details>

View File

@@ -1,15 +1,15 @@
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
evaluationDependsOn(":ball-common") evaluationDependsOn(":ball-common")
dependencies { dependencies {
implementation(project(":ball-common")) { isTransitive = false } implementation(project(":ball-common")) {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT") isTransitive = false
}
compileOnly("org.spigotmc:spigot-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("cn.hamster3.mc.plugin:core-bukkit:+") compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.2.0")
compileOnly("me.clip:placeholderapi:+") { isTransitive = false } compileOnly("me.clip:placeholderapi:2.11.5") {
isTransitive = false
implementation("io.lettuce:lettuce-core:+") }
} }
tasks { tasks {

View File

@@ -7,7 +7,6 @@ import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo; import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType; import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import io.lettuce.core.RedisClient;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -58,7 +57,6 @@ public class BallBukkitAPI extends BallAPI {
BallConfig ballConfig = new BallConfig( BallConfig ballConfig = new BallConfig(
config.getBoolean("debug", false), config.getBoolean("debug", false),
RedisClient.create(config.getString("redis-url")),
config.getString("channel-prefix", "") + ":", config.getString("channel-prefix", "") + ":",
config.getBoolean("game-server-update-player-info", false), config.getBoolean("game-server-update-player-info", false),
serverInfo, serverInfo,

View File

@@ -1,23 +1,19 @@
# 是否允许在控制台输出调试信息 # 是否允许在控制台输出调试信息
debug: false debug: false
# redis 连接配置,连接格式如下:
# redis://用户名:密码@主机名:端口/数据库索引?参数名=参数值&参数名=参数值
# 若没有设置 redis 用户名,则可以省略,以下方法三选一:
# 使用默认用户名: redis://default:密码@localhost:6379?clientName=HamsterBall
# 不写用户名: redis://:密码@localhost:6379?clientName=HamsterBall
# 只写密码: redis://密码@localhost:6379?clientName=HamsterBall
# 若没有设置 redis 用户名和密码,则可以省略:
# redis://localhost:6379?clientName=HamsterBall
# 若不设置数据库,则默认使用 0
# 详细信息https://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-details
redis-url: "redis://localhost:6379?clientName=HamsterBall"
# 频道名前缀 # 频道名前缀
# 使用这个配置选项可以划分子服消息通信分组 # 使用这个配置选项可以划分子服消息通信分组
# 只有在同一个频道名的子服才能互相通信 # 只有在同一个频道名的子服才能互相通信
channel-prefix: "" channel-prefix: ""
# 是否在子服端更新玩家信息
# 默认情况下BC 统一管理玩家信息,包括记录 UUID 和玩家名称
# 如果一个群组服同时拥有多个 BC 入口
# 且每个 BC 入口为不同的玩家名称分配不同的 UUID
# (例如正版、盗版双入口,或网易多入口接同一个子服)
# 则可以启用该功能以防止 UUID 紊乱的问题
game-server-update-player-info: false
# 本服务器信息 # 本服务器信息
server-info: server-info:
# 服务器唯一识别码,最长 32 字符 # 服务器唯一识别码,最长 32 字符
@@ -34,14 +30,6 @@ server-info:
# 不填则自动获取 server.properties 文件中的设置 # 不填则自动获取 server.properties 文件中的设置
# port: 25577 # port: 25577
# 是否在子服端更新玩家信息
# 默认情况下BC 统一管理玩家信息,包括记录 UUID 和玩家名称
# 如果一个群组服同时拥有多个 BC 入口
# 且每个 BC 入口为不同的玩家名称分配不同的 UUID
# (例如正版、盗版双入口,或网易多入口接同一个子服)
# 则可以启用该功能以防止 UUID 紊乱的问题
game-server-update-player-info: false
# 数据库连接池配置 # 数据库连接池配置
# 如果注释该选项则默认使用 HamsterCore 中的连接池配置 # 如果注释该选项则默认使用 HamsterCore 中的连接池配置
# 否则 HamsterBall 将会使用与 HamsterCore 不同的数据库链接 # 否则 HamsterBall 将会使用与 HamsterCore 不同的数据库链接

View File

@@ -1,14 +1,14 @@
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate") @file:Suppress("VulnerableLibrariesLocal")
evaluationDependsOn(":ball-common") evaluationDependsOn(":ball-common")
dependencies { dependencies {
implementation(project(":ball-common")) { isTransitive = false } implementation(project(":ball-common")) {
compileOnly("net.md-5:bungeecord-api:+") isTransitive = false
}
compileOnly("net.md-5:bungeecord-api:1.20-R0.1")
compileOnly("cn.hamster3.mc.plugin:core-bungee:+") compileOnly("cn.hamster3.mc.plugin:core-bungee:1.2.0")
implementation("io.lettuce:lettuce-core:+")
} }
tasks { tasks {

View File

@@ -1,14 +1,13 @@
package cn.hamster3.mc.plugin.ball.bungee.api; package cn.hamster3.mc.plugin.ball.bungee.api;
import cn.hamster3.mc.plugin.ball.bungee.HamsterBallPlugin;
import cn.hamster3.mc.plugin.ball.bungee.util.BallBungeeCordUtils; import cn.hamster3.mc.plugin.ball.bungee.util.BallBungeeCordUtils;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI; import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.config.BallConfig; import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo; import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType; import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.ball.bungee.HamsterBallPlugin;
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils; import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import io.lettuce.core.RedisClient;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -55,7 +54,6 @@ public class BallBungeeCordAPI extends BallAPI {
BallConfig ballConfig = new BallConfig( BallConfig ballConfig = new BallConfig(
config.getBoolean("debug", false), config.getBoolean("debug", false),
RedisClient.create(config.getString("redis-url")),
config.getString("channel-prefix", "") + ":", config.getString("channel-prefix", "") + ":",
config.getBoolean("game-server-update-player-info", false), config.getBoolean("game-server-update-player-info", false),
serverInfo, serverInfo,

View File

@@ -1,18 +1,6 @@
# 是否允许在控制台输出调试信息 # 是否允许在控制台输出调试信息
debug: false debug: false
# redis 连接配置,连接格式如下:
# redis://用户名:密码@主机名:端口/数据库索引?参数名=参数值&参数名=参数值
# 若没有设置 redis 用户名,则可以省略,以下方法三选一:
# 使用默认用户名: redis://default:密码@localhost:6379?clientName=HamsterBall
# 不写用户名: redis://:密码@localhost:6379?clientName=HamsterBall
# 只写密码: redis://密码@localhost:6379?clientName=HamsterBall
# 若没有设置 redis 用户名和密码,则可以省略:
# redis://localhost:6379?clientName=HamsterBall
# 若不设置数据库,则默认使用 0
# 详细信息https://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-details
redis-url: "redis://localhost:6379?clientName=HamsterBall"
# 频道名前缀 # 频道名前缀
# 使用这个配置选项可以划分子服消息通信分组 # 使用这个配置选项可以划分子服消息通信分组
# 只有在同一个频道名的子服才能互相通信 # 只有在同一个频道名的子服才能互相通信

View File

@@ -1,14 +1,10 @@
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate") @file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
dependencies { dependencies {
compileOnly("cn.hamster3.mc.plugin:core-common:+") compileOnly("cn.hamster3.mc.plugin:core-common:1.2.0")
compileOnly("com.google.code.gson:gson:2.8.0") compileOnly("com.google.code.gson:gson:2.8.0")
compileOnly("com.google.guava:guava:31.0-jre") compileOnly("com.google.guava:guava:31.0-jre")
implementation("io.lettuce:lettuce-core:+") {
exclude(group = "io.netty")
}
} }
tasks { tasks {

View File

@@ -15,11 +15,10 @@ import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
import cn.hamster3.mc.plugin.ball.common.listener.BallRedisListener; 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.api.CoreAPI;
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import cn.hamster3.mc.plugin.core.lib.io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component; import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus; import com.google.common.eventbus.EventBus;
import io.lettuce.core.RedisClient;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -55,14 +54,14 @@ public abstract class BallAPI {
@NotNull @NotNull
private final Map<UUID, BallPlayerInfo> allPlayerInfo; private final Map<UUID, BallPlayerInfo> allPlayerInfo;
@NotNull @NotNull
private final StatefulRedisPubSubConnection<String, BallMessage> subConnection; private final StatefulRedisPubSubConnection<String, BallMessage> redisPub;
@NotNull @NotNull
private final StatefulRedisPubSubConnection<String, BallMessage> pubConnection; private final StatefulRedisPubSubConnection<String, BallMessage> redisSub;
public BallAPI(@NotNull BallConfig ballConfig) { public BallAPI(@NotNull BallConfig ballConfig) {
this.ballConfig = ballConfig; this.ballConfig = ballConfig;
subConnection = ballConfig.getRedisClient().connectPubSub(BallMessage.CODEC); redisPub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.CODEC);
pubConnection = ballConfig.getRedisClient().connectPubSub(BallMessage.CODEC); redisSub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.CODEC);
allServerInfo = new ConcurrentHashMap<>(); allServerInfo = new ConcurrentHashMap<>();
allPlayerInfo = new ConcurrentHashMap<>(); allPlayerInfo = new ConcurrentHashMap<>();
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService()); eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
@@ -139,8 +138,8 @@ public abstract class BallAPI {
} }
} }
} }
subConnection.addListener(BallRedisListener.INSTANCE); redisPub.addListener(BallRedisListener.INSTANCE);
subConnection.sync().subscribe(BALL_CHANNEL); redisPub.sync().subscribe(BALL_CHANNEL);
} }
protected void disable() throws SQLException, InterruptedException { protected void disable() throws SQLException, InterruptedException {
@@ -163,9 +162,6 @@ public abstract class BallAPI {
statement.executeUpdate(); statement.executeUpdate();
} }
} }
getLogger().info("正在关闭 redission");
ballConfig.getRedisClient().close();
getLogger().info("已关闭 redission");
} }
/** /**
@@ -451,11 +447,11 @@ public abstract class BallAPI {
channel = ballConfig.getChannelPrefix() + channel; channel = ballConfig.getChannelPrefix() + channel;
} }
if (block) { if (block) {
pubConnection.sync().publish(channel, message); redisSub.sync().publish(channel, message);
eventBus.post(new MessageSentEvent(channel, message)); eventBus.post(new MessageSentEvent(channel, message));
} else { } else {
@NotNull String finalChannel = channel; @NotNull String finalChannel = channel;
pubConnection.async().publish(channel, message).whenComplete((aLong, throwable) -> { redisSub.async().publish(channel, message).whenComplete((aLong, throwable) -> {
if (throwable != null) { if (throwable != null) {
return; return;
} }
@@ -468,22 +464,22 @@ public abstract class BallAPI {
for (int i = 0; i < channel.length; i++) { for (int i = 0; i < channel.length; i++) {
channel[i] = ballConfig.getChannelPrefix() + channel[i]; channel[i] = ballConfig.getChannelPrefix() + channel[i];
} }
subConnection.sync().subscribe(channel); redisPub.sync().subscribe(channel);
} }
public void subscribePatterns(@NotNull String patterns) { public void subscribePatterns(@NotNull String patterns) {
subConnection.sync().psubscribe(patterns); redisPub.sync().psubscribe(patterns);
} }
public void unsubscribe(@NotNull String... channel) { public void unsubscribe(@NotNull String... channel) {
for (int i = 0; i < channel.length; i++) { for (int i = 0; i < channel.length; i++) {
channel[i] = ballConfig.getChannelPrefix() + channel[i]; channel[i] = ballConfig.getChannelPrefix() + channel[i];
} }
subConnection.sync().unsubscribe(channel); redisPub.sync().unsubscribe(channel);
} }
public void unsubscribePatterns(@NotNull String patterns) { public void unsubscribePatterns(@NotNull String patterns) {
subConnection.sync().punsubscribe(patterns); redisPub.sync().punsubscribe(patterns);
} }
/** /**
@@ -642,9 +638,4 @@ public abstract class BallAPI {
public DataSource getDatasource() { public DataSource getDatasource() {
return ballConfig.getDatasource() == null ? CoreAPI.getInstance().getDataSource() : ballConfig.getDatasource(); return ballConfig.getDatasource() == null ? CoreAPI.getInstance().getDataSource() : ballConfig.getDatasource();
} }
@NotNull
public RedisClient getRedisClient() {
return ballConfig.getRedisClient();
}
} }

View File

@@ -1,7 +1,6 @@
package cn.hamster3.mc.plugin.ball.common.config; package cn.hamster3.mc.plugin.ball.common.config;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo; import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import io.lettuce.core.RedisClient;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -14,8 +13,6 @@ import javax.sql.DataSource;
public class BallConfig { public class BallConfig {
private boolean debug; private boolean debug;
@NotNull @NotNull
private RedisClient redisClient;
@NotNull
private String channelPrefix; private String channelPrefix;
private boolean gameServerUpdatePlayerInfo; private boolean gameServerUpdatePlayerInfo;
@NotNull @NotNull

View File

@@ -3,11 +3,11 @@ package cn.hamster3.mc.plugin.ball.common.data;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI; import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType; import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import cn.hamster3.mc.plugin.core.lib.io.lettuce.core.codec.RedisCodec;
import cn.hamster3.mc.plugin.core.lib.io.lettuce.core.codec.StringCodec;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View File

@@ -9,7 +9,7 @@ import cn.hamster3.mc.plugin.ball.common.event.player.*;
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOfflineEvent; import cn.hamster3.mc.plugin.ball.common.event.server.ServerOfflineEvent;
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent; import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import io.lettuce.core.pubsub.RedisPubSubListener; import cn.hamster3.mc.plugin.core.lib.io.lettuce.core.pubsub.RedisPubSubListener;
public class BallRedisListener implements RedisPubSubListener<String, BallMessage> { public class BallRedisListener implements RedisPubSubListener<String, BallMessage> {
public static final BallRedisListener INSTANCE = new BallRedisListener(); public static final BallRedisListener INSTANCE = new BallRedisListener();

View File

@@ -5,7 +5,7 @@ plugins {
} }
group = "cn.hamster3.mc.plugin" group = "cn.hamster3.mc.plugin"
version = "1.4.5" version = "1.5.0"
subprojects { subprojects {
apply { apply {
@@ -24,9 +24,11 @@ subprojects {
} }
dependencies { dependencies {
compileOnly("org.jetbrains:annotations:+") // https://mvnrepository.com/artifact/org.jetbrains/annotations
compileOnly("org.projectlombok:lombok:+") compileOnly("org.jetbrains:annotations:24.1.0")
annotationProcessor("org.projectlombok:lombok:+") // https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly("org.projectlombok:lombok:1.18.30")
annotationProcessor("org.projectlombok:lombok:1.18.30")
} }
java { java {
@@ -49,10 +51,6 @@ subprojects {
shadowJar { shadowJar {
archiveClassifier = "" archiveClassifier = ""
destinationDirectory = rootProject.buildDir destinationDirectory = rootProject.buildDir
relocate("io.netty", "cn.hamster3.mc.plugin.ball.lib.io.netty")
relocate("io.lettuce", "cn.hamster3.mc.plugin.ball.lib.io.lettuce")
relocate("org.reactivestreams", "cn.hamster3.mc.plugin.ball.lib.org.reactivestreams")
relocate("reactor", "cn.hamster3.mc.plugin.ball.lib.reactor")
} }
build { build {
dependsOn(shadowJar) dependsOn(shadowJar)