diff --git a/ball-bukkit/build.gradle.kts b/ball-bukkit/build.gradle.kts index 41e3490..6b93fa0 100644 --- a/ball-bukkit/build.gradle.kts +++ b/ball-bukkit/build.gradle.kts @@ -8,7 +8,7 @@ dependencies { } 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") { isTransitive = false } diff --git a/ball-bungee/build.gradle.kts b/ball-bungee/build.gradle.kts index 25bac2c..06eae5e 100644 --- a/ball-bungee/build.gradle.kts +++ b/ball-bungee/build.gradle.kts @@ -8,7 +8,7 @@ dependencies { } 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 { diff --git a/ball-common/build.gradle.kts b/ball-common/build.gradle.kts index fac50a4..64d7af5 100644 --- a/ball-common/build.gradle.kts +++ b/ball-common/build.gradle.kts @@ -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") diff --git a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java index f8837cf..f5aeb11 100644 --- a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java +++ b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java @@ -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 allPlayerInfo; @NotNull - private final StatefulRedisPubSubConnection redisPub; + private final Jedis redisSub; @NotNull - private final StatefulRedisPubSubConnection 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 diff --git a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessage.java b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessage.java index 48d0f37..8d47be2 100644 --- a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessage.java +++ b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessage.java @@ -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 REDIS_CODEC = new RedisCodec() { - @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); } } diff --git a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/event/message/MessageEvent.java b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/event/message/MessageEvent.java index 5aa4407..6eeb451 100644 --- a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/event/message/MessageEvent.java +++ b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/event/message/MessageEvent.java @@ -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; - } } diff --git a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallRedisListener.java b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallRedisListener.java index 3a80313..d18715b 100644 --- a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallRedisListener.java +++ b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallRedisListener.java @@ -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 { +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