2023-11-04 23:00:51 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: TonytheMacaroni <tonythemacaroni123@gmail.com>
Date: Wed, 6 Sep 2023 19:24:16 -0400
Subject: [PATCH] Add predicate for blocks when raytracing
diff --git a/src/main/java/net/minecraft/world/level/BlockGetter.java b/src/main/java/net/minecraft/world/level/BlockGetter.java
2024-10-25 12:30:19 +02:00
index 7e1a332168357b9af14dbe3299549c2c93903fa6..93738c7dea1ea3d19013a47380391274612a719b 100644
2023-11-04 23:00:51 +01:00
--- a/src/main/java/net/minecraft/world/level/BlockGetter.java
+++ b/src/main/java/net/minecraft/world/level/BlockGetter.java
2024-10-24 12:11:32 +02:00
@@ -71,6 +71,12 @@ public interface BlockGetter extends LevelHeightAccessor {
2023-11-04 23:00:51 +01:00
// CraftBukkit start - moved block handling into separate method for use by Block#rayTrace
default BlockHitResult clip(ClipContext raytrace1, BlockPos blockposition) {
2024-01-13 18:34:33 +01:00
+ // Paper start - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
+ return clip(raytrace1, blockposition, null);
+ }
+
2023-11-25 23:34:42 +01:00
+ default BlockHitResult clip(ClipContext raytrace1, BlockPos blockposition, java.util.function.Predicate<? super org.bukkit.block.Block> canCollide) {
2024-01-13 18:34:33 +01:00
+ // Paper end - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
// Paper start - Prevent raytrace from loading chunks
BlockState iblockdata = this.getBlockStateIfLoaded(blockposition);
if (iblockdata == null) {
2024-10-24 12:11:32 +02:00
@@ -80,7 +86,7 @@ public interface BlockGetter extends LevelHeightAccessor {
2024-10-25 12:30:19 +02:00
return BlockHitResult.miss(raytrace1.getTo(), Direction.getApproximateNearest(vec3d.x, vec3d.y, vec3d.z), BlockPos.containing(raytrace1.getTo()));
2023-11-04 23:00:51 +01:00
}
2024-01-16 19:27:39 +01:00
// Paper end - Prevent raytrace from loading chunks
- if (iblockdata.isAir()) return null; // Paper - Perf: optimise air cases
2024-01-22 19:01:10 +01:00
+ if (iblockdata.isAir() || (canCollide != null && this instanceof LevelAccessor levelAccessor && !canCollide.test(org.bukkit.craftbukkit.block.CraftBlock.at(levelAccessor, blockposition)))) return null; // Paper - Perf: optimise air cases & check canCollide predicate
2024-01-16 19:27:39 +01:00
FluidState fluid = iblockdata.getFluidState(); // Paper - Perf: don't need to go to world state again
2023-11-04 23:00:51 +01:00
Vec3 vec3d = raytrace1.getFrom();
Vec3 vec3d1 = raytrace1.getTo();
2024-10-24 12:11:32 +02:00
@@ -96,8 +102,14 @@ public interface BlockGetter extends LevelHeightAccessor {
2023-11-04 23:00:51 +01:00
// CraftBukkit end
default BlockHitResult clip(ClipContext context) {
2024-01-13 18:34:33 +01:00
+ // Paper start - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
+ return clip(context, (java.util.function.Predicate<org.bukkit.block.Block>) null);
+ }
+
2023-11-25 23:34:42 +01:00
+ default BlockHitResult clip(ClipContext context, java.util.function.Predicate<? super org.bukkit.block.Block> canCollide) {
2024-01-13 18:34:33 +01:00
+ // Paper end - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
return (BlockHitResult) BlockGetter.traverseBlocks(context.getFrom(), context.getTo(), context, (raytrace1, blockposition) -> {
- return this.clip(raytrace1, blockposition); // CraftBukkit - moved into separate method
2024-01-13 18:34:33 +01:00
+ return this.clip(raytrace1, blockposition, canCollide); // CraftBukkit - moved into separate method // Paper - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
}, (raytrace1) -> {
Vec3 vec3d = raytrace1.getFrom().subtract(raytrace1.getTo());
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2024-11-11 20:17:36 +01:00
index bfe7d57310f822edaa4ddb3ebddb2086ffee8a4a..acb9da6f6560f6cb4ac38c07aca449110d0e2e76 100644
2023-11-04 23:00:51 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2024-11-11 20:17:36 +01:00
@@ -1108,9 +1108,15 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2023-11-04 23:00:51 +01:00
@Override
2023-11-25 23:34:42 +01:00
public RayTraceResult rayTraceEntities(Location start, Vector direction, double maxDistance, double raySize, Predicate<? super Entity> filter) {
2024-01-13 18:34:33 +01:00
+ // Paper start - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
+ return rayTraceEntities((io.papermc.paper.math.Position) start, direction, maxDistance, raySize, filter);
+ }
+
2023-11-25 23:34:42 +01:00
+ public RayTraceResult rayTraceEntities(io.papermc.paper.math.Position start, Vector direction, double maxDistance, double raySize, Predicate<? super Entity> filter) {
2023-11-04 23:00:51 +01:00
Preconditions.checkArgument(start != null, "Location start cannot be null");
- Preconditions.checkArgument(this.equals(start.getWorld()), "Location start cannot be in a different world");
- start.checkFinite();
+ Preconditions.checkArgument(!(start instanceof Location location) || this.equals(location.getWorld()), "Location start cannot be in a different world");
+ Preconditions.checkArgument(start.isFinite(), "Location start is not finite");
2024-01-13 18:34:33 +01:00
+ // Paper end - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
Preconditions.checkArgument(direction != null, "Vector direction cannot be null");
direction.checkFinite();
2024-11-11 20:17:36 +01:00
@@ -1160,9 +1166,16 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2023-11-04 23:00:51 +01:00
@Override
public RayTraceResult rayTraceBlocks(Location start, Vector direction, double maxDistance, FluidCollisionMode fluidCollisionMode, boolean ignorePassableBlocks) {
2024-01-13 18:34:33 +01:00
+ // Paper start - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
+ return this.rayTraceBlocks(start, direction, maxDistance, fluidCollisionMode, ignorePassableBlocks, null);
+ }
+
+ @Override
2023-11-25 23:34:42 +01:00
+ public RayTraceResult rayTraceBlocks(io.papermc.paper.math.Position start, Vector direction, double maxDistance, FluidCollisionMode fluidCollisionMode, boolean ignorePassableBlocks, Predicate<? super Block> canCollide) {
2023-11-04 23:00:51 +01:00
Preconditions.checkArgument(start != null, "Location start cannot be null");
- Preconditions.checkArgument(this.equals(start.getWorld()), "Location start cannot be in a different world");
- start.checkFinite();
+ Preconditions.checkArgument(!(start instanceof Location location) || this.equals(location.getWorld()), "Location start cannot be in a different world");
+ Preconditions.checkArgument(start.isFinite(), "Location start is not finite");
2024-01-13 18:34:33 +01:00
+ // Paper end - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
Preconditions.checkArgument(direction != null, "Vector direction cannot be null");
direction.checkFinite();
2024-11-11 20:17:36 +01:00
@@ -1175,16 +1188,23 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2023-11-04 23:00:51 +01:00
}
Vector dir = direction.clone().normalize().multiply(maxDistance);
- Vec3 startPos = CraftLocation.toVec3D(start);
2024-01-13 18:34:33 +01:00
+ Vec3 startPos = io.papermc.paper.util.MCUtil.toVec3(start); // Paper - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
Vec3 endPos = startPos.add(dir.getX(), dir.getY(), dir.getZ());
2023-12-06 19:18:53 +01:00
- HitResult nmsHitResult = this.getHandle().clip(new ClipContext(startPos, endPos, ignorePassableBlocks ? ClipContext.Block.COLLIDER : ClipContext.Block.OUTLINE, CraftFluidCollisionMode.toNMS(fluidCollisionMode), CollisionContext.empty()));
2024-01-13 18:34:33 +01:00
+ HitResult nmsHitResult = this.getHandle().clip(new ClipContext(startPos, endPos, ignorePassableBlocks ? ClipContext.Block.COLLIDER : ClipContext.Block.OUTLINE, CraftFluidCollisionMode.toNMS(fluidCollisionMode), CollisionContext.empty()), canCollide); // Paper - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
return CraftRayTraceResult.fromNMS(this, nmsHitResult);
}
@Override
2023-11-25 23:34:42 +01:00
public RayTraceResult rayTrace(Location start, Vector direction, double maxDistance, FluidCollisionMode fluidCollisionMode, boolean ignorePassableBlocks, double raySize, Predicate<? super Entity> filter) {
2023-11-04 23:00:51 +01:00
- RayTraceResult blockHit = this.rayTraceBlocks(start, direction, maxDistance, fluidCollisionMode, ignorePassableBlocks);
2024-01-13 18:34:33 +01:00
+ // Paper start - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
+ return this.rayTrace(start, direction, maxDistance, fluidCollisionMode, ignorePassableBlocks, raySize, filter, null);
+ }
+
+ @Override
2023-11-25 23:34:42 +01:00
+ public RayTraceResult rayTrace(io.papermc.paper.math.Position start, Vector direction, double maxDistance, FluidCollisionMode fluidCollisionMode, boolean ignorePassableBlocks, double raySize, Predicate<? super Entity> filter, Predicate<? super Block> canCollide) {
2023-11-04 23:00:51 +01:00
+ RayTraceResult blockHit = this.rayTraceBlocks(start, direction, maxDistance, fluidCollisionMode, ignorePassableBlocks, canCollide);
2024-01-13 18:34:33 +01:00
+ // Paper end - Add predicate for blocks when raytracing
2023-11-04 23:00:51 +01:00
Vector startVec = null;
double blockHitDistance = maxDistance;