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

@@ -8,7 +8,7 @@ dependencies {
} }
compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT") compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.2.2") compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.0")
compileOnly("me.clip:placeholderapi:2.11.5") { compileOnly("me.clip:placeholderapi:2.11.5") {
isTransitive = false isTransitive = false
} }

View File

@@ -8,7 +8,7 @@ dependencies {
} }
compileOnly("net.md-5:bungeecord-api:1.20-R0.1") compileOnly("net.md-5:bungeecord-api:1.20-R0.1")
compileOnly("cn.hamster3.mc.plugin:core-bungee:1.2.2") compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.0")
} }
tasks { tasks {

View File

@@ -1,7 +1,7 @@
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate") @file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
dependencies { 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.code.gson:gson:2.8.0")
compileOnly("com.google.guava:guava:31.0-jre") 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.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 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;
import redis.clients.jedis.Jedis;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.*; import java.sql.*;
@@ -54,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> redisPub; private final Jedis redisSub;
@NotNull @NotNull
private final StatefulRedisPubSubConnection<String, BallMessage> redisSub; private final Jedis redisPub;
public BallAPI(@NotNull BallConfig ballConfig) { public BallAPI(@NotNull BallConfig ballConfig) {
this.ballConfig = ballConfig; this.ballConfig = ballConfig;
redisPub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.REDIS_CODEC); redisSub = CoreAPI.getInstance().getJedisPool().getResource();
redisSub = CoreAPI.getInstance().getRedisClient().connectPubSub(BallMessage.REDIS_CODEC); redisPub = CoreAPI.getInstance().getJedisPool().getResource();
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());
@@ -164,7 +164,6 @@ public abstract class BallAPI {
} }
getLogger().info("从数据库中加载了 " + allServerInfo.size() + " 条服务器信息"); getLogger().info("从数据库中加载了 " + allServerInfo.size() + " 条服务器信息");
getLogger().info("从数据库中加载了 " + allPlayerInfo.size() + " 条玩家信息"); getLogger().info("从数据库中加载了 " + allPlayerInfo.size() + " 条玩家信息");
redisPub.addListener(BallRedisListener.INSTANCE);
subscribeIgnorePrefix(BALL_CHANNEL); subscribeIgnorePrefix(BALL_CHANNEL);
} }
@@ -470,15 +469,12 @@ public abstract class BallAPI {
channel = ballConfig.getChannelPrefix() + channel; channel = ballConfig.getChannelPrefix() + channel;
} }
if (block) { if (block) {
redisSub.sync().publish(channel, message); redisPub.publish(channel, CoreAPI.getInstance().getGson().toJson(message));
eventBus.post(new MessageSentEvent(channel, message)); eventBus.post(new MessageSentEvent(channel, message));
} else { } else {
@NotNull String finalChannel = channel; @NotNull String finalChannel = channel;
redisSub.async().publish(channel, message).whenComplete((aLong, throwable) -> { CoreAPI.getInstance().getExecutorService().submit(() -> {
if (throwable != null) { redisPub.publish(finalChannel, CoreAPI.getInstance().getGson().toJson(message));
return;
}
eventBus.post(new MessageSentEvent(finalChannel, message));
}); });
} }
} }
@@ -494,7 +490,7 @@ 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];
} }
redisPub.sync().subscribe(channel); redisSub.subscribe(BallRedisListener.INSTANCE, channel);
} }
/** /**
@@ -503,7 +499,7 @@ public abstract class BallAPI {
* @param channel 频道名称 * @param channel 频道名称
*/ */
public void subscribeIgnorePrefix(@NotNull String... 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 频道名称正则表达式 * @param patterns 频道名称正则表达式
*/ */
public void subscribePatterns(@NotNull String patterns) { public void subscribePatterns(@NotNull String patterns) {
redisPub.sync().psubscribe(patterns); redisSub.psubscribe(BallRedisListener.INSTANCE, patterns);
} }
/** // /**
* 取消订阅 redis 频道 // * 取消订阅 redis 频道
* // *
* @param channel 频道名称 // * @param channel 频道名称
*/ // */
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];
} // }
redisPub.sync().unsubscribe(channel); // //todo
} //// redisSub.
// }
/** //
* 忽略仓鼠球频道前缀配置,取消订阅 redis 频道 // /**
* // * 忽略仓鼠球频道前缀配置,取消订阅 redis 频道
* @param channel 频道名称 // *
*/ // * @param channel 频道名称
public void unsubscribeIgnorePrefix(@NotNull String... channel) { // */
redisPub.sync().unsubscribe(channel); // public void unsubscribeIgnorePrefix(@NotNull String... channel) {
} // //todo
// }
/** //
* 取消订阅 redis 消息频道(正则) // /**
* // * 取消订阅 redis 消息频道(正则)
* @param patterns 频道名称正则表达式 // *
*/ // * @param patterns 频道名称正则表达式
public void unsubscribePatterns(@NotNull String patterns) { // */
redisPub.sync().punsubscribe(patterns); // public void unsubscribePatterns(@NotNull String patterns) {
} // //todo
// }
/** /**
* 获取本地服务器ID * 获取本地服务器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.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 lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@@ -16,7 +15,6 @@ import org.jetbrains.annotations.Nullable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.UUID; import java.util.UUID;
/** /**
@@ -25,33 +23,9 @@ import java.util.UUID;
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class BallMessage { 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); 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 对象获取消息内容 * 以 Java 对象获取消息内容
* *
@@ -199,6 +145,6 @@ public class BallMessage {
@Override @Override
public String toString() { 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; package cn.hamster3.mc.plugin.ball.common.event.message;
import cn.hamster3.mc.plugin.ball.common.data.BallMessage; import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
import com.google.gson.JsonObject;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -23,11 +22,4 @@ public class MessageEvent extends BallMessage {
setAction(message.getAction()); setAction(message.getAction());
setContent(message.getContent()); 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.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.data.BallMessage; import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
import cn.hamster3.mc.plugin.ball.common.event.message.MessageReceivedEvent; 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 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(); public static final BallRedisListener INSTANCE = new BallRedisListener();
private BallRedisListener() { private BallRedisListener() {
} }
@Override @Override
public void message(String channel, BallMessage ballMessage) { public void onMessage(String channel, String message) {
if (channel.startsWith(BallAPI.getInstance().getBallConfig().getChannelPrefix())) { if (channel.startsWith(BallAPI.getInstance().getBallConfig().getChannelPrefix())) {
channel = channel.substring(BallAPI.getInstance().getBallConfig().getChannelPrefix().length()); channel = channel.substring(BallAPI.getInstance().getBallConfig().getChannelPrefix().length());
} }
BallMessage ballMessage = CoreAPI.getInstance().getGson().fromJson(message, BallMessage.class);
BallAPI ballAPI = BallAPI.getInstance(); BallAPI ballAPI = BallAPI.getInstance();
EventBus eventBus = ballAPI.getEventBus(); EventBus eventBus = ballAPI.getEventBus();
if (ballMessage.getReceiverType() != null && ballMessage.getReceiverType() != ballAPI.getLocalServerInfo().getType()) { if (ballMessage.getReceiverType() != null && ballMessage.getReceiverType() != ballAPI.getLocalServerInfo().getType()) {
@@ -29,27 +31,27 @@ public class BallRedisListener implements RedisPubSubListener<String, BallMessag
} }
@Override @Override
public void message(String pattern, String channel, BallMessage info) { public void onPMessage(String pattern, String channel, String message) {
message(channel, info); onMessage(channel, message);
} }
@Override @Override
public void subscribed(String channel, long count) { public void onSubscribe(String channel, int subscribedChannels) {
BallAPI.getInstance().getLogger().info("已订阅 redis 频道 " + channel); BallAPI.getInstance().getLogger().info("已订阅 redis 频道 " + channel);
} }
@Override @Override
public void psubscribed(String pattern, long count) { public void onUnsubscribe(String channel, int subscribedChannels) {
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则) " + pattern);
}
@Override
public void unsubscribed(String channel, long count) {
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道 " + channel); BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道 " + channel);
} }
@Override @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); BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则) " + pattern);
} }
} }

View File

@@ -5,7 +5,7 @@ plugins {
} }
group = "cn.hamster3.mc.plugin" group = "cn.hamster3.mc.plugin"
version = "1.5.7" version = "1.6.0"
subprojects { subprojects {
apply { apply {