Class RayCast
java.lang.Object
tk.airshipcraft.commonlib.utils.math.RayCast
Provides functionality to perform ray casts in Minecraft, which is a process of simulating "rays" to detect
blocks or entities along a line. This can be used for features like shooting mechanics, line of sight calculations,
and interaction with the game world at a distance.
- Since:
- 2023-04-11
- Version:
- 1.0.0
- Author:
- Locutusque, notzune, eerieXanthic
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
raycast
(org.bukkit.entity.Player player, org.bukkit.util.Vector direction, double maxDistance) Casts a ray from the player's eye location in the given direction, returning the first block or entity encountered.static List<org.bukkit.block.Block>
raycastAllBlocks
(org.bukkit.entity.LivingEntity from, int distance) Performs a raycast from the specified living entity and returns all blocks hit by the ray within the specified distance.static org.bukkit.block.Block[]
raycastBetweenBlocks
(org.bukkit.block.Block startBlock, org.bukkit.block.Block endBlock) Raycasts between two blocks, returning a sequence of blocks that lie between them on a straight line.static org.bukkit.block.Block
raycastToBlock
(org.bukkit.entity.Player player, org.bukkit.util.Vector direction, double maxDistance) Performs a raycast to detect the first non-air block from the player's eye location in a specified direction.static Optional<org.bukkit.block.Block>
raycastWithFilter
(org.bukkit.entity.LivingEntity from, int distance, Predicate<org.bukkit.block.Block> predicate) Performs a raycast from the specified living entity and returns the nearest block that satisfies the specified predicate within the specified distance.static Optional<org.bukkit.Location>
raycastWithinRadius
(org.bukkit.entity.LivingEntity from, int distance) Performs a raycast from the specified living entity to the nearest solid block within a specified distance.
-
Method Details
-
raycast
public static Object raycast(org.bukkit.entity.Player player, org.bukkit.util.Vector direction, double maxDistance) Casts a ray from the player's eye location in the given direction, returning the first block or entity encountered. This method can be used to determine what a player is looking at or could interact with at a distance.- Parameters:
player
- The player from whose perspective the ray is cast.direction
- The direction vector along which to cast the ray.maxDistance
- The maximum distance the ray should travel.- Returns:
- The first non-air block or entity the ray intersects, or null if it hits nothing within the max distance.
-
raycastToBlock
public static org.bukkit.block.Block raycastToBlock(org.bukkit.entity.Player player, org.bukkit.util.Vector direction, double maxDistance) Performs a raycast to detect the first non-air block from the player's eye location in a specified direction. Useful for detecting which block a player is looking at from a distance.- Parameters:
player
- The player performing the raycast.direction
- The normalized direction vector for the raycast.maxDistance
- The maximum range of the raycast.- Returns:
- The first solid block encountered by the ray, or null if only air is found within the range.
-
raycastBetweenBlocks
public static org.bukkit.block.Block[] raycastBetweenBlocks(org.bukkit.block.Block startBlock, org.bukkit.block.Block endBlock) Raycasts between two blocks, returning a sequence of blocks that lie between them on a straight line. This is useful for determining the path of a projectile or line of sight between two fixed points.- Parameters:
startBlock
- The block from which to start the raycast.endBlock
- The block where the raycast should end.- Returns:
- An array of blocks intersected by the ray, starting with the startBlock and ending with the endBlock.
-
raycastWithinRadius
public static Optional<org.bukkit.Location> raycastWithinRadius(org.bukkit.entity.LivingEntity from, int distance) Performs a raycast from the specified living entity to the nearest solid block within a specified distance.- Parameters:
from
- the living entity from which the raycast originatesdistance
- the maximum distance to perform the raycast (in blocks)- Returns:
- an Optional containing the location of the nearest solid block, or an empty Optional if no solid block is found
- Throws:
IllegalArgumentException
- if distance is negative
-
raycastAllBlocks
public static List<org.bukkit.block.Block> raycastAllBlocks(org.bukkit.entity.LivingEntity from, int distance) Performs a raycast from the specified living entity and returns all blocks hit by the ray within the specified distance.- Parameters:
from
- the living entity from which the raycast originatesdistance
- the maximum distance to perform the raycast (in blocks)- Returns:
- a list of blocks hit by the ray
- Throws:
IllegalArgumentException
- if distance is negative
-
raycastWithFilter
public static Optional<org.bukkit.block.Block> raycastWithFilter(org.bukkit.entity.LivingEntity from, int distance, Predicate<org.bukkit.block.Block> predicate) Performs a raycast from the specified living entity and returns the nearest block that satisfies the specified predicate within the specified distance.- Parameters:
from
- the living entity from which the raycast originatesdistance
- the maximum distance to perform the raycast (in blocks)predicate
- the predicate to filter blocks- Returns:
- an Optional containing the block that satisfies the predicate, or an empty Optional if no such block is found
- Throws:
IllegalArgumentException
- if distance is negative
-