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

Goat eating sounds aren't played when feeding them the last item of wheat within a stack

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • 24w33a
    • 1.18, 1.18.1, 22w03a, 22w05a, 22w06a, 1.18.2 Pre-release 1, 1.18.2, 22w15a, 22w17a, 22w18a, 1.19, 1.19.1, 1.19.2, 1.19.3, 23w07a, 1.19.4, 1.20.1, 1.20.2, 1.21
    • Confirmed
    • Items, Mob behaviour, Sound
    • Low
    • Gameplay

      The Bug:

      Goat eating sounds aren't played when feeding them the last item of wheat within a stack.

      Steps to Reproduce:

      1. Obtain two pieces of what and ensure that they occupy the same hotbar slot.
      2. Summon two goats and switch into survival mode.
      3. Feed one of the goats a piece of wheat and take note of how goat eating sounds are played.
      4. Feed the other goat the remaining piece of wheat and listen closely as you do this.
      5. Take note as to whether or not goat eating sounds are played when feeding them the last item of wheat within a stack.

      Observed Behavior:

      Goat eating sounds aren't played.

      Expected Behavior:

      Goat eating sounds would be played.

      Code Analysis:

      Code analysis by Apollo30 can be found below.

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

      net.minecraft.world.entity.animal.Goat.java
      public class Goat extends Animal {
         ...
         public InteractionResult mobInteract(Player player, InteractionHand interactionHand) {
            ItemStack itemstack = player.getItemInHand(interactionHand);
            if (itemstack.is(Items.BUCKET) && !this.isBaby()) {
               player.playSound(this.getMilkingSound(), 1.0F, 1.0F);
               ItemStack itemstack1 = ItemUtils.createFilledResult(itemstack, player, Items.MILK_BUCKET.getDefaultInstance());
               player.setItemInHand(interactionHand, itemstack1);
               return InteractionResult.sidedSuccess(this.level().isClientSide);
            } else {
               InteractionResult interactionresult = super.mobInteract(player, interactionHand);
               if (interactionresult.consumesAction() && this.isFood(itemstack)) {
                  this.level().playSound((Player)null, this, this.getEatingSound(itemstack), SoundSource.NEUTRAL, 1.0F, Mth.randomBetween(this.level().random, 0.8F, 1.2F));
               }
               return interactionresult;
            }
         }
         ...

      In the Goat.java class there is the Goat#mobInteract method. We see two important methods; super#mobInteract which is a method of a superclass called Animal. This method handles the decrementing of the held itemstack's amount. 

      The problem with the line after Goat#mobInteract is that we check if the itemstack is food via the method this#isFood, but since the player has one of that itemstack, the method removes that last amount, and then checks if the itemstack is food. Then it would return false as the itemstack is now AIR. Therefore the eating sound doesn't emit globally. 

      Fix:

      To fix this, we can temporarily store a variable called flag which saves the itemstack's value of being food or not in a true or false matter. Then we can handle the mob interaction using super#mobInteract and then use the stored variable, flag, in our "if" statement.

      Current "else" statement:
      } else {
         InteractionResult interactionresult = super.mobInteract(player, interactionHand);
         if (interactionresult.consumesAction() && this.isFood(itemstack)) {
            this.level().playSound((Player)null, this, this.getEatingSound(itemstack), SoundSource.NEUTRAL, 1.0F, Mth.randomBetween(this.level().random, 0.8F, 1.2F));
         }
         return interactionresult;
      }
      Fixed "else" statement:
      } else {
         boolean flag = this.isFood(itemstack);
         InteractionResult interactionresult = super.mobInteract(p_149379_, p_149380_);
         if (interactionresult.consumesAction() && flag) {
            this.level().playSound((Player)null, this, this.getEatingSound(itemstack), SoundSource.NEUTRAL, 1.0F, Mth.randomBetween(this.level().random, 0.8F, 1.2F));
         }
         return interactionresult;
      }

        1. MC-244739.mp4
          6.63 MB
        2. MC-244739.png
          MC-244739.png
          1.36 MB

            Unassigned Unassigned
            Avoma [Mod] Avoma
            Votes:
            8 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              CHK: