2020-05-06 09:48:49 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2019-05-06 07:20:16 +00:00
From: BillyGalbreath <Blake.Galbreath@GMail.com>
Date: Sat, 22 Sep 2018 00:33:08 -0500
Subject: [PATCH] Add LivingEntity#getTargetEntity
diff --git a/src/main/java/net/minecraft/server/AxisAlignedBB.java b/src/main/java/net/minecraft/server/AxisAlignedBB.java
2020-04-22 15:11:50 +00:00
index 3e90b57b6fd5dcb6cb1325861306e2ff84d0cccb..1a466e92956e1eee5c0a7d30f89177e4b189fcb8 100644
2019-05-06 07:20:16 +00:00
--- a/src/main/java/net/minecraft/server/AxisAlignedBB.java
+++ b/src/main/java/net/minecraft/server/AxisAlignedBB.java
@@ -108,6 +108,7 @@ public class AxisAlignedBB {
return this.b(vec3d.x, vec3d.y, vec3d.z);
}
+ public final AxisAlignedBB expand(double x, double y, double z) { return b(x, y, z); } // Paper - OBFHELPER
public AxisAlignedBB b(double d0, double d1, double d2) {
double d3 = this.minX;
double d4 = this.minY;
@@ -137,6 +138,12 @@ public class AxisAlignedBB {
return new AxisAlignedBB(d3, d4, d5, d6, d7, d8);
}
+ // Paper start
+ public AxisAlignedBB grow(double d0) {
+ return grow(d0, d0, d0);
+ }
+ // Paper end
+
public AxisAlignedBB grow(double d0, double d1, double d2) {
double d3 = this.minX - d0;
double d4 = this.minY - d1;
2020-04-22 15:11:50 +00:00
@@ -196,6 +203,7 @@ public class AxisAlignedBB {
2019-05-06 07:20:16 +00:00
return this.minX < d3 && this.maxX > d0 && this.minY < d4 && this.maxY > d1 && this.minZ < d5 && this.maxZ > d2;
}
+ public final boolean contains(Vec3D vec3d) { return c(vec3d); } // Paper - OBFHELPER
public boolean c(Vec3D vec3d) {
return this.e(vec3d.x, vec3d.y, vec3d.z);
}
2020-04-22 15:11:50 +00:00
@@ -229,6 +237,7 @@ public class AxisAlignedBB {
2019-05-06 07:20:16 +00:00
return this.g(-d0);
}
+ public final Optional<Vec3D> calculateIntercept(Vec3D vec3d, Vec3D vec3d1) { return b(vec3d, vec3d1); } // Paper - OBFHELPER
public Optional<Vec3D> b(Vec3D vec3d, Vec3D vec3d1) {
2019-05-27 23:01:45 +00:00
double[] adouble = new double[]{1.0D};
2019-05-06 07:20:16 +00:00
double d0 = vec3d1.x - vec3d.x;
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
2020-04-22 15:11:50 +00:00
index 3d44188e68df31d188f25820000e2c39aa5b6ce0..9368c4afa7bbb336894d73069b3c0858231d1e81 100644
2019-05-06 07:20:16 +00:00
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
2020-04-22 15:11:50 +00:00
@@ -1476,6 +1476,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2019-05-06 07:20:16 +00:00
return this.c(f - 90.0F, f1);
}
+ public final Vec3D getEyePosition(float partialTicks) { return j(partialTicks); } // Paper - OBFHELPER
2019-12-12 16:20:43 +00:00
public final Vec3D j(float f) {
2019-05-06 07:20:16 +00:00
if (f == 1.0F) {
2019-12-12 16:20:43 +00:00
return new Vec3D(this.locX(), this.getHeadY(), this.locZ());
2020-04-22 15:11:50 +00:00
@@ -2132,6 +2133,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2019-05-06 07:20:16 +00:00
return this.getPassengers().size() < 1;
}
2019-12-12 16:20:43 +00:00
+ public final float getCollisionBorderSize() { return aV(); } // Paper - OBFHELPER
public float aV() {
2019-05-06 07:20:16 +00:00
return 0.0F;
}
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
2020-05-06 09:48:49 +00:00
index c7c248f63f8cfef6d6b095a926028c519ec3ffd4..aecd5011cbe8c6f78be436084621f4c31cdac23a 100644
2019-05-06 07:20:16 +00:00
--- a/src/main/java/net/minecraft/server/EntityLiving.java
+++ b/src/main/java/net/minecraft/server/EntityLiving.java
2020-04-27 07:34:45 +00:00
@@ -3310,6 +3310,37 @@ public abstract class EntityLiving extends Entity {
2019-05-06 07:20:16 +00:00
return world.rayTrace(raytrace);
}
+ public MovingObjectPositionEntity getTargetEntity(int maxDistance) {
+ if (maxDistance < 1 || maxDistance > 120) {
+ throw new IllegalArgumentException("maxDistance must be between 1-120");
+ }
+
+ Vec3D start = this.getEyePosition(1.0F);
+ Vec3D direction = this.getLookDirection();
+ Vec3D end = start.add(direction.x * maxDistance, direction.y * maxDistance, direction.z * maxDistance);
+
+ List<Entity> entityList = world.getEntities(this, getBoundingBox().expand(direction.x * maxDistance, direction.y * maxDistance, direction.z * maxDistance).grow(1.0D, 1.0D, 1.0D), IEntitySelector.notSpectator().and(Entity::isInteractable));
+
+ double distance = 0.0D;
+ MovingObjectPositionEntity result = null;
+
+ for (Entity entity : entityList) {
+ AxisAlignedBB aabb = entity.getBoundingBox().grow((double) entity.getCollisionBorderSize());
+ Optional<Vec3D> rayTraceResult = aabb.calculateIntercept(start, end);
+
+ if (rayTraceResult.isPresent()) {
+ Vec3D rayTrace = rayTraceResult.get();
+ double distanceTo = start.distanceSquared(rayTrace);
+ if (distanceTo < distance || distance == 0.0D) {
+ result = new MovingObjectPositionEntity(entity, rayTrace);
+ distance = distanceTo;
+ }
+ }
+ }
+
+ return result;
+ }
+
public int shieldBlockingDelay = world.paperConfig.shieldBlockingDelay;
public int getShieldBlockingDelay() {
diff --git a/src/main/java/net/minecraft/server/IEntitySelector.java b/src/main/java/net/minecraft/server/IEntitySelector.java
2020-05-06 09:48:49 +00:00
index c1f462d9d3eefdab767bea28da484bfb1dfda06a..498f381099b2cf9460104688e12afc5f586e057a 100644
2019-05-06 07:20:16 +00:00
--- a/src/main/java/net/minecraft/server/IEntitySelector.java
+++ b/src/main/java/net/minecraft/server/IEntitySelector.java
@@ -18,6 +18,7 @@ public final class IEntitySelector {
public static final Predicate<Entity> e = (entity) -> {
2019-06-03 03:26:25 +00:00
return !(entity instanceof EntityHuman) || !entity.isSpectator() && !((EntityHuman) entity).isCreative();
2019-05-06 07:20:16 +00:00
};
+ public static Predicate<Entity> notSpectator() { return f; } // Paper - OBFHELPER
public static final Predicate<Entity> f = (entity) -> {
2019-06-03 03:26:25 +00:00
return !entity.isSpectator();
2019-05-06 07:20:16 +00:00
};
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
2020-05-06 09:48:49 +00:00
index 9e082d4514eb57667cb50a390bef7ab72b94e838..7d101d9630848b68b15acc5efdc16ae51dc1f132 100644
2019-05-06 07:20:16 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
2020-02-25 18:57:15 +00:00
@@ -216,6 +216,33 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
2019-05-06 07:20:16 +00:00
new com.destroystokyo.paper.block.TargetBlockInfo(org.bukkit.craftbukkit.block.CraftBlock.at(getHandle().world, ((net.minecraft.server.MovingObjectPositionBlock)rayTrace).getBlockPosition()),
net.minecraft.server.MCUtil.toBukkitBlockFace(((net.minecraft.server.MovingObjectPositionBlock)rayTrace).getDirection()));
}
+
+ public Entity getTargetEntity(int maxDistance, boolean ignoreBlocks) {
+ net.minecraft.server.MovingObjectPositionEntity rayTrace = rayTraceEntity(maxDistance, ignoreBlocks);
+ return rayTrace == null ? null : rayTrace.getEntity().getBukkitEntity();
+ }
+
+ public com.destroystokyo.paper.entity.TargetEntityInfo getTargetEntityInfo(int maxDistance, boolean ignoreBlocks) {
+ net.minecraft.server.MovingObjectPositionEntity rayTrace = rayTraceEntity(maxDistance, ignoreBlocks);
+ return rayTrace == null ? null : new com.destroystokyo.paper.entity.TargetEntityInfo(rayTrace.getEntity().getBukkitEntity(), new org.bukkit.util.Vector(rayTrace.getPos().x, rayTrace.getPos().y, rayTrace.getPos().z));
+ }
+
+ public net.minecraft.server.MovingObjectPositionEntity rayTraceEntity(int maxDistance, boolean ignoreBlocks) {
+ net.minecraft.server.MovingObjectPositionEntity rayTrace = getHandle().getTargetEntity(maxDistance);
+ if (rayTrace == null) {
+ return null;
+ }
+ if (!ignoreBlocks) {
+ net.minecraft.server.MovingObjectPosition rayTraceBlocks = getHandle().getRayTrace(maxDistance, net.minecraft.server.RayTrace.FluidCollisionOption.NONE);
+ if (rayTraceBlocks != null) {
+ net.minecraft.server.Vec3D eye = getHandle().getEyePosition(1.0F);
+ if (eye.distanceSquared(rayTraceBlocks.getPos()) <= eye.distanceSquared(rayTrace.getPos())) {
+ return null;
+ }
+ }
+ }
+ return rayTrace;
+ }
// Paper end
@Override