Add more line of sight methods (#5749)

This commit is contained in:
TwoLeggedCat 2021-06-05 03:25:16 -05:00 committed by GitHub
parent 8fd4e70db7
commit 0daded2f25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,49 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: TwoLeggedCat <80929284+TwoLeggedCat@users.noreply.github.com>
Date: Sat, 29 May 2021 14:33:18 -0500
Subject: [PATCH] Add more line of sight methods
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index a1496fe00a2d5ba6c1af054d4327f868b2cd7344..f66ec2dfdc64871f8b752bf44086954300804f08 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -76,6 +76,14 @@ public interface World extends PluginMessageRecipient, Metadatable, net.kyori.ad
*/
@NotNull
io.papermc.paper.world.MoonPhase getMoonPhase();
+
+ /**
+ * Tell whether a line of sight exists between the given locations
+ * @param from Location to start at
+ * @param to target Location
+ * @return whether a line of sight exists between {@code from} and {@code to}
+ */
+ public boolean lineOfSightExists(@NotNull Location from, @NotNull Location to);
// Paper end
/**
diff --git a/src/main/java/org/bukkit/entity/LivingEntity.java b/src/main/java/org/bukkit/entity/LivingEntity.java
index b6de41e3e718fa5d1b82c6f68b153e60a81265e7..ccb81ceee74fff50ec3ed88ae0a41f790c40ae87 100644
--- a/src/main/java/org/bukkit/entity/LivingEntity.java
+++ b/src/main/java/org/bukkit/entity/LivingEntity.java
@@ -483,6 +483,19 @@ public interface LivingEntity extends Attributable, Damageable, ProjectileSource
*/
public boolean hasLineOfSight(@NotNull Entity other);
+ // Paper start
+ /**
+ * Checks whether the living entity has block line of sight to the given block.
+ * <p>
+ * This uses the same algorithm that hostile mobs use to find the closest
+ * player.
+ *
+ * @param location the location to determine line of sight to
+ * @return true if there is a line of sight, false if not
+ */
+ public boolean hasLineOfSight(@NotNull Location location);
+ // Paper end
+
/**
* Returns if the living entity despawns when away from players or not.
* <p>

View File

@ -0,0 +1,62 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: TwoLeggedCat <80929284+TwoLeggedCat@users.noreply.github.com>
Date: Sat, 29 May 2021 14:33:25 -0500
Subject: [PATCH] Add more line of sight methods
diff --git a/src/main/java/net/minecraft/world/entity/EntityLiving.java b/src/main/java/net/minecraft/world/entity/EntityLiving.java
index 2657dd71213455e2da3a4ff4d338a33896ffe855..b7b07b652b29e6f84f87fc92add99ce68f8bbd09 100644
--- a/src/main/java/net/minecraft/world/entity/EntityLiving.java
+++ b/src/main/java/net/minecraft/world/entity/EntityLiving.java
@@ -3094,6 +3094,7 @@ public abstract class EntityLiving extends Entity {
Vec3D vec3d = new Vec3D(this.locX(), this.getHeadY(), this.locZ());
Vec3D vec3d1 = new Vec3D(entity.locX(), entity.getHeadY(), entity.locZ());
+ // Paper - diff on change - used in CraftLivingEntity#hasLineOfSight(Location) and CraftWorld#lineOfSightExists
return this.world.rayTrace(new RayTrace(vec3d, vec3d1, RayTrace.BlockCollisionOption.COLLIDER, RayTrace.FluidCollisionOption.NONE, this)).getType() == MovingObjectPosition.EnumMovingObjectType.MISS;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 9d858f91828d6c2787ff1dc677a247bcab172701..02198dbe9e80c2990e8a09b2b763748dae727a03 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -333,6 +333,17 @@ public class CraftWorld implements World {
public io.papermc.paper.world.MoonPhase getMoonPhase() {
return io.papermc.paper.world.MoonPhase.getPhase(getFullTime() / 24000L);
}
+
+ @Override
+ public boolean lineOfSightExists(Location from, Location to) {
+ Validate.notNull(from, "from parameter in lineOfSightExists cannot be null");
+ Validate.notNull(to, "to parameter in lineOfSightExists cannot be null");
+ if (from.getWorld() != to.getWorld()) return false;
+ Vec3D vec3d = new Vec3D(from.getX(), from.getY(), from.getZ());
+ Vec3D vec3d1 = new Vec3D(to.getX(), to.getY(), to.getZ());
+
+ return this.getHandle().rayTrace(new RayTrace(vec3d, vec3d1, RayTrace.BlockCollisionOption.COLLIDER, RayTrace.FluidCollisionOption.NONE, null)).getType() == MovingObjectPosition.EnumMovingObjectType.MISS;
+ }
// Paper end
private static final Random rand = new Random();
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index 32428b1260bbc4bae67ec74969c67f71272de418..592dacf8cc924caac339a8810ba5b0d85448ed90 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -561,6 +561,17 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
return getHandle().hasLineOfSight(((CraftEntity) other).getHandle());
}
+ // Paper start
+ @Override
+ public boolean hasLineOfSight(Location loc) {
+ if (this.getHandle().world != ((CraftWorld) loc.getWorld()).getHandle()) return false;
+ Vec3D vec3d = new Vec3D(this.getHandle().locX(), this.getHandle().getHeadY(), this.getHandle().locZ());
+ Vec3D vec3d1 = new Vec3D(loc.getX(), loc.getY(), loc.getZ());
+
+ return this.getHandle().world.rayTrace(new RayTrace(vec3d, vec3d1, RayTrace.BlockCollisionOption.COLLIDER, RayTrace.FluidCollisionOption.NONE, this.getHandle())).getType() == MovingObjectPosition.EnumMovingObjectType.MISS;
+ }
+ // Paper end
+
@Override
public boolean getRemoveWhenFarAway() {
return getHandle() instanceof EntityInsentient && !((EntityInsentient) getHandle()).persistent;