2021-06-13 06:03:02 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: BillyGalbreath <Blake.Galbreath@GMail.com>
Date: Mon, 3 Sep 2018 18:20:03 -0500
Subject: [PATCH] Add ray tracing methods to LivingEntity
2022-10-24 21:43:46 +02:00
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
2022-12-07 17:46:46 +01:00
index b19850ae31f6c796cb3491dd5070d28e0ffd242c..30a5484d0209e7b99e81b70f654f525103471eb8 100644
2022-10-24 21:43:46 +02:00
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -510,6 +510,18 @@ public final class MCUtil {
2021-06-13 06:03:02 +02:00
return getNMSWorld(entity.getWorld());
}
+ public static ClipContext.Fluid getNMSFluidCollisionOption(com.destroystokyo.paper.block.TargetBlockInfo.FluidMode fluidMode) {
+ switch (fluidMode) {
+ case NEVER:
+ return ClipContext.Fluid.NONE;
+ case SOURCE_ONLY:
+ return ClipContext.Fluid.SOURCE_ONLY;
+ case ALWAYS:
+ return ClipContext.Fluid.ANY;
+ }
+ return null;
+ }
+
public static BlockFace toBukkitBlockFace(Direction enumDirection) {
switch (enumDirection) {
case DOWN:
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2022-12-07 20:22:28 +01:00
index 677ba704c32e590f3298575b1cc060bb69414ca4..d825dd9a990f52e378755e86c02c6a35f3bb3659 100644
2021-06-13 06:03:02 +02:00
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2022-12-07 20:22:28 +01:00
@@ -3772,6 +3772,23 @@ public abstract class LivingEntity extends Entity {
2021-06-13 06:03:02 +02:00
}
// Paper start
+ public HitResult getRayTrace(int maxDistance) {
+ return getRayTrace(maxDistance, ClipContext.Fluid.NONE);
+ }
+
+ public HitResult getRayTrace(int maxDistance, ClipContext.Fluid fluidCollisionOption) {
+ if (maxDistance < 1 || maxDistance > 120) {
+ throw new IllegalArgumentException("maxDistance must be between 1-120");
+ }
+
+ Vec3 start = new Vec3(getX(), getY() + getEyeHeight(), getZ());
+ org.bukkit.util.Vector dir = getBukkitEntity().getLocation().getDirection().multiply(maxDistance);
+ Vec3 end = new Vec3(start.x + dir.getX(), start.y + dir.getY(), start.z + dir.getZ());
+ ClipContext raytrace = new ClipContext(start, end, ClipContext.Block.OUTLINE, fluidCollisionOption, this);
+
+ return level.clip(raytrace);
+ }
+
2022-06-09 10:51:45 +02:00
public int shieldBlockingDelay = level.paperConfig().misc.shieldBlockingDelay;
2021-06-13 06:03:02 +02:00
public int getShieldBlockingDelay() {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
2022-10-24 21:43:46 +02:00
index 04829ced2048b07aa4b2dcf98a601d1fdd9431fb..6c5cb2e5fb1f5bb7eb05a465a24d62e1dad57b93 100644
2021-06-13 06:03:02 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
2022-09-12 13:31:45 +02:00
@@ -200,6 +200,28 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
2021-06-13 06:03:02 +02:00
return blocks.get(0);
}
+ // Paper start
+ @Override
+ public Block getTargetBlock(int maxDistance, com.destroystokyo.paper.block.TargetBlockInfo.FluidMode fluidMode) {
2022-10-24 21:43:46 +02:00
+ net.minecraft.world.phys.HitResult rayTrace = getHandle().getRayTrace(maxDistance, io.papermc.paper.util.MCUtil.getNMSFluidCollisionOption(fluidMode));
2021-06-13 06:03:02 +02:00
+ return !(rayTrace instanceof net.minecraft.world.phys.BlockHitResult) ? null : org.bukkit.craftbukkit.block.CraftBlock.at(getHandle().level, ((net.minecraft.world.phys.BlockHitResult)rayTrace).getBlockPos());
+ }
+
+ @Override
+ public org.bukkit.block.BlockFace getTargetBlockFace(int maxDistance, com.destroystokyo.paper.block.TargetBlockInfo.FluidMode fluidMode) {
2022-10-24 21:43:46 +02:00
+ net.minecraft.world.phys.HitResult rayTrace = getHandle().getRayTrace(maxDistance, io.papermc.paper.util.MCUtil.getNMSFluidCollisionOption(fluidMode));
+ return !(rayTrace instanceof net.minecraft.world.phys.BlockHitResult) ? null : io.papermc.paper.util.MCUtil.toBukkitBlockFace(((net.minecraft.world.phys.BlockHitResult)rayTrace).getDirection());
2021-06-13 06:03:02 +02:00
+ }
+
+ @Override
+ public com.destroystokyo.paper.block.TargetBlockInfo getTargetBlockInfo(int maxDistance, com.destroystokyo.paper.block.TargetBlockInfo.FluidMode fluidMode) {
2022-10-24 21:43:46 +02:00
+ net.minecraft.world.phys.HitResult rayTrace = getHandle().getRayTrace(maxDistance, io.papermc.paper.util.MCUtil.getNMSFluidCollisionOption(fluidMode));
2021-06-13 06:03:02 +02:00
+ return !(rayTrace instanceof net.minecraft.world.phys.BlockHitResult) ? null :
+ new com.destroystokyo.paper.block.TargetBlockInfo(org.bukkit.craftbukkit.block.CraftBlock.at(getHandle().level, ((net.minecraft.world.phys.BlockHitResult)rayTrace).getBlockPos()),
2022-10-24 21:43:46 +02:00
+ io.papermc.paper.util.MCUtil.toBukkitBlockFace(((net.minecraft.world.phys.BlockHitResult)rayTrace).getDirection()));
2021-06-13 06:03:02 +02:00
+ }
+ // Paper end
+
@Override
public List<Block> getLastTwoTargetBlocks(Set<Material> transparent, int maxDistance) {
return this.getLineOfSight(transparent, maxDistance, 2);