mirror of https://github.com/PaperMC/Paper.git
377 lines
13 KiB
Diff
377 lines
13 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tamion <70228790+notTamion@users.noreply.github.com>
|
|
Date: Sun, 11 Feb 2024 08:47:54 +0100
|
|
Subject: [PATCH] Add RayTraceConfiguration
|
|
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/raytracing/RayTraceConfiguration.java b/src/main/java/io/papermc/paper/raytracing/RayTraceConfiguration.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..293ad30b365320867beb1784ed07c47d63f29f56
|
|
--- /dev/null
|
|
+++ b/src/main/java/io/papermc/paper/raytracing/RayTraceConfiguration.java
|
|
@@ -0,0 +1,206 @@
|
|
+package io.papermc.paper.raytracing;
|
|
+
|
|
+import org.bukkit.FluidCollisionMode;
|
|
+import org.bukkit.block.Block;
|
|
+import org.bukkit.entity.Entity;
|
|
+import org.jetbrains.annotations.Contract;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+import java.util.List;
|
|
+import java.util.function.Predicate;
|
|
+
|
|
+/**
|
|
+ * Holds information about how to cast a raytrace.
|
|
+ */
|
|
+public interface RayTraceConfiguration {
|
|
+
|
|
+ /**
|
|
+ * Creates a new builder.
|
|
+ *
|
|
+ * @return the new builder
|
|
+ */
|
|
+ @NotNull
|
|
+ static Builder builder() {
|
|
+ return new RayTraceConfigurationBuilderImpl();
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Gets the maximum distance.
|
|
+ *
|
|
+ * @return the maximum distance
|
|
+ */
|
|
+ double maxDistance();
|
|
+
|
|
+ /**
|
|
+ * Gets the FluidCollisionMode when looking for block collisions.
|
|
+ *
|
|
+ * @return the FluidCollisionMode
|
|
+ */
|
|
+ @Nullable
|
|
+ FluidCollisionMode fluidCollisionMode();
|
|
+
|
|
+ /**
|
|
+ * Gets if the raytrace will ignore passable blocks when looking for block collisions.
|
|
+ *
|
|
+ * @return if the raytrace will ignore passable blocks
|
|
+ */
|
|
+ boolean ignorePassableBlocks();
|
|
+
|
|
+ /**
|
|
+ * Gets the size of the raytrace when looking for entity collisions.
|
|
+ *
|
|
+ * @return the raytrace size
|
|
+ */
|
|
+ double raySize();
|
|
+
|
|
+ /**
|
|
+ * Gets the current entity filter.
|
|
+ *
|
|
+ * @return predicate for entities the ray can potentially collide with, or null to consider all entities
|
|
+ */
|
|
+ @Nullable
|
|
+ Predicate<? super Entity> entityFilter();
|
|
+
|
|
+ /**
|
|
+ * Gets the current block filter.
|
|
+ *
|
|
+ * @return predicate for blocks the ray can potentially collide with, or null to consider all blocks
|
|
+ */
|
|
+ @Nullable
|
|
+ Predicate<? super Block> blockFilter();
|
|
+
|
|
+ /**
|
|
+ * Gets which RayTraceTargets this configuration was made for.
|
|
+ *
|
|
+ * @return the targets
|
|
+ */
|
|
+ @NotNull
|
|
+ List<Targets> targets();
|
|
+
|
|
+ /**
|
|
+ * Helps you create a RayTraceConfiguration.
|
|
+ */
|
|
+ interface Builder {
|
|
+
|
|
+ /**
|
|
+ * Gets the maximum distance.
|
|
+ *
|
|
+ * @return the maximum distance
|
|
+ */
|
|
+ double maxDistance();
|
|
+
|
|
+ /**
|
|
+ * Sets the maximum distance.
|
|
+ *
|
|
+ * @param maxDistance the new maxDistance
|
|
+ * @return a reference to this object
|
|
+ */
|
|
+ @NotNull
|
|
+ @Contract("_ -> this")
|
|
+ Builder maxDistance(double maxDistance);
|
|
+
|
|
+ /**
|
|
+ * Gets the FluidCollisionMode when looking for block collisions.
|
|
+ *
|
|
+ * @return the FluidCollisionMode
|
|
+ */
|
|
+ @Nullable
|
|
+ FluidCollisionMode fluidCollisionMode();
|
|
+
|
|
+ /**
|
|
+ * Sets the FluidCollisionMode when looking for block collisions.
|
|
+ *
|
|
+ * @param fluidCollisionMode the new FluidCollisionMode
|
|
+ * @return a reference to this object
|
|
+ */
|
|
+ @NotNull
|
|
+ @Contract("_ -> this")
|
|
+ Builder fluidCollisionMode(@Nullable FluidCollisionMode fluidCollisionMode);
|
|
+
|
|
+ /**
|
|
+ * Gets if the raytrace will ignore passable blocks when looking for block collisions.
|
|
+ *
|
|
+ * @return if the raytrace will ignore passable blocks
|
|
+ */
|
|
+ boolean ignorePassableBlocks();
|
|
+
|
|
+ /**
|
|
+ * Gets if the raytrace will ignore passable blocks when looking for block collisions.
|
|
+ *
|
|
+ * @param ignorePassableBlocks if the raytrace should ignore passable blocks
|
|
+ * @return a reference to this object
|
|
+ */
|
|
+ @NotNull
|
|
+ @Contract("_ -> this")
|
|
+ Builder ignorePassableBlocks(boolean ignorePassableBlocks);
|
|
+
|
|
+ /**
|
|
+ * Gets the size of the raytrace when looking for entity collisions.
|
|
+ *
|
|
+ * @return the raytrace size
|
|
+ */
|
|
+ double raySize();
|
|
+
|
|
+ /**
|
|
+ * Sets the size of the raytrace when looking for entity collisions.
|
|
+ *
|
|
+ * @param raySize the new raytrace size
|
|
+ * @return a reference to this object
|
|
+ */
|
|
+ @NotNull
|
|
+ @Contract("_ -> this")
|
|
+ Builder raySize(double raySize);
|
|
+
|
|
+ /**
|
|
+ * Gets the current entity filter when looking for entity collisions.
|
|
+ *
|
|
+ * @return predicate for entities the ray can potentially collide with, or null to consider all entities
|
|
+ */
|
|
+ @Nullable
|
|
+ Predicate<? super Entity> entityFilter();
|
|
+
|
|
+ /**
|
|
+ * Sets the current entity filter when looking for entity collisions.
|
|
+ *
|
|
+ * @param entityFilter predicate for entities the ray can potentially collide with, or null to consider all entities
|
|
+ * @return a reference to this object
|
|
+ */
|
|
+ @NotNull
|
|
+ @Contract("_ -> this")
|
|
+ Builder entityFilter(@Nullable Predicate<? super Entity> entityFilter);
|
|
+
|
|
+ /**
|
|
+ * Gets the current block filter when looking for block collisions.
|
|
+ *
|
|
+ * @return predicate for blocks the ray can potentially collide with, or null to consider all blocks
|
|
+ */
|
|
+ @Nullable
|
|
+ Predicate<? super Block> blockFilter();
|
|
+
|
|
+ /**
|
|
+ * Sets the current block filter when looking for block collisions.
|
|
+ *
|
|
+ * @param blockFilter predicate for blocks the ray can potentially collide with, or null to consider all blocks
|
|
+ * @return a reference to this object
|
|
+ */
|
|
+ @NotNull
|
|
+ @Contract("_ -> this")
|
|
+ Builder blockFilter(@Nullable Predicate<? super Block> blockFilter);
|
|
+
|
|
+ /**
|
|
+ * Builds a configuration based on the provided targets.
|
|
+ *
|
|
+ * @return the configuration
|
|
+ */
|
|
+ @NotNull
|
|
+ RayTraceConfiguration target(@NotNull Targets first, @NotNull Targets... others);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * List of Targets the builder can target.
|
|
+ */
|
|
+ enum Targets {
|
|
+ ENTITIES,
|
|
+ BLOCKS
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/io/papermc/paper/raytracing/RayTraceConfigurationBuilderImpl.java b/src/main/java/io/papermc/paper/raytracing/RayTraceConfigurationBuilderImpl.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..b6a9e03023ced3d88dc39a59a314d1fdc1d827ce
|
|
--- /dev/null
|
|
+++ b/src/main/java/io/papermc/paper/raytracing/RayTraceConfigurationBuilderImpl.java
|
|
@@ -0,0 +1,93 @@
|
|
+package io.papermc.paper.raytracing;
|
|
+import org.bukkit.FluidCollisionMode;
|
|
+import org.bukkit.block.Block;
|
|
+import org.bukkit.entity.Entity;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+import java.util.ArrayList;
|
|
+import java.util.List;
|
|
+import java.util.function.Predicate;
|
|
+
|
|
+public class RayTraceConfigurationBuilderImpl implements RayTraceConfiguration.Builder {
|
|
+
|
|
+ private double maxDistance;
|
|
+ private FluidCollisionMode fluidCollisionMode = FluidCollisionMode.NEVER;
|
|
+ private boolean ignorePassableBlocks;
|
|
+ private double raySize = 0.0D;
|
|
+ private Predicate<? super Entity> entityFilter;
|
|
+ private Predicate<? super Block> blockFilter;
|
|
+
|
|
+
|
|
+ @Override
|
|
+ public double maxDistance() {
|
|
+ return maxDistance;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @NotNull RayTraceConfiguration.Builder maxDistance(double maxDistance) {
|
|
+ this.maxDistance = maxDistance;
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @Nullable FluidCollisionMode fluidCollisionMode() {
|
|
+ return fluidCollisionMode;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @NotNull RayTraceConfiguration.Builder fluidCollisionMode(@Nullable FluidCollisionMode fluidCollisionMode) {
|
|
+ this.fluidCollisionMode = fluidCollisionMode;
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean ignorePassableBlocks() {
|
|
+ return ignorePassableBlocks;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @NotNull RayTraceConfiguration.Builder ignorePassableBlocks(boolean ignorePassableBlocks) {
|
|
+ this.ignorePassableBlocks = ignorePassableBlocks;
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public double raySize() {
|
|
+ return raySize;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @NotNull RayTraceConfiguration.Builder raySize(double raySize) {
|
|
+ this.raySize = raySize;
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @Nullable Predicate<? super Entity> entityFilter() {
|
|
+ return entityFilter;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @NotNull RayTraceConfiguration.Builder entityFilter(@Nullable Predicate<? super Entity> entityFilter) {
|
|
+ this.entityFilter = entityFilter;
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @Nullable Predicate<? super Block> blockFilter() {
|
|
+ return blockFilter;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @NotNull RayTraceConfiguration.Builder blockFilter(@Nullable Predicate<? super Block> blockFilter) {
|
|
+ this.blockFilter = blockFilter;
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public @NotNull RayTraceConfiguration target(@NotNull RayTraceConfiguration.Targets first, @NotNull RayTraceConfiguration.Targets... others) {
|
|
+ List<RayTraceConfiguration.Targets> targets = new ArrayList<>(List.of(others)); // Need to make this immutable later
|
|
+ targets.add(first);
|
|
+ return new RayTraceConfigurationImpl(maxDistance, fluidCollisionMode, ignorePassableBlocks, raySize, entityFilter, blockFilter, targets);
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/io/papermc/paper/raytracing/RayTraceConfigurationImpl.java b/src/main/java/io/papermc/paper/raytracing/RayTraceConfigurationImpl.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..ffe49cd1ecc9075315c9695dd1aa4a3f94caefcc
|
|
--- /dev/null
|
|
+++ b/src/main/java/io/papermc/paper/raytracing/RayTraceConfigurationImpl.java
|
|
@@ -0,0 +1,15 @@
|
|
+package io.papermc.paper.raytracing;
|
|
+
|
|
+import org.bukkit.FluidCollisionMode;
|
|
+import org.bukkit.block.Block;
|
|
+import org.bukkit.entity.Entity;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+import java.util.List;
|
|
+import java.util.function.Predicate;
|
|
+
|
|
+public record RayTraceConfigurationImpl(double maxDistance, @Nullable FluidCollisionMode fluidCollisionMode,
|
|
+ boolean ignorePassableBlocks, double raySize,
|
|
+ @Nullable Predicate<? super Entity> entityFilter, @Nullable Predicate<? super Block> blockFilter,
|
|
+ @NotNull List<Targets> targets) implements RayTraceConfiguration {
|
|
+}
|
|
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
|
|
index 97f97ea5c6aa513c439f86a9c82821e0f7d9cd1e..972bee9c2fc9a79c7ab2922b88d567fd03ff831a 100644
|
|
--- a/src/main/java/org/bukkit/World.java
|
|
+++ b/src/main/java/org/bukkit/World.java
|
|
@@ -1908,6 +1908,33 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
|
|
@Nullable RayTraceResult rayTrace(io.papermc.paper.math.@NotNull Position start, @NotNull Vector direction, double maxDistance, @NotNull FluidCollisionMode fluidCollisionMode, boolean ignorePassableBlocks, double raySize, @Nullable Predicate<? super Entity> filter, @Nullable Predicate<? super Block> canCollide);
|
|
// Paper end
|
|
|
|
+ // Paper start - Add RayTraceConfiguration
|
|
+ /**
|
|
+ * Performs a ray trace that checks for both block and entity collisions.
|
|
+ * <p>
|
|
+ * Block collisions use the blocks' precise collision shapes. The
|
|
+ * <code>raySize</code> parameter is only taken into account for entity
|
|
+ * collision checks.
|
|
+ * <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.
|
|
+ * <p>
|
|
+ * This may cause loading of chunks! Some implementations may impose
|
|
+ * artificial restrictions on the maximum distance.
|
|
+ *
|
|
+ * @param start the start position
|
|
+ * @param direction the ray direction
|
|
+ * @param config the configuration
|
|
+ * @return the closest ray trace hit result with either a block or an
|
|
+ * entity, or <code>null</code> if there is no hit
|
|
+ */
|
|
+ @Nullable RayTraceResult rayTrace(@NotNull Location start, @NotNull Vector direction, @NotNull io.papermc.paper.raytracing.RayTraceConfiguration config);
|
|
+ // Paper end - Add RayTraceConfiguration
|
|
+
|
|
/**
|
|
* Gets the default spawn {@link Location} of this world
|
|
*
|