Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #29 Lithium breaks stray cat behavior. #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}