Add containerTransactionsLookup API method

This commit is contained in:
takejohn 2023-12-31 18:22:24 +09:00
parent ddce775f78
commit 7c00d4c42f
2 changed files with 130 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package net.coreprotect;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
@ -17,8 +18,12 @@ import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import net.coreprotect.api.BlockAPI;
import net.coreprotect.api.ContainerTransactionsAPI;
import net.coreprotect.api.QueueLookup;
import net.coreprotect.api.SessionLookup;
import net.coreprotect.config.Config;
@ -117,9 +122,21 @@ public class CoreProtectAPI extends Queue {
return null;
}
final ItemMeta itemMeta = getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
return ((BlockStateMeta) itemMeta).getBlockState().getBlockData();
}
String blockData = parse[12];
if (blockData == null || blockData.length() == 0) {
return getType().createBlockData();
final Material type = getType();
if (type.isBlock()) {
return type.createBlockData();
}
else {
return null;
}
}
return Bukkit.getServer().createBlockData(blockData);
}
@ -147,6 +164,27 @@ public class CoreProtectAPI extends Queue {
public String worldName() {
return Util.getWorldName(Integer.parseInt(parse.length < 13 ? parse[5] : parse[9]));
}
public int getAmount() {
if (parse.length < 13) {
return 0;
}
return Integer.parseInt(parse[10]);
}
public ItemMeta getItemMeta() {
if (parse.length < 13) {
return null;
}
if (parse[11] == null || parse[11].isEmpty()) {
return null;
}
final byte[] metadata = parse[11].getBytes(StandardCharsets.ISO_8859_1);
final ItemStack item = (ItemStack) Rollback.populateItemStack(new ItemStack(getType(), getAmount()), metadata)[2];
return item.getItemMeta();
}
}
private static Map<Object, Boolean> parseList(List<Object> list) {
@ -178,6 +216,13 @@ public class CoreProtectAPI extends Queue {
return null;
}
public List<String[]> containerTransactionsLookup(Block block, int time) {
if (Config.getGlobal().API_ENABLED) {
return ContainerTransactionsAPI.performLookup(block, time);
}
return null;
}
public List<String[]> queueLookup(Block block) {
return QueueLookup.performLookup(block);
}

View File

@ -0,0 +1,84 @@
package net.coreprotect.api;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.Util;
import org.bukkit.block.Block;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
public final class ContainerTransactionsAPI {
private static final int CONNECTION_WAIT_TIME = 1000;
private ContainerTransactionsAPI() {
throw new AssertionError();
}
public static List<String[]> performLookup(Block block, int offset) {
List<String[]> result = new ArrayList<>();
if (block == null) {
return result;
}
try (Connection connection = Database.getConnection(false, CONNECTION_WAIT_TIME)) {
final int x = block.getX();
final int y = block.getY();
final int z = block.getZ();
final int now = (int) (System.currentTimeMillis() / 1000L);
final int worldId = Util.getWorldId(block.getWorld().getName());
final int timeFrom = offset > 0 ? now - offset : 0;
if (connection == null) {
return result;
}
try (Statement statement = connection.createStatement()) {
String query = "SELECT time,user,action,type,data,rolled_back,amount,metadata FROM " +
ConfigHandler.prefix + "container " + Util.getWidIndex("container") +
"WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "'" +
" AND time > '" + timeFrom + "' ORDER BY rowid DESC";
try (ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
final String resultTime = resultSet.getString("time");
final int resultUserId = resultSet.getInt("user");
final String resultAction = resultSet.getString("action");
final int resultType = resultSet.getInt("type");
final String resultData = resultSet.getString("data");
final String resultRolledBack = resultSet.getString("rolled_back");
final int resultAmount = resultSet.getInt("amount");
final byte[] resultMetadata = resultSet.getBytes("metadata");
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
UserStatement.loadName(connection, resultUserId);
}
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
final String metadata = resultMetadata != null ? new String(resultMetadata, StandardCharsets.ISO_8859_1) : "";
String[] resultElement = new String[]{ resultTime, resultUser,
String.valueOf(x), String.valueOf(y), String.valueOf(z), String.valueOf(resultType),
resultData, resultAction, resultRolledBack, String.valueOf(worldId),
String.valueOf(resultAmount), metadata, "" };
result.add(resultElement);
}
}
}
}
catch (SQLException e) {
CoreProtect.getInstance().getLogger().log(Level.WARNING, e.toString(), e);
}
return result;
}
}