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 continuation filter(s) (provided by
BlockRay.BlockRayBuilder.whilst(Predicate)
) are checked. If any of the
provided filters return false
then the iterator ends there - no more
matches may be made by this ray.
If this ray has not been stopped, the selection filter(s) (provided by
BlockRay.BlockRayBuilder.select(Predicate)
) are called. If any of the
provided filters returns false
then the iterator proceeds to the
next block and it is never returned as a BlockRayHit
. Otherwise
the block is returned and the iterator is paused, awaiting a next()
call to resume tracing. If the distance limit is enabled then it is applied
before both continuation and select checks and acts like the a false
result has been returned by a continuation 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
continue filter, combined with
continueAfterFilter(Predicate, int)
with a second argument of 1, to
enable the ray to iterate such that it can select the block just after the
last air. A notAirFilter()
can then be used as the select filter
to select the non-air block. Thus:
BlockRay.from(fromLocation) .to(toLocation) .select(BlockRay.notAirFilter()) .whilst(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)) .build();
will scan the blocks between the two specified locations to locate the
first non-air block along the line from the fromLocation
to the
toLocation
.
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 returns
true for any input. |
static <E extends Extent> |
blockTypeFilter(BlockType... types)
|
static <E extends Extent> |
blockTypeFilter(BlockType type)
|
static <E extends Extent> |
continueAfterFilter(Predicate<BlockRayHit<E>> filter,
int numberOfBlocks)
Creates a filter that displaces a
false result from the wrapped
Predicate by the provided numberOfBlocks , if this number
is positive. |
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> |
notAirFilter()
|
static <E extends Extent> |
onlyAirFilter()
|
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()
true
for any input.
In the absence of any other filter, this is the default filter applied to:
Be careful: if no other constraints are placed on
BlockRay.BlockRayBuilder.whilst(Predicate)
and
BlockRay.BlockRayBuilder.select(Predicate)
is given a Predicate
that has a narrower focus, a ray may never find a match and therefore
continue endlessly.
E
- The extent to be applied inpublic static <E extends Extent> Predicate<BlockRayHit<E>> onlyAirFilter()
true
when the BlockRayHit
represents BlockTypes.AIR
.
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>> notAirFilter()
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 block to allowpublic static <E extends Extent> Predicate<BlockRayHit<E>> blockTypeFilter(BlockType... types)
E
- The extent to be applied intypes
- The type of block to allowpublic static <E extends Extent> Predicate<BlockRayHit<E>> continueAfterFilter(Predicate<BlockRayHit<E>> filter, int numberOfBlocks)
false
result from the wrapped
Predicate
by the provided numberOfBlocks
, if this number
is positive.
If numberOfBlocks
is non-positive, this filter will mirror
the behavior of the provided filter.
E
- The extent to be applied infilter
- The filter to extendnumberOfBlocks
- The number of blocks to extend it by