feat: 使用 redission 作为消息中间件
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package cn.hamster3.mc.plugin.ball.common.api;
|
package cn.hamster3.mc.plugin.ball.common.api;
|
||||||
|
|
||||||
|
import cn.hamster3.mc.plugin.ball.common.codec.BallMessageInfoCodec;
|
||||||
import cn.hamster3.mc.plugin.ball.common.data.BallLocation;
|
import cn.hamster3.mc.plugin.ball.common.data.BallLocation;
|
||||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
|
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
|
||||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||||
@@ -11,6 +12,7 @@ import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerInfoUpdateEvent;
|
|||||||
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.ball.common.listener.BallListener;
|
import cn.hamster3.mc.plugin.ball.common.listener.BallListener;
|
||||||
|
import cn.hamster3.mc.plugin.ball.common.listener.BallMessageListener;
|
||||||
import cn.hamster3.mc.plugin.ball.common.listener.ListenerPriority;
|
import cn.hamster3.mc.plugin.ball.common.listener.ListenerPriority;
|
||||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||||
@@ -125,12 +127,6 @@ public abstract class BallAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onConnectRefused() {
|
|
||||||
enabled = false;
|
|
||||||
getLogger().info("连接至服务中心的请求被拒绝,已关闭仓鼠球。");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
@@ -214,6 +210,9 @@ public abstract class BallAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
enabled = true;
|
enabled = true;
|
||||||
|
|
||||||
|
RTopic topic = getRedissonClient().getTopic(BALL_CHANNEL, BallMessageInfoCodec.INSTANCE);
|
||||||
|
topic.addListener(BallMessageInfo.class, BallMessageListener.INSTANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void disable() throws SQLException, InterruptedException {
|
protected void disable() throws SQLException, InterruptedException {
|
||||||
|
@@ -1,25 +0,0 @@
|
|||||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
|
||||||
|
|
||||||
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 玩家的聊天信息
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class BallPlayerChatEvent {
|
|
||||||
public static final String ACTION = "PlayerChat";
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final UUID playerUUID;
|
|
||||||
@NotNull
|
|
||||||
private final Component displayName;
|
|
||||||
@NotNull
|
|
||||||
private final Component message;
|
|
||||||
|
|
||||||
}
|
|
@@ -16,24 +16,12 @@ public interface BallListener {
|
|||||||
return ListenerPriority.NORMAL;
|
return ListenerPriority.NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
default void onConnectActive() {
|
|
||||||
}
|
|
||||||
|
|
||||||
default void onMessageReceived(@NotNull BallMessageInfo event) {
|
default void onMessageReceived(@NotNull BallMessageInfo event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
default void onMessageSend(@NotNull BallMessageInfo event) {
|
default void onMessageSend(@NotNull BallMessageInfo event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
default void onConnectException(Throwable throwable) {
|
|
||||||
}
|
|
||||||
|
|
||||||
default void onConnectRefused() {
|
|
||||||
}
|
|
||||||
|
|
||||||
default void onServiceDead() {
|
|
||||||
}
|
|
||||||
|
|
||||||
default void onBallPlayerPreLogin(@NotNull BallPlayerPreLoginEvent event) {
|
default void onBallPlayerPreLogin(@NotNull BallPlayerPreLoginEvent event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,9 +46,6 @@ public interface BallListener {
|
|||||||
default void onBallPlayerInfoUpdate(@NotNull BallPlayerInfoUpdateEvent event) {
|
default void onBallPlayerInfoUpdate(@NotNull BallPlayerInfoUpdateEvent event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
default void onBallPlayerChat(@NotNull BallPlayerChatEvent event) {
|
|
||||||
}
|
|
||||||
|
|
||||||
default void onServerOnline(@NotNull ServerOnlineEvent event) {
|
default void onServerOnline(@NotNull ServerOnlineEvent event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -0,0 +1,142 @@
|
|||||||
|
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.BallMessageInfo;
|
||||||
|
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.util.CoreUtils;
|
||||||
|
import org.redisson.api.listener.MessageListener;
|
||||||
|
|
||||||
|
public class BallMessageListener implements MessageListener<BallMessageInfo> {
|
||||||
|
public static final BallMessageListener INSTANCE = new BallMessageListener();
|
||||||
|
|
||||||
|
private BallMessageListener() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(CharSequence channel, BallMessageInfo info) {
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onMessageReceived(info);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!BallAPI.BALL_CHANNEL.equals(info.getChannel())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (info.getAction()) {
|
||||||
|
case BallPlayerPreLoginEvent.ACTION: {
|
||||||
|
BallPlayerPreLoginEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPreLoginEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerPreLogin(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BallPlayerLoginEvent.ACTION: {
|
||||||
|
BallPlayerLoginEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerLoginEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerLogin(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BallPlayerPostLoginEvent.ACTION: {
|
||||||
|
BallPlayerPostLoginEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPostLoginEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerPostLogin(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BallPlayerPreConnectServerEvent.ACTION: {
|
||||||
|
BallPlayerPreConnectServerEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPreConnectServerEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerPreConnectServer(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BallPlayerConnectServerEvent.ACTION: {
|
||||||
|
BallPlayerConnectServerEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerConnectServerEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerConnectServer(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BallPlayerPostConnectServerEvent.ACTION: {
|
||||||
|
BallPlayerPostConnectServerEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPostConnectServerEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerPostConnectServer(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BallPlayerLogoutEvent.ACTION: {
|
||||||
|
BallPlayerLogoutEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerLogoutEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerLogout(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BallPlayerInfoUpdateEvent.ACTION: {
|
||||||
|
BallPlayerInfoUpdateEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerInfoUpdateEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onBallPlayerInfoUpdate(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ServerOfflineEvent.ACTION: {
|
||||||
|
ServerOfflineEvent event = CoreUtils.GSON.fromJson(info.getContent(), ServerOfflineEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onServerOffline(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ServerOnlineEvent.ACTION: {
|
||||||
|
ServerOnlineEvent event = CoreUtils.GSON.fromJson(info.getContent(), ServerOnlineEvent.class);
|
||||||
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
|
try {
|
||||||
|
listener.onServerOnline(event);
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user