mojira.dev
MC-245686

Villagers can't restock trades when doDaylightCycle is false

The Bug

I'm playing on my own server and have the gamerule doDaylightCycle set to false. The Villagers I'm trading with will no longer refresh their trades after a certain amount of trading has been done. Only activating the daylight cycle again and waiting a bit seems to fix the issue.

Reproduce

  • Spawn a villager and give it a profession

  • Do /gamerule doDaylightCycle false

  • Trade with the villager multiple times (it should restock a few times so keep trading until it stops restocking)

Observed Result

The villager stopped restocking

Expected Result

The villager would continue to restock when doDaylightCycle is false since it can't sleep to restock

Code Analysis and Fix

Code Analysis provided by @unknown
Simply running a check of the doDaylightCycle gamerule in the shouldRestock boolean method in the Villager class fixes this issue.

Current Code

net/minecraft/world/entity/npc/Villager.java

public boolean shouldRestock() {
      long i = this.lastRestockGameTime + 12000L;
      long j = this.level.getGameTime();
      boolean flag = j > i;
      long k = this.level.getDayTime();
      if (this.lastRestockCheckDayTime > 0L) {
         long l = this.lastRestockCheckDayTime / 24000L;
         long i1 = k / 24000L;
         flag |= i1 > l;
      }

      this.lastRestockCheckDayTime = k;
      //Check for gamerule here to fix MC-245686
      if (flag) {
         this.lastRestockGameTime = j;
         this.resetNumberOfRestocks();
      }

      return this.allowedToRestock() && this.needsToRestock();
   }

Fixed Code

net/minecraft/world/entity/npc/Villager.java

public boolean shouldRestock() {
      long i = this.lastRestockGameTime + 12000L;
      long j = this.level.getGameTime();
      boolean flag = j > i;
      long k = this.level.getDayTime();
      if (this.lastRestockCheckDayTime > 0L) {
         long l = this.lastRestockCheckDayTime / 24000L;
         long i1 = k / 24000L;
         flag |= i1 > l;
      }

      this.lastRestockCheckDayTime = k;
      //Check for gamerule here to fix MC-245686
      if (flag || !level.getGameRules().getBoolean(GameRules.RULE_DAYLIGHT)) {
         this.lastRestockGameTime = j;
         this.resetNumberOfRestocks();
      }

      return this.allowedToRestock() && this.needsToRestock();
   }

Comments 0

No comments.

Alex D

(Unassigned)

Confirmed

Gameplay

Normal

Village system

1.18.1, 1.19.2

Retrieved