E
- The extent in which this ray is being castpublic class BlockRay<E extends Extent> extends Object implements Iterator<BlockRayHit<E>>
This class implements the
Iterator
interface with the exception of Iterator.remove()
.
Filters determine what blocks the BlockRay
should accept. First
the stop filter is called. If it returns false then the iterator ends there.
Otherwise the skip filter is called. If it returns false then the iterator
proceeds to the next block and it is never returned. Otherwise the block is
returned. If the distance limit is enabled then it is applied before both
filters and acts like the stop filter.
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
as the
stopFilter
, combined with
continueAfterFilter(Predicate, int)
with a second argument of 1, to
obtain the block just after the last air.
To get a block ray for an entities' line of sight, use
BlockRay.from(entity);
BlockRayHit
Modifier and Type | Class and Description |
---|---|
static class |
BlockRay.BlockRayBuilder<E extends Extent>
A builder for block ray, which also implements
Iterable , making 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 type.
|
static <E extends Extent> |
continueAfterFilter(Predicate<BlockRayHit<E>> filter,
int numberOfBlocks)
Extends a filter by a number of blocks, regardless of what the extended
filter does.
|
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 with the given starting location.
|
static <E extends Extent> |
from(Location<E> start)
Initializes a block ray builder with the given starting location.
|
boolean |
hasNext() |
BlockRayHit<E> |
next() |
static <E extends Extent> |
onlyAirFilter()
A block type filter that only permits air as a transparent block.
|
void |
reset()
Resets the iterator; it will iterate from the starting location again.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEachRemaining, remove
public 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 static <E extends Extent> BlockRay.BlockRayBuilder<E> from(Location<E> start)
E
- The extent to be applied instart
- The starting locationpublic static <E extends Extent> BlockRay.BlockRayBuilder<E> from(E extent, com.flowpowered.math.vector.Vector3d start)
E
- The extent to be applied inextent
- 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.E
- The extent to be applied inpublic 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.
E
- The extent to be applied inpublic static <E extends Extent> Predicate<BlockRayHit<E>> blockTypeFilter(BlockType type)
E
- The extent to be applied intype
- The type of blocks to allowpublic static <E extends Extent> Predicate<BlockRayHit<E>> continueAfterFilter(Predicate<BlockRayHit<E>> filter, int numberOfBlocks)
E
- The extent to be applied infilter
- The filter to extendnumberOfBlocks
- The number of blocks to extend it by