Mojira Archive
MC-138963

You can trample farmland in adventure mode

The Bug:

You can trample farmland in adventure mode.

Steps to Reproduce:

  1. Place down some water and farmland close to one another.
  2. Switch into adventure mode and jump on top of the farmland several times.
  3. Take note as to whether or not you can trample farmland in adventure mode.

Observed Behavior:

You can trample farmland in adventure mode.

Expected Behavior:

You would not be able to trample farmland in adventure mode.

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.

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 is only one necessary check that's carried out before allowing players to trample farmland. This check is to quite simply see if the entity falling on the farmland is a player. If it is, the farmland can be trampled if the other requirements within the "if" statement are met. The game doesn't check to see what abilities the player possesses (what game mode they are in) 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 what abilities the player possesses 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 Player player && player.getAbilities().mayBuild) || level.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) 
        && entity.getBbWidth() * entity.getBbWidth() * entity.getBbHeight() > 0.512F) {