Skip to content

Commit

Permalink
Villager Optimization
Browse files Browse the repository at this point in the history
Villager Optimization - MrMasrozYTLIVE
  • Loading branch information
Dueris authored Jan 10, 2025
2 parents 74366f4 + 5e4d9ef commit cf2b531
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions patches/server/0030-Villager-Optimization.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrMasrozYTLIVE <[email protected]>
Date: Fri, 10 Jan 2025 23:36:57 +0300
Subject: [PATCH] Villager Optimization


diff --git a/src/main/java/net/minecraft/world/entity/ai/Brain.java b/src/main/java/net/minecraft/world/entity/ai/Brain.java
index d449e48a8310994eed3ff7169e839f34b4a10f01..f54ddb3b9336022b3daf704f42016f5f6f141f04 100644
--- a/src/main/java/net/minecraft/world/entity/ai/Brain.java
+++ b/src/main/java/net/minecraft/world/entity/ai/Brain.java
@@ -232,6 +232,7 @@ public class Brain<E extends LivingEntity> {
}

public <U> void eraseMemory(MemoryModuleType<U> type) {
+ if (!this.memories.containsKey(type)) return; // Canvas - don't do anything if memories doesn't contain key we want to erase
this.setMemory(type, Optional.empty());
}

@@ -247,15 +248,18 @@ public class Brain<E extends LivingEntity> {
this.setMemoryInternal(type, value.map(ExpirableValue::of));
}

+ // Canvas start - optimize memory erasing
<U> void setMemoryInternal(MemoryModuleType<U> type, Optional<? extends ExpirableValue<?>> memory) {
+ if (memory.isPresent() && this.isEmptyCollection(memory.get().getValue())) {
+ this.eraseMemory(type);
+ return;
+ }
+
if (this.memories.containsKey(type)) {
- if (memory.isPresent() && this.isEmptyCollection(memory.get().getValue())) {
- this.eraseMemory(type);
- } else {
- this.increaseMemoryModificationCount(this.memories, type, memory); // Canvas
- }
+ this.increaseMemoryModificationCount(this.memories, type, memory); // Canvas
}
}
+ // Canvas end
// Canvas start - optimize brain

private long memoryModCount = 1;
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/PoiCompetitorScan.java b/src/main/java/net/minecraft/world/entity/ai/behavior/PoiCompetitorScan.java
index 7302d397d39d8400527ab2da4adaf8d792256749..8e09752349c8eb72593753c381c43d5ae415b220 100644
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/PoiCompetitorScan.java
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/PoiCompetitorScan.java
@@ -22,13 +22,19 @@ public class PoiCompetitorScan {
world.getPoiManager()
.getType(globalPos.pos())
.ifPresent(
- poiType -> context.<List<LivingEntity>>get(mobs)
- .stream()
- .filter(mob -> mob instanceof Villager && mob != entity)
- .map(villagerx -> (Villager)villagerx)
- .filter(LivingEntity::isAlive)
- .filter(villagerx -> competesForSameJobsite(globalPos, poiType, villagerx))
- .reduce(entity, PoiCompetitorScan::selectWinner)
+ poiType -> {
+ // Canvas start - Replace stream with for loop
+ Villager winner = entity;
+ for(LivingEntity mob : context.get(mobs)) {
+ if(!(mob instanceof Villager villagerx) || mob == entity) continue;
+ if(!villagerx.isAlive()) continue;
+ if(!competesForSameJobsite(globalPos, poiType, villagerx)) continue;
+
+ winner = selectWinner(winner, villagerx);
+ }
+ // Canvas end
+
+ }
);
return true;
}

0 comments on commit cf2b531

Please sign in to comment.