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
21 changes: 20 additions & 1 deletion docs/api/version/v13.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ LookupOptions options = LookupOptions.builder()
| `excludeMaterials(List<Material> materials)` | Excludes matching logged block/item materials. |
| `includeEntities(List<EntityType> entities)` | Includes any matching entity type in `entityLookup`. |
| `excludeEntities(List<EntityType> entities)` | Excludes matching entity types in `entityLookup`. |
| `victims(List<String> victims)` | Includes kills of any named player in `entityLookup`. Unknown names never match. |
| `excludeVictims(List<String> victims)` | Excludes kills of named players in `entityLookup`, retaining other entity events. |
| `blockActions(List<BlockAction> actions)` | Selects block actions. |
| `entityActions(List<EntityAction> actions)` | Selects entity actions. |
| `containerActions(List<ContainerAction> actions)` | Selects container transaction actions. |
Expand All @@ -194,7 +196,7 @@ Results are returned in descending event order. Limits apply after filtering; om
| Lookup | User/time/limit | Spatial filters | Material filters | Additional filters |
| --- | --- | --- | --- | --- |
| Block | Yes | Yes; supplied `Block` overrides options | Yes | `blockActions` |
| Entity | Yes | Yes | No | `entityActions`, `includeEntities`, `excludeEntities` |
| Entity | Yes | Yes | No | `entityActions`, `includeEntities`, `excludeEntities`, `victims`, `excludeVictims` |
| Container | Yes | Yes | Yes | `containerActions` |
| Item | Yes | Yes | Yes | `itemActions` |
| Inventory | Yes | Yes | Yes | `inventoryActions` |
Expand Down Expand Up @@ -323,6 +325,22 @@ Material filters compare the logged material, regardless of the block currently

Returns entity spawns and kills. Use `entityActions` to select `SPAWN` or `KILL`, and `includeEntities` / `excludeEntities` to filter Bukkit `EntityType` values. Empty lists select both actions and all entity types.

`EntityResult.getPlayer()` identifies the actor. `getVictim()` identifies the killed player, or returns `null` for mob kills, spawns, and player kills whose victim ID cannot be resolved. The name is resolved from the existing user record, not a snapshot of the name at the time of death. This reads the victim ID already logged by CoreProtect; it does not enable logging or recover events that were not recorded.

Use `victims` and `excludeVictims` to filter player-kill victims in SQL before pagination. These filters combine with the actor, time, location, action, and entity filters; other lookup methods ignore them. Names use CoreProtect's case-insensitive user lookup. Empty lists impose no restriction. Blank names and special users beginning with `#` are rejected. Unknown inclusions match no events; unknown exclusions have no effect. Exclusions take precedence and preserve mob kills, spawns, and kills with unresolved victims.

```java
LookupOptions options = LookupOptions.builder()
.users(List.of("Killer"))
.victims(List.of("Victim"))
.time(3 * 24 * 60 * 60)
.limit(0, 100)
.build();
List<EntityResult> kills = api.entityLookup(options);
```

This returns a page of recorded kills of `Victim` by `Killer`, not a total count. Continue pagination if needed. The existing `EntityResult` constructor remains available and sets the victim to `null`.

For spawn events, location/radius filters match either the original spawn location or the persisted current/final location. Whole-world filters similarly match either world. Kill events match the location where the kill was logged. Results always expose the **original event coordinates and world**, even when a tracked position matched the filter.

These synchronous lookups use persisted tracking data and do not wait for a live-entity scan. Material filters and other lookup-specific action filters do not affect entity lookups.
Expand Down Expand Up @@ -441,6 +459,7 @@ Action strings are `break`, `place`, `click`, `kill`, or `spawn` as applicable.
| Additional method | Return type | Meaning |
| --- | --- | --- |
| `getEntityType()` | `EntityType` | Logged entity type, including `PLAYER` for player-kill records. |
| `getVictim()` | `String` | Name resolved from the recorded victim ID for player kills; `null` for other events or unresolved victims. |
| `isRolledBack()` | `boolean` | Whether the entity change is currently rolled back. |

