feat: 使用 guava 的 EventBus
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
@file:Suppress("VulnerableLibrariesLocal")
|
||||
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
|
||||
|
||||
dependencies {
|
||||
compileOnly("cn.hamster3.mc.plugin:core-common:+")
|
||||
|
||||
compileOnly("com.google.code.gson:gson:2.8.0")
|
||||
compileOnly("com.google.guava:guava:31.0-jre")
|
||||
|
||||
// https://mvnrepository.com/artifact/io.lettuce/lettuce-core
|
||||
compileOnly("io.lettuce:lettuce-core:6.2.6.RELEASE")
|
||||
compileOnly("io.lettuce:lettuce-core:+")
|
||||
}
|
||||
|
||||
tasks {
|
||||
|
@@ -1,22 +1,22 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.api;
|
||||
|
||||
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.BallMessage;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.BallActions;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.message.MessageSentEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.operate.*;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerConnectServerEvent;
|
||||
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.ServerOnlineEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.BallCommonListener;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
|
||||
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.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.net.kyori.adventure.text.Component;
|
||||
import com.google.common.eventbus.AsyncEventBus;
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import lombok.Getter;
|
||||
@@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Getter
|
||||
public abstract class BallAPI {
|
||||
/**
|
||||
* API 使用的通信频道
|
||||
@@ -51,105 +52,29 @@ public abstract class BallAPI {
|
||||
@NotNull
|
||||
private final RedisClient redisClient;
|
||||
@NotNull
|
||||
private final StatefulRedisPubSubConnection<String, BallMessageInfo> pubSubConnection;
|
||||
|
||||
private final StatefulRedisPubSubConnection<String, BallMessage> subConnection;
|
||||
@NotNull
|
||||
private List<BallListener> listeners;
|
||||
@Getter
|
||||
private boolean enabled;
|
||||
private final StatefulRedisPubSubConnection<String, BallMessage> pubConnection;
|
||||
@NotNull
|
||||
private final EventBus eventBus;
|
||||
|
||||
public BallAPI(@NotNull BallServerInfo localServerInfo, @Nullable DataSource datasource, @NotNull RedisClient redisClient, boolean debug) {
|
||||
this.localServerInfo = localServerInfo;
|
||||
this.datasource = datasource;
|
||||
this.redisClient = redisClient;
|
||||
pubSubConnection = redisClient.connectPubSub(BallMessageInfo.CODEC);
|
||||
subConnection = redisClient.connectPubSub(BallMessage.CODEC);
|
||||
pubConnection = redisClient.connectPubSub(BallMessage.CODEC);
|
||||
serverInfo = new ConcurrentHashMap<>();
|
||||
playerInfo = new ConcurrentHashMap<>();
|
||||
listeners = new ArrayList<>();
|
||||
enabled = false;
|
||||
initListener(debug);
|
||||
}
|
||||
|
||||
private void initListener(boolean debug) {
|
||||
addListener(new BallListener() {
|
||||
@Override
|
||||
public ListenerPriority getPriority() {
|
||||
return ListenerPriority.LOW;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerConnectServer(@NotNull BallPlayerConnectServerEvent event) {
|
||||
BallPlayerInfo info = event.getPlayerInfo();
|
||||
playerInfo.put(info.getUuid(), info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerInfoUpdate(@NotNull BallPlayerInfoUpdateEvent event) {
|
||||
BallPlayerInfo info = event.getPlayerInfo();
|
||||
playerInfo.put(info.getUuid(), info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServerOnline(@NotNull ServerOnlineEvent event) {
|
||||
BallServerInfo info = event.getServerInfo();
|
||||
serverInfo.put(info.getId(), info);
|
||||
switch (info.getType()) {
|
||||
case GAME: {
|
||||
playerInfo.forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getGameServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case PROXY: {
|
||||
playerInfo.forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getProxyServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServerOffline(@NotNull ServerOfflineEvent event) {
|
||||
String serverID = event.getServerID();
|
||||
BallServerInfo info = serverInfo.remove(serverID);
|
||||
if (info == null) {
|
||||
return;
|
||||
}
|
||||
switch (info.getType()) {
|
||||
case GAME: {
|
||||
playerInfo.forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getGameServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case PROXY: {
|
||||
playerInfo.forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getProxyServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
|
||||
eventBus.register(BallCommonListener.INSTANCE);
|
||||
if (debug) {
|
||||
getLogger().warning("已启用调试模式");
|
||||
pubSubConnection.addListener(BallDebugListener.INSTANCE);
|
||||
subConnection.addListener(BallDebugListener.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
protected void enable() throws SQLException, InterruptedException {
|
||||
if (enabled) {
|
||||
return;
|
||||
}
|
||||
BallServerInfo localInfo = getLocalServerInfo();
|
||||
|
||||
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
||||
@@ -216,13 +141,12 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
RedisClient client = getRedisClient();
|
||||
pubSubConnection.addListener(BallMessageListener.INSTANCE);
|
||||
pubSubConnection.async().subscribe("HamsterBall");
|
||||
enabled = true;
|
||||
subConnection.addListener(BallRedisListener.INSTANCE);
|
||||
subConnection.async().subscribe(BALL_CHANNEL);
|
||||
}
|
||||
|
||||
protected void disable() throws SQLException, InterruptedException {
|
||||
sendBallMessage(new BallMessageInfo(BALL_CHANNEL, ServerOfflineEvent.ACTION, new ServerOfflineEvent(getLocalServerId())), true);
|
||||
sendBallMessage(new BallMessage(BALL_CHANNEL, BallActions.ServerOffline.name(), new ServerOfflineEvent(getLocalServerId())), true);
|
||||
|
||||
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
@@ -279,12 +203,12 @@ public abstract class BallAPI {
|
||||
* @param message 消息
|
||||
*/
|
||||
public void broadcastPlayerMessage(@NotNull DisplayMessage message) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
BroadcastPlayerMessageEvent.ACTION,
|
||||
BallActions.BroadcastPlayerMessage.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new BroadcastPlayerMessageEvent(message)
|
||||
)
|
||||
@@ -299,12 +223,12 @@ public abstract class BallAPI {
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchConsoleCommand(@Nullable BallServerType type, @Nullable String serverID, @NotNull String command) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
DispatchConsoleCommandEvent.ACTION,
|
||||
BallActions.DispatchConsoleCommand.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new DispatchConsoleCommandEvent(type, serverID, command)
|
||||
)
|
||||
@@ -319,12 +243,12 @@ public abstract class BallAPI {
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchPlayerCommand(@Nullable BallServerType type, @Nullable UUID uuid, @NotNull String command) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
DispatchPlayerCommandEvent.ACTION,
|
||||
BallActions.DispatchPlayerCommand.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new DispatchPlayerCommandEvent(type, uuid, command)
|
||||
)
|
||||
@@ -348,12 +272,12 @@ public abstract class BallAPI {
|
||||
* @param reason 原因
|
||||
*/
|
||||
public void kickPlayer(@NotNull UUID uuid, @NotNull Component reason) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
KickPlayerEvent.ACTION,
|
||||
BallActions.KickPlayer.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new KickPlayerEvent(uuid, reason)
|
||||
)
|
||||
@@ -368,34 +292,7 @@ public abstract class BallAPI {
|
||||
* @param cache 当玩家不在线时,是否缓存消息等待玩家上线再发送
|
||||
*/
|
||||
public void sendMessageToPlayer(@NotNull UUID receiver, @NotNull DisplayMessage message, boolean cache) {
|
||||
BallPlayerInfo info = getPlayerInfo(receiver);
|
||||
if (info == null || !info.isOnline()) {
|
||||
if (!cache) {
|
||||
return;
|
||||
}
|
||||
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);"
|
||||
)) {
|
||||
statement.setString(1, receiver.toString());
|
||||
statement.setString(2, message.toJson().toString());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
SendMessageToPlayerEvent.ACTION,
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new SendMessageToPlayerEvent(Collections.singleton(receiver), message)
|
||||
)
|
||||
));
|
||||
sendMessageToPlayer(Collections.singleton(receiver), message, cache);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -425,12 +322,12 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
}
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
sendBallMessage(new BallMessage(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
SendMessageToPlayerEvent.ACTION,
|
||||
BallActions.SendMessageToPlayer.name(),
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new SendMessageToPlayerEvent(new HashSet<>(receivers), message)
|
||||
)
|
||||
@@ -449,11 +346,7 @@ public abstract class BallAPI {
|
||||
* @param doneMessage 传送完成后显示的消息
|
||||
*/
|
||||
public void sendPlayerToLocation(@NotNull UUID sendPlayerUUID, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
SendPlayerToLocationEvent.ACTION,
|
||||
new SendPlayerToLocationEvent(Collections.singleton(sendPlayerUUID), location, doneMessage)
|
||||
);
|
||||
sendPlayerToLocation(Collections.singleton(sendPlayerUUID), location, doneMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -468,9 +361,7 @@ public abstract class BallAPI {
|
||||
* @param doneMessage 传送完成后显示的消息
|
||||
*/
|
||||
public void sendPlayerToLocation(@NotNull Collection<UUID> sendPlayerUUID, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
SendPlayerToLocationEvent.ACTION,
|
||||
sendBallMessage(BALL_CHANNEL, BallActions.SendPlayerToLocation.name(),
|
||||
new SendPlayerToLocationEvent(new HashSet<>(sendPlayerUUID), location, doneMessage)
|
||||
);
|
||||
}
|
||||
@@ -486,11 +377,7 @@ public abstract class BallAPI {
|
||||
* @param doneTargetMessage 传送完成后目标玩家显示的消息,自动将 %player_name% 替换成被传送者的名称
|
||||
*/
|
||||
public void sendPlayerToPlayer(@NotNull UUID sendPlayer, @NotNull UUID toPlayer, @Nullable DisplayMessage doneMessage, @Nullable DisplayMessage doneTargetMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
SendPlayerToPlayerEvent.ACTION,
|
||||
new SendPlayerToPlayerEvent(Collections.singleton(sendPlayer), toPlayer, doneMessage, doneTargetMessage)
|
||||
);
|
||||
sendPlayerToPlayer(Collections.singleton(sendPlayer), toPlayer, doneMessage, doneTargetMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -504,9 +391,7 @@ public abstract class BallAPI {
|
||||
* @param doneTargetMessage 传送完成后目标玩家显示的消息,自动将 %player_name% 替换成被传送者的名称
|
||||
*/
|
||||
public void sendPlayerToPlayer(@NotNull Collection<UUID> sendPlayerUUID, @NotNull UUID toPlayer, @Nullable DisplayMessage doneMessage, @Nullable DisplayMessage doneTargetMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
SendPlayerToPlayerEvent.ACTION,
|
||||
sendBallMessage(BALL_CHANNEL, BallActions.SendPlayerToPlayer.name(),
|
||||
new SendPlayerToPlayerEvent(new HashSet<>(sendPlayerUUID), toPlayer, doneMessage, doneTargetMessage)
|
||||
);
|
||||
}
|
||||
@@ -518,7 +403,7 @@ public abstract class BallAPI {
|
||||
* @param action 执行动作
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action) {
|
||||
sendBallMessage(new BallMessageInfo(channel, action));
|
||||
sendBallMessage(new BallMessage(channel, action));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -529,7 +414,7 @@ public abstract class BallAPI {
|
||||
* @param content 附加参数
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action, @NotNull Object content) {
|
||||
sendBallMessage(new BallMessageInfo(channel, action, content));
|
||||
sendBallMessage(new BallMessage(channel, action, content));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -537,45 +422,29 @@ public abstract class BallAPI {
|
||||
*
|
||||
* @param messageInfo 消息内容
|
||||
*/
|
||||
public void sendBallMessage(@NotNull BallMessageInfo messageInfo) {
|
||||
public void sendBallMessage(@NotNull BallMessage messageInfo) {
|
||||
sendBallMessage(messageInfo, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义服务消息信息并发送
|
||||
*
|
||||
* @param messageInfo 消息内容
|
||||
* @param ballMessage 消息内容
|
||||
* @param block 是否阻塞(设置为 true 则必须等待消息写入网络的操作完成后,该方法才会退出)
|
||||
*/
|
||||
public void sendBallMessage(@NotNull BallMessageInfo messageInfo, boolean block) {
|
||||
String string = CoreAPI.getInstance().getGson().toJson(messageInfo);
|
||||
try (StatefulRedisPubSubConnection<String, BallMessageInfo> connection = getRedisClient().connectPubSub(BallMessageInfo.CODEC)) {
|
||||
if (block) {
|
||||
connection.sync().publish("HamsterBall", messageInfo);
|
||||
} else {
|
||||
connection.async().publish("HamsterBall", messageInfo);
|
||||
}
|
||||
public void sendBallMessage(@NotNull BallMessage ballMessage, boolean block) {
|
||||
String string = CoreAPI.getInstance().getGson().toJson(ballMessage);
|
||||
if (block) {
|
||||
pubConnection.sync().publish(BALL_CHANNEL, ballMessage);
|
||||
eventBus.post(new MessageSentEvent(ballMessage));
|
||||
} else {
|
||||
pubConnection.async().publish(BALL_CHANNEL, ballMessage).whenComplete((aLong, throwable) -> {
|
||||
if (throwable != null) {
|
||||
return;
|
||||
}
|
||||
eventBus.post(new MessageSentEvent(ballMessage));
|
||||
});
|
||||
}
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onMessageSend(messageInfo);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addListener(@NotNull BallListener listener) {
|
||||
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
|
||||
newListeners.add(listener);
|
||||
newListeners.sort(Comparator.comparing(BallListener::getPriority));
|
||||
listeners = newListeners;
|
||||
}
|
||||
|
||||
public void removeListener(@NotNull BallListener listener) {
|
||||
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
|
||||
newListeners.remove(listener);
|
||||
listeners = newListeners;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -705,21 +574,6 @@ public abstract class BallAPI {
|
||||
return info.getName();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Map<String, BallServerInfo> getAllServerInfo() {
|
||||
return serverInfo;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Map<UUID, BallPlayerInfo> getAllPlayerInfo() {
|
||||
return playerInfo;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<BallListener> getListeners() {
|
||||
return listeners;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public abstract Logger getLogger();
|
||||
|
||||
@@ -727,9 +581,4 @@ public abstract class BallAPI {
|
||||
public DataSource getDatasource() {
|
||||
return datasource == null ? CoreAPI.getInstance().getDataSource() : datasource;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public RedisClient getRedisClient() {
|
||||
return redisClient;
|
||||
}
|
||||
}
|
||||
|
@@ -22,20 +22,20 @@ import java.util.UUID;
|
||||
*/
|
||||
@Data
|
||||
@SuppressWarnings("unused")
|
||||
public class BallMessageInfo {
|
||||
public class BallMessage {
|
||||
/**
|
||||
* 编解码器
|
||||
*/
|
||||
public static final RedisCodec<String, BallMessageInfo> CODEC = new RedisCodec<String, BallMessageInfo>() {
|
||||
public static final RedisCodec<String, BallMessage> CODEC = new RedisCodec<String, BallMessage>() {
|
||||
@Override
|
||||
public String decodeKey(ByteBuffer bytes) {
|
||||
return StringCodec.UTF8.decodeKey(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BallMessageInfo decodeValue(ByteBuffer bytes) {
|
||||
public BallMessage decodeValue(ByteBuffer bytes) {
|
||||
String string = StringCodec.UTF8.decodeValue(bytes);
|
||||
return CoreAPI.getInstance().getGson().fromJson(string, BallMessageInfo.class);
|
||||
return CoreAPI.getInstance().getGson().fromJson(string, BallMessage.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,7 +44,7 @@ public class BallMessageInfo {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer encodeValue(BallMessageInfo value) {
|
||||
public ByteBuffer encodeValue(BallMessage value) {
|
||||
return StringCodec.UTF8.encodeValue(CoreAPI.getInstance().getGson().toJson(value));
|
||||
}
|
||||
};
|
||||
@@ -90,21 +90,21 @@ public class BallMessageInfo {
|
||||
*/
|
||||
private JsonElement content;
|
||||
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String action) {
|
||||
public BallMessage(@NotNull String channel, @NotNull String action) {
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.channel = channel;
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String action, @NotNull Object content) {
|
||||
public BallMessage(@NotNull String channel, @NotNull String action, @NotNull Object content) {
|
||||
this.channel = channel;
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.action = action;
|
||||
this.content = CoreAPI.getInstance().getGson().toJsonTree(content);
|
||||
}
|
||||
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String senderID, @Nullable String receiverID,
|
||||
@Nullable BallServerType receiverType, @NotNull String action, @Nullable JsonElement content) {
|
||||
public BallMessage(@NotNull String channel, @NotNull String senderID, @Nullable String receiverID,
|
||||
@Nullable BallServerType receiverType, @NotNull String action, @Nullable JsonElement content) {
|
||||
this.channel = channel;
|
||||
this.senderID = senderID;
|
||||
this.receiverID = receiverID;
|
@@ -0,0 +1,24 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event;
|
||||
|
||||
public enum BallActions {
|
||||
BroadcastPlayerMessage,
|
||||
DispatchConsoleCommand,
|
||||
DispatchPlayerCommand,
|
||||
KickPlayer,
|
||||
SendMessageToPlayer,
|
||||
SendPlayerToLocation,
|
||||
SendPlayerToPlayer,
|
||||
|
||||
BallPlayerPreLogin,
|
||||
BallPlayerLogin,
|
||||
BallPlayerPostLogin,
|
||||
BallPlayerPreConnectServer,
|
||||
BallPlayerConnectServer,
|
||||
BallPlayerPostConnectServer,
|
||||
BallPlayerLogout,
|
||||
|
||||
BallPlayerInfoUpdate,
|
||||
|
||||
ServerOffline,
|
||||
ServerOnline,
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.message;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MessageReceivedEvent {
|
||||
private BallMessage message;
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.message;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MessageSentEvent {
|
||||
private BallMessage message;
|
||||
}
|
@@ -8,9 +8,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BroadcastPlayerMessageEvent {
|
||||
public static final String ACTION = "BroadcastPlayerMessage";
|
||||
|
||||
@NotNull
|
||||
private final DisplayMessage message;
|
||||
|
||||
}
|
||||
|
@@ -9,8 +9,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class DispatchConsoleCommandEvent {
|
||||
public static final String ACTION = "DispatchConsoleCommand";
|
||||
|
||||
@Nullable
|
||||
private final BallServerType type;
|
||||
@Nullable
|
||||
|
@@ -11,8 +11,6 @@ import java.util.UUID;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class DispatchPlayerCommandEvent {
|
||||
public static final String ACTION = "DispatchPlayerCommand";
|
||||
|
||||
@Nullable
|
||||
private final BallServerType type;
|
||||
@Nullable
|
||||
|
@@ -10,8 +10,6 @@ import java.util.UUID;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class KickPlayerEvent {
|
||||
public static final String ACTION = "KickPlayer";
|
||||
|
||||
@NotNull
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
|
@@ -11,8 +11,6 @@ import java.util.UUID;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendMessageToPlayerEvent {
|
||||
public static final String ACTION = "SendMessageToPlayer";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> receivers;
|
||||
@NotNull
|
||||
|
@@ -13,8 +13,6 @@ import java.util.UUID;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendPlayerToLocationEvent {
|
||||
public static final String ACTION = "SendPlayerToLocation";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> sendPlayerUUID;
|
||||
@NotNull
|
||||
|
@@ -12,8 +12,6 @@ import java.util.UUID;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendPlayerToPlayerEvent {
|
||||
public static final String ACTION = "SendPlayerToPlayer";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> sendPlayerUUID;
|
||||
@NotNull
|
||||
@@ -22,5 +20,4 @@ public class SendPlayerToPlayerEvent {
|
||||
private final DisplayMessage doneMessage;
|
||||
@Nullable
|
||||
private final DisplayMessage doneTargetMessage;
|
||||
|
||||
}
|
||||
|
@@ -18,8 +18,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerConnectServerEvent {
|
||||
public static final String ACTION = "PlayerConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
@Nullable
|
||||
|
@@ -11,8 +11,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerInfoUpdateEvent {
|
||||
public static final String ACTION = "PlayerInfoUpdateEvent";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
||||
|
@@ -11,8 +11,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerLoginEvent {
|
||||
public static final String ACTION = "PlayerLogin";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
|
||||
|
@@ -11,8 +11,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerLogoutEvent {
|
||||
public static final String ACTION = "PlayerLogout";
|
||||
|
||||
@NotNull
|
||||
private BallPlayerInfo playerInfo;
|
||||
}
|
||||
|
@@ -15,8 +15,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPostConnectServerEvent {
|
||||
public static final String ACTION = "PlayerPostConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
||||
|
@@ -11,8 +11,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPostLoginEvent {
|
||||
public static final String ACTION = "PlayerPostLogin";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
||||
|
@@ -16,13 +16,10 @@ import org.jetbrains.annotations.Nullable;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPreConnectServerEvent {
|
||||
public static final String ACTION = "PlayerPreConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
@Nullable
|
||||
private final String from;
|
||||
@NotNull
|
||||
private final String to;
|
||||
|
||||
}
|
||||
|
@@ -10,8 +10,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPreLoginEvent {
|
||||
public static final String ACTION = "PlayerPreLogin";
|
||||
|
||||
@NotNull
|
||||
private final String playerName;
|
||||
}
|
||||
|
@@ -10,8 +10,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOfflineEvent {
|
||||
public static final String ACTION = "ServerOffline";
|
||||
|
||||
@NotNull
|
||||
private final String serverID;
|
||||
}
|
||||
|
@@ -11,8 +11,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOnlineEvent {
|
||||
public static final String ACTION = "ServerOnline";
|
||||
|
||||
@NotNull
|
||||
private final BallServerInfo serverInfo;
|
||||
|
||||
|
@@ -0,0 +1,81 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.listener;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerConnectServerEvent;
|
||||
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.ServerOnlineEvent;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class BallCommonListener {
|
||||
public static final BallCommonListener INSTANCE = new BallCommonListener();
|
||||
|
||||
private BallCommonListener() {
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onBallPlayerConnectServer(@NotNull BallPlayerConnectServerEvent event) {
|
||||
BallPlayerInfo info = event.getPlayerInfo();
|
||||
BallAPI.getInstance().getPlayerInfo().put(info.getUuid(), info);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onBallPlayerInfoUpdate(@NotNull BallPlayerInfoUpdateEvent event) {
|
||||
BallPlayerInfo info = event.getPlayerInfo();
|
||||
BallAPI.getInstance().getPlayerInfo().put(info.getUuid(), info);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onServerOnline(@NotNull ServerOnlineEvent event) {
|
||||
BallServerInfo info = event.getServerInfo();
|
||||
BallAPI.getInstance().getServerInfo().put(info.getId(), info);
|
||||
switch (info.getType()) {
|
||||
case GAME: {
|
||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getGameServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case PROXY: {
|
||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getProxyServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onServerOffline(@NotNull ServerOfflineEvent event) {
|
||||
String serverID = event.getServerID();
|
||||
BallServerInfo info = BallAPI.getInstance().getServerInfo().remove(serverID);
|
||||
if (info == null) {
|
||||
return;
|
||||
}
|
||||
switch (info.getType()) {
|
||||
case GAME: {
|
||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getGameServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case PROXY: {
|
||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getProxyServer().equals(info.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,43 +1,38 @@
|
||||
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.data.BallMessage;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
public class BallDebugListener implements RedisPubSubListener<String, BallMessageInfo> {
|
||||
public class BallDebugListener implements RedisPubSubListener<String, BallMessage> {
|
||||
public static final BallDebugListener INSTANCE = new BallDebugListener();
|
||||
|
||||
private BallDebugListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String channel, BallMessageInfo event) {
|
||||
public void message(String channel, BallMessage event) {
|
||||
BallAPI.getInstance().getLogger().info("从 " + channel + " 收到了一条消息: " + event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, BallMessageInfo event) {
|
||||
public void message(String pattern, String channel, BallMessage event) {
|
||||
BallAPI.getInstance().getLogger().info("从 " + pattern + "(" + channel + ") 收到了一条消息: " + event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) {
|
||||
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) {
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道: " + channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) {
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则): " + pattern);
|
||||
}
|
||||
}
|
||||
|
@@ -1,55 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.listener;
|
||||
|
||||
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 org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface BallListener {
|
||||
/**
|
||||
* 该监听器的执行优先级
|
||||
*
|
||||
* @return 优先级
|
||||
*/
|
||||
default ListenerPriority getPriority() {
|
||||
return ListenerPriority.NORMAL;
|
||||
}
|
||||
|
||||
default void onMessageReceived(@NotNull BallMessageInfo event) {
|
||||
}
|
||||
|
||||
default void onMessageSend(@NotNull BallMessageInfo event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerPreLogin(@NotNull BallPlayerPreLoginEvent event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerLogin(@NotNull BallPlayerLoginEvent event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerPostLogin(@NotNull BallPlayerPostLoginEvent event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerPreConnectServer(@NotNull BallPlayerPreConnectServerEvent event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerConnectServer(@NotNull BallPlayerConnectServerEvent event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerPostConnectServer(@NotNull BallPlayerPostConnectServerEvent event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerLogout(@NotNull BallPlayerLogoutEvent event) {
|
||||
}
|
||||
|
||||
default void onBallPlayerInfoUpdate(@NotNull BallPlayerInfoUpdateEvent event) {
|
||||
}
|
||||
|
||||
default void onServerOnline(@NotNull ServerOnlineEvent event) {
|
||||
}
|
||||
|
||||
default void onServerOffline(@NotNull ServerOfflineEvent event) {
|
||||
}
|
||||
|
||||
}
|
@@ -1,167 +0,0 @@
|
||||
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.api.CoreAPI;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
public class BallMessageListener implements RedisPubSubListener<String, BallMessageInfo> {
|
||||
public static final BallMessageListener INSTANCE = new BallMessageListener();
|
||||
|
||||
private BallMessageListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String 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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().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 = CoreAPI.getInstance().getGson().fromJson(info.getContent(), ServerOnlineEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onServerOnline(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, BallMessageInfo info) {
|
||||
message(channel, info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void psubscribed(String pattern, long count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribed(String channel, long count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) {
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,143 @@
|
||||
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.BallActions;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.message.MessageReceivedEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.operate.*;
|
||||
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.api.CoreAPI;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
public class BallRedisListener implements RedisPubSubListener<String, BallMessage> {
|
||||
public static final BallRedisListener INSTANCE = new BallRedisListener();
|
||||
|
||||
private BallRedisListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String channel, BallMessage ballMessage) {
|
||||
BallAPI.getInstance().getEventBus().post(new MessageReceivedEvent(ballMessage));
|
||||
if (!BallAPI.BALL_CHANNEL.equals(ballMessage.getChannel())) {
|
||||
return;
|
||||
}
|
||||
switch (BallActions.valueOf(ballMessage.getAction())) {
|
||||
case BroadcastPlayerMessage: {
|
||||
BroadcastPlayerMessageEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BroadcastPlayerMessageEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case DispatchConsoleCommand: {
|
||||
DispatchConsoleCommandEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), DispatchConsoleCommandEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case DispatchPlayerCommand: {
|
||||
DispatchPlayerCommandEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), DispatchPlayerCommandEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case KickPlayer: {
|
||||
KickPlayerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), KickPlayerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case SendMessageToPlayer: {
|
||||
SendMessageToPlayerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), SendMessageToPlayerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case SendPlayerToLocation: {
|
||||
SendPlayerToLocationEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), SendPlayerToLocationEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case SendPlayerToPlayer: {
|
||||
SendPlayerToPlayerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), SendPlayerToPlayerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
|
||||
case BallPlayerPreLogin: {
|
||||
BallPlayerPreLoginEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPreLoginEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerLogin: {
|
||||
BallPlayerLoginEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerLoginEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerPostLogin: {
|
||||
BallPlayerPostLoginEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPostLoginEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerPreConnectServer: {
|
||||
BallPlayerPreConnectServerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPreConnectServerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerConnectServer: {
|
||||
BallPlayerConnectServerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerConnectServerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerPostConnectServer: {
|
||||
BallPlayerPostConnectServerEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerPostConnectServerEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case BallPlayerLogout: {
|
||||
BallPlayerLogoutEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerLogoutEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
|
||||
case BallPlayerInfoUpdate: {
|
||||
BallPlayerInfoUpdateEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), BallPlayerInfoUpdateEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
|
||||
case ServerOffline: {
|
||||
ServerOfflineEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), ServerOfflineEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
case ServerOnline: {
|
||||
ServerOnlineEvent event = CoreAPI.getInstance().getGson().fromJson(ballMessage.getContent(), ServerOnlineEvent.class);
|
||||
BallAPI.getInstance().getEventBus().post(event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, BallMessage info) {
|
||||
message(channel, info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) {
|
||||
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) {
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道: " + channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) {
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则): " + pattern);
|
||||
}
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.listener;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public enum ListenerPriority {
|
||||
|
||||
/**
|
||||
* Event call is of very low importance and should be run first, to allow
|
||||
* other plugins to further customise the outcome
|
||||
*/
|
||||
LOWEST(0),
|
||||
/**
|
||||
* Event call is of low importance
|
||||
*/
|
||||
LOW(1),
|
||||
/**
|
||||
* Event call is neither important nor unimportant, and may be run
|
||||
* normally
|
||||
*/
|
||||
NORMAL(2),
|
||||
/**
|
||||
* Event call is of high importance
|
||||
*/
|
||||
HIGH(3),
|
||||
/**
|
||||
* Event call is critical and must have the final say in what happens
|
||||
* to the event
|
||||
*/
|
||||
HIGHEST(4),
|
||||
/**
|
||||
* Event is listened to purely for monitoring the outcome of an event.
|
||||
* <p>
|
||||
* No modifications to the event should be made under this priority
|
||||
*/
|
||||
MONITOR(5);
|
||||
|
||||
private final int slot;
|
||||
|
||||
ListenerPriority(int slot) {
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
public int getSlot() {
|
||||
return slot;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user