From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Tue, 23 Jul 2019 08:28:21 -0500
Subject: [PATCH] Configurable villager brain ticks


diff --git a/src/main/java/net/minecraft/world/entity/npc/Villager.java b/src/main/java/net/minecraft/world/entity/npc/Villager.java
index 682feb6f3c109b0b3034f3942f7dfa73721d359b..1d0263c594a41ef01d33ebfbd31d8dad0de1933f 100644
--- a/src/main/java/net/minecraft/world/entity/npc/Villager.java
+++ b/src/main/java/net/minecraft/world/entity/npc/Villager.java
@@ -141,6 +141,7 @@ public class Villager extends AbstractVillager implements ReputationEventHandler
     }, MemoryModuleType.MEETING_POINT, (entityvillager, villageplacetype) -> {
         return villageplacetype == PoiType.MEETING;
     });
+    private final int brainTickOffset; // Purpur
 
     public long nextGolemPanic = -1; // Airplane
 
@@ -155,6 +156,7 @@ public class Villager extends AbstractVillager implements ReputationEventHandler
         this.getNavigation().setCanFloat(true);
         this.setCanPickUpLoot(true);
         this.setVillagerData(this.getVillagerData().setType(type).setProfession(VillagerProfession.NONE));
+        this.brainTickOffset = getRandom().nextInt(100); // Purpur
     }
 
     // Purpur start
@@ -274,6 +276,10 @@ public class Villager extends AbstractVillager implements ReputationEventHandler
     protected void customServerAiStep() { mobTick(false); }
     protected void mobTick(boolean inactive) {
         this.level.getProfiler().push("villagerBrain");
+        // Purpur start
+        boolean tick = (level.getGameTime() + brainTickOffset) % level.purpurConfig.villagerBrainTicks == 0;
+        if (((ServerLevel) level).getServer().lagging ? tick : level.purpurConfig.villagerUseBrainTicksOnlyWhenLagging || tick)
+        // Purpur end
         // Airplane start
         if (!inactive) {
             if (this.behaviorTick++ % this.activatedPriority == 0) // Airplane
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index 613dd74b460c0bd15fc8fd4e78380a81aef5f800..a1d6a2235e6c8025c8f5516ccc69bae610eb9c72 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -1071,6 +1071,8 @@ public class PurpurWorldConfig {
     public boolean villagerRidable = false;
     public boolean villagerRidableInWater = false;
     public double villagerMaxHealth = 20.0D;
+    public int villagerBrainTicks = 1;
+    public boolean villagerUseBrainTicksOnlyWhenLagging = true;
     private void villagerSettings() {
         villagerRidable = getBoolean("mobs.villager.ridable", villagerRidable);
         villagerRidableInWater = getBoolean("mobs.villager.ridable-in-water", villagerRidableInWater);
@@ -1080,6 +1082,8 @@ public class PurpurWorldConfig {
             set("mobs.villager.attributes.max_health", oldValue);
         }
         villagerMaxHealth = getDouble("mobs.villager.attributes.max_health", villagerMaxHealth);
+        villagerBrainTicks = getInt("mobs.villager.brain-ticks", villagerBrainTicks);
+        villagerUseBrainTicksOnlyWhenLagging = getBoolean("mobs.villager.use-brain-ticks-only-when-lagging", villagerUseBrainTicksOnlyWhenLagging);
     }
 
     public boolean vindicatorRidable = false;
