for cat server

This commit is contained in:
mlus-Asuka 2024-01-06 20:23:09 +08:00
parent 2e3694d7cb
commit 079ccb4c09
2 changed files with 11 additions and 19 deletions

View File

@ -12,6 +12,7 @@ import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import vip.fubuki.playersync.util.JDBCsetUp;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -24,7 +25,11 @@ public abstract class MixinPlayerProgress {
tag.putBoolean("PlayerSync",false);
String nbt = tag.toString().replace(",","|").replace("\"","^").replace("{","<").replace("}",">").replace("'","~");
try {
JDBCsetUp.executeUpdate("INSERT INTO AstralSorcery(player,tag) VALUES('" + tag.getString("UUID") + "','" + nbt + "') ON DUPLICATE KEY UPDATE tag='" + tag + "';");
PreparedStatement preparedStatement= JDBCsetUp.getConnection().prepareStatement("INSERT INTO AstralSorcery(player,tag) VALUES(?,?) ON DUPLICATE KEY UPDATE player=?");
preparedStatement.setString(1,tag.getString("UUID"));
preparedStatement.setString(2,nbt);
preparedStatement.setString(3,tag.getString("UUID"));
preparedStatement.executeUpdate();
} catch (SQLException throwable) {
throwable.printStackTrace();
}
@ -34,12 +39,13 @@ public abstract class MixinPlayerProgress {
@Inject(method = "load",at=@At(value="HEAD"), cancellable = true)
private void load(CompoundNBT compound, CallbackInfo ci){
if(compound.get("PlayerSync")==null || !compound.getBoolean("PlayerSync")){
compound.putBoolean("PlayerSync",true);
try {
ResultSet result= JDBCsetUp.executeQuery("SELECT * FROM AstralSorcery WHERE player='" + compound.getString("UUID") + "';").getResultSet();
if(result.next()){
String nbt = result.getString("tag").replace("|",",").replace("^","\"").replace("<","{").replace(">","}").replace("~", "'");
load(JsonToNBT.parseTag(nbt));
compound=JsonToNBT.parseTag(nbt);
compound.putBoolean("PlayerSync",true);
load(compound);
}
}catch (SQLException e){

View File

@ -1,41 +1,27 @@
package vip.fubuki.playersync.util;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import vip.fubuki.playersync.config.JdbcConfig;
import java.sql.*;
import java.util.Map;
public class JDBCsetUp {
public static Connection getConnection() throws SQLException {
String url= "jdbc:mysql://"+JdbcConfig.HOST.get()+":"+JdbcConfig.PORT.get()+"?useUnicode=true&characterEncoding=utf-8&useSSL="+JdbcConfig.USE_SSL.get()+"&serverTimezone=UTC&allowPublicKeyRetrieval=true";
String url= "jdbc:mysql://"+JdbcConfig.HOST.get()+":"+JdbcConfig.PORT.get()+"/playersync?useUnicode=true&characterEncoding=utf-8&useSSL="+JdbcConfig.USE_SSL.get()+"&serverTimezone=UTC&allowPublicKeyRetrieval=true";
return DriverManager.getConnection(url, JdbcConfig.USERNAME.get(), JdbcConfig.PASSWORD.get());
}
public static QueryResult executeQuery(String sql) throws SQLException{
Connection connection = getConnection();
PreparedStatement useStatement = connection.prepareStatement("USE playersync");
useStatement.executeUpdate();
PreparedStatement queryStatement = connection.prepareStatement(sql);
ResultSet resultSet =queryStatement.executeQuery();
ResultSet resultSet = queryStatement.executeQuery();
return new QueryResult(connection,resultSet);
}
public static int executeUpdate(String sql) throws SQLException{
return executeUpdate(sql,0);
}
public static int executeUpdate(String sql,int init) throws SQLException{
try (Connection connection = getConnection()) {
if(init==0){
sql="USE playersync;" + sql;
}
try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {
return updateStatement.executeUpdate();
}