-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RayTraceConfigurationBuilder (#11907)
- Loading branch information
Showing
5 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
...api/src/main/java/io/papermc/paper/raytracing/PositionedRayTraceConfigurationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package io.papermc.paper.raytracing; | ||
|
||
import java.util.function.Predicate; | ||
import org.bukkit.FluidCollisionMode; | ||
import org.bukkit.Location; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.util.Vector; | ||
import org.checkerframework.checker.index.qual.NonNegative; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jspecify.annotations.NullMarked; | ||
|
||
/** | ||
* A builder for configuring a raytrace with a starting location | ||
* and direction. | ||
*/ | ||
@NullMarked | ||
public interface PositionedRayTraceConfigurationBuilder { | ||
|
||
/** | ||
* Sets the starting location. | ||
* | ||
* @param start the new starting location | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder start(Location start); | ||
|
||
/** | ||
* Sets the direction. | ||
* | ||
* @param direction the new direction | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder direction(Vector direction); | ||
|
||
/** | ||
* Sets the maximum distance. | ||
* | ||
* @param maxDistance the new maxDistance | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder maxDistance(@NonNegative double maxDistance); | ||
|
||
/** | ||
* Sets the FluidCollisionMode when looking for block collisions. | ||
* <p> | ||
* If collisions with passable blocks are ignored, fluid collisions are | ||
* ignored as well regardless of the fluid collision mode. | ||
* | ||
* @param fluidCollisionMode the new FluidCollisionMode | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder fluidCollisionMode(FluidCollisionMode fluidCollisionMode); | ||
|
||
/** | ||
* Sets whether the raytrace should ignore passable blocks when looking for | ||
* block collisions. | ||
* <p> | ||
* If collisions with passable blocks are ignored, fluid collisions are | ||
* ignored as well regardless of the fluid collision mode. | ||
* <p> | ||
* Portal blocks are only considered passable if the ray starts within them. | ||
* Apart from that collisions with portal blocks will be considered even if | ||
* collisions with passable blocks are otherwise ignored. | ||
* | ||
* @param ignorePassableBlocks if the raytrace should ignore passable blocks | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder ignorePassableBlocks(boolean ignorePassableBlocks); | ||
|
||
/** | ||
* Sets the size of the raytrace when looking for entity collisions. | ||
* | ||
* @param raySize the new raytrace size | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder raySize(@NonNegative double raySize); | ||
|
||
/** | ||
* Sets the current entity filter when looking for entity collisions. | ||
* | ||
* @param entityFilter predicate for entities the ray can potentially collide with | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder entityFilter(Predicate<? super Entity> entityFilter); | ||
|
||
/** | ||
* Sets the current block filter when looking for block collisions. | ||
* | ||
* @param blockFilter predicate for blocks the ray can potentially collide with | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder blockFilter(Predicate<? super Block> blockFilter); | ||
|
||
/** | ||
* Sets the targets for the rayTrace. | ||
* | ||
* @param first the first target | ||
* @param others the other targets | ||
* @return a reference to this object | ||
*/ | ||
@Contract(value = "_, _ -> this", mutates = "this") | ||
PositionedRayTraceConfigurationBuilder targets(RayTraceTarget first, RayTraceTarget... others); | ||
} |
9 changes: 9 additions & 0 deletions
9
paper-api/src/main/java/io/papermc/paper/raytracing/RayTraceTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.papermc.paper.raytracing; | ||
|
||
/** | ||
* List of Targets a builder can target. | ||
*/ | ||
public enum RayTraceTarget { | ||
ENTITY, | ||
BLOCK | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...src/main/java/io/papermc/paper/raytracing/PositionedRayTraceConfigurationBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.papermc.paper.raytracing; | ||
|
||
import com.google.common.base.Preconditions; | ||
import java.util.EnumSet; | ||
import java.util.OptionalDouble; | ||
import java.util.function.Predicate; | ||
import org.bukkit.FluidCollisionMode; | ||
import org.bukkit.Location; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.util.Vector; | ||
import org.jspecify.annotations.NullMarked; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
@NullMarked | ||
public class PositionedRayTraceConfigurationBuilderImpl implements PositionedRayTraceConfigurationBuilder { | ||
|
||
public @Nullable Location start; | ||
public @Nullable Vector direction; | ||
public OptionalDouble maxDistance = OptionalDouble.empty(); | ||
public FluidCollisionMode fluidCollisionMode = FluidCollisionMode.NEVER; | ||
public boolean ignorePassableBlocks; | ||
public double raySize = 0.0D; | ||
public @Nullable Predicate<? super Entity> entityFilter; | ||
public @Nullable Predicate<? super Block> blockFilter; | ||
public EnumSet<RayTraceTarget> targets = EnumSet.noneOf(RayTraceTarget.class); | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder start(final Location start) { | ||
Preconditions.checkArgument(start != null, "start must not be null"); | ||
this.start = start.clone(); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder direction(final Vector direction) { | ||
Preconditions.checkArgument(direction != null, "direction must not be null"); | ||
this.direction = direction.clone(); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder maxDistance(final double maxDistance) { | ||
Preconditions.checkArgument(maxDistance >= 0, "maxDistance must be non-negative"); | ||
this.maxDistance = OptionalDouble.of(maxDistance); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder fluidCollisionMode(final FluidCollisionMode fluidCollisionMode) { | ||
Preconditions.checkArgument(fluidCollisionMode != null, "fluidCollisionMode must not be null"); | ||
this.fluidCollisionMode = fluidCollisionMode; | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder ignorePassableBlocks(final boolean ignorePassableBlocks) { | ||
this.ignorePassableBlocks = ignorePassableBlocks; | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder raySize(final double raySize) { | ||
Preconditions.checkArgument(raySize >= 0, "raySize must be non-negative"); | ||
this.raySize = raySize; | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder entityFilter(final Predicate<? super Entity> entityFilter) { | ||
Preconditions.checkArgument(entityFilter != null, "entityFilter must not be null"); | ||
this.entityFilter = entityFilter; | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder blockFilter(final Predicate<? super Block> blockFilter) { | ||
Preconditions.checkArgument(blockFilter != null, "blockFilter must not be null"); | ||
this.blockFilter = blockFilter; | ||
return this; | ||
} | ||
|
||
@Override | ||
public PositionedRayTraceConfigurationBuilder targets(final RayTraceTarget first, final RayTraceTarget... others) { | ||
Preconditions.checkArgument(first != null, "first must not be null"); | ||
Preconditions.checkArgument(others != null, "others must not be null"); | ||
this.targets = EnumSet.of(first, others); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters