Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 45 additions & 2 deletions docs/api/version/v11.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ List<String[]> sessionLookup(String user, int time)

List<String[]> queueLookup(Block block)

List<ContainerResult> containerLookup(Location location, int time)

List<ContainerResult> containerLookup(World world, BoundingBox boundingBox, int time)

ParseResult parseResult(String[] result)

boolean logChat(Player player, String message)
Expand Down Expand Up @@ -224,6 +228,25 @@ This will perform a session lookup on a single player.

---

#### `containerLookup(Location location, int time)`

This will perform a container lookup at a single location.

* **location:** The location to perform the lookup on.
* **time:** Specify the amount of time to search back. "5" would return results from the last 5 seconds. Use "0" for no time constraint.

---

#### `containerLookup(World world, BoundingBox boundingBox, int time)`

This will perform a container lookup within a bounding box area.

* **world:** The world to perform the lookup in.
* **boundingBox:** The bounding box defining the area to search within.
* **time:** Specify the amount of time to search back. "5" would return results from the last 5 seconds. Use "0" for no time constraint.

---

#### `ParseResult parseResult(String[] result)`

This will parse results from a lookup. You'll then be able to view the following:
Expand Down Expand Up @@ -518,13 +541,33 @@ class BasicThread implements Runnable {
}
}
catch (Exception e){
e.printStackTrace();
e.printStackTrace();
}
}
}
Runnable runnable = new BasicThread();
Thread thread = new Thread(runnable);
thread.start();
```
```

---

- Get the last 5 minutes of container transactions within a bounding box area.
```java
CoreProtectAPI CoreProtect = getCoreProtect();
if (CoreProtect != null){ // Ensure we have access to the API
World world = Bukkit.getWorld("world");
BoundingBox area = new BoundingBox(100, 60, 100, 200, 80, 200);
List<ContainerResult> lookup = CoreProtect.containerLookup(world, area, (5 * 60));
if (lookup != null){
for (ContainerResult result : lookup){
int x = result.getX();
int y = result.getY();
int z = result.getZ();
// ...
}
}
}
```

---
33 changes: 33 additions & 0 deletions src/main/java/net/coreprotect/CoreProtectAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
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;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.util.BoundingBox;
import org.bukkit.entity.Player;

import net.coreprotect.api.BlockAPI;
import net.coreprotect.api.BoxAPI;
import net.coreprotect.api.QueueLookup;
import net.coreprotect.api.SessionLookup;
import net.coreprotect.config.Config;
Expand Down Expand Up @@ -117,6 +121,35 @@ 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 of container-related actions within the specified bounding box area.
*
* @param world The world to perform the lookup in
* @param boundingBox The bounding box defining the area to search
* @param time Time constraint in seconds (0 means no time constraint)
* @return List of results in a ContainerResult list format, or null if API is disabled
*/
public List<ContainerResult> containerLookup(World world, BoundingBox boundingBox, int time) {
if (isEnabled()) {
return BoxAPI.performAreaContainerLookup(world, boundingBox, 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
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