Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-148671

Villagers can trample farmland

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • 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
    • Confirmed
    • Mob behaviour, Village system
    • Normal
    • Gameplay

      The Bug:

      Villagers can trample farmland.

      This issue can commonly be seen when farmer villagers wander and jump around their workstations.

      Steps to Reproduce:

      1. Build the setup as shown in the attachment below. setup.png
      2. Summon a villager and wait for it to claim the composter as its workstation.
      3. Give the villager some carrots so it can begin working.
      4. Observe the behavior of the villager for around a minute and watch closely as it wanders and jumps around its workstation.
      5. 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 Avoma can be found below.

      The following is based on a decompiled version of Minecraft 1.19.2 using MCP-Reborn.

      net.minecraft.world.level.block.FarmBlock.java
      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.

      Current "if" statement:
      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)
      Fixed "if" statement:
      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)

        1. Villager Jumping Next To Water Irrigation.mp4
          5.06 MB
        2. setup.png
          setup.png
          326 kB
        3. MC-148671 - Fixed Code.png
          MC-148671 - Fixed Code.png
          39 kB
        4. MC-148671 - Current Code.png
          MC-148671 - Current Code.png
          38 kB
        5. MC-148671.png
          MC-148671.png
          1.58 MB

            Unassigned Unassigned
            Avoma [Mod] Avoma
            Votes:
            50 Vote for this issue
            Watchers:
            25 Start watching this issue

              Created:
              Updated:
              CHK: