Fixed SQLFeatureNotSupportedException when logging entity data (fixes #455)

This commit is contained in:
Intelli 2023-09-25 14:04:50 -06:00
parent f76b0d45bd
commit d944c2bd89
7 changed files with 99 additions and 26 deletions

View File

@ -106,6 +106,10 @@ public class Database extends Queue {
}
}
public static boolean hasReturningKeys() {
return (!Config.getGlobal().MYSQL && ConfigHandler.SERVER_VERSION >= 20);
}
public static void containerBreakCheck(String user, Material type, Object container, ItemStack[] contents, Location location) {
if (BlockGroup.CONTAINERS.contains(type) && !BlockGroup.SHULKER_BOXES.contains(type)) {
if (Config.getConfig(location.getWorld()).ITEM_TRANSACTIONS) {
@ -286,7 +290,12 @@ public class Database extends Queue {
PreparedStatement preparedStatement = null;
try {
if (keys) {
preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
if (hasReturningKeys()) {
preparedStatement = connection.prepareStatement(query + " RETURNING rowid");
}
else {
preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
}
}
else {
preparedStatement = connection.prepareStatement(query);

View File

@ -10,6 +10,7 @@ import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.EntityStatement;
import net.coreprotect.database.statement.UserStatement;
@ -43,11 +44,21 @@ public class EntityKillLogger {
int x = block.getX();
int y = block.getY();
int z = block.getZ();
EntityStatement.insert(preparedStmt2, time, data);
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
int entity_key = keys.getInt(1);
keys.close();
int entity_key = 0;
ResultSet resultSet = EntityStatement.insert(preparedStmt2, time, data);
if (Database.hasReturningKeys()) {
resultSet.next();
entity_key = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
entity_key = keys.getInt(1);
keys.close();
}
BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, type, entity_key, null, null, 3, 0);
}
catch (Exception e) {

View File

@ -8,6 +8,7 @@ import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.SkullStatement;
import net.coreprotect.utility.Util;
@ -29,11 +30,18 @@ public class SkullBreakLogger {
int skullKey = 0;
if (skull.hasOwner()) {
skullOwner = skull.getOwningPlayer().getUniqueId().toString();
SkullStatement.insert(preparedStmt2, time, skullOwner);
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
ResultSet resultSet = SkullStatement.insert(preparedStmt2, time, skullOwner);
if (Database.hasReturningKeys()) {
resultSet.next();
skullKey = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
}
}
BlockBreakLogger.log(preparedStmt, batchCount, user, block.getLocation(), type, skullKey, null, block.getBlockData().getAsString(), null);

View File

@ -9,6 +9,7 @@ import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.SkullStatement;
public class SkullPlaceLogger {
@ -31,11 +32,18 @@ public class SkullPlaceLogger {
String skullOwner = "";
if (skull.hasOwner()) {
skullOwner = skull.getOwningPlayer().getUniqueId().toString();
SkullStatement.insert(preparedStmt2, time, skullOwner);
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
ResultSet resultSet = SkullStatement.insert(preparedStmt2, time, skullOwner);
if (Database.hasReturningKeys()) {
resultSet.next();
skullKey = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
}
}
}

View File

@ -12,13 +12,15 @@ import org.bukkit.block.BlockState;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import net.coreprotect.database.Database;
public class EntityStatement {
private EntityStatement() {
throw new IllegalStateException("Database class");
}
public static void insert(PreparedStatement preparedStmt, int time, List<Object> data) {
public static ResultSet insert(PreparedStatement preparedStmt, int time, List<Object> data) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BukkitObjectOutputStream oos = new BukkitObjectOutputStream(bos);
@ -30,11 +32,18 @@ public class EntityStatement {
byte[] byte_data = bos.toByteArray();
preparedStmt.setInt(1, time);
preparedStmt.setObject(2, byte_data);
preparedStmt.executeUpdate();
if (Database.hasReturningKeys()) {
return preparedStmt.executeQuery();
}
else {
preparedStmt.executeUpdate();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static List<Object> getData(Statement statement, BlockState block, String query) {

View File

@ -9,21 +9,30 @@ import org.bukkit.Bukkit;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import net.coreprotect.database.Database;
public class SkullStatement {
private SkullStatement() {
throw new IllegalStateException("Database class");
}
public static void insert(PreparedStatement preparedStmt, int time, String owner) {
public static ResultSet insert(PreparedStatement preparedStmt, int time, String owner) {
try {
preparedStmt.setInt(1, time);
preparedStmt.setString(2, owner);
preparedStmt.executeUpdate();
if (Database.hasReturningKeys()) {
return preparedStmt.executeQuery();
}
else {
preparedStmt.executeUpdate();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void getData(Statement statement, BlockState block, String query) {

View File

@ -9,6 +9,7 @@ import java.util.Locale;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
public class UserStatement {
@ -21,14 +22,32 @@ public class UserStatement {
try {
int unixtimestamp = (int) (System.currentTimeMillis() / 1000L);
PreparedStatement preparedStmt = connection.prepareStatement("INSERT INTO " + ConfigHandler.prefix + "user (time, user) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
PreparedStatement preparedStmt = null;
if (Database.hasReturningKeys()) {
preparedStmt = connection.prepareStatement("INSERT INTO " + ConfigHandler.prefix + "user (time, user) VALUES (?, ?) RETURNING rowid");
}
else {
preparedStmt = connection.prepareStatement("INSERT INTO " + ConfigHandler.prefix + "user (time, user) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
}
preparedStmt.setInt(1, unixtimestamp);
preparedStmt.setString(2, user);
preparedStmt.executeUpdate();
ResultSet keys = preparedStmt.getGeneratedKeys();
keys.next();
id = keys.getInt(1);
keys.close();
if (Database.hasReturningKeys()) {
ResultSet resultSet = preparedStmt.executeQuery();
resultSet.next();
id = resultSet.getInt(1);
resultSet.close();
}
else {
preparedStmt.executeUpdate();
ResultSet keys = preparedStmt.getGeneratedKeys();
keys.next();
id = keys.getInt(1);
keys.close();
}
preparedStmt.close();
}
catch (Exception e) {