Skip to content

Commit 59a8a56

Browse files
committed
Refactor ray-tracing logic to integrate vanilla ray-tracing results and improve accuracy.
1 parent 1315259 commit 59a8a56

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/main/java/net/bitbylogic/packetblocks/util/PacketBlockUtil.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.NonNull;
44
import net.bitbylogic.packetblocks.PacketBlocks;
55
import net.bitbylogic.packetblocks.block.PacketBlockManager;
6+
import org.bukkit.FluidCollisionMode;
67
import org.bukkit.Location;
78
import org.bukkit.Material;
89
import org.bukkit.World;
@@ -47,38 +48,34 @@ public static RayTraceResult rayTrace(Player player, double range) {
4748
Location eye = player.getEyeLocation();
4849
Vector direction = eye.getDirection().normalize();
4950

50-
Vector current = eye.toVector();
5151
World world = player.getWorld();
5252

53-
double step = 0.1;
54-
double eps = 1e-6;
53+
RayTraceResult vanillaResult = world.rayTraceBlocks(eye, direction, range, FluidCollisionMode.NEVER, false);
54+
55+
Vector current = eye.toVector();
56+
double step = 0.05;
5557

5658
for (double traveled = 0; traveled <= range; traveled += step) {
5759
current.add(direction.clone().multiply(step));
5860
Block block = world.getBlockAt(current.getBlockX(), current.getBlockY(), current.getBlockZ());
5961

60-
BoundingBox pointBox = new BoundingBox(
61-
current.getX() - eps, current.getY() - eps, current.getZ() - eps,
62-
current.getX() + eps, current.getY() + eps, current.getZ() + eps
63-
);
64-
6562
if (PacketBlockManager.getBlock(block.getLocation()).isPresent()) {
6663
Material blockMaterial = PacketBlockManager.getBlock(block.getLocation()).get().getBlockData().getMaterial();
6764
BoundingBox boundingBox = BoundingBoxes.getBoxAt(blockMaterial, block.getLocation());
6865

69-
if(boundingBox == null || !boundingBox.overlaps(pointBox)) {
66+
if(boundingBox == null) {
7067
continue;
7168
}
7269

73-
return new RayTraceResult(current, block, null);
74-
}
70+
RayTraceResult boxResult = boundingBox.rayTrace(eye.toVector(), direction, range);
7571

76-
if (!block.isEmpty()) {
77-
return new RayTraceResult(current, block, null);
72+
if (boxResult != null) {
73+
return new RayTraceResult(boxResult.getHitPosition(), block, boxResult.getHitBlockFace());
74+
}
7875
}
7976
}
8077

81-
return null;
78+
return vanillaResult;
8279
}
8380

8481
}

0 commit comments

Comments
 (0)