Paper/Spigot-Server-Patches/0342-Add-LivingEntity-getTargetEntity.patch
Aikar ab347c4c96
Updated Upstream (Bukkit/CraftBukkit/Spigot)
Upstream has released updates that appears to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
42d5a714 SPIGOT-5899: Hoglins API similar to Piglins
2c1ee10e SPIGOT-5887: ClickType doesn't include off hand swaps
5ff7c7ce SPIGOT-5886: Missing BlockData

CraftBukkit Changes:
7560f5f5 SPIGOT-5905: Fix hex colours not being allowed in MOTD
d47c47ee SPIGOT-5889: Villager using composter should call EntityChangeBlockEvent
2fe6b4a3 SPIGOT-5899: Hoglins API similar to Piglins
e09dbeca SPIGOT-5887: ClickType doesn't include off hand swaps
23aac2a5 SPIGOT-5903: EntityDismountEvent cannot be triggered asynchronously
92cbf656 SPIGOT-5884: Tab completions lost on reloadData / minecraft:reload
fb4e54ad SPIGOT-5902: PlayerRespawnEvent places player at spawn before event is called
aa8f3d5a SPIGOT-5901: Structures are generated in all worlds based on the setting for the main world
a0c35937 SPIGOT-5895: PlayerChangedWorldEvent#getFrom is incorrect
89c0a5c3 SPIGOT-5886: Missing BlockData

Spigot Changes:
0287a20d SPIGOT-5903: EntityDismountEvent cannot be triggered asynchronously
2020-06-30 01:20:29 -04:00

160 lines
8.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/server/AxisAlignedBB.java b/src/main/java/net/minecraft/server/AxisAlignedBB.java
index 02c09f39848399a86d46bd17569b4f01a7b5ab1f..ed9b2f9adfecdc6d1b9925579ec510657adde11f 100644
--- a/src/main/java/net/minecraft/server/AxisAlignedBB.java
+++ b/src/main/java/net/minecraft/server/AxisAlignedBB.java
@@ -112,6 +112,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;
@@ -141,6 +142,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;
@@ -200,6 +207,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);
}
@@ -233,6 +241,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/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index 5c37d6f2523ead3eee082dc615d9b0c9fbd71443..c5abac3eb3f98a948ff7a3423cfcad70abae86ff 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -1426,6 +1426,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
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());
@@ -2057,6 +2058,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
return this.getPassengers().size() < 1;
}
+ public final float getCollisionBorderSize() { return bc(); } // Paper - OBFHELPER
public float bc() {
return 0.0F;
}
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
index 84de18a6c207612ec3d3cca61749934b1d421155..eeca12bd7c6e59b872303fda1378a69d03dcf783 100644
--- a/src/main/java/net/minecraft/server/EntityLiving.java
+++ b/src/main/java/net/minecraft/server/EntityLiving.java
@@ -3495,6 +3495,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.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
index d7e3580067bcbca09b66f05f02e8736863bd7ef5..28f10ab2e427872c04bc97ebc392cf6d58854cf9 100644
--- 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) -> {
return !(entity instanceof EntityHuman) || !entity.isSpectator() && !((EntityHuman) entity).isCreative();
};
+ public static Predicate<Entity> notSpectator() { 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/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index 2e7bacff55f57fda8b5644fb9d7934a615e5f550..39b8ab223bc708e5ee3dc967e489fdc76d944416 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -219,6 +219,33 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
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