Skip to content

Commit

Permalink
Fixed NearbyEntityTracker not checking predicate and box criteria whe…
Browse files Browse the repository at this point in the history
…n getting closest entity. (#31)
  • Loading branch information
2No2Name committed Apr 13, 2020
1 parent 86ce3aa commit 2d6104b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.jellysquid.mods.lithium.common.entity.tracker.nearby;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.TargetPredicate;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper;

import java.util.HashSet;
Expand Down Expand Up @@ -51,7 +53,15 @@ public void onEntityLeftRange(LivingEntity entity) {
this.nearby.remove((T) entity);
}

public T getClosestEntity() {
/**
* Gets the closest T (extends LivingEntity) to the center of this tracker that also intersects with the given box and meets the
* requirements of the targetPredicate.
* The result may be different from vanilla if there are multiple closest entities.
* @param box the box the entities have to intersect
* @param targetPredicate predicate the entity has to meet
* @return the closest Entity that meets all requirements (distance, box intersection, predicate, type T)
*/
public T getClosestEntity(Box box, TargetPredicate targetPredicate) {
double x = this.self.getX();
double y = this.self.getY();
double z = this.self.getZ();
Expand All @@ -62,7 +72,7 @@ public T getClosestEntity() {
for (T entity : this.nearby) {
double distance = entity.squaredDistanceTo(x, y, z);

if (distance < nearestDistance) {
if (distance < nearestDistance && (box == null || box.intersects(entity.getBoundingBox())) && targetPredicate.test(this.self, entity)) {
nearest = entity;
nearestDistance = distance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ private void init(MobEntityWithAi mob, Class<T> fleeFromType, Predicate<LivingEn

@Redirect(method = "canStart", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getClosestEntityIncludingUngeneratedChunks(Ljava/lang/Class;Lnet/minecraft/entity/ai/TargetPredicate;Lnet/minecraft/entity/LivingEntity;DDDLnet/minecraft/util/math/Box;)Lnet/minecraft/entity/LivingEntity;"))
private T redirectGetNearestEntity(World world, Class<? extends T> entityClass, TargetPredicate targetPredicate, LivingEntity entity, double x, double y, double z, Box box) {
return this.tracker.getClosestEntity();
return this.tracker.getClosestEntity(box, targetPredicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ private void init(MobEntity mob, Class<? extends LivingEntity> targetType, float

@Redirect(method = "canStart", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getClosestEntityIncludingUngeneratedChunks(Ljava/lang/Class;Lnet/minecraft/entity/ai/TargetPredicate;Lnet/minecraft/entity/LivingEntity;DDDLnet/minecraft/util/math/Box;)Lnet/minecraft/entity/LivingEntity;"))
private <T extends LivingEntity> LivingEntity redirectGetClosestEntity(World world, Class<? extends T> entityClass, TargetPredicate targetPredicate, LivingEntity entity, double x, double y, double z, Box box) {
return this.tracker.getClosestEntity();
return this.tracker.getClosestEntity(box, targetPredicate);
}

@Redirect(method = "canStart", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getClosestPlayer(Lnet/minecraft/entity/ai/TargetPredicate;Lnet/minecraft/entity/LivingEntity;DDD)Lnet/minecraft/entity/player/PlayerEntity;"))
private PlayerEntity redirectGetClosestPlayer(World world, TargetPredicate targetPredicate, LivingEntity entity, double x, double y, double z) {
return (PlayerEntity) this.tracker.getClosestEntity();
return (PlayerEntity) this.tracker.getClosestEntity(null, targetPredicate);
}
}

0 comments on commit 2d6104b

Please sign in to comment.