feat: 优化代码,添加 auto-register-game-server 功能
This commit is contained in:
@@ -8,7 +8,9 @@ import cn.hamster3.mc.plugin.ball.velocity.api.CoreVelocityAPI;
|
||||
import cn.hamster3.mc.plugin.ball.velocity.listener.BallVelocityListener;
|
||||
import cn.hamster3.mc.plugin.ball.velocity.listener.BallVelocityMainListener;
|
||||
import cn.hamster3.mc.plugin.ball.velocity.listener.UpdatePlayerInfoListener;
|
||||
import cn.hamster3.mc.plugin.ball.velocity.listener.VelocityServerListener;
|
||||
import cn.hamster3.mc.plugin.ball.velocity.util.BallVelocityUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.PostOrder;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
@@ -48,6 +50,8 @@ public class HamsterBallPlugin {
|
||||
private final ProxyServer proxyServer;
|
||||
@Getter
|
||||
private final File dataFolder;
|
||||
@Getter
|
||||
private YamlConfig config;
|
||||
|
||||
@Inject
|
||||
public HamsterBallPlugin(Logger slf4jLogger, ProxyServer proxyServer, @DataDirectory Path dataPath) {
|
||||
@@ -71,7 +75,8 @@ public class HamsterBallPlugin {
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
);
|
||||
}
|
||||
CoreVelocityAPI.init(configFile);
|
||||
config = YamlConfig.load(configFile);
|
||||
CoreVelocityAPI.init(config);
|
||||
logger.info("已初始化 BallAPI");
|
||||
} catch (Exception e) {
|
||||
slf4jLogger.error("BallAPI 初始化失败", e);
|
||||
@@ -100,6 +105,11 @@ public class HamsterBallPlugin {
|
||||
logger.info("已注册监听器 BallVelocityMainListener");
|
||||
proxyServer.getEventManager().register(this, UpdatePlayerInfoListener.INSTANCE);
|
||||
logger.info("已注册监听器 UpdatePlayerInfoListener");
|
||||
if (config.getBoolean("auto-register-game-server", false)) {
|
||||
BallAPI.getInstance().getEventBus().register(VelocityServerListener.INSTANCE);
|
||||
logger.info("已注册监听器 VelocityServerListener");
|
||||
VelocityServerListener.INSTANCE.onEnable();
|
||||
}
|
||||
|
||||
if (BallAPI.getInstance().getBallConfig().isGameServerUpdatePlayerInfo()) {
|
||||
BallAPI.getInstance().subscribePatterns("*" + BallAPI.PLAYER_INFO_CHANNEL);
|
||||
|
@@ -8,8 +8,6 @@ import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||
import com.velocitypowered.api.proxy.config.ProxyConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Logger;
|
||||
@@ -24,11 +22,10 @@ public final class CoreVelocityAPI extends BallAPI {
|
||||
return (CoreVelocityAPI) instance;
|
||||
}
|
||||
|
||||
public static void init(@NotNull File configFile) throws IOException {
|
||||
public static void init(@NotNull YamlConfig config) {
|
||||
if (instance != null) {
|
||||
return;
|
||||
}
|
||||
YamlConfig config = YamlConfig.load(configFile);
|
||||
instance = new CoreVelocityAPI(config);
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,65 @@
|
||||
package cn.hamster3.mc.plugin.ball.velocity.listener;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
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.server.ServerOfflineEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent;
|
||||
import cn.hamster3.mc.plugin.ball.velocity.HamsterBallPlugin;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import com.velocitypowered.api.proxy.server.ServerInfo;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class VelocityServerListener {
|
||||
public static final VelocityServerListener INSTANCE = new VelocityServerListener();
|
||||
|
||||
private VelocityServerListener() {
|
||||
}
|
||||
|
||||
public void onEnable() {
|
||||
for (BallServerInfo info : BallAPI.getInstance().getAllServerInfo().values()) {
|
||||
if (info.getType() != BallServerType.GAME) {
|
||||
continue;
|
||||
}
|
||||
ProxyServer server = HamsterBallPlugin.getInstance().getProxyServer();
|
||||
server.getServer(info.getId())
|
||||
.map(RegisteredServer::getServerInfo)
|
||||
.ifPresent(server::unregisterServer);
|
||||
ServerInfo serverInfo = new ServerInfo(info.getId(), new InetSocketAddress(info.getHost(), info.getPort()));
|
||||
server.registerServer(serverInfo);
|
||||
BallAPI.getInstance().getLogger().info("已添加服务器入口: " + info.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onServerOnline(ServerOnlineEvent event) {
|
||||
if (event.getType() != BallServerType.GAME) {
|
||||
return;
|
||||
}
|
||||
ProxyServer server = HamsterBallPlugin.getInstance().getProxyServer();
|
||||
server.getServer(event.getId())
|
||||
.map(RegisteredServer::getServerInfo)
|
||||
.ifPresent(server::unregisterServer);
|
||||
ServerInfo serverInfo = new ServerInfo(event.getId(), new InetSocketAddress(event.getHost(), event.getPort()));
|
||||
server.registerServer(serverInfo);
|
||||
BallAPI.getInstance().getLogger().info("已添加服务器入口: " + event.getId());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onServerOffline(ServerOfflineEvent event) {
|
||||
if (event.getType() != BallServerType.GAME) {
|
||||
return;
|
||||
}
|
||||
ProxyServer server = HamsterBallPlugin.getInstance().getProxyServer();
|
||||
ServerInfo serverInfo = server.getServer(event.getId())
|
||||
.map(RegisteredServer::getServerInfo)
|
||||
.orElse(null);
|
||||
if (serverInfo != null) {
|
||||
server.unregisterServer(serverInfo);
|
||||
BallAPI.getInstance().getLogger().info("已移除服务器入口: " + event.getId());
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,12 +8,14 @@ channel-prefix: ""
|
||||
|
||||
# 是否在子服端更新玩家信息
|
||||
# 默认情况下,BC 统一管理玩家信息,包括记录 UUID 和玩家名称
|
||||
# 如果一个群组服同时拥有多个 BC 入口
|
||||
# 且每个 BC 入口为不同的玩家名称分配不同的 UUID
|
||||
# 如果一个群组服同时拥有多个 BC 入口,且每个 BC 入口为不同的玩家名称分配不同的 UUID
|
||||
# (例如正版、盗版双入口,或网易多入口接同一个子服)
|
||||
# 则可以启用该功能以防止 UUID 紊乱的问题
|
||||
# 则可以启用该功能以防止同一个名称占用多个 UUID 的问题
|
||||
game-server-update-player-info: false
|
||||
|
||||
# 启用后,子服启动时会自动注册该子服的入口配置,关闭时也会自动移除该子服的入口配置
|
||||
auto-register-game-server: false
|
||||
|
||||
# 本服务器信息
|
||||
server-info:
|
||||
# 服务器唯一识别码,最长 32 字符
|
||||
@@ -34,21 +36,20 @@ server-info:
|
||||
# 这个选项就会很有用
|
||||
#datasource:
|
||||
# # 数据库链接驱动地址
|
||||
# driver: "com.mysql.jdbc.Driver"
|
||||
# # 数据库链接填写格式:
|
||||
# driver: "com.mysql.cj.jdbc.Driver"
|
||||
# # MySQL数据库链接填写格式:
|
||||
# # jdbc:mysql://{数据库地址}:{数据库端口}/{使用的库名}?参数
|
||||
# # 除非你知道自己在做什么,否则不建议随意更改参数
|
||||
# url: "jdbc:mysql://localhost:3306/Test1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true"
|
||||
# url: "jdbc:mysql://localhost:3306/Test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true"
|
||||
# # 用户名
|
||||
# username: "Test"
|
||||
# username: "root"
|
||||
# # 密码
|
||||
# password: "Test123.."
|
||||
# password: "Root123.."
|
||||
# # 最小闲置链接数
|
||||
# # 推荐值:1~3
|
||||
# minimum-idle: 0
|
||||
# # 最大链接数
|
||||
# # 推荐值:不低于3
|
||||
# maximum-pool-size: 3
|
||||
# # 推荐值:不低于5
|
||||
# maximum-pool-size: 5
|
||||
# # 保持连接池可用的间隔
|
||||
# # 除非你的服务器数据库连接经常断开,否则不建议启用该选项
|
||||
# # 单位:毫秒
|
||||
|
Reference in New Issue
Block a user