feat: 新版 API

This commit is contained in:
2023-11-04 16:40:22 +08:00
parent 167e845bae
commit 4fe49f83aa
73 changed files with 1947 additions and 1004 deletions

View File

@@ -1,23 +1,20 @@
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.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.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.event.server.ServerOffline;
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnline;
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugLogListener;
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 io.lettuce.core.RedisClient;
import io.lettuce.core.pubsub.RedisPubSubListener;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
@@ -45,111 +42,32 @@ public abstract class BallAPI {
@NotNull
protected final Map<UUID, BallPlayerInfo> playerInfo;
@NotNull
protected final StatefulRedisPubSubConnection<String, BallMessage> pubConnection;
@NotNull
protected final StatefulRedisPubSubConnection<String, BallMessage> subConnection;
@NotNull
private final BallServerInfo localServerInfo;
@Nullable
private final DataSource datasource;
@NotNull
private final RedisClient redisClient;
@NotNull
private final StatefulRedisPubSubConnection<String, BallMessageInfo> pubSubConnection;
@NotNull
private List<BallListener> listeners;
@Getter
private boolean enabled;
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);
pubConnection = redisClient.connectPubSub(BallMessage.REDIS_CODEC);
subConnection = redisClient.connectPubSub(BallMessage.REDIS_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;
}
}
}
});
if (debug) {
getLogger().warning("已启用调试模式");
pubSubConnection.addListener(BallDebugListener.INSTANCE);
subConnection.addListener(BallDebugLogListener.INSTANCE);
}
subConnection.addListener(new InnerListener());
}
protected void enable() throws SQLException, InterruptedException {
if (enabled) {
return;
}
BallServerInfo localInfo = getLocalServerInfo();
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
@@ -199,6 +117,7 @@ public abstract class BallAPI {
}
}
}
getLogger().info("已加载 " + serverInfo.size() + " 条服务器信息");
try (PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM `hamster_ball_player_info`;"
)) {
@@ -214,15 +133,15 @@ public abstract class BallAPI {
}
}
}
getLogger().info("已加载 " + playerInfo.size() + " 条玩家信息");
}
RedisClient client = getRedisClient();
pubSubConnection.addListener(BallMessageListener.INSTANCE);
pubSubConnection.async().subscribe("HamsterBall");
enabled = true;
subscribe(BALL_CHANNEL);
sendBallMessage(BALL_CHANNEL, new BallMessage(BallActions.ServerOnline.name(), new ServerOnline(getLocalServerInfo())), true);
}
protected void disable() throws SQLException, InterruptedException {
sendBallMessage(new BallMessageInfo(BALL_CHANNEL, ServerOfflineEvent.ACTION, new ServerOfflineEvent(getLocalServerId())), true);
sendBallMessage(BALL_CHANNEL, new BallMessage(BallActions.ServerOffline.name(), new ServerOffline(getLocalServerInfo())), true);
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
@@ -239,9 +158,9 @@ public abstract class BallAPI {
statement.executeUpdate();
}
}
getLogger().info("正在关闭 redission");
getLogger().info("正在关闭 redis 客户端");
getRedisClient().close();
getLogger().info("已关闭 redission");
getLogger().info("已关闭 redis 客户端");
}
/**
@@ -279,14 +198,13 @@ public abstract class BallAPI {
* @param message 消息
*/
public void broadcastPlayerMessage(@NotNull DisplayMessage message) {
sendBallMessage(new BallMessageInfo(
BALL_CHANNEL,
sendBallMessage(BALL_CHANNEL, new BallMessage(
getLocalServerId(),
null,
BallServerType.PROXY,
BroadcastPlayerMessageEvent.ACTION,
BallActions.BroadcastPlayerMessage.name(),
CoreAPI.getInstance().getGson().toJsonTree(
new BroadcastPlayerMessageEvent(message)
new BroadcastPlayerMessage(message)
)
));
}
@@ -299,14 +217,13 @@ public abstract class BallAPI {
* @param command 命令内容
*/
public void dispatchConsoleCommand(@Nullable BallServerType type, @Nullable String serverID, @NotNull String command) {
sendBallMessage(new BallMessageInfo(
BALL_CHANNEL,
sendBallMessage(BALL_CHANNEL, new BallMessage(
getLocalServerId(),
null,
BallServerType.GAME,
DispatchConsoleCommandEvent.ACTION,
BallActions.DispatchConsoleCommand.name(),
CoreAPI.getInstance().getGson().toJsonTree(
new DispatchConsoleCommandEvent(type, serverID, command)
new DispatchConsoleCommand(type, serverID, command)
)
));
}
@@ -319,14 +236,13 @@ public abstract class BallAPI {
* @param command 命令内容
*/
public void dispatchPlayerCommand(@Nullable BallServerType type, @Nullable UUID uuid, @NotNull String command) {
sendBallMessage(new BallMessageInfo(
BALL_CHANNEL,
sendBallMessage(BALL_CHANNEL, new BallMessage(
getLocalServerId(),
null,
BallServerType.GAME,
DispatchPlayerCommandEvent.ACTION,
BallActions.DispatchPlayerCommand.name(),
CoreAPI.getInstance().getGson().toJsonTree(
new DispatchPlayerCommandEvent(type, uuid, command)
new DispatchPlayerCommand(type, uuid, command)
)
));
}
@@ -348,14 +264,13 @@ public abstract class BallAPI {
* @param reason 原因
*/
public void kickPlayer(@NotNull UUID uuid, @NotNull Component reason) {
sendBallMessage(new BallMessageInfo(
BALL_CHANNEL,
sendBallMessage(BALL_CHANNEL, new BallMessage(
getLocalServerId(),
null,
BallServerType.PROXY,
KickPlayerEvent.ACTION,
BallActions.KickPlayer.name(),
CoreAPI.getInstance().getGson().toJsonTree(
new KickPlayerEvent(uuid, reason)
new KickPlayer(uuid, reason)
)
));
}
@@ -386,14 +301,13 @@ public abstract class BallAPI {
}
return;
}
sendBallMessage(new BallMessageInfo(
BALL_CHANNEL,
sendBallMessage(BALL_CHANNEL, new BallMessage(
getLocalServerId(),
null,
BallServerType.PROXY,
SendMessageToPlayerEvent.ACTION,
BallActions.SendMessageToPlayer.name(),
CoreAPI.getInstance().getGson().toJsonTree(
new SendMessageToPlayerEvent(Collections.singleton(receiver), message)
new SendMessageToPlayer(Collections.singleton(receiver), message)
)
));
}
@@ -425,14 +339,13 @@ public abstract class BallAPI {
}
}
}
sendBallMessage(new BallMessageInfo(
BALL_CHANNEL,
sendBallMessage(BALL_CHANNEL, new BallMessage(
getLocalServerId(),
null,
BallServerType.PROXY,
SendMessageToPlayerEvent.ACTION,
BallActions.SendMessageToPlayer.name(),
CoreAPI.getInstance().getGson().toJsonTree(
new SendMessageToPlayerEvent(new HashSet<>(receivers), message)
new SendMessageToPlayer(new HashSet<>(receivers), message)
)
));
}
@@ -444,15 +357,15 @@ public abstract class BallAPI {
* <p>
* 则会先尝试将玩家连接至目标服务器再进行传送
*
* @param sendPlayerUUID 玩家的uuid
* @param location 坐标
* @param doneMessage 传送完成后显示的消息
* @param sendPlayer 玩家的uuid
* @param location 坐标
* @param doneMessage 传送完成后显示的消息
*/
public void sendPlayerToLocation(@NotNull UUID sendPlayerUUID, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
public void sendPlayerToLocation(@NotNull UUID sendPlayer, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
sendBallMessage(
BALL_CHANNEL,
SendPlayerToLocationEvent.ACTION,
new SendPlayerToLocationEvent(Collections.singleton(sendPlayerUUID), location, doneMessage)
BallActions.SendPlayerToLocation.name(),
new SendPlayerToLocation(Collections.singleton(sendPlayer), location, doneMessage)
);
}
@@ -463,15 +376,15 @@ public abstract class BallAPI {
* <p>
* 则会先尝试将玩家连接至目标服务器再进行传送
*
* @param sendPlayerUUID 玩家的uuid
* @param location 坐标
* @param doneMessage 传送完成后显示的消息
* @param sendPlayer 玩家的uuid
* @param location 坐标
* @param doneMessage 传送完成后显示的消息
*/
public void sendPlayerToLocation(@NotNull Collection<UUID> sendPlayerUUID, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
public void sendPlayerToLocation(@NotNull Collection<UUID> sendPlayer, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
sendBallMessage(
BALL_CHANNEL,
SendPlayerToLocationEvent.ACTION,
new SendPlayerToLocationEvent(new HashSet<>(sendPlayerUUID), location, doneMessage)
BallActions.SendPlayerToLocation.name(),
new SendPlayerToLocation(new HashSet<>(sendPlayer), location, doneMessage)
);
}
@@ -488,8 +401,8 @@ public abstract class BallAPI {
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)
BallActions.SendPlayerToPlayer.name(),
new SendPlayerToPlayer(Collections.singleton(sendPlayer), toPlayer, doneMessage, doneTargetMessage)
);
}
@@ -498,16 +411,16 @@ public abstract class BallAPI {
* <p>
* 支持跨服传送
*
* @param sendPlayerUUID 被传送的玩家
* @param sendPlayer 被传送的玩家
* @param toPlayer 传送的目标玩家
* @param doneMessage 传送完成后显示的消息,自动将 %player_name% 替换成传送目标玩家的名称
* @param doneTargetMessage 传送完成后目标玩家显示的消息,自动将 %player_name% 替换成被传送者的名称
*/
public void sendPlayerToPlayer(@NotNull Collection<UUID> sendPlayerUUID, @NotNull UUID toPlayer, @Nullable DisplayMessage doneMessage, @Nullable DisplayMessage doneTargetMessage) {
public void sendPlayerToPlayer(@NotNull Collection<UUID> sendPlayer, @NotNull UUID toPlayer, @Nullable DisplayMessage doneMessage, @Nullable DisplayMessage doneTargetMessage) {
sendBallMessage(
BALL_CHANNEL,
SendPlayerToPlayerEvent.ACTION,
new SendPlayerToPlayerEvent(new HashSet<>(sendPlayerUUID), toPlayer, doneMessage, doneTargetMessage)
BallActions.SendPlayerToPlayer.name(),
new SendPlayerToPlayer(new HashSet<>(sendPlayer), toPlayer, doneMessage, doneTargetMessage)
);
}
@@ -518,7 +431,7 @@ public abstract class BallAPI {
* @param action 执行动作
*/
public void sendBallMessage(@NotNull String channel, @NotNull String action) {
sendBallMessage(new BallMessageInfo(channel, action));
sendBallMessage(channel, new BallMessage(action));
}
/**
@@ -529,53 +442,48 @@ 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(channel, new BallMessage(action, content));
}
/**
* 发送自定义消息
*
* @param messageInfo 消息内容
* @param channel 消息频道
* @param message 消息内容
*/
public void sendBallMessage(@NotNull BallMessageInfo messageInfo) {
sendBallMessage(messageInfo, false);
public void sendBallMessage(@NotNull String channel, @NotNull BallMessage message) {
sendBallMessage(channel, message, false);
}
/**
* 自定义服务消息信息并发送
*
* @param messageInfo 消息内容
* @param block 是否阻塞(设置为 true 则必须等待消息写入网络的操作完成后,该方法才会退出)
* @param channel 消息频道
* @param message 消息内容
* @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);
}
}
for (BallListener listener : BallAPI.getInstance().getListeners()) {
try {
listener.onMessageSend(messageInfo);
} catch (Exception | Error e) {
e.printStackTrace();
}
public void sendBallMessage(@NotNull String channel, @NotNull BallMessage message, boolean block) {
if (block) {
pubConnection.sync().publish(channel, message);
} else {
pubConnection.async().publish(channel, message);
}
}
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 subscribe(@NotNull String... channel) {
subConnection.sync().subscribe(channel);
}
public void removeListener(@NotNull BallListener listener) {
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
newListeners.remove(listener);
listeners = newListeners;
public void subscribePatterns(@NotNull String patterns) {
subConnection.sync().psubscribe(patterns);
}
public void unsubscribe(@NotNull String... channel) {
subConnection.sync().unsubscribe(channel);
}
public void unsubscribePatterns(@NotNull String patterns) {
subConnection.sync().punsubscribe(patterns);
}
/**
@@ -715,10 +623,7 @@ public abstract class BallAPI {
return playerInfo;
}
@NotNull
public List<BallListener> getListeners() {
return listeners;
}
protected abstract void onMessage(String channel, BallMessage message);
@NotNull
public abstract Logger getLogger();
@@ -732,4 +637,36 @@ public abstract class BallAPI {
public RedisClient getRedisClient() {
return redisClient;
}
private class InnerListener implements RedisPubSubListener<String, BallMessage> {
@Override
public void message(String channel, BallMessage message) {
onMessage(channel, message);
}
@Override
public void message(String pattern, String channel, BallMessage message) {
}
@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) {
}
}
}

View File

@@ -1,17 +0,0 @@
package cn.hamster3.mc.plugin.ball.common.config;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
@Data
@AllArgsConstructor
public class BallConfig {
@NotNull
private BallServerInfo localInfo;
@NotNull
private String host;
private int port;
private int eventLoopThread;
}

View File

@@ -20,22 +20,22 @@ import java.util.UUID;
/**
* 服务消息
*/
@Data
@SuppressWarnings("unused")
public class BallMessageInfo {
@Data
public class BallMessage {
/**
* 编解码器
*/
public static final RedisCodec<String, BallMessageInfo> CODEC = new RedisCodec<String, BallMessageInfo>() {
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 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));
}
};
@@ -71,11 +71,6 @@ public class BallMessageInfo {
*/
@Nullable
private BallServerType receiverType;
/**
* 消息的频道
*/
@NotNull
private String channel;
/**
* 消息动作
* <p>
@@ -90,22 +85,19 @@ public class BallMessageInfo {
*/
private JsonElement content;
public BallMessageInfo(@NotNull String channel, @NotNull String action) {
public BallMessage(@NotNull String action) {
senderID = BallAPI.getInstance().getLocalServerId();
this.channel = channel;
this.action = action;
}
public BallMessageInfo(@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 BallMessageInfo(@NotNull String channel, @NotNull String senderID, @Nullable String receiverID,
@Nullable BallServerType receiverType, @NotNull String action, @Nullable JsonElement content) {
this.channel = channel;
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;
@@ -121,7 +113,6 @@ public class BallMessageInfo {
@NotNull
public JsonObject toJson() {
JsonObject object = new JsonObject();
object.addProperty("channel", channel);
object.addProperty("senderID", senderID);
if (receiverID != null) {
object.addProperty("toServer", receiverID);

View File

@@ -0,0 +1,26 @@
package cn.hamster3.mc.plugin.ball.common.event;
public enum BallActions {
// Operate
BroadcastPlayerMessage,
DispatchConsoleCommand,
DispatchPlayerCommand,
KickPlayer,
SendMessageToPlayer,
SendPlayerToLocation,
SendPlayerToPlayer,
// Player
PlayerInfoUpdate,
PlayerPreLogin,
PlayerLogin,
PlayerPostLogin,
PlayerPreConnectServer,
PlayerConnectServer,
PlayerPostConnectServer,
PlayerLogout,
// Server
ServerOffline,
ServerOnline
}

View File

@@ -7,10 +7,7 @@ import org.jetbrains.annotations.NotNull;
@Data
@AllArgsConstructor
public class BroadcastPlayerMessageEvent {
public static final String ACTION = "BroadcastPlayerMessage";
public class BroadcastPlayerMessage {
@NotNull
private final DisplayMessage message;
}
}

View File

@@ -8,13 +8,11 @@ import org.jetbrains.annotations.Nullable;
@Data
@AllArgsConstructor
public class DispatchConsoleCommandEvent {
public static final String ACTION = "DispatchConsoleCommand";
public class DispatchConsoleCommand {
@Nullable
private final BallServerType type;
private final BallServerType serverType;
@Nullable
private final String serverID;
@NotNull
private final String command;
}
}

View File

@@ -10,13 +10,11 @@ import java.util.UUID;
@Data
@AllArgsConstructor
public class DispatchPlayerCommandEvent {
public static final String ACTION = "DispatchPlayerCommand";
public class DispatchPlayerCommand {
@Nullable
private final BallServerType type;
private final BallServerType serverType;
@Nullable
private final UUID uuid;
@NotNull
private final String command;
}
}

View File

@@ -9,12 +9,9 @@ import java.util.UUID;
@Data
@AllArgsConstructor
public class KickPlayerEvent {
public static final String ACTION = "KickPlayer";
public class KickPlayer {
@NotNull
private final UUID uuid;
@NotNull
private final Component reason;
}
}

View File

@@ -10,11 +10,9 @@ import java.util.UUID;
@Data
@AllArgsConstructor
public class SendMessageToPlayerEvent {
public static final String ACTION = "SendMessageToPlayer";
public class SendMessageToPlayer {
@NotNull
private final Set<UUID> receivers;
@NotNull
private final DisplayMessage message;
}
}

View File

@@ -12,14 +12,11 @@ import java.util.UUID;
@Data
@AllArgsConstructor
public class SendPlayerToLocationEvent {
public static final String ACTION = "SendPlayerToLocation";
public class SendPlayerToLocation {
@NotNull
private final Set<UUID> sendPlayerUUID;
private final Set<UUID> sendPlayer;
@NotNull
private final BallLocation location;
@Nullable
private final DisplayMessage doneMessage;
}
}

View File

@@ -11,16 +11,13 @@ import java.util.UUID;
@Data
@AllArgsConstructor
public class SendPlayerToPlayerEvent {
public static final String ACTION = "SendPlayerToPlayer";
public class SendPlayerToPlayer {
@NotNull
private final Set<UUID> sendPlayerUUID;
private final Set<UUID> sendPlayer;
@NotNull
private final UUID toPlayerUUID;
private final UUID toPlayer;
@Nullable
private final DisplayMessage doneMessage;
@Nullable
private final DisplayMessage doneTargetMessage;
}
}

View File

@@ -11,19 +11,17 @@ import org.jetbrains.annotations.Nullable;
* <p>
* 仅在使用 velocity 代理端时才会触发这个事件
*
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
* @see BallPlayerConnectServerEvent 玩家进入子服
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
* @see BallPlayerPreConnectServer 玩家准备进入子服
* @see BallPlayerConnectServer 玩家进入子服
* @see BallPlayerPostConnectServer 玩家已经进入子服
*/
@Data
@AllArgsConstructor
public class BallPlayerConnectServerEvent {
public static final String ACTION = "PlayerConnectServer";
public class BallPlayerConnectServer {
@NotNull
private final BallPlayerInfo playerInfo;
@Nullable
private final String from;
@NotNull
private final String to;
}
}

View File

@@ -10,9 +10,7 @@ import org.jetbrains.annotations.NotNull;
*/
@Data
@AllArgsConstructor
public class BallPlayerInfoUpdateEvent {
public static final String ACTION = "PlayerInfoUpdateEvent";
public class BallPlayerInfoUpdate {
@NotNull
private final BallPlayerInfo playerInfo;
}
}

View File

@@ -10,10 +10,7 @@ import org.jetbrains.annotations.NotNull;
*/
@Data
@AllArgsConstructor
public class BallPlayerLoginEvent {
public static final String ACTION = "PlayerLogin";
public class BallPlayerLogin {
@NotNull
private final BallPlayerInfo playerInfo;
}
}

View File

@@ -10,9 +10,7 @@ import org.jetbrains.annotations.NotNull;
*/
@Data
@AllArgsConstructor
public class BallPlayerLogoutEvent {
public static final String ACTION = "PlayerLogout";
public class BallPlayerLogout {
@NotNull
private BallPlayerInfo playerInfo;
}
}

View File

@@ -8,15 +8,17 @@ import org.jetbrains.annotations.NotNull;
/**
* 玩家已经进入子服
*
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
* @see BallPlayerConnectServerEvent 玩家进入子服
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
* @see BallPlayerPreConnectServer 玩家准备进入子服
* @see BallPlayerConnectServer 玩家进入子服
* @see BallPlayerPostConnectServer 玩家已经进入子服
*/
@Data
@AllArgsConstructor
public class BallPlayerPostConnectServerEvent {
public static final String ACTION = "PlayerPostConnectServer";
public class BallPlayerPostConnectServer {
@NotNull
private final BallPlayerInfo playerInfo;
}
@NotNull
private final String from;
@NotNull
private final String to;
}

View File

@@ -10,9 +10,7 @@ import org.jetbrains.annotations.NotNull;
*/
@Data
@AllArgsConstructor
public class BallPlayerPostLoginEvent {
public static final String ACTION = "PlayerPostLogin";
public class BallPlayerPostLogin {
@NotNull
private final BallPlayerInfo playerInfo;
}
}

View File

@@ -9,20 +9,17 @@ import org.jetbrains.annotations.Nullable;
/**
* 玩家准备进入子服
*
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
* @see BallPlayerConnectServerEvent 玩家进入子服
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
* @see BallPlayerPreConnectServer 玩家准备进入子服
* @see BallPlayerConnectServer 玩家进入子服
* @see BallPlayerPostConnectServer 玩家已经进入子服
*/
@Data
@AllArgsConstructor
public class BallPlayerPreConnectServerEvent {
public static final String ACTION = "PlayerPreConnectServer";
public class BallPlayerPreConnectServer {
@NotNull
private final BallPlayerInfo playerInfo;
@Nullable
private final String from;
@NotNull
private final String to;
}
}

View File

@@ -9,9 +9,7 @@ import org.jetbrains.annotations.NotNull;
*/
@Data
@AllArgsConstructor
public class BallPlayerPreLoginEvent {
public static final String ACTION = "PlayerPreLogin";
public class BallPlayerPreLogin {
@NotNull
private final String playerName;
}
}

View File

@@ -1,5 +1,6 @@
package cn.hamster3.mc.plugin.ball.common.event.server;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
@@ -9,9 +10,7 @@ import org.jetbrains.annotations.NotNull;
*/
@Data
@AllArgsConstructor
public class ServerOfflineEvent {
public static final String ACTION = "ServerOffline";
public class ServerOffline {
@NotNull
private final String serverID;
}
private final BallServerInfo serverInfo;
}

View File

@@ -10,10 +10,7 @@ import org.jetbrains.annotations.NotNull;
*/
@Data
@AllArgsConstructor
public class ServerOnlineEvent {
public static final String ACTION = "ServerOnline";
public class ServerOnline {
@NotNull
private final BallServerInfo serverInfo;
}
}

View File

@@ -1,34 +1,33 @@
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 static final BallDebugListener INSTANCE = new BallDebugListener();
public class BallDebugLogListener implements RedisPubSubListener<String, BallMessage> {
public static final BallDebugLogListener INSTANCE = new BallDebugLogListener();
private BallDebugListener() {
private BallDebugLogListener() {
}
@Override
public void message(String channel, BallMessageInfo event) {
BallAPI.getInstance().getLogger().info("" + channel + " 收到了一条消息: " + event);
public void message(String channel, BallMessage message) {
BallAPI.getInstance().getLogger().info("" + channel + " 收到了一条消息: " + message);
}
@Override
public void message(String pattern, String channel, BallMessageInfo event) {
BallAPI.getInstance().getLogger().info("" + pattern + "(" + channel + ") 收到了一条消息: " + event);
public void message(String pattern, String channel, BallMessage message) {
BallAPI.getInstance().getLogger().info("" + channel + "(" + pattern + ") 收到了一条消息: " + message);
}
@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);
BallAPI.getInstance().getLogger().info("已订阅 redis 频道(正则): " + pattern);
}
@Override

View File

@@ -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) {
}
}

View File

@@ -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) {
}
}

View File

@@ -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;
}
}