feat: 优化结构
This commit is contained in:
@@ -146,7 +146,7 @@ public abstract class BallAPI {
|
||||
}
|
||||
|
||||
protected void disable() throws SQLException, InterruptedException {
|
||||
sendBallMessage(new BallMessage(BALL_CHANNEL, BallActions.ServerOffline.name(), new ServerOfflineEvent(getLocalServerId())), true);
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(BallActions.ServerOffline.name(), new ServerOfflineEvent(getLocalServerId())), true);
|
||||
|
||||
try (Connection connection = getDatasource().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
@@ -203,11 +203,8 @@ public abstract class BallAPI {
|
||||
* @param message 消息
|
||||
*/
|
||||
public void broadcastPlayerMessage(@NotNull DisplayMessage message) {
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
getLocalServerId(), null, BallServerType.PROXY,
|
||||
BallActions.BroadcastPlayerMessage.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new BroadcastPlayerMessageEvent(message)
|
||||
@@ -223,11 +220,8 @@ public abstract class BallAPI {
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchConsoleCommand(@Nullable BallServerType type, @Nullable String serverID, @NotNull String command) {
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
getLocalServerId(), null, BallServerType.GAME,
|
||||
BallActions.DispatchConsoleCommand.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new DispatchConsoleCommandEvent(type, serverID, command)
|
||||
@@ -243,11 +237,8 @@ public abstract class BallAPI {
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchPlayerCommand(@Nullable BallServerType type, @Nullable UUID uuid, @NotNull String command) {
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
getLocalServerId(), null, BallServerType.GAME,
|
||||
BallActions.DispatchPlayerCommand.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new DispatchPlayerCommandEvent(type, uuid, command)
|
||||
@@ -272,11 +263,8 @@ public abstract class BallAPI {
|
||||
* @param reason 原因
|
||||
*/
|
||||
public void kickPlayer(@NotNull UUID uuid, @NotNull Component reason) {
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
getLocalServerId(), null, BallServerType.PROXY,
|
||||
BallActions.KickPlayer.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new KickPlayerEvent(uuid, reason)
|
||||
@@ -322,11 +310,8 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
}
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
getLocalServerId(), null, BallServerType.PROXY,
|
||||
BallActions.SendMessageToPlayer.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new SendMessageToPlayerEvent(new HashSet<>(receivers), message)
|
||||
@@ -403,7 +388,7 @@ public abstract class BallAPI {
|
||||
* @param action 执行动作
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action) {
|
||||
sendBallMessage(new BallMessage(channel, action));
|
||||
sendBallMessage(channel, new BallMessage(action));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -414,35 +399,36 @@ public abstract class BallAPI {
|
||||
* @param content 附加参数
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action, @NotNull Object content) {
|
||||
sendBallMessage(new BallMessage(channel, action, content));
|
||||
sendBallMessage(channel, new BallMessage(action, content));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送自定义消息
|
||||
*
|
||||
* @param messageInfo 消息内容
|
||||
* @param message 消息内容
|
||||
*/
|
||||
public void sendBallMessage(@NotNull BallMessage messageInfo) {
|
||||
sendBallMessage(messageInfo, false);
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull BallMessage message) {
|
||||
sendBallMessage(channel, message, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义服务消息信息并发送
|
||||
*
|
||||
* @param ballMessage 消息内容
|
||||
* @param block 是否阻塞(设置为 true 则必须等待消息写入网络的操作完成后,该方法才会退出)
|
||||
* @param channel 消息标签
|
||||
* @param message 消息内容
|
||||
* @param block 是否阻塞(设置为 true 则必须等待消息写入网络的操作完成后,该方法才会退出)
|
||||
*/
|
||||
public void sendBallMessage(@NotNull BallMessage ballMessage, boolean block) {
|
||||
String string = CoreAPI.getInstance().getGson().toJson(ballMessage);
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull BallMessage message, boolean block) {
|
||||
String string = CoreAPI.getInstance().getGson().toJson(message);
|
||||
if (block) {
|
||||
pubConnection.sync().publish(BALL_CHANNEL, ballMessage);
|
||||
eventBus.post(new MessageSentEvent(ballMessage));
|
||||
pubConnection.sync().publish(BALL_CHANNEL, message);
|
||||
eventBus.post(new MessageSentEvent(channel, message));
|
||||
} else {
|
||||
pubConnection.async().publish(BALL_CHANNEL, ballMessage).whenComplete((aLong, throwable) -> {
|
||||
pubConnection.async().publish(BALL_CHANNEL, message).whenComplete((aLong, throwable) -> {
|
||||
if (throwable != null) {
|
||||
return;
|
||||
}
|
||||
eventBus.post(new MessageSentEvent(ballMessage));
|
||||
eventBus.post(new MessageSentEvent(channel, message));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ 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;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -21,6 +22,7 @@ import java.util.UUID;
|
||||
* 服务消息
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@SuppressWarnings("unused")
|
||||
public class BallMessage {
|
||||
/**
|
||||
@@ -71,11 +73,6 @@ public class BallMessage {
|
||||
*/
|
||||
@Nullable
|
||||
private BallServerType receiverType;
|
||||
/**
|
||||
* 消息的频道
|
||||
*/
|
||||
@NotNull
|
||||
private String channel;
|
||||
/**
|
||||
* 消息动作
|
||||
* <p>
|
||||
@@ -90,22 +87,19 @@ public class BallMessage {
|
||||
*/
|
||||
private JsonElement content;
|
||||
|
||||
public BallMessage(@NotNull String channel, @NotNull String action) {
|
||||
public BallMessage(@NotNull String action) {
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.channel = channel;
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public BallMessage(@NotNull String channel, @NotNull String action, @NotNull Object content) {
|
||||
this.channel = channel;
|
||||
public BallMessage(@NotNull String action, @NotNull Object content) {
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.action = action;
|
||||
this.content = CoreAPI.getInstance().getGson().toJsonTree(content);
|
||||
}
|
||||
|
||||
public BallMessage(@NotNull String channel, @NotNull String senderID, @Nullable String receiverID,
|
||||
public BallMessage(@NotNull String senderID, @Nullable String receiverID,
|
||||
@Nullable BallServerType receiverType, @NotNull String action, @Nullable JsonElement content) {
|
||||
this.channel = channel;
|
||||
this.senderID = senderID;
|
||||
this.receiverID = receiverID;
|
||||
this.receiverType = receiverType;
|
||||
@@ -121,7 +115,6 @@ public class BallMessage {
|
||||
@NotNull
|
||||
public JsonObject toJson() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("channel", channel);
|
||||
object.addProperty("senderID", senderID);
|
||||
if (receiverID != null) {
|
||||
object.addProperty("toServer", receiverID);
|
||||
|
@@ -3,9 +3,19 @@ package cn.hamster3.mc.plugin.ball.common.event.message;
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MessageReceivedEvent {
|
||||
/**
|
||||
* 消息的频道
|
||||
*/
|
||||
@NotNull
|
||||
private String channel;
|
||||
/**
|
||||
* 消息的内容
|
||||
*/
|
||||
@NotNull
|
||||
private BallMessage message;
|
||||
}
|
||||
|
@@ -3,9 +3,19 @@ package cn.hamster3.mc.plugin.ball.common.event.message;
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MessageSentEvent {
|
||||
/**
|
||||
* 消息的频道
|
||||
*/
|
||||
@NotNull
|
||||
private String channel;
|
||||
/**
|
||||
* 消息的内容
|
||||
*/
|
||||
@NotNull
|
||||
private BallMessage message;
|
||||
}
|
||||
|
@@ -19,97 +19,104 @@ public class BallRedisListener implements RedisPubSubListener<String, BallMessag
|
||||
|
||||
@Override
|
||||
public void message(String channel, BallMessage ballMessage) {
|
||||
BallAPI.getInstance().getEventBus().post(new MessageReceivedEvent(ballMessage));
|
||||
if (!BallAPI.BALL_CHANNEL.equals(ballMessage.getChannel())) {
|
||||
BallAPI api = BallAPI.getInstance();
|
||||
if (ballMessage.getReceiverType() != null && ballMessage.getReceiverType() != api.getLocalServerInfo().getType()) {
|
||||
return;
|
||||
}
|
||||
if (ballMessage.getReceiverID() != null && !api.isLocalServer(ballMessage.getReceiverID())) {
|
||||
return;
|
||||
}
|
||||
api.getEventBus().post(new MessageReceivedEvent(channel, ballMessage));
|
||||
if (!BallAPI.BALL_CHANNEL.equals(channel)) {
|
||||
return;
|
||||
}
|
||||
switch (BallActions.valueOf(ballMessage.getAction())) {
|
||||
case BroadcastPlayerMessage: {
|
||||
BroadcastPlayerMessageEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BroadcastPlayerMessageEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case DispatchConsoleCommand: {
|
||||
DispatchConsoleCommandEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), DispatchConsoleCommandEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case DispatchPlayerCommand: {
|
||||
DispatchPlayerCommandEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), DispatchPlayerCommandEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case KickPlayer: {
|
||||
KickPlayerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), KickPlayerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case SendMessageToPlayer: {
|
||||
SendMessageToPlayerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), SendMessageToPlayerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case SendPlayerToLocation: {
|
||||
SendPlayerToLocationEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), SendPlayerToLocationEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case SendPlayerToPlayer: {
|
||||
SendPlayerToPlayerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), SendPlayerToPlayerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
|
||||
case BallPlayerPreLogin: {
|
||||
BallPlayerPreLoginEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPreLoginEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerLogin: {
|
||||
BallPlayerLoginEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerLoginEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerPostLogin: {
|
||||
BallPlayerPostLoginEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPostLoginEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerPreConnectServer: {
|
||||
BallPlayerPreConnectServerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPreConnectServerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerConnectServer: {
|
||||
BallPlayerConnectServerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerConnectServerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerPostConnectServer: {
|
||||
BallPlayerPostConnectServerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPostConnectServerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerLogout: {
|
||||
BallPlayerLogoutEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerLogoutEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
|
||||
case BallPlayerInfoUpdate: {
|
||||
BallPlayerInfoUpdateEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerInfoUpdateEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
|
||||
case ServerOffline: {
|
||||
ServerOfflineEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), ServerOfflineEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case ServerOnline: {
|
||||
ServerOnlineEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), ServerOnlineEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
api.getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -123,7 +130,6 @@ public class BallRedisListener implements RedisPubSubListener<String, BallMessag
|
||||
@Override
|
||||
public void subscribed(String channel, long count) {
|
||||
BallAPI.getInstance().getLogger().info("已订阅 redis 频道: " + channel);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user