Skip to content

Commit

Permalink
issue fix backport
Browse files Browse the repository at this point in the history
  • Loading branch information
mlus-asuka committed Jun 7, 2024
1 parent d16e42c commit 226bc39
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/main/java/vip/fubuki/playersync/config/JdbcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/vip/fubuki/playersync/sync/ChatSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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;
Expand All @@ -25,7 +27,14 @@ public static void register(){

@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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vip/fubuki/playersync/util/JDBCsetUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static Connection getConnection() throws SQLException {

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);
Expand All @@ -25,7 +25,7 @@ public static QueryResult executeQuery(String sql) throws SQLException{
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)) {
Expand Down

0 comments on commit 226bc39

Please sign in to comment.