E
- The extent in which this ray is being castpublic class BlockRay<E extends Extent> extends Object implements Iterator<BlockRayHit<E>>
Iterator
interface
with the exception of Iterator.remove()
.
Filters determine at what location a BlockRay
should stop. A filter
is called at the boundary of each new location that a BlockRay
passes through in order
to determine whether the ray cast should continue or terminate at that location.
Any one instance of a Predicate
should only be run on one path.
It is not specified that Predicate
s have to be stateless, pure functions.
They are allowed to keep state along an individual path, based on the assertion that a
single instance is only called on one path.
Filters are most useful for limiting the target block a player is looking
at based on some metric, like transparency, block model, or even distance. The standard
Bukkit-like behavior for finding the target block can be achieved with using
ONLY_AIR_FILTER
, optionally combined with
maxDistanceFilter(Vector3d, double)
to limit the target block to be within some
distance.
To get the block targeted by an entity, use the following:
final Optional<BlockRayHit> block = BlockRay.from(entity).filter(BlockRay.ONLY_AIR_FILTER).end();
BlockRayHit
Modifier and Type | Class and Description |
---|---|
static class |
BlockRay.BlockRayBuilder<E extends Extent>
A builder for block ray, which also implements
Iterable , which makes it
useful for 'advanced for loops'. |
Modifier and Type | Method and Description |
---|---|
static <E extends Extent> |
allFilter()
A filter that accepts all blocks.
|
static <E extends Extent> |
blockTypeFilter(BlockType type)
A filter that only allows blocks of a certain block type.
|
Optional<BlockRayHit<E>> |
end()
Traces the block ray to the end and returns the last block
accepted by the filter, or none if the extent or block limit was reached.
|
static BlockRay.BlockRayBuilder<World> |
from(Entity entity)
Initializes a block ray builder for the entity's eye.
|
static <E extends Extent> |
from(E extent,
com.flowpowered.math.vector.Vector3d start)
Initializes a block ray builder, starting with the starting location.
|
static <E extends Extent> |
from(Location<E> start)
Initializes a block ray builder, starting with the starting location.
|
boolean |
hasNext() |
static <E extends Extent> |
maxDistanceFilter(com.flowpowered.math.vector.Vector3d start,
double distance)
A filter that stops at a certain distance.
|
BlockRayHit<E> |
next() |
static <E extends Extent> |
onlyAirFilter()
A block type filter that only permits air as a transparent block.
|
void |
remove() |
void |
reset()
Resets the iterator; it will iterate from the starting location again.
|
void |
setBlockLimit(int blockLimit)
Sets the maximum number of blocks to intersect before stopping.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEachRemaining
public void setBlockLimit(int blockLimit)
blockLimit
- The block limitpublic final void reset()
public boolean hasNext()
hasNext
in interface Iterator<BlockRayHit<E extends Extent>>
public BlockRayHit<E> next()
next
in interface Iterator<BlockRayHit<E extends Extent>>
public Optional<BlockRayHit<E>> end()
public void remove()
remove
in interface Iterator<BlockRayHit<E extends Extent>>
public static <E extends Extent> BlockRay.BlockRayBuilder<E> from(Location<E> start)
start
- The starting locationpublic static <E extends Extent> BlockRay.BlockRayBuilder<E> from(E extent, com.flowpowered.math.vector.Vector3d start)
extent
- The extent in which to trace the raystart
- The starting positionpublic static BlockRay.BlockRayBuilder<World> from(Entity entity)
entity
- The entitypublic static <E extends Extent> Predicate<BlockRayHit<E>> allFilter()
BlockRay
combined with no other filter than this
one could run endlessly.public static <E extends Extent> Predicate<BlockRayHit<E>> onlyAirFilter()
This is provided for convenience, as the default behavior in previous systems was to pass through air blocks only until a non-air block was hit.
public static <E extends Extent> Predicate<BlockRayHit<E>> blockTypeFilter(BlockType type)
type
- The type of blocks to allowpublic static <E extends Extent> Predicate<BlockRayHit<E>> maxDistanceFilter(com.flowpowered.math.vector.Vector3d start, double distance)
Note the behavior of a BlockRay
under this filter: ray casting stops once the
distance is greater than the given distance, meaning that the ending location can at a
distance greater than the distance given. However, this filter still maintains that all
locations on the path are within the maximum distance.
start
- The starting point of the raydistance
- The maximum distance allowed