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`
<details>

View File

@@ -1,15 +1,15 @@
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
evaluationDependsOn(":ball-common")
dependencies {
implementation(project(":ball-common")) { isTransitive = false }
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")
implementation(project(":ball-common")) {
isTransitive = false
}
compileOnly("org.spigotmc:spigot-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("cn.hamster3.mc.plugin:core-bukkit:+")
compileOnly("me.clip:placeholderapi:+") { isTransitive = false }
implementation("io.lettuce:lettuce-core:+")
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.2.0")
compileOnly("me.clip:placeholderapi:2.11.5") {
isTransitive = false
}
}
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.BallServerType;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import io.lettuce.core.RedisClient;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
@@ -58,7 +57,6 @@ public class BallBukkitAPI extends BallAPI {
BallConfig ballConfig = new BallConfig(
config.getBoolean("debug", false),
RedisClient.create(config.getString("redis-url")),
config.getString("channel-prefix", "") + ":",
config.getBoolean("game-server-update-player-info", false),
serverInfo,

View File

@@ -1,23 +1,19 @@
# 是否允许在控制台输出调试信息
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: ""
# 是否在子服端更新玩家信息
# 默认情况下BC 统一管理玩家信息,包括记录 UUID 和玩家名称
# 如果一个群组服同时拥有多个 BC 入口
# 且每个 BC 入口为不同的玩家名称分配不同的 UUID
# (例如正版、盗版双入口,或网易多入口接同一个子服)
# 则可以启用该功能以防止 UUID 紊乱的问题
game-server-update-player-info: false
# 本服务器信息
server-info:
# 服务器唯一识别码,最长 32 字符
@@ -34,14 +30,6 @@ server-info:
# 不填则自动获取 server.properties 文件中的设置
# port: 25577
# 是否在子服端更新玩家信息
# 默认情况下BC 统一管理玩家信息,包括记录 UUID 和玩家名称
# 如果一个群组服同时拥有多个 BC 入口
# 且每个 BC 入口为不同的玩家名称分配不同的 UUID
# (例如正版、盗版双入口,或网易多入口接同一个子服)
# 则可以启用该功能以防止 UUID 紊乱的问题
game-server-update-player-info: false
# 数据库连接池配置
# 如果注释该选项则默认使用 HamsterCore 中的连接池配置
# 否则 HamsterBall 将会使用与 HamsterCore 不同的数据库链接

View File

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

View File

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

View File

@@ -1,18 +1,6 @@
# 是否允许在控制台输出调试信息
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")
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.guava:guava:31.0-jre")
implementation("io.lettuce:lettuce-core:+") {
exclude(group = "io.netty")
}
}
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.core.common.api.CoreAPI;
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 com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import io.lettuce.core.RedisClient;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -55,14 +54,14 @@ public abstract class BallAPI {
@NotNull
private final Map<UUID, BallPlayerInfo> allPlayerInfo;
@NotNull
private final StatefulRedisPubSubConnection<String, BallMessage> subConnection;
private final StatefulRedisPubSubConnection<String, BallMessage> redisPub;
@NotNull
private final StatefulRedisPubSubConnection<String, BallMessage> pubConnection;
private final StatefulRedisPubSubConnection<String, BallMessage> redisSub;
public BallAPI(@NotNull BallConfig ballConfig) {
this.ballConfig = ballConfig;
subConnection = ballConfig.getRedisClient().connectPubSub(BallMessage.CODEC);
pubConnection = ballConfig.getRedisClient().connectPubSub(BallMessage.CODEC);
redisPub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.CODEC);
redisSub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.CODEC);
allServerInfo = new ConcurrentHashMap<>();
allPlayerInfo = new ConcurrentHashMap<>();
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
@@ -139,8 +138,8 @@ public abstract class BallAPI {
}
}
}
subConnection.addListener(BallRedisListener.INSTANCE);
subConnection.sync().subscribe(BALL_CHANNEL);
redisPub.addListener(BallRedisListener.INSTANCE);
redisPub.sync().subscribe(BALL_CHANNEL);
}
protected void disable() throws SQLException, InterruptedException {
@@ -163,9 +162,6 @@ public abstract class BallAPI {
statement.executeUpdate();
}
}
getLogger().info("正在关闭 redission");
ballConfig.getRedisClient().close();
getLogger().info("已关闭 redission");
}
/**
@@ -451,11 +447,11 @@ public abstract class BallAPI {
channel = ballConfig.getChannelPrefix() + channel;
}
if (block) {
pubConnection.sync().publish(channel, message);
redisSub.sync().publish(channel, message);
eventBus.post(new MessageSentEvent(channel, message));
} else {
@NotNull String finalChannel = channel;
pubConnection.async().publish(channel, message).whenComplete((aLong, throwable) -> {
redisSub.async().publish(channel, message).whenComplete((aLong, throwable) -> {
if (throwable != null) {
return;
}
@@ -468,22 +464,22 @@ public abstract class BallAPI {
for (int i = 0; i < channel.length; i++) {
channel[i] = ballConfig.getChannelPrefix() + channel[i];
}
subConnection.sync().subscribe(channel);
redisPub.sync().subscribe(channel);
}
public void subscribePatterns(@NotNull String patterns) {
subConnection.sync().psubscribe(patterns);
redisPub.sync().psubscribe(patterns);
}
public void unsubscribe(@NotNull String... channel) {
for (int i = 0; i < channel.length; i++) {
channel[i] = ballConfig.getChannelPrefix() + channel[i];
}
subConnection.sync().unsubscribe(channel);
redisPub.sync().unsubscribe(channel);
}
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() {
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;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import io.lettuce.core.RedisClient;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
@@ -14,8 +13,6 @@ import javax.sql.DataSource;
public class BallConfig {
private boolean debug;
@NotNull
private RedisClient redisClient;
@NotNull
private String channelPrefix;
private boolean gameServerUpdatePlayerInfo;
@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.entity.BallServerType;
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.JsonElement;
import com.google.gson.JsonObject;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
import lombok.Data;
import lombok.NoArgsConstructor;
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.ServerOnlineEvent;
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 static final BallRedisListener INSTANCE = new BallRedisListener();

View File

@@ -5,7 +5,7 @@ plugins {
}
group = "cn.hamster3.mc.plugin"
version = "1.4.5"
version = "1.5.0"
subprojects {
apply {
@@ -24,9 +24,11 @@ subprojects {
}
dependencies {
compileOnly("org.jetbrains:annotations:+")
compileOnly("org.projectlombok:lombok:+")
annotationProcessor("org.projectlombok:lombok:+")
// https://mvnrepository.com/artifact/org.jetbrains/annotations
compileOnly("org.jetbrains:annotations:24.1.0")
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly("org.projectlombok:lombok:1.18.30")
annotationProcessor("org.projectlombok:lombok:1.18.30")
}
java {
@@ -49,10 +51,6 @@ subprojects {
shadowJar {
archiveClassifier = ""
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 {
dependsOn(shadowJar)