Find Nearby Blocks or Entities

Canary provides a very handy feature in net.canarymod.BlockIterator. A BlockIterator lets you find all the blocks along a line in the game. Most useful is probably the version where you pass in a LineTracer (made from a Player) and a boolean, which is declared in the Canary API like this:[25]

 
public​ BlockIterator(LineTracer tracer, ​boolean​ include_air)

That gives you a BlockIterator object, which you can use to retrieve blocks along the line of sight from that entity. The boolean flag says to include Air blocks, and it works like this:

 
BlockIterator sightItr = ​new​ BlockIterator(​new​ LineTracer(me), true);
 
while​ (sightItr.hasNext()) {
 
Block b = sightItr.next();
 
// do something with this block, ...

Get Learn to Program with Minecraft Plugins, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.