2021-06-13 08:48:25 +02:00
|
|
|
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/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
2023-12-05 18:20:55 +01:00
|
|
|
index 3c7dd373c557f3ceef0173e415d3d6c310fa9e95..5a638da349424a9d3bc5df87d81ba268b8052568 100644
|
2021-06-13 08:48:25 +02:00
|
|
|
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
2023-07-04 10:22:56 +02:00
|
|
|
@@ -116,6 +116,7 @@ import net.minecraft.world.level.storage.loot.LootTable;
|
2021-06-13 08:48:25 +02:00
|
|
|
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;
|
2023-10-29 00:50:26 +02:00
|
|
|
@@ -3889,6 +3890,38 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2023-06-07 23:35:19 +02:00
|
|
|
return this.level().clip(raytrace);
|
2021-06-13 08:48:25 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 18:10:03 +01:00
|
|
|
+ public @Nullable EntityHitResult getTargetEntity(int maxDistance) {
|
2021-06-13 08:48:25 +02:00
|
|
|
+ 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);
|
|
|
|
+
|
2023-06-07 23:35:19 +02:00
|
|
|
+ List<Entity> entityList = this.level().getEntities(this, getBoundingBox().expandTowards(direction.x * maxDistance, direction.y * maxDistance, direction.z * maxDistance).inflate(1.0D, 1.0D, 1.0D), EntitySelector.NO_SPECTATORS.and(Entity::isPickable));
|
2021-06-13 08:48:25 +02:00
|
|
|
+
|
|
|
|
+ double distance = 0.0D;
|
|
|
|
+ EntityHitResult result = null;
|
|
|
|
+
|
|
|
|
+ for (Entity entity : entityList) {
|
2021-06-17 23:39:36 +02:00
|
|
|
+ final double inflationAmount = (double) entity.getPickRadius();
|
2021-06-13 08:48:25 +02:00
|
|
|
+ AABB aabb = entity.getBoundingBox().inflate(inflationAmount, inflationAmount, inflationAmount);
|
|
|
|
+ Optional<Vec3> rayTraceResult = aabb.clip(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;
|
|
|
|
+ }
|
|
|
|
+
|
2023-06-07 23:35:19 +02:00
|
|
|
public int shieldBlockingDelay = this.level().paperConfig().misc.shieldBlockingDelay;
|
2021-06-13 08:48:25 +02:00
|
|
|
|
|
|
|
public int getShieldBlockingDelay() {
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
2023-12-05 18:20:55 +01:00
|
|
|
index 540422f3148a92a25de8398c8616de307c85d0ae..9e15c5e2adfb46ffa3548b3a2eda225de4c44fb5 100644
|
2021-06-13 08:48:25 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
|
|
|
@@ -1,5 +1,6 @@
|
|
|
|
package org.bukkit.craftbukkit.entity;
|
|
|
|
|
|
|
|
+import com.destroystokyo.paper.entity.TargetEntityInfo;
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
import com.google.common.collect.Sets;
|
|
|
|
import java.util.ArrayList;
|
2023-07-04 10:22:56 +02:00
|
|
|
@@ -226,6 +227,39 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
|
2022-12-15 18:10:03 +01:00
|
|
|
}
|
|
|
|
return null;
|
2021-06-13 08:48:25 +02:00
|
|
|
}
|
|
|
|
+
|
|
|
|
+ public Entity getTargetEntity(int maxDistance, boolean ignoreBlocks) {
|
|
|
|
+ net.minecraft.world.phys.EntityHitResult rayTrace = rayTraceEntity(maxDistance, ignoreBlocks);
|
|
|
|
+ return rayTrace == null ? null : rayTrace.getEntity().getBukkitEntity();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TargetEntityInfo getTargetEntityInfo(int maxDistance, boolean ignoreBlocks) {
|
|
|
|
+ net.minecraft.world.phys.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));
|
|
|
|
+ }
|
|
|
|
+
|
2022-12-15 18:10:03 +01:00
|
|
|
+ @Override
|
|
|
|
+ public RayTraceResult rayTraceEntities(int maxDistance, boolean ignoreBlocks) {
|
|
|
|
+ net.minecraft.world.phys.EntityHitResult rayTrace = this.rayTraceEntity(maxDistance, ignoreBlocks);
|
|
|
|
+ return rayTrace == null ? null : new org.bukkit.util.RayTraceResult(org.bukkit.craftbukkit.util.CraftVector.toBukkit(rayTrace.getLocation()), rayTrace.getEntity().getBukkitEntity());
|
|
|
|
+ }
|
|
|
|
+
|
2021-06-13 08:48:25 +02:00
|
|
|
+ public net.minecraft.world.phys.EntityHitResult rayTraceEntity(int maxDistance, boolean ignoreBlocks) {
|
|
|
|
+ net.minecraft.world.phys.EntityHitResult rayTrace = getHandle().getTargetEntity(maxDistance);
|
|
|
|
+ if (rayTrace == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ if (!ignoreBlocks) {
|
|
|
|
+ net.minecraft.world.phys.HitResult rayTraceBlocks = getHandle().getRayTrace(maxDistance, net.minecraft.world.level.ClipContext.Fluid.NONE);
|
|
|
|
+ if (rayTraceBlocks != null) {
|
|
|
|
+ net.minecraft.world.phys.Vec3 eye = getHandle().getEyePosition(1.0F);
|
|
|
|
+ if (eye.distanceToSqr(rayTraceBlocks.getLocation()) <= eye.distanceToSqr(rayTrace.getLocation())) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return rayTrace;
|
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
|
|
|
|
@Override
|