feat: 适配仓鼠球更新 jedis 版本

This commit is contained in:
2024-03-13 17:37:41 +08:00
parent d3f2a516c8
commit 98fd32f1ed
8 changed files with 63 additions and 126 deletions

View File

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

View File

@@ -15,13 +15,13 @@ 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 lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import redis.clients.jedis.Jedis;
import javax.sql.DataSource;
import java.sql.*;
@@ -54,14 +54,14 @@ public abstract class BallAPI {
@NotNull
private final Map<UUID, BallPlayerInfo> allPlayerInfo;
@NotNull
private final StatefulRedisPubSubConnection<String, BallMessage> redisPub;
private final Jedis redisSub;
@NotNull
private final StatefulRedisPubSubConnection<String, BallMessage> redisSub;
private final Jedis redisPub;
public BallAPI(@NotNull BallConfig ballConfig) {
this.ballConfig = ballConfig;
redisPub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.REDIS_CODEC);
redisSub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.REDIS_CODEC);
redisSub = CoreAPI.getInstance().getJedisPool().getResource();
redisPub = CoreAPI.getInstance().getJedisPool().getResource();
allServerInfo = new ConcurrentHashMap<>();
allPlayerInfo = new ConcurrentHashMap<>();
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
@@ -164,7 +164,6 @@ public abstract class BallAPI {
}
getLogger().info("从数据库中加载了 " + allServerInfo.size() + " 条服务器信息");
getLogger().info("从数据库中加载了 " + allPlayerInfo.size() + " 条玩家信息");
redisPub.addListener(BallRedisListener.INSTANCE);
subscribeIgnorePrefix(BALL_CHANNEL);
}
@@ -470,15 +469,12 @@ public abstract class BallAPI {
channel = ballConfig.getChannelPrefix() + channel;
}
if (block) {
redisSub.sync().publish(channel, message);
redisPub.publish(channel, CoreAPI.getInstance().getGson().toJson(message));
eventBus.post(new MessageSentEvent(channel, message));
} else {
@NotNull String finalChannel = channel;
redisSub.async().publish(channel, message).whenComplete((aLong, throwable) -> {
if (throwable != null) {
return;
}
eventBus.post(new MessageSentEvent(finalChannel, message));
CoreAPI.getInstance().getExecutorService().submit(() -> {
redisPub.publish(finalChannel, CoreAPI.getInstance().getGson().toJson(message));
});
}
}
@@ -494,7 +490,7 @@ public abstract class BallAPI {
for (int i = 0; i < channel.length; i++) {
channel[i] = ballConfig.getChannelPrefix() + channel[i];
}
redisPub.sync().subscribe(channel);
redisSub.subscribe(BallRedisListener.INSTANCE, channel);
}
/**
@@ -503,7 +499,7 @@ public abstract class BallAPI {
* @param channel 频道名称
*/
public void subscribeIgnorePrefix(@NotNull String... channel) {
redisPub.sync().subscribe(channel);
redisSub.subscribe(BallRedisListener.INSTANCE, channel);
}
/**
@@ -512,38 +508,39 @@ public abstract class BallAPI {
* @param patterns 频道名称正则表达式
*/
public void subscribePatterns(@NotNull String patterns) {
redisPub.sync().psubscribe(patterns);
redisSub.psubscribe(BallRedisListener.INSTANCE, patterns);
}
/**
* 取消订阅 redis 频道
*
* @param channel 频道名称
*/
public void unsubscribe(@NotNull String... channel) {
for (int i = 0; i < channel.length; i++) {
channel[i] = ballConfig.getChannelPrefix() + channel[i];
}
redisPub.sync().unsubscribe(channel);
}
/**
* 忽略仓鼠球频道前缀配置,取消订阅 redis 频道
*
* @param channel 频道名称
*/
public void unsubscribeIgnorePrefix(@NotNull String... channel) {
redisPub.sync().unsubscribe(channel);
}
/**
* 取消订阅 redis 消息频道(正则)
*
* @param patterns 频道名称正则表达式
*/
public void unsubscribePatterns(@NotNull String patterns) {
redisPub.sync().punsubscribe(patterns);
}
// /**
// * 取消订阅 redis 频道
// *
// * @param channel 频道名称
// */
// public void unsubscribe(@NotNull String... channel) {
// for (int i = 0; i < channel.length; i++) {
// channel[i] = ballConfig.getChannelPrefix() + channel[i];
// }
// //todo
//// redisSub.
// }
//
// /**
// * 忽略仓鼠球频道前缀配置,取消订阅 redis 频道
// *
// * @param channel 频道名称
// */
// public void unsubscribeIgnorePrefix(@NotNull String... channel) {
// //todo
// }
//
// /**
// * 取消订阅 redis 消息频道(正则)
// *
// * @param patterns 频道名称正则表达式
// */
// public void unsubscribePatterns(@NotNull String patterns) {
// //todo
// }
/**
* 获取本地服务器ID

View File

@@ -3,11 +3,10 @@ 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 lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@@ -16,7 +15,6 @@ import org.jetbrains.annotations.Nullable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.UUID;
/**
@@ -25,33 +23,9 @@ import java.util.UUID;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("unused")
public class BallMessage {
/**
* lettuce 编解码器
*/
public static final RedisCodec<String, BallMessage> REDIS_CODEC = new RedisCodec<String, BallMessage>() {
@Override
public String decodeKey(ByteBuffer bytes) {
return StringCodec.UTF8.decodeKey(bytes);
}
@Override
public BallMessage decodeValue(ByteBuffer bytes) {
String string = StringCodec.UTF8.decodeValue(bytes);
return CoreAPI.getInstance().getGson().fromJson(string, BallMessage.class);
}
@Override
public ByteBuffer encodeKey(String key) {
return StringCodec.UTF8.encodeKey(key);
}
@Override
public ByteBuffer encodeValue(BallMessage value) {
return StringCodec.UTF8.encodeValue(CoreAPI.getInstance().getGson().toJson(value));
}
};
/**
* 消息发送者
*/
@@ -100,34 +74,6 @@ public class BallMessage {
this.content = CoreAPI.getInstance().getGson().toJsonTree(content);
}
public BallMessage(@NotNull String senderID, @Nullable String receiverID, @Nullable BallServerType receiverType, @NotNull String action, @Nullable JsonElement content) {
this.senderID = senderID;
this.receiverID = receiverID;
this.receiverType = receiverType;
this.action = action;
this.content = content;
}
/**
* 序列化至 Json
*
* @return json对象
*/
@NotNull
public JsonObject toJson() {
JsonObject object = new JsonObject();
object.addProperty("senderID", senderID);
if (receiverID != null) {
object.addProperty("toServer", receiverID);
}
if (receiverType != null) {
object.addProperty("toServer", receiverType.name());
}
object.addProperty("action", action);
object.add("content", content);
return object;
}
/**
* 以 Java 对象获取消息内容
*
@@ -199,6 +145,6 @@ public class BallMessage {
@Override
public String toString() {
return toJson().toString();
return CoreAPI.getInstance().getGson().toJson(this);
}
}

View File

@@ -1,7 +1,6 @@
package cn.hamster3.mc.plugin.ball.common.event.message;
import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
import com.google.gson.JsonObject;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
@@ -23,11 +22,4 @@ public class MessageEvent extends BallMessage {
setAction(message.getAction());
setContent(message.getContent());
}
@Override
public @NotNull JsonObject toJson() {
JsonObject object = super.toJson();
object.addProperty("channel", channel);
return object;
}
}

View File

@@ -3,20 +3,22 @@ package cn.hamster3.mc.plugin.ball.common.listener;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
import cn.hamster3.mc.plugin.ball.common.event.message.MessageReceivedEvent;
import cn.hamster3.mc.plugin.core.lib.io.lettuce.core.pubsub.RedisPubSubListener;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import com.google.common.eventbus.EventBus;
import redis.clients.jedis.JedisPubSub;
public class BallRedisListener implements RedisPubSubListener<String, BallMessage> {
public class BallRedisListener extends JedisPubSub {
public static final BallRedisListener INSTANCE = new BallRedisListener();
private BallRedisListener() {
}
@Override
public void message(String channel, BallMessage ballMessage) {
public void onMessage(String channel, String message) {
if (channel.startsWith(BallAPI.getInstance().getBallConfig().getChannelPrefix())) {
channel = channel.substring(BallAPI.getInstance().getBallConfig().getChannelPrefix().length());
}
BallMessage ballMessage = CoreAPI.getInstance().getGson().fromJson(message, BallMessage.class);
BallAPI ballAPI = BallAPI.getInstance();
EventBus eventBus = ballAPI.getEventBus();
if (ballMessage.getReceiverType() != null && ballMessage.getReceiverType() != ballAPI.getLocalServerInfo().getType()) {
@@ -29,27 +31,27 @@ public class BallRedisListener implements RedisPubSubListener<String, BallMessag
}
@Override
public void message(String pattern, String channel, BallMessage info) {
message(channel, info);
public void onPMessage(String pattern, String channel, String message) {
onMessage(channel, message);
}
@Override
public void subscribed(String channel, long count) {
public void onSubscribe(String channel, int subscribedChannels) {
BallAPI.getInstance().getLogger().info("已订阅 redis 频道 " + channel);
}
@Override
public void psubscribed(String pattern, long count) {
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则) " + pattern);
}
@Override
public void unsubscribed(String channel, long count) {
public void onUnsubscribe(String channel, int subscribedChannels) {
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道 " + channel);
}
@Override
public void punsubscribed(String pattern, long count) {
public void onPSubscribe(String pattern, int subscribedChannels) {
BallAPI.getInstance().getLogger().info("已订阅 redis 频道(正则) " + pattern);
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则) " + pattern);
}
}