Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/net/coreprotect/CoreProtectAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;

import net.coreprotect.api.result.ContainerResult;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -117,6 +118,20 @@ public List<String[]> queueLookup(Block block) {
return QueueLookup.performLookup(block);
}

/**
* Performs a lookup of container-related actions at the specified location.
*
* @param location The location to look up
* @param time Time constraint in seconds (0 means no time constraint)
* @return List of results in a ContainerResult list format
*/
public List<ContainerResult> containerLookup(Location location, int time) {
if (isEnabled()) {
return BlockAPI.performContainerLookup(location, time);
}
return null;
}

/**
* Performs a lookup on session data for the specified user.
*
Expand Down
133 changes: 114 additions & 19 deletions src/main/java/net/coreprotect/api/BlockAPI.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package net.coreprotect.api;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.block.Block;

import net.coreprotect.api.result.ContainerResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.BlockUtils;
import net.coreprotect.utility.StringUtils;
import net.coreprotect.utility.WorldUtils;
import org.bukkit.Location;
import org.bukkit.block.Block;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE moved imports let me know if you want me to revert this

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
* Provides API methods for block-related lookups in the CoreProtect database.
Expand All @@ -29,13 +30,42 @@ private BlockAPI() {
throw new IllegalStateException("API class");
}

/**
* Creates a prepared statement with WHERE clause for block/container lookups.
*
* @param connection Database connection
* @param tableType Either "block" or "container"
* @param selectClause The SELECT portion of the query
* @param location The block to look up
* @param checkTime The minimum time constraint
* @return PreparedStatement with parameters set
* @throws Exception if there's an error creating the statement
*/
private static PreparedStatement createLocationQuery(Connection connection, String tableType, String selectClause, Location location, int checkTime) throws Exception {
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
int worldId = WorldUtils.getWorldId(location.getWorld().getName());

String query = "SELECT " + selectClause + " FROM " + ConfigHandler.prefix + tableType + " " +
WorldUtils.getWidIndex(tableType) +
" WHERE wid = ? AND x = ? AND z = ? AND y = ? AND time > ? ORDER BY rowid DESC";

PreparedStatement statement = connection.prepareStatement(query);
statement.setInt(1, worldId);
statement.setInt(2, x);
statement.setInt(3, z);
statement.setInt(4, y);
statement.setInt(5, checkTime);

return statement;
}

/**
* Performs a lookup of block-related actions at the specified block.
*
* @param block
* The block to look up
* @param offset
* Time constraint in seconds (0 means no time constraint)
*
* @param block The block to look up
* @param offset Time constraint in seconds (0 means no time constraint)
* @return List of results in a String array format
*/
public static List<String[]> performLookup(Block block, int offset) {
Expand Down Expand Up @@ -65,10 +95,8 @@ public static List<String[]> performLookup(Block block, int offset) {
checkTime = time - offset;
}

try (Statement statement = connection.createStatement()) {
String query = "SELECT time,user,action,type,data,blockdata,rolled_back FROM " + ConfigHandler.prefix + "block " + WorldUtils.getWidIndex("block") + "WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "' AND time > '" + checkTime + "' ORDER BY rowid DESC";

try (ResultSet results = statement.executeQuery(query)) {
try (PreparedStatement statement = createLocationQuery(connection, "block", "time,user,action,type,data,blockdata,rolled_back", block.getLocation(), checkTime)) {
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
String resultTime = results.getString("time");
int resultUserId = results.getInt("user");
Expand All @@ -85,14 +113,81 @@ public static List<String[]> performLookup(Block block, int offset) {
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
String blockData = BlockUtils.byteDataToString(resultBlockData, resultType);

String[] lookupData = new String[] { resultTime, resultUser, String.valueOf(x), String.valueOf(y), String.valueOf(z), String.valueOf(resultType), resultData, resultAction, resultRolledBack, String.valueOf(worldId), blockData };
String[] lookupData = new String[]{resultTime, resultUser, String.valueOf(x), String.valueOf(y), String.valueOf(z), String.valueOf(resultType), resultData, resultAction, resultRolledBack, String.valueOf(worldId), blockData};

result.add(StringUtils.toStringArray(lookupData));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
catch (Exception e) {

return result;
}

/**
* Performs a lookup of container-related actions at the specified location.
*
* @param location The location to look up
* @param offset Time constraint in seconds (0 means no time constraint)
* @return List of results in a ContainerResult array format
*/
public static List<ContainerResult> performContainerLookup(Location location, int offset) {
List<ContainerResult> result = new ArrayList<>();

if (!Config.getGlobal().API_ENABLED) {
return result;
}

if (location == null) {
return result;
}

try (Connection connection = Database.getConnection(false, 1000)) {
if (connection == null) {
return result;
}

int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
int time = (int) (System.currentTimeMillis() / 1000L);
int checkTime = 0;

if (offset > 0) {
checkTime = time - offset;
}

try (PreparedStatement statement = createLocationQuery(connection, "container", "time,user,type,data,amount,metadata,action,rolled_back", location, checkTime)) {
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
int resultUserId = results.getInt("user");
int resultAction = results.getInt("action");
int resultType = results.getInt("type");
int resultData = results.getInt("data");
long resultTime = results.getLong("time");
int resultAmount = results.getInt("amount");
int resultRolledBack = results.getInt("rolled_back");
byte[] resultMetadata = results.getBytes("metadata");

if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
UserStatement.loadName(connection, resultUserId);
}

String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);

ContainerResult containerResult = new ContainerResult(
resultTime, resultUser, location.getWorld().getName(), x, y, z,
resultType, resultData, resultAmount, resultMetadata,
resultAction, resultRolledBack
);

result.add(containerResult);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

Expand Down
Loading