fix: Prevent mobs from targeting players in creative mode

Mobs were incorrectly targeting and attacking players who are in
creative mode (invulnerable).
This commit is contained in:
George V. 2026-04-18 20:29:47 +03:00
parent 7ee7588364
commit eabde3527c
No known key found for this signature in database
GPG key ID: 1DB61094F2DD4982
3 changed files with 24 additions and 1 deletions

View file

@ -2,6 +2,7 @@
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.h"
#include "LeapAtTargetGoal.h"
#include "Player.h"
LeapAtTargetGoal::LeapAtTargetGoal(Mob *mob, float yd)
{
@ -25,7 +26,16 @@ bool LeapAtTargetGoal::canUse()
bool LeapAtTargetGoal::canContinueToUse()
{
return target.lock() != nullptr && !mob->onGround;
shared_ptr<LivingEntity> mobTarget = target.lock();
if (mobTarget == nullptr) return false;
if (!mobTarget->isAlive()) return false;
if (mobTarget->instanceof(eTYPE_PLAYER)) {
shared_ptr<Player> player = dynamic_pointer_cast<Player>(mobTarget);
if (player->abilities.invulnerable) return false;
}
return !mob->onGround;
}
void LeapAtTargetGoal::start()

View file

@ -63,6 +63,14 @@ void PathfinderMob::serverAiStep()
{
if (attackTarget->isAlive())
{
if (attackTarget->instanceof(eTYPE_PLAYER)) {
shared_ptr<Player> player = dynamic_pointer_cast<Player>(attackTarget);
if (player->abilities.invulnerable)
{
attackTarget = nullptr;
return;
}
}
float d = attackTarget->distanceTo(shared_from_this());
if (canSee(attackTarget))
{

View file

@ -37,6 +37,11 @@ bool TargetGoal::canContinueToUse()
if (target == nullptr) return false;
if (!target->isAlive()) return false;
if (target->instanceof(eTYPE_PLAYER)) {
shared_ptr<Player> player = dynamic_pointer_cast<Player>(target);
if (player->abilities.invulnerable) return false;
}
double within = getFollowDistance();
if (mob->distanceToSqr(target) > within * within) return false;
if (mustSee)