Villagers can trample farmland
The Bug:
Villagers can trample farmland.
This issue can commonly be seen when farmer villagers wander and jump around their workstations.
Steps to Reproduce:
- Build the setup as shown in the attachment below. setup.png

- Summon a villager and wait for it to claim the composter as its workstation.
- Give the villager some carrots so it can begin working.
- Observe the behavior of the villager for around a minute and watch closely as it wanders and jumps around its workstation.
- Take note as to whether or not villagers can trample farmland.
Observed Behavior:
Villagers can trample farmland.
Expected Behavior:
Villagers would not be able to trample farmland.
Code Analysis:
Code analysis by [Mod] Avoma can be found below.
The following is based on a decompiled version of Minecraft 1.19.2 using MCP-Reborn.
public class FarmBlock extends Block { ... public void fallOn(Level level, BlockState blockState, BlockPos blockPos, Entity entity, float f) { if (!level.isClientSide && level.random.nextFloat() < f - 0.5F && entity instanceof LivingEntity && (entity instanceof Player || level.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) && entity.getBbWidth() * entity.getBbWidth() * entity.getBbHeight() > 0.512F) { turnToDirt(blockState, level, blockPos); } super.fallOn(level, blockState, blockPos, entity, f); } ...
If we look at the above class, we can see that there are only two necessary checks that are carried out before allowing villagers to trample farmland. One of these checks is to quite simply see if the entity falling on the farmland is a living entity and if it is, the other check is then to see if "mobGriefing" gamerule is set to "true". If these two requirements are met along with the other criteria within the "if" statement, the farmland can be trampled. The game doesn't check to see if the living entity is a villager before allowing them to trample farmland, therefore resulting in this problem occurring.
Fix:
Simply altering the existing "if" statement within this piece of code to check if the living entity is a villager before allowing them to trample farmland will resolve this problem.
if (!level.isClientSide && level.random.nextFloat() < f - 0.5F && entity instanceof LivingEntity && (entity instanceof Player || level.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) && entity.getBbWidth() * entity.getBbWidth() * entity.getBbHeight() > 0.512F)
if (!level.isClientSide && level.random.nextFloat() < f - 0.5F && (entity instanceof LivingEntity && !(entity instanceof Villager)) && (entity instanceof Player || level.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) && entity.getBbWidth() * entity.getBbWidth() * entity.getBbHeight() > 0.512F)
2019-04-18, 04:26 PM
2024-12-07, 09:48 PM
50
25
Minecraft 1.14 Pre-Release 5 - 1.21.4
Minecraft 1.14 Pre-Release 5, Minecraft 1.14, Minecraft 1.14.1 Pre-Release 1, Minecraft 1.14.1 Pre-Release 2, Minecraft 1.14.2, Minecraft 1.14.3 Pre-Release 3, Minecraft 1.14.3 Pre-Release 4, Minecraft 1.14.3, 1.14.4, 19w44a, 1.15.1, 1.15.2, 20w13b, 20w14a, 20w16a, 20w19a, 20w22a, 1.16 Release Candidate 1, 1.16, 1.16.2, 1.16.3, 1.17, 1.17.1, 1.18.1, 1.18.2 Pre-release 1, 1.18.2, 1.19, 1.19.2, 22w43a, 1.19.3, 23w03a, 1.19.4, 1.20, 1.20.1, 1.20.4, 23w51b, 1.20.6, 1.21, 1.21.3, 1.21.4
-