Actions are `KILL` (`3`, `kill`) and `SPAWN` (`13`, `spawn`).
Expand Down
50 changes: 48 additions & 2 deletions src/main/java/net/coreprotect/api/EntityAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -55,7 +56,7 @@ public static List<EntityResult> performLookup(LookupOptions options) {

boolean snapshot = filter.beginDuckDBSnapshot(connection);
try {
StringBuilder query = new StringBuilder("SELECT entity_rows.time,entity_rows." + ConfigHandler.databaseType.getUserColumn() + ",entity_rows.wid,entity_rows.x,entity_rows.y,entity_rows.z,entity_rows.type,entity_rows.action,entity_rows.rolled_back FROM ");
StringBuilder query = new StringBuilder("SELECT entity_rows.time,entity_rows." + ConfigHandler.databaseType.getUserColumn() + ",entity_rows.wid,entity_rows.x,entity_rows.y,entity_rows.z,entity_rows.type,entity_rows.data,entity_rows.action,entity_rows.rolled_back FROM ");
query.append(filter.entityTable(connection, "entity_rows"));
query.append(' ');
filter.appendEntityWhere(connection, query, "entity_rows");
Expand All @@ -69,6 +70,7 @@ public static List<EntityResult> performLookup(LookupOptions options) {
if (!options.getExcludeEntities().isEmpty()) {
query.append(" AND NOT ").append(entityPredicate(options.getExcludeEntities()));
}
appendVictimFilters(connection, query, options);
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder().replace("time", "entity_rows.time").replace("rowid", "entity_rows.rowid"));
filter.appendLimit(query);

Expand All @@ -78,7 +80,7 @@ public static List<EntityResult> performLookup(LookupOptions options) {
while (results.next()) {
result.add(new EntityResult(
results.getLong("time"), UserStatement.getName(connection, results.getInt("user")), WorldUtils.getWorldName(results.getInt("wid")),
results.getInt("x"), results.getInt("y"), results.getInt("z"), results.getInt("type"), results.getInt("action"), results.getInt("rolled_back")
results.getInt("x"), results.getInt("y"), results.getInt("z"), results.getInt("type"), results.getInt("action"), results.getInt("rolled_back"), victim(connection, results)
));
}
}
Expand All @@ -94,6 +96,50 @@ public static List<EntityResult> performLookup(LookupOptions options) {
return result;
}

private static String victim(Connection connection, ResultSet results) throws SQLException {
// data holds a victim user ID only for player kills; other events reuse it.
if (results.getInt("action") != LookupActions.ENTITY_KILL || results.getInt("type") != 0 || results.wasNull()) {
return null;
}
long id = results.getLong("data");
if (results.wasNull() || id <= 0 || id > Integer.MAX_VALUE) {
return null;
}
String name = UserStatement.getName(connection, (int) id);
return name == null || name.isEmpty() ? null : name;
}

private static void appendVictimFilters(Connection connection, StringBuilder query, LookupOptions options) throws SQLException {
if (!options.getVictims().isEmpty()) {
String ids = victimIds(connection, options.getVictims());
if (ids.isEmpty()) {
query.append(" AND 1 = 0");
}
else {
query.append(" AND entity_rows.action=").append(LookupActions.ENTITY_KILL)
.append(" AND entity_rows.type=0 AND entity_rows.data IN (").append(ids).append(')');
}
}
if (!options.getExcludeVictims().isEmpty()) {
String ids = victimIds(connection, options.getExcludeVictims());
if (!ids.isEmpty()) {
query.append(" AND NOT (COALESCE(entity_rows.action,-1)=").append(LookupActions.ENTITY_KILL)
.append(" AND COALESCE(entity_rows.type,-1)=0 AND COALESCE(entity_rows.data,0) IN (").append(ids).append("))");
}
}
}

private static String victimIds(Connection connection, List<String> names) throws SQLException {
StringJoiner ids = new StringJoiner(",");
for (String name : names) {
int id = UserStatement.findId(connection, name);
if (id > 0) {
ids.add(Integer.toString(id));
}
}
return ids.toString();
}

private static String entityPredicate(List<EntityType> entities) {
StringJoiner names = new StringJoiner(",");
for (EntityType entity : entities) {
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/net/coreprotect/api/LookupOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public final class LookupOptions {
private final List<Material> excludeMaterials;
private final List<String> users;
private final List<String> excludeUsers;
private final List<String> victims;
private final List<String> excludeVictims;
private final List<String> textStartsWithAny;
private final List<String> textStartsWithNone;
private final List<ContainerAction> containerActions;
Expand All @@ -45,6 +47,8 @@ private LookupOptions(Builder builder) {
this.excludeMaterials = builder.excludeMaterials;
this.users = builder.users;
this.excludeUsers = builder.excludeUsers;
this.victims = builder.victims;
this.excludeVictims = builder.excludeVictims;
this.textStartsWithAny = builder.textStartsWithAny;
this.textStartsWithNone = builder.textStartsWithNone;
this.containerActions = builder.containerActions;
Expand Down Expand Up @@ -109,6 +113,14 @@ public List<String> getExcludeUsers() {
return excludeUsers;
}

public List<String> getVictims() {
return victims;
}

public List<String> getExcludeVictims() {
return excludeVictims;
}

public List<String> getTextStartsWithAny() {
return textStartsWithAny;
}
Expand Down Expand Up @@ -161,6 +173,8 @@ public static final class Builder {
private List<Material> excludeMaterials = List.of();
private List<String> users = List.of();
private List<String> excludeUsers = List.of();
private List<String> victims = List.of();
private List<String> excludeVictims = List.of();
private List<String> textStartsWithAny = List.of();
private List<String> textStartsWithNone = List.of();
private List<ContainerAction> containerActions = List.of();
Expand Down Expand Up @@ -232,6 +246,36 @@ public Builder excludeUsers(List<String> users) {
return this;
}

/**
* Includes kills of any named player in entityLookup only, before pagination.
* Empty means no victim restriction; unknown names never match.
* @throws IllegalArgumentException for blank names or special users beginning with '#'
*/
public Builder victims(List<String> victims) {
this.victims = victimNames(victims);
return this;
}

/**
* Excludes kills of named players in entityLookup only, before pagination.
* Other entity events and kills with an unknown victim are retained.
* @throws IllegalArgumentException for blank names or special users beginning with '#'
*/
public Builder excludeVictims(List<String> victims) {
this.excludeVictims = victimNames(victims);
return this;
}

private static List<String> victimNames(List<String> victims) {
List<String> copy = List.copyOf(victims);
for (String victim : copy) {
if (victim.isBlank() || victim.startsWith("#")) {
throw new IllegalArgumentException("Victim filters require player names");
}
}
return copy;
}

/**
* Includes chat, command and sign text matching any literal prefix.
* Empty means no inclusion restriction. Other lookup types ignore this option.
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/coreprotect/api/result/EntityResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class EntityResult implements CoreProtectResult {
private final long time;
private final String username;
private final String victim;
private final String world;
private final int x;
private final int y;
Expand All @@ -20,6 +21,11 @@ public class EntityResult implements CoreProtectResult {
private final int rolledBack;

public EntityResult(long time, String username, String world, int x, int y, int z, int type, int actionId, int rolledBack) {
this(time, username, world, x, y, z, type, actionId, rolledBack, null);
}

public EntityResult(long time, String username, String world, int x, int y, int z, int type, int actionId, int rolledBack, String victim) {
this.victim = actionId == LookupActions.ENTITY_KILL && type == 0 ? victim : null;
this.time = time;
this.username = username;
this.world = world;
Expand All @@ -43,6 +49,14 @@ public String getPlayer() {
return username;
}

/**
* Returns the recorded name of the killed player, or null for other events
* and player kills whose victim cannot be resolved.
*/
public String getVictim() {
return victim;
}

public long getTimestamp() {
return time * 1000L;
}
Expand Down
Loading
Loading