From 8b1c20a4418311fdd0065002f93b52f7d97f19de Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:38:27 +0900 Subject: [PATCH 1/5] Set projectBranch to 'development' --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 79eaf20d..430f2859 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { group = 'net.coreprotect' String projectVersion = '22.2' -String projectBranch = '' +String projectBranch = 'development' version = projectVersion // `version` might be modified, we don't always want that (e.g. plugin.yml) description = 'Provides block protection for your server.' sourceCompatibility = '17' From 59166ad45b12ddee39f4381dadffab4f83bf7fdd Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Fri, 29 Dec 2023 23:03:48 +0900 Subject: [PATCH 2/5] Update Folia API dependency --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 79eaf20d..872d215c 100644 --- a/build.gradle +++ b/build.gradle @@ -34,7 +34,7 @@ dependencies { compileOnly('com.sk89q.worldedit:worldedit-bukkit:7.0.0-SNAPSHOT') { exclude group: 'org.bukkit' } - compileOnly 'dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT' + compileOnly 'dev.folia:folia-api:1.20.2-R0.1-SNAPSHOT' implementation 'org.bstats:bstats-bukkit-lite:1.8' implementation 'com.zaxxer:HikariCP:5.0.1' } From 7c00d4c42fec2a04fdd8a049223be0eed1687555 Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:22:24 +0900 Subject: [PATCH 3/5] Add containerTransactionsLookup API method --- .../java/net/coreprotect/CoreProtectAPI.java | 47 ++++++++++- .../api/ContainerTransactionsAPI.java | 84 +++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java diff --git a/src/main/java/net/coreprotect/CoreProtectAPI.java b/src/main/java/net/coreprotect/CoreProtectAPI.java index 464d0bad..a36a9a38 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(Block block, int time) { + if (Config.getGlobal().API_ENABLED) { + return ContainerTransactionsAPI.performLookup(block, 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..00bf8356 --- /dev/null +++ b/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java @@ -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 performLookup(Block block, int offset) { + List 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; + } + +} From a58d764075c98afe93e46778d6601a2244eed523 Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:40:48 +0900 Subject: [PATCH 4/5] Revert "Set projectBranch to 'development'" This reverts commit 8b1c20a4418311fdd0065002f93b52f7d97f19de. --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 8a477abc..872d215c 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { group = 'net.coreprotect' String projectVersion = '22.2' -String projectBranch = 'development' +String projectBranch = '' version = projectVersion // `version` might be modified, we don't always want that (e.g. plugin.yml) description = 'Provides block protection for your server.' sourceCompatibility = '17' From 1540fecf93c11fb8859d0af7854f82bf54074519 Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Sun, 31 Dec 2023 19:26:05 +0900 Subject: [PATCH 5/5] Change first argument of containerTransactionsLookup to Location --- .../java/net/coreprotect/CoreProtectAPI.java | 4 +-- .../api/ContainerTransactionsAPI.java | 27 ++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/coreprotect/CoreProtectAPI.java b/src/main/java/net/coreprotect/CoreProtectAPI.java index a36a9a38..ad29ec0f 100755 --- a/src/main/java/net/coreprotect/CoreProtectAPI.java +++ b/src/main/java/net/coreprotect/CoreProtectAPI.java @@ -216,9 +216,9 @@ public List blockLookup(Block block, int time) { return null; } - public List containerTransactionsLookup(Block block, int time) { + public List containerTransactionsLookup(Location location, int time) { if (Config.getGlobal().API_ENABLED) { - return ContainerTransactionsAPI.performLookup(block, time); + return ContainerTransactionsAPI.performLookup(location, time); } return null; } diff --git a/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java b/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java index 00bf8356..03db4d27 100644 --- a/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java +++ b/src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java @@ -1,12 +1,5 @@ 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; @@ -16,6 +9,14 @@ 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; @@ -24,19 +25,19 @@ private ContainerTransactionsAPI() { throw new AssertionError(); } - public static List performLookup(Block block, int offset) { + public static List performLookup(Location location, int offset) { List result = new ArrayList<>(); - if (block == null) { + if (location == 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 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(block.getWorld().getName()); + final int worldId = Util.getWorldId(location.getWorld().getName()); final int timeFrom = offset > 0 ? now - offset : 0; if (connection == null) {