mojira.dev
MC-222492

You can break turtle eggs by standing on them in adventure mode

The Bug:

You can break turtle eggs by standing on them in adventure mode.

Steps to Reproduce:

  1. Place down some turtle eggs.

  2. Switch into adventure mode and stand on the turtle eggs.

  3. Take note as to whether or not you can break turtle eggs by standing on them in adventure mode.

Observed Behavior:

You can break turtle eggs by standing on them.

Expected Behavior:

You would not be able to break turtle eggs by standing on them.

Code Analysis:

Code analysis by @unknown can be found below.

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

net.minecraft.world.level.block.TurtleEggBlock.java

public class TurtleEggBlock extends Block {
   ...
   private boolean canDestroyEgg(Level $l, Entity $e) {
      if (!($e instanceof Turtle) && !($e instanceof Bat)) {
         if (!($e instanceof LivingEntity)) {
            return false;
         } else {
            return $e instanceof Player || $l.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING);
         }
      } else {
         return false;
      }
   }
   ...

If we look at the above class, we can see that no checks are carried out to see what abilities the player possesses before allowing them to break turtle eggs by standing on them. The only relevant check that is in place is to see if the entity standing on the turtle egg is a player, and if it is, the turtle eggs can be trampled. This is evident through the following line of code:

$e instanceof Player

Since the presence of a player and not what abilities they possess is the only relevant requirement for a turtle egg to be trampled, we can therefore safely assume that players in adventure mode can break turtle eggs by standing on them.

Potential Fix:

Simply altering the existing "if" statement within this piece of code to check what abilities the player possesses before allowing them to break turtle eggs by standing on them, should resolve this problem. The following line of code could be used in order to fix this:

!$PLAYER.getAbilities().mayBuild

The fixed and correct piece of code within its class should look something like the following:

net.minecraft.world.level.block.TurtleEggBlock.java

public class TurtleEggBlock extends Block {
   ...
   private boolean canDestroyEgg(Level $l, Entity $e) {
      if (!($e instanceof Turtle) && !($e instanceof Bat)) {
         if (!($e instanceof LivingEntity)) {
            return false;
         } else {
            return $e instanceof Player && !((Player)$e).getAbilities().mayBuild || $l.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING);
         }
      } else {
         return false;
      }
   }
   ...

Attachments

Comments 1

Avoma

(Unassigned)

Confirmed

Block states, Player

turtle_egg

1.16.5, 21w13a, 21w14a, 21w15a, 21w16a, ..., 1.21, 1.21.3, 1.21.4, 1.21.5, 1.21.7

Retrieved