issue fix backport

This commit is contained in:
mlus 2024-06-07 17:36:03 +08:00
parent d16e42c505
commit 226bc39185
4 changed files with 15 additions and 4 deletions

View File

@ -3,4 +3,4 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
mod_version=1.18.2-1.2.1
mod_version=1.18.2-1.3.2

View File

@ -14,6 +14,7 @@ public class JdbcConfig {
public static ForgeConfigSpec.IntValue PORT;
public static ForgeConfigSpec.ConfigValue<String> USERNAME;
public static ForgeConfigSpec.ConfigValue<String> PASSWORD;
public static ForgeConfigSpec.ConfigValue<String> DATABASE_NAME;
public static ForgeConfigSpec.ConfigValue<List<String>> 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);

View File

@ -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

View File

@ -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)) {