Description

The bug

I just created a villager in MCEdit with the Create Shops filter to have a custom shop. I saved it and launched MC. The villager started to run around normally in his house but then he went to the door where a zombie was knocking at (wooden door on normal difficulty) and the villager blinked red and got damage and after a few hits he died. The zombie killed him through a closed door and the villager did not run away he always came back to the door until he died.

from KingSupernova:
There are a number of bug reports about attack radius that are all very similar. MC-2310, MC-18326, MC-50668, MC-63965, MC-71834, and MC-74907 are all about the attack radius of mobs extending through blocks. (Some mobs are more bugged than others, but it’s the same basic problem). There are also a few related issues:

MC-1297 is the same as the above, but for players.MC-3059 is the same as the above, but for arrows.

Code analysis

Based on 1.11.2 decompiled using MCP 9.35 rc1

The problem seems to be the method net.minecraft.entity.ai.EntityAIAttackMelee.checkAndPerformAttack(EntityLivingBase, double) and methods overriding it. They all only test if the mob to attack is in a certain radius to the attacker without testing if blocks are between them.

Possible solutions

Bounding box check

The current behavior would be replaced by only allowing mobs to attack other mobs when their bounding boxes intersect.

Ray casting

The current behavior would be extended to require a ray cast from the attacker to the mob to attack (excluding liquids and blocks without collision box) to return no colliding blocks. Possible use y + height / 1.5 as attack height or have a method for mobs to define their attack height(s?). The height at which the mob to attack will be attacked could for example be y + height / 2 or with multiple tries depending on the height of the mob, for example