From 29f02c4413d2417cd0993077e2302937ad518aa3 Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Thu, 3 Nov 2022 23:36:52 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=BB=BA=E8=A1=A8=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/BallBungeeCordListener.java | 3 +-- .../mc/plugin/ball/common/api/BallAPI.java | 26 +++++++++---------- .../common/constant/BallCommonConstants.java | 9 ------- 3 files changed, 14 insertions(+), 24 deletions(-) delete mode 100644 hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/constant/BallCommonConstants.java 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 0acf75f..e04243d 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 @@ -1,7 +1,6 @@ package cn.hamster3.mc.plugin.core.bungee.listener; import cn.hamster3.mc.plugin.ball.common.api.BallAPI; -import cn.hamster3.mc.plugin.ball.common.constant.BallCommonConstants; 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; @@ -208,7 +207,7 @@ public final class BallBungeeCordListener extends BallListener implements Listen private void uploadPlayerInfo(BallPlayerInfo playerInfo) { ProxyServer.getInstance().getScheduler().runAsync(HamsterBallPlugin.getInstance(), () -> { try (Connection connection = CoreAPI.getInstance().getConnection()) { - PreparedStatement statement = connection.prepareStatement("REPLACE INTO " + BallCommonConstants.SQL.PLAYER_INFO_TABLE + " VALUES(?, ?, ?, ?, ?);"); + PreparedStatement statement = connection.prepareStatement("REPLACE INTO `hamster_ball_player_info` VALUES(?, ?, ?, ?, ?);"); statement.setString(1, playerInfo.getUuid().toString()); statement.setString(2, playerInfo.getName()); statement.setString(3, playerInfo.getGameServer()); 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 5cd9587..9f78eb9 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 @@ -2,7 +2,6 @@ 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.constant.BallCommonConstants; 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; @@ -155,30 +154,31 @@ public abstract class BallAPI { { Statement statement = connection.createStatement(); - statement.execute("CREATE TABLE IF NOT EXISTS " + BallCommonConstants.SQL.PLAYER_INFO_TABLE + "(" + + 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 " + BallCommonConstants.SQL.SERVER_INFO_TABLE + "(" + + 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 " + BallCommonConstants.SQL.CACHED_MESSAGE_TABLE + "(" + + statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_cached_message`(" + "`uuid` CHAR(36) NOT NULL," + "`message` TEXT NOT NULL," + - "CONSTRAINT `fk_uuid` FOREIGN KEY (`uuid`) REFERENCES " + BallCommonConstants.SQL.PLAYER_INFO_TABLE + "(`uuid`) ON DELETE CASCADE ON UPDATE CASCADE" + + "CONSTRAINT `fk_uuid_ball` FOREIGN KEY (`uuid`) REFERENCES `hamster_ball_player_info`(`uuid`) " + + "ON DELETE CASCADE ON UPDATE CASCADE" + ") CHARSET utf8mb4;"); statement.close(); } { - PreparedStatement statement = connection.prepareStatement("REPLACE INTO " + BallCommonConstants.SQL.SERVER_INFO_TABLE + " VALUES(?, ?, ?, ?, ?);"); + 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()); @@ -189,7 +189,7 @@ public abstract class BallAPI { } { - PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + BallCommonConstants.SQL.SERVER_INFO_TABLE + ";"); + PreparedStatement statement = connection.prepareStatement("SELECT * FROM `hamster_ball_server_info`;"); ResultSet set = statement.executeQuery(); while (set.next()) { String serverID = set.getString("id"); @@ -206,7 +206,7 @@ public abstract class BallAPI { } { - PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + BallCommonConstants.SQL.PLAYER_INFO_TABLE + ";"); + PreparedStatement statement = connection.prepareStatement("SELECT * FROM `hamster_ball_player_info`;"); ResultSet set = statement.executeQuery(); while (set.next()) { UUID uuid = UUID.fromString(set.getString("uuid")); @@ -282,7 +282,7 @@ 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 " + BallCommonConstants.SQL.SERVER_INFO_TABLE + " WHERE `id`=?;"); + PreparedStatement statement = connection.prepareStatement("DELETE FROM `hamster_ball_server_info` WHERE `id`=?;"); statement.setString(1, getLocalServerId()); statement.executeUpdate(); statement.close(); @@ -422,7 +422,7 @@ public abstract class BallAPI { return; } try (Connection connection = CoreAPI.getInstance().getConnection()) { - PreparedStatement statement = connection.prepareStatement("INSERT INTO " + BallCommonConstants.SQL.CACHED_MESSAGE_TABLE + " VALUES(?, ?);"); + PreparedStatement statement = connection.prepareStatement("INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);"); statement.setString(1, receiver.toString()); statement.setString(2, message.saveToJson().toString()); statement.executeUpdate(); @@ -459,7 +459,7 @@ public abstract class BallAPI { continue; } try (Connection connection = CoreAPI.getInstance().getConnection()) { - PreparedStatement statement = connection.prepareStatement("INSERT INTO " + BallCommonConstants.SQL.CACHED_MESSAGE_TABLE + " VALUES(?, ?);"); + PreparedStatement statement = connection.prepareStatement("INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);"); statement.setString(1, receiver.toString()); statement.setString(2, message.saveToJson().toString()); statement.executeUpdate(); @@ -636,7 +636,7 @@ public abstract class BallAPI { public List getCachedPlayerMessage(@NotNull UUID uuid) throws SQLException { ArrayList list = new ArrayList<>(); try (Connection connection = CoreAPI.getInstance().getConnection()) { - PreparedStatement statement = connection.prepareStatement("SELECT message FROM " + BallCommonConstants.SQL.CACHED_MESSAGE_TABLE + " WHERE `uuid`=?;"); + 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()) { @@ -650,7 +650,7 @@ public abstract class BallAPI { public void removeCachedPlayerMessage(@NotNull UUID uuid) throws SQLException { try (Connection connection = CoreAPI.getInstance().getConnection()) { - PreparedStatement statement = connection.prepareStatement("DELETE FROM " + BallCommonConstants.SQL.CACHED_MESSAGE_TABLE + " WHERE `uuid`=?;"); + PreparedStatement statement = connection.prepareStatement("DELETE FROM `hamster_ball_cached_message` WHERE `uuid`=?;"); statement.setString(1, uuid.toString()); statement.executeUpdate(); statement.close(); diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/constant/BallCommonConstants.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/constant/BallCommonConstants.java deleted file mode 100644 index d240bff..0000000 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/constant/BallCommonConstants.java +++ /dev/null @@ -1,9 +0,0 @@ -package cn.hamster3.mc.plugin.ball.common.constant; - -public interface BallCommonConstants { - interface SQL { - String PLAYER_INFO_TABLE = "hamster_ball_player_info"; - String SERVER_INFO_TABLE = "hamster_ball_server_info"; - String CACHED_MESSAGE_TABLE = "hamster_ball_cached_message"; - } -}