From 226bc39185d4718fa346a46d7da4e8c2603441a3 Mon Sep 17 00:00:00 2001 From: mlus <1319237806@qq.com> Date: Fri, 7 Jun 2024 17:36:03 +0800 Subject: [PATCH] issue fix backport --- gradle.properties | 2 +- .../java/vip/fubuki/playersync/config/JdbcConfig.java | 2 ++ .../java/vip/fubuki/playersync/sync/ChatSync.java | 11 ++++++++++- .../java/vip/fubuki/playersync/util/JDBCsetUp.java | 4 ++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index cc04406..3450076 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,4 +3,4 @@ org.gradle.jvmargs=-Xmx3G org.gradle.daemon=false -mod_version=1.18.2-1.2.1 \ No newline at end of file +mod_version=1.18.2-1.3.2 \ No newline at end of file diff --git a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java index 3408b14..7193036 100644 --- a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java +++ b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java @@ -14,6 +14,7 @@ public class JdbcConfig { public static ForgeConfigSpec.IntValue PORT; public static ForgeConfigSpec.ConfigValue USERNAME; public static ForgeConfigSpec.ConfigValue PASSWORD; + public static ForgeConfigSpec.ConfigValue DATABASE_NAME; public static ForgeConfigSpec.ConfigValue> SYNC_WORLD; public static ForgeConfigSpec.BooleanValue USE_SSL; public static ForgeConfigSpec.BooleanValue SYNC_CHAT; @@ -29,6 +30,7 @@ public class JdbcConfig { USE_SSL = COMMON_BUILDER.comment("whether use SSL").define("use_ssl", false); USERNAME = COMMON_BUILDER.comment("username").define("user_name", "root"); PASSWORD = COMMON_BUILDER.comment("password").define("password", "password"); + DATABASE_NAME = COMMON_BUILDER.comment("database name").define("db_name","playersync"); SERVER_ID = COMMON_BUILDER.comment("the server id should be unique").define("Server_id", new Random().nextInt(1,Integer.MAX_VALUE-1)); SYNC_WORLD = COMMON_BUILDER.comment("The worlds that will be synchronized.If running in server it is supposed to have only one").define("sync_world", new ArrayList<>()); SYNC_CHAT= COMMON_BUILDER.comment("Whether synchronize chat").define("sync_chat", true); diff --git a/src/main/java/vip/fubuki/playersync/sync/ChatSync.java b/src/main/java/vip/fubuki/playersync/sync/ChatSync.java index 1dfdd09..82475e8 100644 --- a/src/main/java/vip/fubuki/playersync/sync/ChatSync.java +++ b/src/main/java/vip/fubuki/playersync/sync/ChatSync.java @@ -8,6 +8,8 @@ import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; import vip.fubuki.playersync.util.JDBCsetUp; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Objects; @@ -25,7 +27,14 @@ public class ChatSync { @SubscribeEvent public static void onPlayerChat(net.minecraftforge.event.ServerChatEvent event) throws SQLException { - JDBCsetUp.executeUpdate("INSERT INTO chat (player, message, timestamp) VALUES ('" + event.getUsername() + "', '" + event.getMessage() + "', '" + current + "')"); + String sql = "INSERT INTO chat (player, message, timestamp) VALUES (?, ?, ?)"; + try (Connection connection = JDBCsetUp.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, event.getUsername()); + preparedStatement.setString(2, event.getMessage()); + preparedStatement.setLong(3, current); + preparedStatement.executeUpdate(); + } } @SubscribeEvent diff --git a/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java b/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java index b6b6300..4e08e57 100644 --- a/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java +++ b/src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java @@ -14,7 +14,7 @@ public class JDBCsetUp { public static QueryResult executeQuery(String sql) throws SQLException{ Connection connection = getConnection(); - PreparedStatement useStatement = connection.prepareStatement("USE `playersync`"); + PreparedStatement useStatement = connection.prepareStatement("USE "+JdbcConfig.DATABASE_NAME.get()); useStatement.executeUpdate(); PreparedStatement queryStatement = connection.prepareStatement(sql); @@ -25,7 +25,7 @@ public class JDBCsetUp { public static int executeUpdate(String sql) throws SQLException{ try (Connection connection = getConnection()) { - PreparedStatement useStatement = connection.prepareStatement("USE `playersync`"); + PreparedStatement useStatement = connection.prepareStatement("USE "+JdbcConfig.DATABASE_NAME.get()); useStatement.executeUpdate(); try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {