Line Of Sight Changes

This commit is contained in:
TwoLeggedCat
2021-05-29 14:33:25 -05:00
parent c002a90053
commit 750a4590ce
3 changed files with 89 additions and 40 deletions

View File

@@ -526,5 +526,21 @@ public abstract class CraftRegionAccessor implements RegionAccessor {
public org.bukkit.NamespacedKey getKey() {
return org.bukkit.craftbukkit.util.CraftNamespacedKey.fromMinecraft(this.getHandle().getLevel().dimension().location());
}
public boolean lineOfSightExists(Location from, Location to) {
Preconditions.checkArgument(from != null, "from parameter in lineOfSightExists cannot be null");
Preconditions.checkArgument(to != null, "to parameter in lineOfSightExists cannot be null");
if (from.getWorld() != to.getWorld()) {
return false;
}
net.minecraft.world.phys.Vec3 start = new net.minecraft.world.phys.Vec3(from.getX(), from.getY(), from.getZ());
net.minecraft.world.phys.Vec3 end = new net.minecraft.world.phys.Vec3(to.getX(), to.getY(), to.getZ());
if (end.distanceToSqr(start) > 128D * 128D) {
return false; // Return early if the distance is greater than 128 blocks
}
return this.getHandle().clip(new net.minecraft.world.level.ClipContext(start, end, net.minecraft.world.level.ClipContext.Block.COLLIDER, net.minecraft.world.level.ClipContext.Fluid.NONE, net.minecraft.world.phys.shapes.CollisionContext.empty())).getType() == net.minecraft.world.phys.HitResult.Type.MISS;
}
// Paper end
}

View File

@@ -643,6 +643,23 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
return this.getHandle().hasLineOfSight(((CraftEntity) other).getHandle());
}
// Paper start
@Override
public boolean hasLineOfSight(Location loc) {
if (this.getHandle().level() != ((CraftWorld) loc.getWorld()).getHandle()) {
return false;
}
net.minecraft.world.phys.Vec3 start = new net.minecraft.world.phys.Vec3(this.getHandle().getX(), this.getHandle().getEyeY(), this.getHandle().getZ());
net.minecraft.world.phys.Vec3 end = new net.minecraft.world.phys.Vec3(loc.getX(), loc.getY(), loc.getZ());
if (end.distanceToSqr(start) > 128D * 128D) {
return false; // Return early if the distance is greater than 128 blocks
}
return this.getHandle().level().clipDirect(start, end, net.minecraft.world.phys.shapes.CollisionContext.of(this.getHandle())) == net.minecraft.world.phys.HitResult.Type.MISS;
}
// Paper end
@Override
public boolean getRemoveWhenFarAway() {
return this.getHandle() instanceof Mob && !((Mob) this.getHandle()).isPersistenceRequired();