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

Villagers can pathfind and move towards items while sleeping in a bed

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • 1.14.4, 1.15, 1.15.2, 20w09a, 20w13b, 20w15a, 20w16a, 20w17a, 20w18a, 20w19a, 20w22a, 1.16.1, 1.16.4, 20w51a, 1.16.5, 21w13a, 21w18a, 1.17, 1.17.1, 21w41a, 1.18.1, 1.18.2, 1.19, 1.19.2, 1.19.3, 1.19.4, 23w18a, 1.20.1, 1.20.2 Pre-release 2, 1.20.2, 23w40a, 23w42a, 23w46a, 1.20.3, 1.20.4 Release Candidate 1, 1.20.4, 24w10a, 1.20.6, 24w19b, 1.21 Release Candidate 1, 1.21, 24w38a
    • Confirmed
    • Mob behaviour
    • Normal
    • Gameplay

      When a villager detects a desired item while sleeping (such as carrots, bread, or beetroot), it will move towards that item before entirely waking up. This causes an incorrect visual for the player where the villager appears to still be asleep (laying down horizontally) while it pathfinds towards any applicable item(s).

      Steps to Reproduce:

      1. Place down one or more beds
      2. Spawn in one or more villagers
      3. Drop an item on the ground that entice villagers
        /give @p minecraft:carrot 1
        

      Observed Behavior:

      The villager will move across the ground as if it is still asleep to pick up items.

      Expected Behavior:

      The villager should first wake up entirely (standing upright) before walking to the dropped item, or not detect items while asleep in the first place.

      Video:

      2019-07-19 22-58-59.mp4

      Code Analysis:

      Here in the create() method of the GoToWantedItem class (which handles a villagers ai behavior to pathfind to desired items) it checks the following conditions for the villager and/or the desired item:

      1. The item pickup cooldown is empty
      2. It can start to pickup the item
      3. The desired item is close enough to the villager
      4. The villager and desired item are both within the level's world border

      However, it does not check for whether the villager is asleep. The villager waking up is only ever executed from create() in WakeUp.

         public static <E extends LivingEntity> BehaviorControl<E> create(Predicate<E> startCondition, float speed, boolean requiresWalkTarget, int radius) {
            return BehaviorBuilder.create((context) -> {
               BehaviorBuilder<E, ? extends MemoryAccessor<? extends K1, WalkTarget>> behaviorbuilder = requiresWalkTarget ? context.registered(MemoryModuleType.WALK_TARGET) : context.absent(MemoryModuleType.WALK_TARGET);
               return context.group(context.registered(MemoryModuleType.LOOK_TARGET), behaviorbuilder, context.present(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM), context.registered(MemoryModuleType.ITEM_PICKUP_COOLDOWN_TICKS)).apply(context, (lookTarget, walkTarget, nearestVisibleWantedItem, itemPickupCooldownTicks) -> {
                  return (level, entity, time) -> {
                     ItemEntity itementity = context.get(nearestVisibleWantedItem);
                     if (
                             context.tryGet(itemPickupCooldownTicks).isEmpty()
                                     && startCondition.test(entity)
                                     && itementity.closerThan(entity, (double)radius)
                                     && entity.level().getWorldBorder().isWithinBounds(itementity.blockPosition())
                     ) {
                        WalkTarget walktarget = new WalkTarget(new EntityTracker(itementity, false), speed, 0);
                        lookTarget.set(new EntityTracker(itementity, true));
                        walkTarget.set(walktarget);
                        return true;
                     } else {
                        return false;
                     }
                  };
               });
            });
         }
      }
      

      Two Possible Solutions:

      1. If the villager should not be able to pathfind/collect the desired item until it has been woken up via the time turning to day, or being manually woken by a player input on it's bed, a check could be added for whether the villager is currently sleeping. If it is, do not try and pathfind. Like so:
        if (
                context.tryGet(itemPickupCooldownTicks).isEmpty()
                        && startCondition.test(entity)
                        && itementity.closerThan(entity, (double)radius)
                        && entity.level().getWorldBorder().isWithinBounds(itementity.blockPosition())
        //Fix
                        && !entity.isSleeping()
        //Fix end
        )
        
      2. If the villager should still be able to wake up as it does currently while sleeping, the villager should first properly and entirely wake up before pathfinding to collect the item. This could be done by adding a check to see if the villager is sleeping. If it is, wake up the villager first before executing the rest of the code.
        //Fix
        if (entity.isSleeping()) {
           entity.stopSleeping();
        }
        //Fix end
        
        WalkTarget walktarget = new WalkTarget(new EntityTracker(itementity, false), speed, 0);
        lookTarget.set(new EntityTracker(itementity, true));
        walkTarget.set(walktarget);
        return true;
        

      This is how it looks compared with the new behavior in suggestion 2:
      2023-07-19_20-02-36.mp4

        1. Preview Villager Bug (1).mp4
          5.20 MB
        2. 2019-07-19 22-58-59.mp4
          9.54 MB
        3. image-2023-07-19-15-37-10-660.png
          image-2023-07-19-15-37-10-660.png
          681 kB
        4. 2023-07-19_20-02-36.mp4
          1.17 MB
        5. VillagerBread.mov
          2.79 MB

            Unassigned Unassigned
            Jingy [Helper] Jiingy
            Votes:
            38 Vote for this issue
            Watchers:
            19 Start watching this issue

              Created:
              Updated:
              CHK: