diff --git a/src/main/java/net/coreprotect/CoreProtectAPI.java b/src/main/java/net/coreprotect/CoreProtectAPI.java index 464d0bad..ad29ec0f 100755 --- a/src/main/java/net/coreprotect/CoreProtectAPI.java +++ b/src/main/java/net/coreprotect/CoreProtectAPI.java @@ -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.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 BlockData getBlockData() { 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 boolean isRolledBack() { 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 parseList(List list) { @@ -178,6 +216,13 @@ public List blockLookup(Block block, int time) { return null; } + public List containerTransactionsLookup(Location location, int time) { + if (Config.getGlobal().API_ENABLED) { + return ContainerTransactionsAPI.performLookup(location, time); + } + return null; + } + public List queueLookup(Block block) { return QueueLookup.performLookup(block); } diff --git a/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java b/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java new file mode 100644 index 00000000..03db4d27 --- /dev/null +++ b/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java @@ -0,0 +1,85 @@ +package net.coreprotect.api; + +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; + +import org.bukkit.Location; + +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; + +public final class ContainerTransactionsAPI { + + private static final int CONNECTION_WAIT_TIME = 1000; + + private ContainerTransactionsAPI() { + throw new AssertionError(); + } + + public static List performLookup(Location location, int offset) { + List result = new ArrayList<>(); + + if (location == null) { + return result; + } + + try (Connection connection = Database.getConnection(false, CONNECTION_WAIT_TIME)) { + final int x = (int) location.getX(); + final int y = (int) location.getY(); + final int z = (int) location.getZ(); + final int now = (int) (System.currentTimeMillis() / 1000L); + final int worldId = Util.getWorldId(location.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; + } + +}