⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Add Container Lookup and Area Query Support#854

Open
Restitutor wants to merge 2 commits intoPlayPro:masterfrom
Restitutor:master
Open

Add Container Lookup and Area Query Support#854
Restitutor wants to merge 2 commits intoPlayPro:masterfrom
Restitutor:master

Conversation

@Restitutor
Copy link

Motivation

CoreProtect logs container transactions but doesn't expose dedicated lookup methods in the public API. Plugin developers must use the generic performLookup() with action=4 and parse String[] arrays.

Use case: Anti-theft plugins that detect when players take items from containers they didn't deposit. This requires querying container history by location. BoxAPI allows for region based searches to scan a player base in a single query.

Changes

1. ContainerResult — Typed result object

New: api/result/ContainerResult.java

Immutable result class for container transactions with typed accessors:

  • getTimestamp(), getPlayer(), getX/Y/Z(), worldName()
  • getType() (Material), getData(), getAmount(), getMetadata()
  • getActionId() / getActionString() (0=remove, 1=add)
  • isRolledBack()

2. Single-location container lookup

New: BlockAPI.performContainerLookup(Location, offset)

Returns List<ContainerResult> for all container transactions at a location within the time window.

Refactored BlockAPI.performLookup() to use PreparedStatement via shared createLocationQuery() helper. Both block and container lookups now use parameterized queries.

3. Area-based queries

New: api/BoxAPI.java

Query containers and blocks across bounding boxes or radii:

Container methods:

  • performAreaContainerLookup(World, BoundingBox, offset)
  • performCubicContainerLookup(World, x, y, z, radius, offset) — cubic area
  • performSphericalContainerLookup(World, x, y, z, radius, offset) — spherical filtering

Block methods:

  • performAreaLookup(World, BoundingBox, offset)List<String[]>
  • performCubicLookup(...), performSphericalLookup(...)

Safety features:

  • Max bounding box volume: 1,000,000 blocks³ (100×100×100)
  • Result limit: 10,000 rows per query
  • Input validation (null checks, negative radius rejection)
  • Uses PreparedStatement with parameterized queries

4. Public API entry point

New: CoreProtectAPI.containerLookup(Location, time)

Convenience wrapper that calls BlockAPI.performContainerLookup(). Follows the same pattern as blockLookup().

Usage Example

// Single location lookup
List<ContainerResult> results = api.containerLookup(chestLocation, 86400);

// Calculate net item flow
int netDiamonds = results.stream()
    .filter(r -> r.getType() == Material.DIAMOND)
    .mapToInt(r -> r.getActionId() == 1 ? r.getAmount() : -r.getAmount())
    .sum();

// Area query (all container transactions in region)
BoundingBox region = new BoundingBox(x1, y1, z1, x2, y2, z2);
List<ContainerResult> areaResults = BoxAPI.performAreaContainerLookup(world, region, 0);

// Find who last deposited an item
ContainerResult lastDeposit = results.stream()
    .filter(r -> r.getType() == Material.DIAMOND && r.getActionId() == 1)
    .findFirst()
    .orElse(null);

No breaking changes: Existing BlockAPI.performLookup() behavior is unchanged; internal refactor to PreparedStatement is transparent.

We could add methods to the CoreProtectAPI instead of having clients use BlockAPI and BoxAPI directly. Let me know what you would prefer.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant