-
-
Notifications
You must be signed in to change notification settings - Fork 808
feat(api): extend typed block lookups and add entity lookup #995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Intelli
merged 6 commits into
PlayPro:master
from
Vanilla-Game:codex/typed-block-material-filters
Sep 15, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a6a2abf
feat(api): add material filters to typed block lookup
strobil 817ded8
feat(api): support scoped typed block lookups
strobil ced8cac
refactor(api): simplify block material predicates
strobil d4aee7f
feat(api): add typed entity lookup and filters
strobil 66d4fd9
fix(api): separate block history and optimize entity scopes
strobil bfebbda
fix(api): cast ClickHouse entity tracking IDs safely
strobil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package net.coreprotect.api; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.StringJoiner; | ||
|
|
||
| import org.bukkit.entity.EntityType; | ||
|
|
||
| import net.coreprotect.api.result.EntityResult; | ||
| import net.coreprotect.config.Config; | ||
| import net.coreprotect.config.ConfigHandler; | ||
| import net.coreprotect.database.Database; | ||
| import net.coreprotect.database.statement.UserStatement; | ||
| import net.coreprotect.model.action.LookupActions; | ||
| import net.coreprotect.utility.ErrorReporter; | ||
| import net.coreprotect.utility.WorldUtils; | ||
|
|
||
| /** | ||
| * Provides API methods for looking up entity spawn and kill events. | ||
| */ | ||
| public class EntityAPI { | ||
|
|
||
| private EntityAPI() { | ||
| throw new IllegalStateException("API class"); | ||
| } | ||
|
|
||
| /** | ||
| * Performs a typed entity lookup, matching original or persisted current/final spawn locations. | ||
| * | ||
| * @param options | ||
| * Lookup options | ||
| * @return List of entity results at their original event locations | ||
| */ | ||
| public static List<EntityResult> performLookup(LookupOptions options) { | ||
| List<EntityResult> result = new ArrayList<>(); | ||
| if (!Config.getGlobal().API_ENABLED) { | ||
| return result; | ||
| } | ||
| if (options == null) { | ||
| options = LookupOptions.builder().build(); | ||
| } | ||
|
|
||
| try (Connection connection = Database.getConnection(false, 1000)) { | ||
| if (connection == null) { | ||
| return result; | ||
| } | ||
| LookupFilter filter = LookupFilter.fromOptions(connection, options); | ||
| if (filter.hasInvalidUser() || filter.hasInvalidLocation()) { | ||
| return result; | ||
| } | ||
|
|
||
| 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 "); | ||
| query.append(filter.entityTable(connection, "entity_rows")); | ||
| query.append(' '); | ||
| filter.appendEntityWhere(connection, query, "entity_rows"); | ||
| int[] actions = options.getEntityActions().isEmpty() | ||
| ? new int[] { LookupActions.ENTITY_KILL, LookupActions.ENTITY_SPAWN } | ||
| : options.getEntityActions().stream().mapToInt(EntityAction::id).toArray(); | ||
| LookupFilter.appendActionWhere(query, "entity_rows", actions); | ||
| if (!options.getIncludeEntities().isEmpty()) { | ||
| query.append(" AND ").append(entityPredicate(options.getIncludeEntities())); | ||
| } | ||
| if (!options.getExcludeEntities().isEmpty()) { | ||
| query.append(" AND NOT ").append(entityPredicate(options.getExcludeEntities())); | ||
| } | ||
| query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder().replace("time", "entity_rows.time").replace("rowid", "entity_rows.rowid")); | ||
| filter.appendLimit(query); | ||
|
|
||
| try (PreparedStatement statement = connection.prepareStatement(query.toString())) { | ||
| filter.bindTrackedEntity(statement, 1); | ||
| try (ResultSet results = statement.executeQuery()) { | ||
| 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") | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| finally { | ||
| filter.endDuckDBSnapshot(connection, snapshot); | ||
| } | ||
| } | ||
| catch (Exception e) { | ||
| ErrorReporter.report(e); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static String entityPredicate(List<EntityType> entities) { | ||
| StringJoiner names = new StringJoiner(","); | ||
| for (EntityType entity : entities) { | ||
| String name = entity.name().toLowerCase(Locale.ROOT); | ||
| names.add("'" + name + "'"); | ||
| names.add("'minecraft:" + name + "'"); | ||
| } | ||
| String predicate = "entity_rows.type IN (SELECT id FROM " + ConfigHandler.prefix + "entity_map WHERE LOWER(entity) IN (" + names + "))"; | ||
| if (entities.contains(EntityType.PLAYER)) { | ||
| predicate += " OR (entity_rows.action=" + LookupActions.ENTITY_KILL + " AND entity_rows.type=0)"; | ||
| } | ||
| return "(" + predicate + ")"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package net.coreprotect.api; | ||
|
|
||
| import net.coreprotect.model.action.LookupActions; | ||
|
|
||
| /** | ||
| * Entity actions used by typed entity lookup filters. | ||
| */ | ||
| public enum EntityAction { | ||
| KILL(LookupActions.ENTITY_KILL), | ||
| SPAWN(LookupActions.ENTITY_SPAWN); | ||
|
|
||
| private final int id; | ||
|
|
||
| EntityAction(int id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public int id() { | ||
| return id; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.