feat: 适配新版API更新
This commit is contained in:
19
ball-common/build.gradle
Normal file
19
ball-common/build.gradle
Normal file
@@ -0,0 +1,19 @@
|
||||
setArchivesBaseName("HamsterBall-Common")
|
||||
|
||||
dependencies {
|
||||
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||
//noinspection GradlePackageUpdate
|
||||
//noinspection VulnerableLibrariesLocal
|
||||
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||
// https://mvnrepository.com/artifact/io.netty/netty-all
|
||||
compileOnly 'io.netty:netty-all:4.1.86.Final'
|
||||
|
||||
compileOnly "cn.hamster3.mc.plugin:core-common:${hamster_core_version}"
|
||||
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
@@ -0,0 +1,849 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.api;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
|
||||
import cn.hamster3.mc.plugin.ball.common.connector.BallChannelInitializer;
|
||||
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.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.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.BallListener;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.ListenerPriority;
|
||||
import cn.hamster3.mc.plugin.ball.common.utils.OS;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class BallAPI {
|
||||
/**
|
||||
* API 使用的通信频道
|
||||
*/
|
||||
public static final String BALL_CHANNEL = "HamsterBall";
|
||||
/**
|
||||
* API 实例
|
||||
*/
|
||||
protected static BallAPI instance;
|
||||
@NotNull
|
||||
protected final Map<String, BallServerInfo> serverInfo;
|
||||
@NotNull
|
||||
protected final Map<UUID, BallPlayerInfo> playerInfo;
|
||||
|
||||
@NotNull
|
||||
private final BallConfig config;
|
||||
|
||||
@NotNull
|
||||
private final Bootstrap bootstrap;
|
||||
@NotNull
|
||||
private final EventLoopGroup executors;
|
||||
protected Channel channel;
|
||||
|
||||
private boolean enabled;
|
||||
private boolean connected;
|
||||
|
||||
@NotNull
|
||||
private List<BallListener> listeners;
|
||||
|
||||
protected BallAPI(@NotNull BallConfig config) {
|
||||
this.config = config;
|
||||
this.enabled = false;
|
||||
this.connected = false;
|
||||
|
||||
serverInfo = new ConcurrentHashMap<>();
|
||||
playerInfo = new ConcurrentHashMap<>();
|
||||
listeners = new ArrayList<>();
|
||||
|
||||
OS currentOS = OS.getCurrentOS();
|
||||
getLogger().info(String.format(
|
||||
"当前操作系统为: %s. 选择 IO 模式为: %s",
|
||||
currentOS.name(), currentOS.getIOModeName()
|
||||
));
|
||||
|
||||
executors = currentOS.getEventLoopGroup(config.getEventLoopThread());
|
||||
bootstrap = new Bootstrap()
|
||||
.group(executors)
|
||||
.channel(currentOS.getSocketChannel())
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.handler(new BallChannelInitializer());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectRefused() {
|
||||
enabled = false;
|
||||
connected = false;
|
||||
executors.shutdownGracefully();
|
||||
getLogger().info("连接至服务中心的请求被拒绝,已关闭仓鼠球。");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 API 实例
|
||||
*
|
||||
* @return API 实例
|
||||
*/
|
||||
public static BallAPI getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
protected void enable() throws SQLException, InterruptedException {
|
||||
if (enabled) {
|
||||
return;
|
||||
}
|
||||
enabled = true;
|
||||
BallServerInfo localInfo = getLocalServerInfo();
|
||||
|
||||
connect();
|
||||
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_player_info`(" +
|
||||
"`uuid` CHAR(36) PRIMARY KEY," +
|
||||
"`name` VARCHAR(16) NOT NULL," +
|
||||
"`game_server` VARCHAR(32) NOT NULL," +
|
||||
"`proxy_server` VARCHAR(32) NOT NULL," +
|
||||
"`online` BOOLEAN NOT NULL" +
|
||||
") CHARSET utf8mb4;");
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_server_info`(" +
|
||||
"`id` VARCHAR(32) PRIMARY KEY NOT NULL," +
|
||||
"`name` VARCHAR(32) NOT NULL," +
|
||||
"`type` VARCHAR(16) NOT NULL," +
|
||||
"`host` VARCHAR(32) NOT NULL," +
|
||||
"`port` INT NOT NULL" +
|
||||
") CHARSET utf8mb4;");
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_cached_message`(" +
|
||||
"`uuid` CHAR(36) NOT NULL," +
|
||||
"`message` TEXT NOT NULL" +
|
||||
") CHARSET utf8mb4;");
|
||||
}
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"REPLACE INTO `hamster_ball_server_info` VALUES(?, ?, ?, ?, ?);"
|
||||
)) {
|
||||
statement.setString(1, localInfo.getId());
|
||||
statement.setString(2, localInfo.getName());
|
||||
statement.setString(3, localInfo.getType().name());
|
||||
statement.setString(4, localInfo.getHost());
|
||||
statement.setInt(5, localInfo.getPort());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"SELECT * FROM `hamster_ball_server_info`;"
|
||||
)) {
|
||||
try (ResultSet set = statement.executeQuery()) {
|
||||
while (set.next()) {
|
||||
String serverID = set.getString("id");
|
||||
serverInfo.put(serverID, new BallServerInfo(
|
||||
serverID,
|
||||
set.getString("name"),
|
||||
BallServerType.valueOf(set.getString("type")),
|
||||
set.getString("host"),
|
||||
set.getInt("port")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"SELECT * FROM `hamster_ball_player_info`;"
|
||||
)) {
|
||||
try (ResultSet set = statement.executeQuery()) {
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||
playerInfo.put(uuid, new BallPlayerInfo(uuid,
|
||||
set.getString("name"),
|
||||
set.getString("game_server"),
|
||||
set.getString("proxy_server"),
|
||||
set.getBoolean("online")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void connect() throws InterruptedException {
|
||||
if (!enabled) {
|
||||
getLogger().info("仓鼠球已关闭,拒绝启动连接!");
|
||||
return;
|
||||
}
|
||||
getLogger().info("准备连接至仓鼠球服务中心!");
|
||||
ChannelFuture future = bootstrap.connect(config.getHost(), config.getPort()).await();
|
||||
if (future.isSuccess()) {
|
||||
channel = future.channel();
|
||||
connected = true;
|
||||
for (BallListener listener : listeners) {
|
||||
listener.onConnectActive();
|
||||
}
|
||||
getLogger().info("已连接至仓鼠球服务中心!");
|
||||
} else {
|
||||
getLogger().warning("连接至仓鼠球服务中心失败!");
|
||||
}
|
||||
}
|
||||
|
||||
public void reconnect(int tryCount) {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
if (channel != null && channel.isOpen() && channel.isRegistered() && channel.isActive() && channel.isWritable()) {
|
||||
return;
|
||||
}
|
||||
channel = null;
|
||||
connected = false;
|
||||
if (tryCount <= 0) {
|
||||
for (BallListener listener : getListeners()) {
|
||||
try {
|
||||
listener.onServiceDead();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
connect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (channel != null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
reconnect(tryCount - 1);
|
||||
}
|
||||
|
||||
protected void disable() throws SQLException, InterruptedException {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
enabled = false;
|
||||
|
||||
sendBallMessage(new BallMessageInfo(BALL_CHANNEL, ServerOfflineEvent.ACTION, new ServerOfflineEvent(getLocalServerId())), true);
|
||||
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"DELETE FROM `hamster_ball_server_info` WHERE `id`=?;"
|
||||
)) {
|
||||
statement.setString(1, getLocalServerId());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"UPDATE `hamster_ball_player_info` SET `online`=false WHERE `game_server`=? OR `proxy_server`=?"
|
||||
)) {
|
||||
statement.setString(1, getLocalServerId());
|
||||
statement.setString(2, getLocalServerId());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
channel = null;
|
||||
connected = false;
|
||||
executors.shutdownGracefully().await();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断该服务器信息是否为本服
|
||||
*
|
||||
* @param info 服务器信息
|
||||
* @return true 代表该服务器信息是本服服务器
|
||||
*/
|
||||
public boolean isLocalServer(@NotNull BallServerInfo info) {
|
||||
return getLocalServerInfo().equals(info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断该服务器信息是否为本服
|
||||
*
|
||||
* @param serverID 服务器ID
|
||||
* @return true 代表该服务器信息是本服服务器
|
||||
*/
|
||||
public boolean isLocalServer(@Nullable String serverID) {
|
||||
return getLocalServerId().equalsIgnoreCase(serverID);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给服务器的在线玩家广播一条消息
|
||||
*
|
||||
* @param message 消息
|
||||
*/
|
||||
public void broadcastPlayerMessage(@NotNull String message) {
|
||||
broadcastPlayerMessage(new DisplayMessage().setMessage(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* 给服务器的在线玩家广播一条消息
|
||||
*
|
||||
* @param message 消息
|
||||
*/
|
||||
public void broadcastPlayerMessage(@NotNull DisplayMessage message) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
BroadcastPlayerMessageEvent.ACTION,
|
||||
CoreUtils.GSON.toJsonTree(
|
||||
new BroadcastPlayerMessageEvent(message)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制控制台执行命令
|
||||
*
|
||||
* @param type 执行对象的服务端类型
|
||||
* @param serverID 执行对象的 ID
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchConsoleCommand(@Nullable BallServerType type, @Nullable String serverID, @NotNull String command) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
DispatchConsoleCommandEvent.ACTION,
|
||||
CoreUtils.GSON.toJsonTree(
|
||||
new DispatchConsoleCommandEvent(type, serverID, command)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制玩家执行命令
|
||||
*
|
||||
* @param type 执行对象的服务端类型
|
||||
* @param uuid 执行对象的 UUID
|
||||
* @param command 命令内容
|
||||
*/
|
||||
public void dispatchPlayerCommand(@Nullable BallServerType type, @Nullable UUID uuid, @NotNull String command) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.GAME,
|
||||
DispatchPlayerCommandEvent.ACTION,
|
||||
CoreUtils.GSON.toJsonTree(
|
||||
new DispatchPlayerCommandEvent(type, uuid, command)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 踢出玩家
|
||||
*
|
||||
* @param uuid 玩家
|
||||
* @param reason 原因
|
||||
*/
|
||||
public void kickPlayer(@NotNull UUID uuid, @NotNull String reason) {
|
||||
kickPlayer(uuid, Component.text(reason));
|
||||
}
|
||||
|
||||
/**
|
||||
* 踢出玩家
|
||||
*
|
||||
* @param uuid 玩家
|
||||
* @param reason 原因
|
||||
*/
|
||||
public void kickPlayer(@NotNull UUID uuid, @NotNull Component reason) {
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
KickPlayerEvent.ACTION,
|
||||
CoreUtils.GSON.toJsonTree(
|
||||
new KickPlayerEvent(uuid, reason)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 给玩家发送一条消息
|
||||
*
|
||||
* @param receiver 玩家
|
||||
* @param message 消息
|
||||
* @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 = CoreAPI.getInstance().getConnection()) {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);");
|
||||
statement.setString(1, receiver.toString());
|
||||
statement.setString(2, message.toJson().toString());
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
SendMessageToPlayerEvent.ACTION,
|
||||
CoreUtils.GSON.toJsonTree(
|
||||
new SendMessageToPlayerEvent(Collections.singleton(receiver), message)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 给玩家发送一条消息
|
||||
*
|
||||
* @param receivers 玩家
|
||||
* @param message 消息
|
||||
* @param cache 当玩家不在线时,是否缓存消息等待玩家上线再发送
|
||||
*/
|
||||
public void sendMessageToPlayer(@NotNull Collection<UUID> receivers, @NotNull DisplayMessage message, boolean cache) {
|
||||
if (cache) {
|
||||
for (UUID receiver : receivers) {
|
||||
BallPlayerInfo info = getPlayerInfo(receiver);
|
||||
if (info != null && info.isOnline()) {
|
||||
continue;
|
||||
}
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);");
|
||||
statement.setString(1, receiver.toString());
|
||||
statement.setString(2, message.toJson().toString());
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
sendBallMessage(new BallMessageInfo(
|
||||
BALL_CHANNEL,
|
||||
getLocalServerId(),
|
||||
null,
|
||||
BallServerType.PROXY,
|
||||
SendMessageToPlayerEvent.ACTION,
|
||||
CoreUtils.GSON.toJsonTree(
|
||||
new SendMessageToPlayerEvent(new HashSet<>(receivers), message)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 把玩家传送到一个位置
|
||||
* <p>
|
||||
* 如果目标位置不在当前服务器
|
||||
* <p>
|
||||
* 则会先尝试将玩家连接至目标服务器再进行传送
|
||||
*
|
||||
* @param sendPlayerUUID 玩家的uuid
|
||||
* @param location 坐标
|
||||
* @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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把玩家传送到一个位置
|
||||
* <p>
|
||||
* 如果目标位置不在当前服务器
|
||||
* <p>
|
||||
* 则会先尝试将玩家连接至目标服务器再进行传送
|
||||
*
|
||||
* @param sendPlayerUUID 玩家的uuid
|
||||
* @param location 坐标
|
||||
* @param doneMessage 传送完成后显示的消息
|
||||
*/
|
||||
public void sendPlayerToLocation(@NotNull Collection<UUID> sendPlayerUUID, @NotNull BallLocation location, @Nullable DisplayMessage doneMessage) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
SendPlayerToLocationEvent.ACTION,
|
||||
new SendPlayerToLocationEvent(new HashSet<>(sendPlayerUUID), location, doneMessage)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把玩家传送到另一个玩家身边
|
||||
* <p>
|
||||
* 支持跨服传送
|
||||
*
|
||||
* @param sendPlayer 被传送的玩家
|
||||
* @param toPlayer 传送的目标玩家
|
||||
* @param doneMessage 传送完成后显示的消息,自动将 %player_name% 替换成传送目标玩家的名称
|
||||
* @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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把玩家传送到另一个玩家身边
|
||||
* <p>
|
||||
* 支持跨服传送
|
||||
*
|
||||
* @param sendPlayerUUID 被传送的玩家
|
||||
* @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) {
|
||||
sendBallMessage(
|
||||
BALL_CHANNEL,
|
||||
SendPlayerToPlayerEvent.ACTION,
|
||||
new SendPlayerToPlayerEvent(new HashSet<>(sendPlayerUUID), toPlayer, doneMessage, doneTargetMessage)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送一条服务消息
|
||||
*
|
||||
* @param channel 消息标签
|
||||
* @param action 执行动作
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action) {
|
||||
sendBallMessage(new BallMessageInfo(channel, action));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送一条有附加参数的服务消息
|
||||
*
|
||||
* @param channel 消息标签
|
||||
* @param action 执行动作
|
||||
* @param content 附加参数
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action, @NotNull String content) {
|
||||
sendBallMessage(new BallMessageInfo(channel, action, new JsonPrimitive(content)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送一条有附加参数的消息
|
||||
*
|
||||
* @param channel 消息频道
|
||||
* @param action 执行动作
|
||||
* @param content 附加参数
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action, @NotNull JsonElement content) {
|
||||
sendBallMessage(new BallMessageInfo(channel, action, content));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送一条有附加参数的服务消息
|
||||
*
|
||||
* @param channel 消息标签
|
||||
* @param action 执行动作
|
||||
* @param content 附加参数
|
||||
*/
|
||||
public void sendBallMessage(@NotNull String channel, @NotNull String action, @NotNull Object content) {
|
||||
sendBallMessage(new BallMessageInfo(channel, action, content));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送自定义消息
|
||||
*
|
||||
* @param messageInfo 消息内容
|
||||
*/
|
||||
public void sendBallMessage(@NotNull BallMessageInfo messageInfo) {
|
||||
sendBallMessage(messageInfo, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义服务消息信息并发送
|
||||
*
|
||||
* @param messageInfo 消息内容
|
||||
* @param block 是否阻塞(设置为 true 则必须等待消息写入网络的操作完成后,该方法才会退出)
|
||||
*/
|
||||
public void sendBallMessage(@NotNull BallMessageInfo messageInfo, boolean block) {
|
||||
if (channel == null || !channel.isWritable()) {
|
||||
getLogger().warning("由于服务不可用,有一条消息发送失败了: " + messageInfo);
|
||||
return;
|
||||
}
|
||||
ChannelFuture future = channel.writeAndFlush(CoreUtils.GSON.toJson(messageInfo));
|
||||
if (block) {
|
||||
try {
|
||||
future.await();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onMessageSend(messageInfo);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<DisplayMessage> getCachedPlayerMessage(@NotNull UUID uuid) throws SQLException {
|
||||
ArrayList<DisplayMessage> list = new ArrayList<>();
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT message FROM `hamster_ball_cached_message` WHERE `uuid`=?;");
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next()) {
|
||||
DisplayMessage msg = CoreUtils.GSON.fromJson(set.getString("msg"), DisplayMessage.class);
|
||||
list.add(msg);
|
||||
}
|
||||
statement.close();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public void removeCachedPlayerMessage(@NotNull UUID uuid) throws SQLException {
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM `hamster_ball_cached_message` WHERE `uuid`=?;");
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本地服务器ID
|
||||
*
|
||||
* @return 服务器ID
|
||||
*/
|
||||
@NotNull
|
||||
public BallServerInfo getLocalServerInfo() {
|
||||
return config.getLocalInfo();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getLocalServerId() {
|
||||
return config.getLocalInfo().getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器信息
|
||||
*
|
||||
* @param serverID 服务器ID
|
||||
* @return 可能为 null
|
||||
*/
|
||||
public BallServerInfo getServerInfo(@NotNull String serverID) {
|
||||
return serverInfo.get(serverID);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家信息
|
||||
*
|
||||
* @param uuid 玩家的 UUID
|
||||
* @return 玩家信息
|
||||
*/
|
||||
public BallPlayerInfo getPlayerInfo(@NotNull UUID uuid) {
|
||||
return playerInfo.get(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家信息
|
||||
*
|
||||
* @param playerName 玩家名称
|
||||
* @return 玩家信息
|
||||
*/
|
||||
public BallPlayerInfo getPlayerInfo(@NotNull String playerName) {
|
||||
for (BallPlayerInfo info : playerInfo.values()) {
|
||||
if (info.getName().equalsIgnoreCase(playerName)) {
|
||||
return info;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家的UUID
|
||||
*
|
||||
* @param playerName 玩家名称
|
||||
* @return 玩家的UUID
|
||||
*/
|
||||
@Nullable
|
||||
public UUID getPlayerUUID(String playerName) {
|
||||
BallPlayerInfo info = getPlayerInfo(playerName);
|
||||
if (info == null) {
|
||||
return null;
|
||||
}
|
||||
return info.getUuid();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家的UUID
|
||||
*
|
||||
* @param playerName 玩家名称
|
||||
* @param defaultValue 如果未找到玩家信息则返回该值
|
||||
* @return 玩家的UUID
|
||||
*/
|
||||
@NotNull
|
||||
public UUID getPlayerUUID(String playerName, @NotNull UUID defaultValue) {
|
||||
BallPlayerInfo info = getPlayerInfo(playerName);
|
||||
if (info == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return info.getUuid();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家名称
|
||||
*
|
||||
* @param uuid 玩家的 UUID
|
||||
* @return 如果数据不存在,则返回字符串形式的 "null"
|
||||
*/
|
||||
@Nullable
|
||||
public String getPlayerName(@NotNull UUID uuid) {
|
||||
BallPlayerInfo info = getPlayerInfo(uuid);
|
||||
if (info == null) {
|
||||
return null;
|
||||
}
|
||||
return info.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家名称
|
||||
*
|
||||
* @param uuid 玩家的 UUID
|
||||
* @param defaultValue 如果未找到玩家信息则返回该值
|
||||
* @return 玩家名称
|
||||
*/
|
||||
@NotNull
|
||||
public String getPlayerName(@NotNull UUID uuid, @NotNull String defaultValue) {
|
||||
BallPlayerInfo info = getPlayerInfo(uuid);
|
||||
if (info == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return info.getName();
|
||||
}
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
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;
|
||||
}
|
@@ -0,0 +1,195 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.connector;
|
||||
|
||||
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.ball.common.listener.BallListener;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
public class BallChannelHandler extends SimpleChannelInboundHandler<String> {
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext context, String message) {
|
||||
if ("pong".equals(message)) {
|
||||
return;
|
||||
}
|
||||
if ("connection refused".equals(message)) {
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onConnectRefused();
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
BallMessageInfo info = CoreUtils.GSON.fromJson(message, BallMessageInfo.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onMessageReceived(info);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (!BallAPI.BALL_CHANNEL.equals(info.getChannel())) {
|
||||
return;
|
||||
}
|
||||
switch (info.getAction()) {
|
||||
case BallPlayerPreLoginEvent.ACTION: {
|
||||
BallPlayerPreLoginEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPreLoginEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerPreLogin(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerLoginEvent.ACTION: {
|
||||
BallPlayerLoginEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerLoginEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerLogin(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerPostLoginEvent.ACTION: {
|
||||
BallPlayerPostLoginEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPostLoginEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerPostLogin(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerPreConnectServerEvent.ACTION: {
|
||||
BallPlayerPreConnectServerEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPreConnectServerEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerPreConnectServer(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerConnectServerEvent.ACTION: {
|
||||
BallPlayerConnectServerEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerConnectServerEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerConnectServer(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerPostConnectServerEvent.ACTION: {
|
||||
BallPlayerPostConnectServerEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerPostConnectServerEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerPostConnectServer(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerLogoutEvent.ACTION: {
|
||||
BallPlayerLogoutEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerLogoutEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerLogout(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerInfoUpdateEvent.ACTION: {
|
||||
BallPlayerInfoUpdateEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerInfoUpdateEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerInfoUpdate(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BallPlayerChatEvent.ACTION: {
|
||||
BallPlayerChatEvent event = CoreUtils.GSON.fromJson(info.getContent(), BallPlayerChatEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onBallPlayerChat(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ServerOfflineEvent.ACTION: {
|
||||
ServerOfflineEvent event = CoreUtils.GSON.fromJson(info.getContent(), ServerOfflineEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onServerOffline(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ServerOnlineEvent.ACTION: {
|
||||
ServerOnlineEvent event = CoreUtils.GSON.fromJson(info.getContent(), ServerOnlineEvent.class);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onServerOnline(event);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(@NotNull ChannelHandlerContext context) {
|
||||
BallAPI.getInstance().getLogger().info("与服务器 " + context.channel().remoteAddress() + " 建立了连接.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(@NotNull ChannelHandlerContext context) {
|
||||
context.close();
|
||||
BallAPI.getInstance().getLogger().warning("与服务器 " + context.channel().remoteAddress() + " 的连接已断开.");
|
||||
CoreUtils.WORKER_EXECUTOR.submit(() -> BallAPI.getInstance().reconnect(5));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
|
||||
SocketAddress address = context.channel().remoteAddress();
|
||||
BallAPI.getInstance().getLogger().log(Level.WARNING, "与服务器 " + address + " 通信时出现了一个错误: ", cause);
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onConnectException(cause);
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.connector;
|
||||
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
||||
import io.netty.handler.codec.LengthFieldPrepender;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BallChannelInitializer extends ChannelInitializer<SocketChannel> {
|
||||
@Override
|
||||
protected void initChannel(@NotNull SocketChannel channel) {
|
||||
channel.pipeline()
|
||||
.addLast(new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS))
|
||||
.addLast(new BallKeepAliveHandler())
|
||||
.addLast(new LengthFieldPrepender(8))
|
||||
.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, 0, 8))
|
||||
.addLast(new StringDecoder(StandardCharsets.UTF_8))
|
||||
.addLast(new StringEncoder(StandardCharsets.UTF_8))
|
||||
.addLast(new BallChannelHandler());
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.connector;
|
||||
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
public class BallKeepAliveHandler extends ChannelInboundHandlerAdapter {
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext context, Object event) throws Exception {
|
||||
if (event instanceof IdleStateEvent) {
|
||||
context.channel().writeAndFlush("ping");
|
||||
return;
|
||||
}
|
||||
super.userEventTriggered(context, event);
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.data;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import com.google.gson.JsonElement;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@SuppressWarnings("unused")
|
||||
public class BallBlockPos {
|
||||
private String serverID;
|
||||
private String worldName;
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
|
||||
public static BallBlockPos fromJson(String json) {
|
||||
return CoreUtils.GSON.fromJson(json, BallBlockPos.class);
|
||||
}
|
||||
|
||||
public JsonElement toJson() {
|
||||
return CoreUtils.GSON.toJsonTree(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BallLocation toServiceLocation() {
|
||||
return new BallLocation(getServerID(), getWorldName(), getX(), getY(), getZ(), 0, 0);
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.data;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import com.google.gson.JsonElement;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@SuppressWarnings("unused")
|
||||
public class BallLocation {
|
||||
private String serverID;
|
||||
private String worldName;
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
|
||||
public static BallLocation fromJson(String json) {
|
||||
return CoreUtils.GSON.fromJson(json, BallLocation.class);
|
||||
}
|
||||
|
||||
public JsonElement toJson() {
|
||||
return CoreUtils.GSON.toJsonTree(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BallBlockPos toServiceBlockPos() {
|
||||
return new BallBlockPos(getServerID(), getWorldName(), getBlockX(), getBlockY(), getBlockZ());
|
||||
}
|
||||
|
||||
public int getBlockX() {
|
||||
return (int) x;
|
||||
}
|
||||
|
||||
public int getBlockY() {
|
||||
return (int) y;
|
||||
}
|
||||
|
||||
public int getBlockZ() {
|
||||
return (int) z;
|
||||
}
|
||||
}
|
@@ -0,0 +1,196 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.data;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 服务消息
|
||||
*/
|
||||
@Data
|
||||
@SuppressWarnings("unused")
|
||||
public class BallMessageInfo {
|
||||
/**
|
||||
* 消息发送者
|
||||
*/
|
||||
@NotNull
|
||||
private String senderID;
|
||||
/**
|
||||
* 接受该消息的目标服务器ID
|
||||
* <p>
|
||||
* 若设定该值,则仅服务器名称匹配的子端才能接收到这条消息
|
||||
* <p>
|
||||
* 若不设定(即为 null),则该消息会广播给所有子端
|
||||
*/
|
||||
@Nullable
|
||||
private String receiverID;
|
||||
/**
|
||||
* 接受该消息的目标服务器类型
|
||||
* <p>
|
||||
* 若设定该值,则仅服务器类型匹配的子端才能接收到这条消息
|
||||
* <p>
|
||||
* 若不设定(值为null),则该消息会广播给所有子端
|
||||
*/
|
||||
@Nullable
|
||||
private BallServerType receiverType;
|
||||
/**
|
||||
* 消息的频道
|
||||
*/
|
||||
@NotNull
|
||||
private String channel;
|
||||
/**
|
||||
* 消息动作
|
||||
* <p>
|
||||
* 一般用这个来判断插件应该如何处理这条消息
|
||||
*/
|
||||
@NotNull
|
||||
private String action;
|
||||
/**
|
||||
* 消息内容
|
||||
* <p>
|
||||
* 这里是消息的附加参数
|
||||
*/
|
||||
private JsonElement content;
|
||||
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String action) {
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.channel = channel;
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String action, String content) {
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.channel = channel;
|
||||
this.action = action;
|
||||
this.content = new JsonPrimitive(content);
|
||||
}
|
||||
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String action, JsonElement content) {
|
||||
this.channel = channel;
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.action = action;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public BallMessageInfo(@NotNull String channel, @NotNull String action, Object content) {
|
||||
this.channel = channel;
|
||||
senderID = BallAPI.getInstance().getLocalServerId();
|
||||
this.action = action;
|
||||
this.content = CoreUtils.GSON.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;
|
||||
this.senderID = senderID;
|
||||
this.receiverID = receiverID;
|
||||
this.receiverType = receiverType;
|
||||
this.action = action;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化至 Json
|
||||
*
|
||||
* @return json对象
|
||||
*/
|
||||
@NotNull
|
||||
public JsonObject toJson() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("channel", channel);
|
||||
object.addProperty("senderID", senderID);
|
||||
if (receiverID != null) {
|
||||
object.addProperty("toServer", receiverID);
|
||||
}
|
||||
if (receiverType != null) {
|
||||
object.addProperty("toServer", receiverType.name());
|
||||
}
|
||||
object.addProperty("action", action);
|
||||
object.add("content", content);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 Java 对象获取消息内容
|
||||
*
|
||||
* @param clazz 对象所属的类
|
||||
* @param <T> 对象类型
|
||||
* @return Java 对象
|
||||
*/
|
||||
public <T> T getContentAs(@NotNull Class<T> clazz) {
|
||||
return CoreUtils.GSON.fromJson(content, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以字符串形式获取消息内容
|
||||
*
|
||||
* @return 消息内容
|
||||
*/
|
||||
@NotNull
|
||||
public String getContentAsString() {
|
||||
return content.getAsString();
|
||||
}
|
||||
|
||||
public boolean getContentAsBoolean() {
|
||||
return content.getAsBoolean();
|
||||
}
|
||||
|
||||
public int getContentAsInt() {
|
||||
return content.getAsInt();
|
||||
}
|
||||
|
||||
public long getContentAsLong() {
|
||||
return content.getAsLong();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BigInteger getContentAsBigInteger() {
|
||||
return content.getAsBigInteger();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BigDecimal getContentAsBigDecimal() {
|
||||
return content.getAsBigDecimal();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public UUID getContentAsUUID() {
|
||||
return UUID.fromString(content.getAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 JsonObject 对象获取消息内容
|
||||
*
|
||||
* @return 消息内容
|
||||
*/
|
||||
@NotNull
|
||||
public JsonObject getContentAsJsonObject() {
|
||||
return content.getAsJsonObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 JsonArray 对象获取消息内容
|
||||
*
|
||||
* @return 消息内容
|
||||
*/
|
||||
@NotNull
|
||||
public JsonArray getContentAsJsonArray() {
|
||||
return content.getAsJsonArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toJson().toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 玩家信息
|
||||
*/
|
||||
@Data
|
||||
@NotNull
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerInfo {
|
||||
/**
|
||||
* 玩家的uuid
|
||||
*/
|
||||
@NotNull
|
||||
private UUID uuid;
|
||||
/**
|
||||
* 玩家的名称
|
||||
*/
|
||||
@NotNull
|
||||
private String name;
|
||||
/**
|
||||
* 玩家所在的游戏服务器 ID
|
||||
* <p>
|
||||
* 不应超过 32 个字符
|
||||
*/
|
||||
@NotNull
|
||||
private String gameServer;
|
||||
/**
|
||||
* 玩家所在的代理服务器 ID
|
||||
* <p>
|
||||
* 不应超过 32 个字符
|
||||
*/
|
||||
@NotNull
|
||||
private String proxyServer;
|
||||
/**
|
||||
* 玩家是否在线
|
||||
*/
|
||||
private boolean online;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
BallPlayerInfo that = (BallPlayerInfo) o;
|
||||
|
||||
return uuid.equals(that.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return uuid.hashCode();
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 消息发送者信息
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BallServerInfo {
|
||||
/**
|
||||
* 服务器 ID
|
||||
* <p>
|
||||
* 不应超过 32 个字符
|
||||
*/
|
||||
private String id;
|
||||
/**
|
||||
* 服务器名称
|
||||
* <p>
|
||||
* 不应超过 32 个字符
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 服务器类型
|
||||
*/
|
||||
private BallServerType type;
|
||||
/**
|
||||
* 服务器主机名
|
||||
* <p>
|
||||
* 不应超过 32 个字符
|
||||
*/
|
||||
private String host;
|
||||
/**
|
||||
* 服务器端口号
|
||||
*/
|
||||
private int port;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
BallServerInfo that = (BallServerInfo) o;
|
||||
return id.equals(that.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.entity;
|
||||
|
||||
/**
|
||||
* Service 接入者的类型
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public enum BallServerType {
|
||||
/**
|
||||
* 游戏服务器
|
||||
*/
|
||||
GAME,
|
||||
/**
|
||||
* 代理服务器
|
||||
*/
|
||||
PROXY,
|
||||
/**
|
||||
* 测试服务器
|
||||
*/
|
||||
TEST,
|
||||
/**
|
||||
* 其他类型的接入者
|
||||
*/
|
||||
OTHER
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.operate;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BroadcastPlayerMessageEvent {
|
||||
public static final String ACTION = "BroadcastPlayerMessage";
|
||||
|
||||
@NotNull
|
||||
private final DisplayMessage message;
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.operate;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class DispatchConsoleCommandEvent {
|
||||
public static final String ACTION = "DispatchConsoleCommand";
|
||||
|
||||
@Nullable
|
||||
private final BallServerType type;
|
||||
@Nullable
|
||||
private final String serverID;
|
||||
@NotNull
|
||||
private final String command;
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.operate;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class DispatchPlayerCommandEvent {
|
||||
public static final String ACTION = "DispatchPlayerCommand";
|
||||
|
||||
@Nullable
|
||||
private final BallServerType type;
|
||||
@Nullable
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
private final String command;
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.operate;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class KickPlayerEvent {
|
||||
public static final String ACTION = "KickPlayer";
|
||||
|
||||
@NotNull
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
private final Component reason;
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.operate;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendMessageToPlayerEvent {
|
||||
public static final String ACTION = "SendMessageToPlayer";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> receivers;
|
||||
@NotNull
|
||||
private final DisplayMessage message;
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.operate;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallLocation;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendPlayerToLocationEvent {
|
||||
public static final String ACTION = "SendPlayerToLocation";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> sendPlayerUUID;
|
||||
@NotNull
|
||||
private final BallLocation location;
|
||||
@Nullable
|
||||
private final DisplayMessage doneMessage;
|
||||
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.operate;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class SendPlayerToPlayerEvent {
|
||||
public static final String ACTION = "SendPlayerToPlayer";
|
||||
|
||||
@NotNull
|
||||
private final Set<UUID> sendPlayerUUID;
|
||||
@NotNull
|
||||
private final UUID toPlayerUUID;
|
||||
@Nullable
|
||||
private final DisplayMessage doneMessage;
|
||||
@Nullable
|
||||
private final DisplayMessage doneTargetMessage;
|
||||
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 玩家的聊天信息
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerChatEvent {
|
||||
public static final String ACTION = "PlayerChat";
|
||||
|
||||
@NotNull
|
||||
private final UUID playerUUID;
|
||||
@NotNull
|
||||
private final Component displayName;
|
||||
@NotNull
|
||||
private final Component message;
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 玩家进入子服
|
||||
* <p>
|
||||
* 仅在使用 velocity 代理端时才会触发这个事件
|
||||
*
|
||||
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
|
||||
* @see BallPlayerConnectServerEvent 玩家进入子服
|
||||
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerConnectServerEvent {
|
||||
public static final String ACTION = "PlayerConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
@Nullable
|
||||
private final String from;
|
||||
@NotNull
|
||||
private final String to;
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 玩家已进入子服
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerInfoUpdateEvent {
|
||||
public static final String ACTION = "PlayerInfoUpdateEvent";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 玩家连接到代理服务器
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerLoginEvent {
|
||||
public static final String ACTION = "PlayerLogin";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 玩家与代理服务器断开连接
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerLogoutEvent {
|
||||
public static final String ACTION = "PlayerLogout";
|
||||
|
||||
@NotNull
|
||||
private BallPlayerInfo playerInfo;
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 玩家已经进入子服
|
||||
*
|
||||
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
|
||||
* @see BallPlayerConnectServerEvent 玩家进入子服
|
||||
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPostConnectServerEvent {
|
||||
public static final String ACTION = "PlayerPostConnectServer";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 玩家已连接到代理服务器
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPostLoginEvent {
|
||||
public static final String ACTION = "PlayerPostLogin";
|
||||
|
||||
@NotNull
|
||||
private final BallPlayerInfo playerInfo;
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 玩家准备进入子服
|
||||
*
|
||||
* @see BallPlayerPreConnectServerEvent 玩家准备进入子服
|
||||
* @see BallPlayerConnectServerEvent 玩家进入子服
|
||||
* @see BallPlayerPostConnectServerEvent 玩家已经进入子服
|
||||
*/
|
||||
@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;
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.player;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 玩家准备连接到代理服务器
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BallPlayerPreLoginEvent {
|
||||
public static final String ACTION = "PlayerPreLogin";
|
||||
|
||||
@NotNull
|
||||
private final String playerName;
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.server;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 服务器离线
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOfflineEvent {
|
||||
public static final String ACTION = "ServerOffline";
|
||||
|
||||
@NotNull
|
||||
private final String serverID;
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 服务器上线
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOnlineEvent {
|
||||
public static final String ACTION = "ServerOnline";
|
||||
|
||||
@NotNull
|
||||
private final BallServerInfo serverInfo;
|
||||
|
||||
}
|
@@ -0,0 +1,109 @@
|
||||
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.lib.net.kyori.adventure.text.serializer.json.JSONComponentSerializer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public final class BallDebugListener implements BallListener {
|
||||
public static final BallDebugListener INSTANCE = new BallDebugListener();
|
||||
|
||||
private BallDebugListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListenerPriority getPriority() {
|
||||
return ListenerPriority.MONITOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectActive() {
|
||||
BallAPI.getInstance().getLogger().info("连接已可用。");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectException(Throwable throwable) {
|
||||
BallAPI.getInstance().getLogger().log(Level.INFO, "连接出现错误!", throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDead() {
|
||||
BallAPI.getInstance().getLogger().info("已无法建立与服务中心的连接!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerPreLogin(@NotNull BallPlayerPreLoginEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerPreLoginEvent: " + event.getPlayerName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerLogin(@NotNull BallPlayerLoginEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerLoginEvent: " + event.getPlayerInfo().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerPostLogin(@NotNull BallPlayerPostLoginEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerPostLoginEvent: " + event.getPlayerInfo().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerPreConnectServer(@NotNull BallPlayerPreConnectServerEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerPreConnectServerEvent: ");
|
||||
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
|
||||
BallAPI.getInstance().getLogger().info("from: " + event.getFrom());
|
||||
BallAPI.getInstance().getLogger().info("to: " + event.getTo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerConnectServer(@NotNull BallPlayerConnectServerEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerConnectServerEvent: ");
|
||||
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
|
||||
BallAPI.getInstance().getLogger().info("from: " + event.getFrom());
|
||||
BallAPI.getInstance().getLogger().info("to: " + event.getTo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerPostConnectServer(@NotNull BallPlayerPostConnectServerEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerPostConnectServerEvent: ");
|
||||
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerLogout(@NotNull BallPlayerLogoutEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerLogoutEvent: ");
|
||||
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBallPlayerChat(@NotNull BallPlayerChatEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("BallPlayerChatEvent: ");
|
||||
BallAPI.getInstance().getLogger().info("displayName: " + event.getDisplayName());
|
||||
BallAPI.getInstance().getLogger().info("playerUUID: " + event.getPlayerUUID());
|
||||
BallAPI.getInstance().getLogger().info("message: " + JSONComponentSerializer.json().serialize(event.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServerOffline(@NotNull ServerOfflineEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("服务器已离线: " + event.getServerID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServerOnline(@NotNull ServerOnlineEvent event) {
|
||||
BallAPI.getInstance().getLogger().info("服务器已上线: " + event.getServerInfo().getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageSend(@NotNull BallMessageInfo event) {
|
||||
BallAPI.getInstance().getLogger().info("发送了一条消息: " + event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(@NotNull BallMessageInfo event) {
|
||||
BallAPI.getInstance().getLogger().info("收到了一条消息: " + event);
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
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 onConnectActive() {
|
||||
}
|
||||
|
||||
default void onMessageReceived(@NotNull BallMessageInfo event) {
|
||||
}
|
||||
|
||||
default void onMessageSend(@NotNull BallMessageInfo event) {
|
||||
}
|
||||
|
||||
default void onConnectException(Throwable throwable) {
|
||||
}
|
||||
|
||||
default void onConnectRefused() {
|
||||
}
|
||||
|
||||
default void onServiceDead() {
|
||||
}
|
||||
|
||||
default void onBallPlayerPreLogin(@NotNull BallPlayerPreLoginEvent event) {
|
||||
}
|
||||
|
||||
default void 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 onBallPlayerChat(@NotNull BallPlayerChatEvent event) {
|
||||
}
|
||||
|
||||
default void onServerOnline(@NotNull ServerOnlineEvent event) {
|
||||
}
|
||||
|
||||
default void onServerOffline(@NotNull ServerOfflineEvent event) {
|
||||
}
|
||||
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.utils;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.ServerChannel;
|
||||
import io.netty.channel.epoll.EpollEventLoopGroup;
|
||||
import io.netty.channel.epoll.EpollServerSocketChannel;
|
||||
import io.netty.channel.epoll.EpollSocketChannel;
|
||||
import io.netty.channel.kqueue.KQueueEventLoopGroup;
|
||||
import io.netty.channel.kqueue.KQueueServerSocketChannel;
|
||||
import io.netty.channel.kqueue.KQueueSocketChannel;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
public enum OS {
|
||||
WINDOWS,
|
||||
LINUX,
|
||||
MACOS,
|
||||
OTHER;
|
||||
|
||||
private static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory("HamsterBall-IO");
|
||||
|
||||
@NotNull
|
||||
public static OS getCurrentOS() {
|
||||
String s = System.getProperties().get("os.name").toString().toLowerCase();
|
||||
if (s.contains("windows")) {
|
||||
return WINDOWS;
|
||||
}
|
||||
if (s.contains("linux")) {
|
||||
return LINUX;
|
||||
}
|
||||
if (s.contains("mac")) {
|
||||
return MACOS;
|
||||
}
|
||||
return OTHER;
|
||||
}
|
||||
|
||||
public String getIOModeName() {
|
||||
switch (this) {
|
||||
case LINUX:
|
||||
return "epoll";
|
||||
case MACOS:
|
||||
return "kqueue";
|
||||
default:
|
||||
return "nio";
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public EventLoopGroup getEventLoopGroup(int nThread) {
|
||||
switch (this) {
|
||||
case LINUX:
|
||||
return new EpollEventLoopGroup(nThread, THREAD_FACTORY);
|
||||
case MACOS:
|
||||
return new KQueueEventLoopGroup(nThread, THREAD_FACTORY);
|
||||
default:
|
||||
return new NioEventLoopGroup(nThread, THREAD_FACTORY);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Class<? extends Channel> getSocketChannel() {
|
||||
switch (this) {
|
||||
case LINUX:
|
||||
return EpollSocketChannel.class;
|
||||
case MACOS:
|
||||
return KQueueSocketChannel.class;
|
||||
default:
|
||||
return NioSocketChannel.class;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Class<? extends ServerChannel> getServerSocketChannel() {
|
||||
switch (this) {
|
||||
case LINUX:
|
||||
return EpollServerSocketChannel.class;
|
||||
case MACOS:
|
||||
return KQueueServerSocketChannel.class;
|
||||
default:
|
||||
return NioServerSocketChannel.class;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user