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) { 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 (BlockGroup.CONTAINERS.contains(type) && !BlockGroup.SHULKER_BOXES.contains(type)) {
if (Config.getConfig(location.getWorld()).ITEM_TRANSACTIONS) { if (Config.getConfig(location.getWorld()).ITEM_TRANSACTIONS) {
@ -286,8 +290,13 @@ public class Database extends Queue {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
if (keys) { if (keys) {
if (hasReturningKeys()) {
preparedStatement = connection.prepareStatement(query + " RETURNING rowid");
}
else {
preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
} }
}
else { else {
preparedStatement = connection.prepareStatement(query); preparedStatement = connection.prepareStatement(query);
} }

View file

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

View file

@ -8,6 +8,7 @@ import org.bukkit.block.BlockState;
import org.bukkit.block.Skull; import org.bukkit.block.Skull;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.SkullStatement; import net.coreprotect.database.statement.SkullStatement;
import net.coreprotect.utility.Util; import net.coreprotect.utility.Util;
@ -29,12 +30,19 @@ public class SkullBreakLogger {
int skullKey = 0; int skullKey = 0;
if (skull.hasOwner()) { if (skull.hasOwner()) {
skullOwner = skull.getOwningPlayer().getUniqueId().toString(); skullOwner = skull.getOwningPlayer().getUniqueId().toString();
SkullStatement.insert(preparedStmt2, time, skullOwner); ResultSet resultSet = SkullStatement.insert(preparedStmt2, time, skullOwner);
if (Database.hasReturningKeys()) {
resultSet.next();
skullKey = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys(); ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next(); keys.next();
skullKey = keys.getInt(1); skullKey = keys.getInt(1);
keys.close(); keys.close();
} }
}
BlockBreakLogger.log(preparedStmt, batchCount, user, block.getLocation(), type, skullKey, null, block.getBlockData().getAsString(), null); 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 org.bukkit.block.Skull;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.SkullStatement; import net.coreprotect.database.statement.SkullStatement;
public class SkullPlaceLogger { public class SkullPlaceLogger {
@ -31,13 +32,20 @@ public class SkullPlaceLogger {
String skullOwner = ""; String skullOwner = "";
if (skull.hasOwner()) { if (skull.hasOwner()) {
skullOwner = skull.getOwningPlayer().getUniqueId().toString(); skullOwner = skull.getOwningPlayer().getUniqueId().toString();
SkullStatement.insert(preparedStmt2, time, skullOwner); ResultSet resultSet = SkullStatement.insert(preparedStmt2, time, skullOwner);
if (Database.hasReturningKeys()) {
resultSet.next();
skullKey = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys(); ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next(); keys.next();
skullKey = keys.getInt(1); skullKey = keys.getInt(1);
keys.close(); keys.close();
} }
} }
}
BlockPlaceLogger.log(preparedStmt, batchCount, user, block, replaceType, replaceData, type, skullKey, true, null, null, null); BlockPlaceLogger.log(preparedStmt, batchCount, user, block, replaceType, replaceData, type, skullKey, true, null, null, null);
} }

View file

@ -12,13 +12,15 @@ import org.bukkit.block.BlockState;
import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream; import org.bukkit.util.io.BukkitObjectOutputStream;
import net.coreprotect.database.Database;
public class EntityStatement { public class EntityStatement {
private EntityStatement() { private EntityStatement() {
throw new IllegalStateException("Database class"); 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 { try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
BukkitObjectOutputStream oos = new BukkitObjectOutputStream(bos); BukkitObjectOutputStream oos = new BukkitObjectOutputStream(bos);
@ -30,11 +32,18 @@ public class EntityStatement {
byte[] byte_data = bos.toByteArray(); byte[] byte_data = bos.toByteArray();
preparedStmt.setInt(1, time); preparedStmt.setInt(1, time);
preparedStmt.setObject(2, byte_data); preparedStmt.setObject(2, byte_data);
if (Database.hasReturningKeys()) {
return preparedStmt.executeQuery();
}
else {
preparedStmt.executeUpdate(); preparedStmt.executeUpdate();
} }
}
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
public static List<Object> getData(Statement statement, BlockState block, String query) { 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.BlockState;
import org.bukkit.block.Skull; import org.bukkit.block.Skull;
import net.coreprotect.database.Database;
public class SkullStatement { public class SkullStatement {
private SkullStatement() { private SkullStatement() {
throw new IllegalStateException("Database class"); 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 { try {
preparedStmt.setInt(1, time); preparedStmt.setInt(1, time);
preparedStmt.setString(2, owner); preparedStmt.setString(2, owner);
if (Database.hasReturningKeys()) {
return preparedStmt.executeQuery();
}
else {
preparedStmt.executeUpdate(); preparedStmt.executeUpdate();
} }
}
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
public static void getData(Statement statement, BlockState block, String query) { 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.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
public class UserStatement { public class UserStatement {
@ -21,14 +22,32 @@ public class UserStatement {
try { try {
int unixtimestamp = (int) (System.currentTimeMillis() / 1000L); 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.setInt(1, unixtimestamp);
preparedStmt.setString(2, user); preparedStmt.setString(2, user);
if (Database.hasReturningKeys()) {
ResultSet resultSet = preparedStmt.executeQuery();
resultSet.next();
id = resultSet.getInt(1);
resultSet.close();
}
else {
preparedStmt.executeUpdate(); preparedStmt.executeUpdate();
ResultSet keys = preparedStmt.getGeneratedKeys(); ResultSet keys = preparedStmt.getGeneratedKeys();
keys.next(); keys.next();
id = keys.getInt(1); id = keys.getInt(1);
keys.close(); keys.close();
}
preparedStmt.close(); preparedStmt.close();
} }
catch (Exception e) { catch (Exception e) {