feat: 允许数据库独立于core配置

This commit is contained in:
2023-08-12 12:59:52 +08:00
parent 11327d368e
commit fa7f07657f
12 changed files with 251 additions and 63 deletions

View File

@@ -15,7 +15,6 @@ import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent;
import cn.hamster3.mc.plugin.ball.common.listener.BallListener;
import cn.hamster3.mc.plugin.ball.common.listener.ListenerPriority;
import cn.hamster3.mc.plugin.ball.common.utils.OS;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
@@ -29,6 +28,7 @@ import io.netty.channel.EventLoopGroup;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.sql.DataSource;
import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -183,7 +183,7 @@ public abstract class BallAPI {
connect();
try (Connection connection = CoreAPI.getInstance().getConnection()) {
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_player_info`(" +
"`uuid` CHAR(36) PRIMARY KEY," +
@@ -310,7 +310,7 @@ public abstract class BallAPI {
sendBallMessage(new BallMessageInfo(BALL_CHANNEL, ServerOfflineEvent.ACTION, new ServerOfflineEvent(getLocalServerId())), true);
try (Connection connection = CoreAPI.getInstance().getConnection()) {
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
"DELETE FROM `hamster_ball_server_info` WHERE `id`=?;"
)) {
@@ -460,12 +460,14 @@ public abstract class BallAPI {
if (!cache) {
return;
}
try (Connection connection = CoreAPI.getInstance().getConnection()) {
PreparedStatement statement = connection.prepareStatement("INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);");
statement.setString(1, receiver.toString());
statement.setString(2, message.toJson().toString());
statement.executeUpdate();
statement.close();
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);"
)) {
statement.setString(1, receiver.toString());
statement.setString(2, message.toJson().toString());
statement.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}
@@ -497,12 +499,14 @@ public abstract class BallAPI {
if (info != null && info.isOnline()) {
continue;
}
try (Connection connection = CoreAPI.getInstance().getConnection()) {
PreparedStatement statement = connection.prepareStatement("INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);");
statement.setString(1, receiver.toString());
statement.setString(2, message.toJson().toString());
statement.executeUpdate();
statement.close();
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);"
)) {
statement.setString(1, receiver.toString());
statement.setString(2, message.toJson().toString());
statement.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}
@@ -677,25 +681,30 @@ public abstract class BallAPI {
@NotNull
public List<DisplayMessage> getCachedPlayerMessage(@NotNull UUID uuid) throws SQLException {
ArrayList<DisplayMessage> list = new ArrayList<>();
try (Connection connection = CoreAPI.getInstance().getConnection()) {
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()) {
DisplayMessage msg = CoreUtils.GSON.fromJson(set.getString("message"), DisplayMessage.class);
list.add(msg);
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
"SELECT message FROM `hamster_ball_cached_message` WHERE `uuid`=?;"
)) {
statement.setString(1, uuid.toString());
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
DisplayMessage msg = CoreUtils.GSON.fromJson(set.getString("message"), DisplayMessage.class);
list.add(msg);
}
}
}
statement.close();
}
return list;
}
public void removeCachedPlayerMessage(@NotNull UUID uuid) throws SQLException {
try (Connection connection = CoreAPI.getInstance().getConnection()) {
PreparedStatement statement = connection.prepareStatement("DELETE FROM `hamster_ball_cached_message` WHERE `uuid`=?;");
statement.setString(1, uuid.toString());
statement.executeUpdate();
statement.close();
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
"DELETE FROM `hamster_ball_cached_message` WHERE `uuid`=?;"
)) {
statement.setString(1, uuid.toString());
statement.executeUpdate();
}
}
}
@@ -861,4 +870,7 @@ public abstract class BallAPI {
@NotNull
public abstract Logger getLogger();
@NotNull
public abstract DataSource getDatasource();
}