testserver/Remapped-Spigot-Server-Patches/0334-Add-LivingEntity-getTa...

188 lines
9.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
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/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index c5d6235a132818dfc78105e9d03d0687f697bb00..d106118dbf4fb270f8526e40a767dd4c563a333f 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -1503,6 +1503,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
return this.calculateViewVector(pitch - 90.0F, yaw);
}
+ public final Vec3 getEyePosition(float partialTicks) { return getEyePosition(partialTicks); } // Paper - OBFHELPER
public final Vec3 getEyePosition(float tickDelta) {
if (tickDelta == 1.0F) {
return new Vec3(this.getX(), this.getEyeY(), this.getZ());
@@ -2153,6 +2154,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
return this.getPassengers().size() < 1;
}
+ public final float getCollisionBorderSize() { return getPickRadius(); } // Paper - OBFHELPER
public float getPickRadius() {
return 0.0F;
}
diff --git a/src/main/java/net/minecraft/world/entity/EntitySelector.java b/src/main/java/net/minecraft/world/entity/EntitySelector.java
index e92a8c4c49c452e1f3f0c06398f2a74e3432262f..d3640975c5a33b4911428760691215905b987385 100644
--- a/src/main/java/net/minecraft/world/entity/EntitySelector.java
+++ b/src/main/java/net/minecraft/world/entity/EntitySelector.java
@@ -22,6 +22,7 @@ public final class EntitySelector {
public static final Predicate<Entity> NO_CREATIVE_OR_SPECTATOR = (entity) -> {
return !(entity instanceof Player) || !entity.isSpectator() && !((Player) entity).isCreative();
};
+ public static Predicate<Entity> canAITarget() { return ATTACK_ALLOWED; } // Paper - OBFHELPER
public static final Predicate<Entity> ATTACK_ALLOWED = (entity) -> {
return !(entity instanceof Player) || !entity.isSpectator() && !((Player) entity).isCreative() && entity.level.getDifficulty() != Difficulty.PEACEFUL;
};
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index 29d4ed42e5d763639a50d849ef274c4d848bc9c9..046a05925739005080af35c4be984303b575bf68 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -108,6 +108,7 @@ import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.AABB;
+import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.scores.PlayerTeam;
@@ -3657,6 +3658,37 @@ public abstract class LivingEntity extends Entity {
return level.clip(raytrace);
}
+ public EntityHitResult getTargetEntity(int maxDistance) {
+ if (maxDistance < 1 || maxDistance > 120) {
+ throw new IllegalArgumentException("maxDistance must be between 1-120");
+ }
+
+ Vec3 start = this.getEyePosition(1.0F);
+ Vec3 direction = this.getLookAngle();
+ Vec3 end = start.add(direction.x * maxDistance, direction.y * maxDistance, direction.z * maxDistance);
+
+ List<Entity> entityList = level.getEntities(this, getBoundingBox().expand(direction.x * maxDistance, direction.y * maxDistance, direction.z * maxDistance).inflate(1.0D, 1.0D, 1.0D), EntitySelector.canAITarget().and(Entity::isPickable));
+
+ double distance = 0.0D;
+ EntityHitResult result = null;
+
+ for (Entity entity : entityList) {
+ AABB aabb = entity.getBoundingBox().grow((double) entity.getCollisionBorderSize());
+ Optional<Vec3> rayTraceResult = aabb.calculateIntercept(start, end);
+
+ if (rayTraceResult.isPresent()) {
+ Vec3 rayTrace = rayTraceResult.get();
+ double distanceTo = start.distanceToSqr(rayTrace);
+ if (distanceTo < distance || distance == 0.0D) {
+ result = new EntityHitResult(entity, rayTrace);
+ distance = distanceTo;
+ }
+ }
+ }
+
+ return result;
+ }
+
public int shieldBlockingDelay = level.paperConfig.shieldBlockingDelay;
public int getShieldBlockingDelay() {
diff --git a/src/main/java/net/minecraft/world/phys/AABB.java b/src/main/java/net/minecraft/world/phys/AABB.java
index 983d0495ec35128ca3ef68566ada065bc4b21efc..143a160a7577e9e34d34a9f3b900db03d3f297af 100644
--- a/src/main/java/net/minecraft/world/phys/AABB.java
+++ b/src/main/java/net/minecraft/world/phys/AABB.java
@@ -116,6 +116,7 @@ public class AABB {
return this.expandTowards(scale.x, scale.y, scale.z);
}
+ public final AABB expand(double x, double y, double z) { return expandTowards(x, y, z); } // Paper - OBFHELPER
public AABB expandTowards(double x, double y, double z) {
double d3 = this.minX;
double d4 = this.minY;
@@ -145,6 +146,12 @@ public class AABB {
return new AABB(d3, d4, d5, d6, d7, d8);
}
+ // Paper start
+ public AABB grow(double d0) {
+ return inflate(d0, d0, d0);
+ }
+ // Paper end
+
public AABB inflate(double x, double y, double z) {
double d3 = this.minX - x;
double d4 = this.minY - y;
@@ -204,6 +211,7 @@ public class AABB {
return this.minX < maxX && this.maxX > minX && this.minY < maxY && this.maxY > minY && this.minZ < maxZ && this.maxZ > minZ;
}
+ public final boolean contains(Vec3 vec3d) { return contains(vec3d); } // Paper - OBFHELPER
public boolean contains(Vec3 vec) {
return this.contains(vec.x, vec.y, vec.z);
}
@@ -237,6 +245,7 @@ public class AABB {
return this.inflate(-value);
}
+ public final Optional<Vec3> calculateIntercept(Vec3 vec3d, Vec3 vec3d1) { return clip(vec3d, vec3d1); } // Paper - OBFHELPER
public Optional<Vec3> clip(Vec3 min, Vec3 max) {
double[] adouble = new double[]{1.0D};
double d0 = max.x - min.x;
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index 7e3a215f1592bed9f35e22076d9e35a5a49a430e..a01bd035846df0e2e28dc55e2ef2f5f35b83f905 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.entity;
import com.destroystokyo.paper.block.TargetBlockInfo;
+import com.destroystokyo.paper.entity.TargetEntityInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import java.util.ArrayList;
@@ -30,8 +31,11 @@ import net.minecraft.world.entity.projectile.ThrownEgg;
import net.minecraft.world.entity.projectile.ThrownEnderpearl;
import net.minecraft.world.entity.projectile.ThrownExperienceBottle;
import net.minecraft.world.entity.projectile.ThrownTrident;
+import net.minecraft.world.level.ClipContext;
import net.minecraft.world.phys.BlockHitResult;
+import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
+import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang.Validate;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Location;
@@ -215,6 +219,33 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
new TargetBlockInfo(CraftBlock.at(getHandle().level, ((BlockHitResult)rayTrace).getBlockPos()),
MCUtil.toBukkitBlockFace(((BlockHitResult)rayTrace).getDirection()));
}
+
+ public Entity getTargetEntity(int maxDistance, boolean ignoreBlocks) {
+ EntityHitResult rayTrace = rayTraceEntity(maxDistance, ignoreBlocks);
+ return rayTrace == null ? null : rayTrace.getEntity().getBukkitEntity();
+ }
+
+ public TargetEntityInfo getTargetEntityInfo(int maxDistance, boolean ignoreBlocks) {
+ EntityHitResult rayTrace = rayTraceEntity(maxDistance, ignoreBlocks);
+ return rayTrace == null ? null : new TargetEntityInfo(rayTrace.getEntity().getBukkitEntity(), new org.bukkit.util.Vector(rayTrace.getLocation().x, rayTrace.getLocation().y, rayTrace.getLocation().z));
+ }
+
+ public EntityHitResult rayTraceEntity(int maxDistance, boolean ignoreBlocks) {
+ EntityHitResult rayTrace = getHandle().getTargetEntity(maxDistance);
+ if (rayTrace == null) {
+ return null;
+ }
+ if (!ignoreBlocks) {
+ HitResult rayTraceBlocks = getHandle().getRayTrace(maxDistance, ClipContext.Fluid.NONE);
+ if (rayTraceBlocks != null) {
+ Vec3 eye = getHandle().getEyePosition(1.0F);
+ if (eye.distanceToSqr(rayTraceBlocks.getLocation()) <= eye.distanceToSqr(rayTrace.getLocation())) {
+ return null;
+ }
+ }
+ }
+ return rayTrace;
+ }
// Paper end
@Override