From 84696bc408c50dfcbfad795192f0c6f2dbea47f7 Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Wed, 7 Dec 2022 19:30:47 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 8 ++-- hamster-ball-bukkit/build.gradle | 2 +- .../src/main/resources/config.yml | 4 +- hamster-ball-bungeecord/build.gradle | 2 +- .../plugin/core/bungee/HamsterBallPlugin.java | 16 ++++--- .../core/bungee/api/BallBungeeCordAPI.java | 5 +-- .../core/bungee/core/BallConfigManager.java | 27 +++++++++++ .../listener/BallBungeeCordListener.java | 7 +++ .../src/main/resources/config.yml | 6 +++ .../mc/plugin/ball/common/api/BallAPI.java | 45 ++++++++++++++----- 10 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/core/BallConfigManager.java diff --git a/build.gradle b/build.gradle index aba4dc2..7e1534f 100644 --- a/build.gradle +++ b/build.gradle @@ -39,11 +39,11 @@ subprojects { withSourcesJar() } - tasks.withType(JavaCompile) { + tasks.withType(JavaCompile).configureEach { options.setEncoding("UTF-8") } - tasks.withType(Jar) { + tasks.withType(Jar).configureEach { from([rootProject.file("LICENSE")]) duplicatesStrategy(DuplicatesStrategy.EXCLUDE) } @@ -77,8 +77,8 @@ subprojects { url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl credentials { - username rootProject.properties.getOrDefault("maven_username", "") - password rootProject.properties.getOrDefault("maven_password", "") + username = rootProject.properties.getOrDefault("maven_username", "") + password = rootProject.properties.getOrDefault("maven_password", "") } } } diff --git a/hamster-ball-bukkit/build.gradle b/hamster-ball-bukkit/build.gradle index dde6561..993b8df 100644 --- a/hamster-ball-bukkit/build.gradle +++ b/hamster-ball-bukkit/build.gradle @@ -20,7 +20,7 @@ processResources { } tasks.compileJava.dependsOn(":hamster-ball-common:build") -tasks.create("shadowJar", Jar) { +tasks.register("shadowJar", Jar) { dependsOn("jar") from([ tasks.jar.outputs.files.collect { diff --git a/hamster-ball-bukkit/src/main/resources/config.yml b/hamster-ball-bukkit/src/main/resources/config.yml index 3cc1f6c..ba56c22 100644 --- a/hamster-ball-bukkit/src/main/resources/config.yml +++ b/hamster-ball-bukkit/src/main/resources/config.yml @@ -19,8 +19,8 @@ server-info: # 当前子服的内网地址 # 不填则自动获取 server.properties 文件中的设置 # 若都为空,则自动设定为 127.0.0.1 - # 连接上 HamsterBall 之后,BC 端的插件会自动将这个服务器的地址和端口加入到列表 - # 因此只需在这里填上无需每次都手动更改 BC 的配置文件 + # BC 端的插件会自动将这个服务器的地址和端口加入到列表 + # 因此只需在这里配置好,以后新增子服时无需每次都手动更改 BC 的配置文件 # host: "127.0.0.1" # 当前子服的内网端口 # 不填则自动获取 server.properties 文件中的设置 diff --git a/hamster-ball-bungeecord/build.gradle b/hamster-ball-bungeecord/build.gradle index f337305..9be566e 100644 --- a/hamster-ball-bungeecord/build.gradle +++ b/hamster-ball-bungeecord/build.gradle @@ -19,7 +19,7 @@ processResources { } tasks.compileJava.dependsOn(":hamster-ball-common:build") -tasks.create("shadowJar", Jar) { +tasks.register("shadowJar", Jar) { dependsOn("jar") from([ tasks.jar.outputs.files.collect { diff --git a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterBallPlugin.java b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterBallPlugin.java index 6cbe1af..dd71c37 100644 --- a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterBallPlugin.java +++ b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterBallPlugin.java @@ -6,6 +6,7 @@ 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.ServerOnlineEvent; import cn.hamster3.mc.plugin.core.bungee.api.BallBungeeCordAPI; +import cn.hamster3.mc.plugin.core.bungee.core.BallConfigManager; import cn.hamster3.mc.plugin.core.bungee.listener.BallBungeeCordListener; import cn.hamster3.mc.plugin.core.bungee.util.BallBungeeCordUtils; import net.md_5.bungee.api.ProxyServer; @@ -24,6 +25,8 @@ public class HamsterBallPlugin extends Plugin { public void onLoad() { instance = this; Logger logger = getLogger(); + BallConfigManager.init(); + logger.info("BallConfigManager 已初始化."); BallBungeeCordAPI.init(); logger.info("BallBungeeCordAPI 已初始化."); try { @@ -40,12 +43,15 @@ public class HamsterBallPlugin extends Plugin { Logger logger = getLogger(); ProxyServer.getInstance().getPluginManager().registerListener(this, BallBungeeCordListener.INSTANCE); logger.info("已注册 BallBungeeCordListener."); - for (BallServerInfo serverInfo : BallAPI.getInstance().getAllServerInfo().values()) { - if (serverInfo.getType() != BallServerType.GAME) { - continue; + if (BallConfigManager.isAutoRegisterServer()) { + ProxyServer.getInstance().getServers().clear(); + for (BallServerInfo serverInfo : BallAPI.getInstance().getAllServerInfo().values()) { + if (serverInfo.getType() != BallServerType.GAME) { + continue; + } + ProxyServer.getInstance().getServers().put(serverInfo.getId(), BallBungeeCordUtils.getServerInfo(serverInfo)); + HamsterBallPlugin.getInstance().getLogger().info("已添加子服 " + serverInfo.getId() + " 的接入点配置."); } - ProxyServer.getInstance().getServers().put(serverInfo.getId(), BallBungeeCordUtils.getServerInfo(serverInfo)); - HamsterBallPlugin.getInstance().getLogger().info("已添加子服 " + serverInfo.getId() + " 的接入点配置."); } BallAPI.getInstance().sendBallMessage( BallAPI.BALL_CHANNEL, diff --git a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java index fd9fa42..6f7b925 100644 --- a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java +++ b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java @@ -6,8 +6,8 @@ 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.listener.BallDebugListener; import cn.hamster3.mc.plugin.core.bungee.HamsterBallPlugin; +import cn.hamster3.mc.plugin.core.bungee.core.BallConfigManager; import cn.hamster3.mc.plugin.core.bungee.listener.BallBungeeCordListener; -import cn.hamster3.mc.plugin.core.bungee.util.BungeeCordUtils; import net.md_5.bungee.config.Configuration; import org.jetbrains.annotations.NotNull; @@ -27,8 +27,7 @@ public class BallBungeeCordAPI extends BallAPI { if (instance != null) { return; } - HamsterBallPlugin plugin = HamsterBallPlugin.getInstance(); - Configuration pluginConfig = BungeeCordUtils.getPluginConfig(plugin); + Configuration pluginConfig = BallConfigManager.getPluginConfig(); BallConfig config = new BallConfig( new BallServerInfo( pluginConfig.getString("server-info.id"), diff --git a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/core/BallConfigManager.java b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/core/BallConfigManager.java new file mode 100644 index 0000000..4d7d354 --- /dev/null +++ b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/core/BallConfigManager.java @@ -0,0 +1,27 @@ +package cn.hamster3.mc.plugin.core.bungee.core; + +import cn.hamster3.mc.plugin.core.bungee.HamsterBallPlugin; +import cn.hamster3.mc.plugin.core.bungee.util.BungeeCordUtils; +import net.md_5.bungee.config.Configuration; + +public final class BallConfigManager { + private static Configuration pluginConfig; + private static boolean autoRegisterServer; + + private BallConfigManager() { + } + + public static void init() { + HamsterBallPlugin plugin = HamsterBallPlugin.getInstance(); + pluginConfig = BungeeCordUtils.getPluginConfig(plugin); + autoRegisterServer = pluginConfig.getBoolean("auto-register-server", true); + } + + public static Configuration getPluginConfig() { + return pluginConfig; + } + + public static boolean isAutoRegisterServer() { + return autoRegisterServer; + } +} diff --git a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/listener/BallBungeeCordListener.java b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/listener/BallBungeeCordListener.java index 1c8cc4f..6650b1b 100644 --- a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/listener/BallBungeeCordListener.java +++ b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/listener/BallBungeeCordListener.java @@ -11,6 +11,7 @@ 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.bungee.HamsterBallPlugin; +import cn.hamster3.mc.plugin.core.bungee.core.BallConfigManager; import cn.hamster3.mc.plugin.core.bungee.util.BallBungeeCordUtils; import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.constant.CoreConstantObjects; @@ -102,6 +103,9 @@ public final class BallBungeeCordListener extends BallListener implements Listen @Override public void onServerOnline(@NotNull ServerOnlineEvent event) { + if (!BallConfigManager.isAutoRegisterServer()) { + return; + } BallServerInfo serverInfo = event.getServerInfo(); if (serverInfo.getType() != BallServerType.GAME) { return; @@ -112,6 +116,9 @@ public final class BallBungeeCordListener extends BallListener implements Listen @Override public void onServerOffline(@NotNull ServerOfflineEvent event) { + if (!BallConfigManager.isAutoRegisterServer()) { + return; + } Map map = ProxyServer.getInstance().getServers(); if (map.remove(event.getServerID()) != null) { HamsterBallPlugin.getInstance().getLogger().info("已移除子服 " + event.getServerID() + " 的接入点配置."); diff --git a/hamster-ball-bungeecord/src/main/resources/config.yml b/hamster-ball-bungeecord/src/main/resources/config.yml index 93dc002..3591f54 100644 --- a/hamster-ball-bungeecord/src/main/resources/config.yml +++ b/hamster-ball-bungeecord/src/main/resources/config.yml @@ -13,3 +13,9 @@ server-info: name: "代理端" host: 0.0.0.0 port: 25577 + +# 是否允许 HamsterBall 自动添加子服接入点配置 +# 开启该功能后,插件启动时将会自动清除 BC 中配置的所有子服信息 +# 并从数据库中读取子服的数据,然后自动填充 +# 使用的链接地址取决于子服 HamsterBall 配置文件中的 server-info 信息 +auto-register-server: true diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java index 1f9c05b..09f228f 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java @@ -95,16 +95,32 @@ public abstract class BallAPI { playerInfo.put(info.getUuid(), info); } - @Override - public void onServerOffline(@NotNull ServerOfflineEvent event) { - String serverID = event.getServerID(); - serverInfo.remove(serverID); - } - @Override public void onServerOnline(@NotNull ServerOnlineEvent event) { BallServerInfo info = event.getServerInfo(); serverInfo.put(info.getId(), info); + playerInfo.forEachValue(1, playerInfo -> { + if (playerInfo.getGameServer().equals(info.getId())) { + playerInfo.setOnline(false); + } + if (playerInfo.getProxyServer().equals(info.getId())) { + playerInfo.setOnline(false); + } + }); + } + + @Override + public void onServerOffline(@NotNull ServerOfflineEvent event) { + String serverID = event.getServerID(); + serverInfo.remove(serverID); + playerInfo.forEachValue(1, playerInfo -> { + if (playerInfo.getGameServer().equals(serverID)) { + playerInfo.setOnline(false); + } + if (playerInfo.getProxyServer().equals(serverID)) { + playerInfo.setOnline(false); + } + }); } }); } @@ -255,10 +271,19 @@ public abstract class BallAPI { sendBallMessage(new BallMessageInfo(BALL_CHANNEL, ServerOfflineEvent.ACTION, new ServerOfflineEvent(getLocalServerId())), true); try (Connection connection = CoreAPI.getInstance().getConnection()) { - PreparedStatement statement = connection.prepareStatement("DELETE FROM `hamster_ball_server_info` WHERE `id`=?;"); - statement.setString(1, getLocalServerId()); - statement.executeUpdate(); - statement.close(); + 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;