aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0335-Add-LivingEntity-getTargetEntity.patch
blob: 3e94f14b60d4b6b5852c2baad44db65c8f19e28c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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 f0c18a55900774ba67eaf094f175a9e37c16e56c..36e9a33d22fc2572e23e457adf62bd2bcebd10d3 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -1504,6 +1504,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, ne
         return this.c(f - 90.0F, f1);
     }
 
+    public final Vec3D getEyePosition(float partialTicks) { return j(partialTicks); } // Paper - OBFHELPER
     public final Vec3D j(float f) {
         if (f == 1.0F) {
             return new Vec3D(this.locX(), this.getHeadY(), this.locZ());
@@ -2149,6 +2150,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, ne
         return this.getPassengers().size() < 1;
     }
 
+    public final float getCollisionBorderSize() { return bg(); } // Paper - OBFHELPER
     public float bg() {
         return 0.0F;
     }
diff --git a/src/main/java/net/minecraft/world/entity/EntityLiving.java b/src/main/java/net/minecraft/world/entity/EntityLiving.java
index 58270f45de665b5c1cfd9fc548eadb263ad230e9..ecceb272028e1ae1dd40c0c39d6f687fccfabb58 100644
--- a/src/main/java/net/minecraft/world/entity/EntityLiving.java
+++ b/src/main/java/net/minecraft/world/entity/EntityLiving.java
@@ -108,6 +108,7 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParameterSet
 import net.minecraft.world.level.storage.loot.parameters.LootContextParameters;
 import net.minecraft.world.phys.AxisAlignedBB;
 import net.minecraft.world.phys.MovingObjectPosition;
+import net.minecraft.world.phys.MovingObjectPositionEntity;
 import net.minecraft.world.phys.Vec3D;
 import net.minecraft.world.scores.ScoreboardTeam;
 import org.apache.logging.log4j.Logger;
@@ -3637,6 +3638,37 @@ public abstract class EntityLiving extends Entity {
         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.canAITarget().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/world/entity/IEntitySelector.java b/src/main/java/net/minecraft/world/entity/IEntitySelector.java
index 4776a47566aac487dc77fd6b4b9b42b95974e31a..cb5cda5e6497edeb801ef712f9bd8823cb055750 100644
--- a/src/main/java/net/minecraft/world/entity/IEntitySelector.java
+++ b/src/main/java/net/minecraft/world/entity/IEntitySelector.java
@@ -22,6 +22,7 @@ public final class IEntitySelector {
     public static final Predicate<Entity> e = (entity) -> {
         return !(entity instanceof EntityHuman) || !entity.isSpectator() && !((EntityHuman) entity).isCreative();
     };
+    public static Predicate<Entity> canAITarget() { return f; } // Paper - OBFHELPER
     public static final Predicate<Entity> f = (entity) -> {
         return !(entity instanceof EntityHuman) || !entity.isSpectator() && !((EntityHuman) entity).isCreative() && entity.world.getDifficulty() != EnumDifficulty.PEACEFUL;
     };
diff --git a/src/main/java/net/minecraft/world/phys/AxisAlignedBB.java b/src/main/java/net/minecraft/world/phys/AxisAlignedBB.java
index 3941dd33da4b5c09d0087143f1d8a2d76fc18792..62513c812b497bb9d8dafe1d9c2f574059aebf15 100644
--- a/src/main/java/net/minecraft/world/phys/AxisAlignedBB.java
+++ b/src/main/java/net/minecraft/world/phys/AxisAlignedBB.java
@@ -116,6 +116,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;
@@ -145,6 +146,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;
@@ -204,6 +211,7 @@ public class AxisAlignedBB {
         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 d(vec3d); } // Paper - OBFHELPER
     public boolean d(Vec3D vec3d) {
         return this.e(vec3d.x, vec3d.y, vec3d.z);
     }
@@ -237,6 +245,7 @@ public class AxisAlignedBB {
         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) {
         double[] adouble = new double[]{1.0D};
         double d0 = vec3d1.x - vec3d.x;
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index c692626b747008a5418ecabf550fc67e3b676f5b..ff586b8366a6298f1906551b068e8abb26fcabc7 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;
@@ -42,8 +43,11 @@ import net.minecraft.world.entity.projectile.EntityThrownExpBottle;
 import net.minecraft.world.entity.projectile.EntityThrownTrident;
 import net.minecraft.world.entity.projectile.EntityTippedArrow;
 import net.minecraft.world.entity.projectile.EntityWitherSkull;
+import net.minecraft.world.level.RayTrace;
 import net.minecraft.world.phys.MovingObjectPosition;
 import net.minecraft.world.phys.MovingObjectPositionBlock;
+import net.minecraft.world.phys.MovingObjectPositionEntity;
+import net.minecraft.world.phys.Vec3D;
 import org.apache.commons.lang.Validate;
 import org.bukkit.FluidCollisionMode;
 import org.bukkit.Location;
@@ -227,6 +231,33 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
             new TargetBlockInfo(CraftBlock.at(getHandle().world, ((MovingObjectPositionBlock)rayTrace).getBlockPosition()),
                 MCUtil.toBukkitBlockFace(((MovingObjectPositionBlock)rayTrace).getDirection()));
     }
+
+    public Entity getTargetEntity(int maxDistance, boolean ignoreBlocks) {
+        MovingObjectPositionEntity rayTrace = rayTraceEntity(maxDistance, ignoreBlocks);
+        return rayTrace == null ? null : rayTrace.getEntity().getBukkitEntity();
+    }
+
+    public TargetEntityInfo getTargetEntityInfo(int maxDistance, boolean ignoreBlocks) {
+        MovingObjectPositionEntity rayTrace = rayTraceEntity(maxDistance, ignoreBlocks);
+        return rayTrace == null ? null : new TargetEntityInfo(rayTrace.getEntity().getBukkitEntity(), new org.bukkit.util.Vector(rayTrace.getPos().x, rayTrace.getPos().y, rayTrace.getPos().z));
+    }
+
+    public MovingObjectPositionEntity rayTraceEntity(int maxDistance, boolean ignoreBlocks) {
+        MovingObjectPositionEntity rayTrace = getHandle().getTargetEntity(maxDistance);
+        if (rayTrace == null) {
+            return null;
+        }
+        if (!ignoreBlocks) {
+            MovingObjectPosition rayTraceBlocks = getHandle().getRayTrace(maxDistance, RayTrace.FluidCollisionOption.NONE);
+            if (rayTraceBlocks != null) {
+                Vec3D eye = getHandle().getEyePosition(1.0F);
+                if (eye.distanceSquared(rayTraceBlocks.getPos()) <= eye.distanceSquared(rayTrace.getPos())) {
+                    return null;
+                }
+            }
+        }
+        return rayTrace;
+    }
     // Paper end
 
     @Override