@@ -1,20 +1,23 @@
|
||||
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.BallMessage;
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.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.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.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.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;
|
||||
@@ -42,32 +45,111 @@ 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;
|
||||
pubConnection = redisClient.connectPubSub(BallMessage.REDIS_CODEC);
|
||||
subConnection = redisClient.connectPubSub(BallMessage.REDIS_CODEC);
|
||||
pubSubConnection = redisClient.connectPubSub(BallMessageInfo.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("已启用调试模式");
|
||||
subConnection.addListener(BallDebugLogListener.INSTANCE);
|
||||
pubSubConnection.addListener(BallDebugListener.INSTANCE);
|
||||
}
|
||||
subConnection.addListener(new InnerListener());
|
||||
}
|
||||
|
||||
protected void enable() throws SQLException, InterruptedException {
|
||||
if (enabled) {
|
||||
return;
|
||||
}
|
||||
BallServerInfo localInfo = getLocalServerInfo();
|
||||
|
||||
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
||||
@@ -117,7 +199,6 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
}
|
||||
getLogger().info("已加载 " + serverInfo.size() + " 条服务器信息");
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"SELECT * FROM `hamster_ball_player_info`;"
|
||||
)) {
|
||||
@@ -133,15 +214,15 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
}
|
||||
getLogger().info("已加载 " + playerInfo.size() + " 条玩家信息");
|
||||
}
|
||||
|
||||
subscribe(BALL_CHANNEL);
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(BallActions.ServerOnline.name(), new ServerOnline(getLocalServerInfo())), true);
|
||||
RedisClient client = getRedisClient();
|
||||
pubSubConnection.addListener(BallMessageListener.INSTANCE);
|
||||
pubSubConnection.async().subscribe("HamsterBall");
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
protected void disable() throws SQLException, InterruptedException {
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(BallActions.ServerOffline.name(), new ServerOffline(getLocalServerInfo())), true);
|
||||
sendBallMessage(new BallMessageInfo(BALL_CHANNEL, ServerOfflineEvent.ACTION, new ServerOfflineEvent(getLocalServerId())), true);
|
||||
|
||||
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
@@ -158,9 +239,9 @@ public abstract class BallAPI {
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
||||
getLogger().info("正在关闭 redis 客户端");
|
||||
getLogger().info("正在关闭 redission");
|
||||
getRedisClient().close();
|
||||
getLogger().info("已关闭 redis 客户端");
|
||||
getLogger().info("已关闭 redission");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,13 +279,14 @@ public abstract class BallAPI {
|
||||
* @param message 消息
|
||||
*/
|
||||
public void broadcastPlayerMessage(@NotNull DisplayMessage message) {
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
BallActions.BroadcastPlayerMessage.name(),
|
||||
BroadcastPlayerMessageEvent.ACTION,
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new BroadcastPlayerMessage(message)
|
||||
new BroadcastPlayerMessageEvent(message)
|
||||
)
|
||||
));
|
||||
}
|
||||
@@ -217,13 +299,14 @@ public abstract class BallAPI {
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchConsoleCommand(@Nullable BallServerType type, @Nullable String serverID, @NotNull String command) {
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
BallActions.DispatchConsoleCommand.name(),
|
||||
DispatchConsoleCommandEvent.ACTION,
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new DispatchConsoleCommand(type, serverID, command)
|
||||
new DispatchConsoleCommandEvent(type, serverID, command)
|
||||
)
|
||||
));
|
||||
}
|
||||
@@ -236,13 +319,14 @@ public abstract class BallAPI {
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchPlayerCommand(@Nullable BallServerType type, @Nullable UUID uuid, @NotNull String command) {
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
BallActions.DispatchPlayerCommand.name(),
|
||||
DispatchPlayerCommandEvent.ACTION,
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new DispatchPlayerCommand(type, uuid, command)
|
||||
new DispatchPlayerCommandEvent(type, uuid, command)
|
||||
)
|
||||
));
|
||||
}
|
||||
@@ -264,13 +348,14 @@ public abstract class BallAPI {
|
||||
* @param reason 原因
|
||||
*/
|
||||
public void kickPlayer(@NotNull UUID uuid, @NotNull Component reason) {
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
BallActions.KickPlayer.name(),
|
||||
KickPlayerEvent.ACTION,
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new KickPlayer(uuid, reason)
|
||||
new KickPlayerEvent(uuid, reason)
|
||||
)
|
||||
));
|
||||
}
|
||||
@@ -301,13 +386,14 @@ public abstract class BallAPI {
|
||||
}
|
||||
return;
|
||||
}
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
BallActions.SendMessageToPlayer.name(),
|
||||
SendMessageToPlayerEvent.ACTION,
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new SendMessageToPlayer(Collections.singleton(receiver), message)
|
||||
new SendMessageToPlayerEvent(Collections.singleton(receiver), message)
|
||||
)
|
||||
));
|
||||
}
|
||||
@@ -339,13 +425,14 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
}
|
||||
sendBallMessage(BALL_CHANNEL, new BallMessage(
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
BallActions.SendMessageToPlayer.name(),
|
||||
SendMessageToPlayerEvent.ACTION,
|
||||
CoreAPI.getInstance().getGson().toJsonTree(
|
||||
new SendMessageToPlayer(new HashSet<>(receivers), message)
|
||||
new SendMessageToPlayerEvent(new HashSet<>(receivers), message)
|
||||
)
|
||||
));
|
||||
}
|
||||
@@ -357,15 +444,15 @@ public abstract class BallAPI {
|
||||
* <p>
|
||||
* 则会先尝试将玩家连接至目标服务器再进行传送
|
||||
*
|
||||
* @param sendPlayer 玩家的uuid
|
||||
* @param location 坐标
|
||||
* @param doneMessage 传送完成后显示的消息
|
||||
* @param sendPlayerUUID 玩家的uuid
|
||||
* @param location 坐标
|
||||
* @param doneMessage 传送完成后显示的消息
|
||||
*/
|
||||
public void sendPlayerToLocation(@NotNull UUID sendPlayer, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
|
||||
public void sendPlayerToLocation(@NotNull UUID sendPlayerUUID, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
BallActions.SendPlayerToLocation.name(),
|
||||
new SendPlayerToLocation(Collections.singleton(sendPlayer), location, doneMessage)
|
||||
SendPlayerToLocationEvent.ACTION,
|
||||
new SendPlayerToLocationEvent(Collections.singleton(sendPlayerUUID), location, doneMessage)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -376,15 +463,15 @@ public abstract class BallAPI {
|
||||
* <p>
|
||||
* 则会先尝试将玩家连接至目标服务器再进行传送
|
||||
*
|
||||
* @param sendPlayer 玩家的uuid
|
||||
* @param location 坐标
|
||||
* @param doneMessage 传送完成后显示的消息
|
||||
* @param sendPlayerUUID 玩家的uuid
|
||||
* @param location 坐标
|
||||
* @param doneMessage 传送完成后显示的消息
|
||||
*/
|
||||
public void sendPlayerToLocation(@NotNull Collection<UUID> sendPlayer, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
|
||||
public void sendPlayerToLocation(@NotNull Collection<UUID> sendPlayerUUID, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
BallActions.SendPlayerToLocation.name(),
|
||||
new SendPlayerToLocation(new HashSet<>(sendPlayer), location, doneMessage)
|
||||
SendPlayerToLocationEvent.ACTION,
|
||||
new SendPlayerToLocationEvent(new HashSet<>(sendPlayerUUID), location, doneMessage)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -401,8 +488,8 @@ public abstract class BallAPI {
|
||||
public void sendPlayerToPlayer(@NotNull UUID sendPlayer, @NotNull UUID toPlayer, @Nullable DisplayMessage doneMessage, @Nullable DisplayMessage doneTargetMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
BallActions.SendPlayerToPlayer.name(),
|
||||
new SendPlayerToPlayer(Collections.singleton(sendPlayer), toPlayer, doneMessage, doneTargetMessage)
|
||||
SendPlayerToPlayerEvent.ACTION,
|
||||
new SendPlayerToPlayerEvent(Collections.singleton(sendPlayer), toPlayer, doneMessage, doneTargetMessage)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -411,16 +498,16 @@ public abstract class BallAPI {
|
||||
* <p>
|
||||
* 支持跨服传送
|
||||
*
|
||||
* @param sendPlayer 被传送的玩家
|
||||
* @param sendPlayerUUID 被传送的玩家
|
||||
* @param toPlayer 传送的目标玩家
|
||||
* @param doneMessage 传送完成后显示的消息,自动将 %player_name% 替换成传送目标玩家的名称
|
||||
* @param doneTargetMessage 传送完成后目标玩家显示的消息,自动将 %player_name% 替换成被传送者的名称
|
||||
*/
|
||||
public void sendPlayerToPlayer(@NotNull Collection<UUID> sendPlayer, @NotNull UUID toPlayer, @Nullable DisplayMessage doneMessage, @Nullable DisplayMessage doneTargetMessage) {
|
||||
public void sendPlayerToPlayer(@NotNull Collection<UUID> sendPlayerUUID, @NotNull UUID toPlayer, @Nullable DisplayMessage doneMessage, @Nullable DisplayMessage doneTargetMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
BallActions.SendPlayerToPlayer.name(),
|
||||
new SendPlayerToPlayer(new HashSet<>(sendPlayer), toPlayer, doneMessage, doneTargetMessage)
|
||||
SendPlayerToPlayerEvent.ACTION,
|
||||
new SendPlayerToPlayerEvent(new HashSet<>(sendPlayerUUID), toPlayer, doneMessage, doneTargetMessage)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -431,7 +518,7 @@ public abstract class BallAPI {
|
||||
* @param action 执行动作
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action) {
|
||||
sendBallMessage(channel, new BallMessage(action));
|
||||
sendBallMessage(new BallMessageInfo(channel, action));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,48 +529,53 @@ public abstract class BallAPI {
|
||||
* @param content 附加参数
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action, @NotNull Object content) {
|
||||
sendBallMessage(channel, new BallMessage(action, content));
|
||||
sendBallMessage(new BallMessageInfo(channel, action, content));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送自定义消息
|
||||
*
|
||||
* @param channel 消息频道
|
||||
* @param message 消息内容
|
||||
* @param messageInfo 消息内容
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull BallMessage message) {
|
||||
sendBallMessage(channel, message, false);
|
||||
public void sendBallMessage(@NotNull BallMessageInfo messageInfo) {
|
||||
sendBallMessage(messageInfo, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义服务消息信息并发送
|
||||
*
|
||||
* @param channel 消息频道
|
||||
* @param message 消息内容
|
||||
* @param block 是否阻塞(设置为 true 则必须等待消息写入网络的操作完成后,该方法才会退出)
|
||||
* @param messageInfo 消息内容
|
||||
* @param block 是否阻塞(设置为 true 则必须等待消息写入网络的操作完成后,该方法才会退出)
|
||||
*/
|
||||
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 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 subscribe(@NotNull String... channel) {
|
||||
subConnection.sync().subscribe(channel);
|
||||
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 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);
|
||||
public void removeListener(@NotNull BallListener listener) {
|
||||
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
|
||||
newListeners.remove(listener);
|
||||
listeners = newListeners;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -623,7 +715,10 @@ public abstract class BallAPI {
|
||||
return playerInfo;
|
||||
}
|
||||
|
||||
protected abstract void onMessage(String channel, BallMessage message);
|
||||
@NotNull
|
||||
public List<BallListener> getListeners() {
|
||||
return listeners;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public abstract Logger getLogger();
|
||||
@@ -637,36 +732,4 @@ 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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
@@ -20,22 +20,22 @@ import java.util.UUID;
|
||||
/**
|
||||
* 服务消息
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@Data
|
||||
public class BallMessage {
|
||||
@SuppressWarnings("unused")
|
||||
public class BallMessageInfo {
|
||||
/**
|
||||
* 编解码器
|
||||
*/
|
||||
public static final RedisCodec<String, BallMessage> REDIS_CODEC = new RedisCodec<String, BallMessage>() {
|
||||
public static final RedisCodec<String, BallMessageInfo> CODEC = new RedisCodec<String, BallMessageInfo>() {
|
||||
@Override
|
||||
public String decodeKey(ByteBuffer bytes) {
|
||||
return StringCodec.UTF8.decodeKey(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BallMessage decodeValue(ByteBuffer bytes) {
|
||||
public BallMessageInfo decodeValue(ByteBuffer bytes) {
|
||||
String string = StringCodec.UTF8.decodeValue(bytes);
|
||||
return CoreAPI.getInstance().getGson().fromJson(string, BallMessage.class);
|
||||
return CoreAPI.getInstance().getGson().fromJson(string, BallMessageInfo.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,7 +44,7 @@ public class BallMessage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer encodeValue(BallMessage value) {
|
||||
public ByteBuffer encodeValue(BallMessageInfo value) {
|
||||
return StringCodec.UTF8.encodeValue(CoreAPI.getInstance().getGson().toJson(value));
|
||||
}
|
||||
};
|
||||
@@ -71,6 +71,11 @@ public class BallMessage {
|
||||
*/
|
||||
@Nullable
|
||||
private BallServerType receiverType;
|
||||
/**
|
||||
* 消息的频道
|
||||
*/
|
||||
@NotNull
|
||||
private String channel;
|
||||
/**
|
||||
* 消息动作
|
||||
* <p>
|
||||
@@ -85,19 +90,22 @@ public class BallMessage {
|
||||
*/
|
||||
private JsonElement content;
|
||||
|
||||
public BallMessage(@NotNull String action) {
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String action) {
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.channel = channel;
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public BallMessage(@NotNull String action, @NotNull Object content) {
|
||||
public BallMessageInfo(@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 BallMessage(@NotNull String senderID, @Nullable String receiverID,
|
||||
@Nullable BallServerType receiverType, @NotNull String action, @Nullable JsonElement content) {
|
||||
public BallMessageInfo(@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;
|
||||
this.receiverType = receiverType;
|
||||
@@ -113,6 +121,7 @@ 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);
|
@@ -1,26 +0,0 @@
|
||||
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
|
||||
}
|
@@ -7,7 +7,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BroadcastPlayerMessage {
|
||||
public class BroadcastPlayerMessageEvent {
|
||||
public static final String ACTION = "BroadcastPlayerMessage";
|
||||
|
||||
@NotNull
|
||||
private final DisplayMessage message;
|
||||
}
|
||||
|
||||
}
|
@@ -8,11 +8,13 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class DispatchConsoleCommand {
|
||||
public class DispatchConsoleCommandEvent {
|
||||
public static final String ACTION = "DispatchConsoleCommand";
|
||||
|
||||
@Nullable
|
||||
private final BallServerType serverType;
|
||||
private final BallServerType type;
|
||||
@Nullable
|
||||
private final String serverID;
|
||||
@NotNull
|
||||
private final String command;
|
||||
}
|
||||
}
|
@@ -10,11 +10,13 @@ import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class DispatchPlayerCommand {
|
||||
public class DispatchPlayerCommandEvent {
|
||||
public static final String ACTION = "DispatchPlayerCommand";
|
||||
|
||||
@Nullable
|
||||
private final BallServerType serverType;
|
||||
private final BallServerType type;
|
||||
@Nullable
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
private final String command;
|
||||
}
|
||||
}
|
@@ -9,9 +9,12 @@ import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class KickPlayer {
|
||||
public class KickPlayerEvent {
|
||||
public static final String ACTION = "KickPlayer";
|
||||
|
||||
@NotNull
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
private final Component reason;
|
||||
}
|
||||
|
||||
}
|
@@ -10,9 +10,11 @@ import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendMessageToPlayer {
|
||||
public class SendMessageToPlayerEvent {
|
||||
public static final String ACTION = "SendMessageToPlayer";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> receivers;
|
||||
@NotNull
|
||||
private final DisplayMessage message;
|
||||
}
|
||||
}
|
@@ -12,11 +12,14 @@ import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendPlayerToLocation {
|
||||
public class SendPlayerToLocationEvent {
|
||||
public static final String ACTION = "SendPlayerToLocation";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> sendPlayer;
|
||||
private final Set<UUID> sendPlayerUUID;
|
||||
@NotNull
|
||||
private final BallLocation location;
|
||||
@Nullable
|
||||
private final DisplayMessage doneMessage;
|
||||
}
|
||||
|
||||
}
|
@@ -11,13 +11,16 @@ import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendPlayerToPlayer {
|
||||
public class SendPlayerToPlayerEvent {
|
||||
public static final String ACTION = "SendPlayerToPlayer";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> sendPlayer;
|
||||
private final Set<UUID> sendPlayerUUID;
|
||||
@NotNull
|
||||
private final UUID toPlayer;
|
||||
private final UUID toPlayerUUID;
|
||||
@Nullable
|
||||
private final DisplayMessage doneMessage;
|
||||
@Nullable
|
||||
private final DisplayMessage doneTargetMessage;
|
||||
}
|
||||
|
||||
}
|
@@ -11,17 +11,19 @@ import org.jetbrains.annotations.Nullable;
|
||||
* <p>
|
||||
* 仅在使用 velocity 代理端时才会触发这个事件
|
||||
*
|
||||
* @see BallPlayerPreConnectServer 玩家准备进入子服
|
||||
* @see BallPlayerConnectServer 玩家进入子服
|
||||
* @see BallPlayerPostConnectServer 玩家已经进入子服
|
||||
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
|
||||
* @see BallPlayerConnectServerEvent 玩家进入子服
|
||||
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerConnectServer {
|
||||
public class BallPlayerConnectServerEvent {
|
||||
public static final String ACTION = "PlayerConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
@Nullable
|
||||
private final String from;
|
||||
@NotNull
|
||||
private final String to;
|
||||
}
|
||||
}
|
@@ -10,7 +10,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerInfoUpdate {
|
||||
public class BallPlayerInfoUpdateEvent {
|
||||
public static final String ACTION = "PlayerInfoUpdateEvent";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
||||
}
|
@@ -10,7 +10,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerLogin {
|
||||
public class BallPlayerLoginEvent {
|
||||
public static final String ACTION = "PlayerLogin";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
||||
|
||||
}
|
@@ -10,7 +10,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerLogout {
|
||||
public class BallPlayerLogoutEvent {
|
||||
public static final String ACTION = "PlayerLogout";
|
||||
|
||||
@NotNull
|
||||
private BallPlayerInfo playerInfo;
|
||||
}
|
||||
}
|
@@ -8,17 +8,15 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* 玩家已经进入子服
|
||||
*
|
||||
* @see BallPlayerPreConnectServer 玩家准备进入子服
|
||||
* @see BallPlayerConnectServer 玩家进入子服
|
||||
* @see BallPlayerPostConnectServer 玩家已经进入子服
|
||||
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
|
||||
* @see BallPlayerConnectServerEvent 玩家进入子服
|
||||
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPostConnectServer {
|
||||
public class BallPlayerPostConnectServerEvent {
|
||||
public static final String ACTION = "PlayerPostConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
@NotNull
|
||||
private final String from;
|
||||
@NotNull
|
||||
private final String to;
|
||||
}
|
||||
}
|
@@ -10,7 +10,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPostLogin {
|
||||
public class BallPlayerPostLoginEvent {
|
||||
public static final String ACTION = "PlayerPostLogin";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
||||
}
|
@@ -9,17 +9,20 @@ import org.jetbrains.annotations.Nullable;
|
||||
/**
|
||||
* 玩家准备进入子服
|
||||
*
|
||||
* @see BallPlayerPreConnectServer 玩家准备进入子服
|
||||
* @see BallPlayerConnectServer 玩家进入子服
|
||||
* @see BallPlayerPostConnectServer 玩家已经进入子服
|
||||
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
|
||||
* @see BallPlayerConnectServerEvent 玩家进入子服
|
||||
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPreConnectServer {
|
||||
public class BallPlayerPreConnectServerEvent {
|
||||
public static final String ACTION = "PlayerPreConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
@Nullable
|
||||
private final String from;
|
||||
@NotNull
|
||||
private final String to;
|
||||
}
|
||||
|
||||
}
|
@@ -9,7 +9,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPreLogin {
|
||||
public class BallPlayerPreLoginEvent {
|
||||
public static final String ACTION = "PlayerPreLogin";
|
||||
|
||||
@NotNull
|
||||
private final String playerName;
|
||||
}
|
||||
}
|
@@ -1,6 +1,5 @@
|
||||
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;
|
||||
@@ -10,7 +9,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOffline {
|
||||
public class ServerOfflineEvent {
|
||||
public static final String ACTION = "ServerOffline";
|
||||
|
||||
@NotNull
|
||||
private final BallServerInfo serverInfo;
|
||||
}
|
||||
private final String serverID;
|
||||
}
|
@@ -10,7 +10,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOnline {
|
||||
public class ServerOnlineEvent {
|
||||
public static final String ACTION = "ServerOnline";
|
||||
|
||||
@NotNull
|
||||
private final BallServerInfo serverInfo;
|
||||
}
|
||||
|
||||
}
|
@@ -1,33 +1,34 @@
|
||||
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.data.BallMessageInfo;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
public class BallDebugLogListener implements RedisPubSubListener<String, BallMessage> {
|
||||
public static final BallDebugLogListener INSTANCE = new BallDebugLogListener();
|
||||
public class BallDebugListener implements RedisPubSubListener<String, BallMessageInfo> {
|
||||
public static final BallDebugListener INSTANCE = new BallDebugListener();
|
||||
|
||||
private BallDebugLogListener() {
|
||||
private BallDebugListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String channel, BallMessage message) {
|
||||
BallAPI.getInstance().getLogger().info("从 " + channel + " 收到了一条消息: " + message);
|
||||
public void message(String channel, BallMessageInfo event) {
|
||||
BallAPI.getInstance().getLogger().info("从 " + channel + " 收到了一条消息: " + event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, BallMessage message) {
|
||||
BallAPI.getInstance().getLogger().info("从 " + channel + "(" + pattern + ") 收到了一条消息: " + message);
|
||||
public void message(String pattern, String channel, BallMessageInfo 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);
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则): " + pattern);
|
||||
}
|
||||
|
||||
@Override
|
@@ -0,0 +1,55 @@
|
||||
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) {
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,167 @@
|
||||
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,45 @@
|
||||
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