Currently, there is no API method to find an arbitrary block - and further, no way to do so with customizable restrictions (e.g., ignore certain Vector3s, safely dodge lava, don't jump fences, etc.). In short, there is no easy way to say "I want to find the nearest tree" or "I want to find the nearest Iron Ore vein".
With a proper flood-fill algorithm and the usage of (currently Beta) system.runJob, you can efficiently find a block with many search restrictions in an arbitrary search zone.
Source code + documentation: https://github.com/nox7/mc-bedrock-script-utilities/tree/main/Iterators/FloodFill
Code examples are provided in the link above.
See the attached videos for
- Example of using Flood-Fill to find the closest tree
- Visualization of how it searches and the speed.
It has two search algorithms:
- Gravity-based flood fill (what you see in the videos, an algorithm to use for entities to safely jump over blocks, safely fall down, jump over gaps, etc.)
- Y-axis flood support: This is a full 3D flood-fill that searches an entire spherical space from a starting location.
Finally, it is fully customizable in the FloodFillIteratorOptions.ts file. You can set a max search distance, Vector3s to ignore, Tags to include/ignore/consider air, TypeIds to include/ignore/consider air, and TypeIds that cannot be jumped over (fences, walls, etc.)
This is part of an ongoing effort to decouple a lot of my internal algorithms I use in my personal Add-On as free scripts/utilities for public use. The A* pathfinder shown in the videos below is soon to come.