Skip to content

Commit

Permalink
Cleanup Remove stream in Brain
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Oct 26, 2024
1 parent 3dcdc3a commit e8bc348
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
10 changes: 5 additions & 5 deletions patches/server/0090-Remove-stream-in-trial-spawner-ticking.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Remove stream in trial spawner ticking


diff --git a/src/main/java/net/minecraft/world/level/block/entity/trialspawner/TrialSpawnerState.java b/src/main/java/net/minecraft/world/level/block/entity/trialspawner/TrialSpawnerState.java
index 005aea8a747e9cbbc352b8b57c64b84ec71ad321..a66e0b303aac2952c06f1ec704df0edc27d85409 100644
index 005aea8a747e9cbbc352b8b57c64b84ec71ad321..f687af7374e62ed0d004e12283fbc9008cf15e28 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/trialspawner/TrialSpawnerState.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/trialspawner/TrialSpawnerState.java
@@ -173,17 +173,22 @@ public enum TrialSpawnerState implements StringRepresentable {
Expand All @@ -23,7 +23,7 @@ index 005aea8a747e9cbbc352b8b57c64b84ec71ad321..a66e0b303aac2952c06f1ec704df0edc
- && player.distanceToSqr(pos.getCenter()) <= (double)Mth.square(logic.getRequiredPlayerRange())
- )
- .toList();
+ // Leaf start - Optimize trial spawner ticking
+ // Leaf start - Remove stream in trial spawner ticking
+ List<Player> list = new java.util.ArrayList<>();
+
+ for (UUID uuid : data.detectedPlayers) {
Expand All @@ -37,7 +37,7 @@ index 005aea8a747e9cbbc352b8b57c64b84ec71ad321..a66e0b303aac2952c06f1ec704df0edc
+ list.add(player);
+ }
+ }
+ // Leaf end - Optimize trial spawner ticking
+ // Leaf end - Remove stream in trial spawner ticking
+
if (list.isEmpty()) {
return Optional.empty();
Expand All @@ -53,7 +53,7 @@ index 005aea8a747e9cbbc352b8b57c64b84ec71ad321..a66e0b303aac2952c06f1ec704df0edc
- List<? extends Entity> list = world.random.nextBoolean() ? stream.toList() : players;
- if (list.isEmpty()) {
- return null;
+ // Leaf start - Optimize trial spawner ticking
+ // Leaf start - Remove stream in trial spawner ticking
+ if (world.random.nextBoolean()) {
+ List<Entity> list = new java.util.ArrayList<>();
+ for (UUID uuid : entityUuids) {
Expand All @@ -76,7 +76,7 @@ index 005aea8a747e9cbbc352b8b57c64b84ec71ad321..a66e0b303aac2952c06f1ec704df0edc
+ return players.size() == 1 ? players.getFirst() : Util.getRandom(players, world.random);
+ }
}
+ // Leaf end - Optimize trial spawner ticking
+ // Leaf end - Remove stream in trial spawner ticking
}

private boolean timeToSpawnItemSpawner(ServerLevel world, TrialSpawnerData data) {
59 changes: 44 additions & 15 deletions patches/server/0091-Remove-stream-in-Brain.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ From: HaHaWTH <[email protected]>
Date: Sat, 26 Oct 2024 00:06:04 +0800
Subject: [PATCH] Remove stream in Brain

Dreeam TODO: Check this

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 2ae8c9d56d88987b750e57025d313cfa9300c7e4..f6439fb10031a8f17ce3a5bee9f6e89275edebc7 100644
index 2ae8c9d56d88987b750e57025d313cfa9300c7e4..88ba505fc5ca084317aaf6be402472ccd42413d8 100644
--- a/src/main/java/net/minecraft/world/entity/ai/Brain.java
+++ b/src/main/java/net/minecraft/world/entity/ai/Brain.java
@@ -69,13 +69,17 @@ public class Brain<E extends LivingEntity> {
@@ -69,13 +69,22 @@ public class Brain<E extends LivingEntity> {
mutableObject.setValue(
(new MapCodec<Brain<E>>() {
public <T> Stream<T> keys(DynamicOps<T> dynamicOps) {
Expand All @@ -20,32 +19,62 @@ index 2ae8c9d56d88987b750e57025d313cfa9300c7e4..f6439fb10031a8f17ce3a5bee9f6e892
- .stream()
- )
- .map(id -> dynamicOps.createString(id.toString()));
+ // Leaf start - Optimize Brain
+ List<String> results = new java.util.ArrayList<>();
+ // Leaf start - Remove stream in Brain
+ List<T> results = new java.util.ArrayList<>();
+
+ for (MemoryModuleType<?> memoryType : memoryModules) {
+ Optional<?> codecOptional = memoryType.getCodec();
+ if (codecOptional.isPresent()) {
+ net.minecraft.resources.ResourceLocation id = BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(memoryType);
+ results.add(id.toString());
+ final Optional<?> codec = memoryType.getCodec();
+
+ if (codec.isPresent()) {
+ final net.minecraft.resources.ResourceLocation id = BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(memoryType);
+ final T ops = dynamicOps.createString(id.toString());
+
+ results.add(ops);
+ }
+ }
+ return results.stream().map(dynamicOps::createString);
+ // Leaf end - Optimize Brain
+
+ return results.stream();
+ // Leaf end - Remove stream in Brain
}

public <T> DataResult<Brain<E>> decode(DynamicOps<T> dynamicOps, MapLike<T> mapLike) {
@@ -152,7 +156,13 @@ public class Brain<E extends LivingEntity> {
@@ -110,7 +119,8 @@ public class Brain<E extends LivingEntity> {
}

public <T> RecordBuilder<T> encode(Brain<E> brain, DynamicOps<T> dynamicOps, RecordBuilder<T> recordBuilder) {
- brain.memories().forEach(entry -> entry.serialize(dynamicOps, recordBuilder));
+ brain.serializeMemories(dynamicOps, recordBuilder); // Leaf - Remove stream in Brain
+
return recordBuilder;
}
})
@@ -152,8 +162,28 @@ public class Brain<E extends LivingEntity> {
}

Stream<Brain.MemoryValue<?>> memories() {
- return this.memories.entrySet().stream().map(entry -> Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue()));
+ // Leaf start - Optimize Brain
+ // Leaf start - Remove stream in Brain
+ return memoriesList().stream();
+ }
+
+ List<Brain.MemoryValue<?>> memoriesList() {
+ List<Brain.MemoryValue<?>> result = new java.util.ArrayList<>();
+
+ for (Entry<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> entry : this.memories.entrySet()) {
+ result.add(Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue()));
+ }
+ return result.stream();
+ // Leaf end - Optimize Brain
+
+ return result;
+ }
+
+ <T> void serializeMemories(DynamicOps<T> dynamicOps, RecordBuilder<T> recordBuilder) {
+ for (Entry<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> entry : this.memories.entrySet()) {
+ final Brain.MemoryValue<?> result = Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue());
+
+ result.serialize(dynamicOps, recordBuilder);
+ }
}
+ // Leaf end - Remove stream in Brain

public boolean hasMemoryValue(MemoryModuleType<?> type) {
return this.checkMemory(type, MemoryStatus.VALUE_PRESENT);
6 changes: 3 additions & 3 deletions patches/server/0092-Remove-stream-in-BehaviorUtils.patch
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Subject: [PATCH] Remove stream in BehaviorUtils
Dreeam TODO: Check this

diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java b/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
index 74fe07a3737728b67987ef794103346c1e428109..e2de86d7a86fde183c3ece4de0a1f899a7c3b8b0 100644
index 74fe07a3737728b67987ef794103346c1e428109..2c602383d1e440cb250a31d41f3fd0ecb74d7247 100644
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
@@ -122,12 +122,20 @@ public class BehaviorUtils {
Expand All @@ -16,7 +16,7 @@ index 74fe07a3737728b67987ef794103346c1e428109..e2de86d7a86fde183c3ece4de0a1f899
- Stream<SectionPos> stream = SectionPos.cube(center, radius).filter((sectionposition1) -> { // CraftBukkit - decompile error
- return world.sectionsToVillage(sectionposition1) < j;
- });
+ // Leaf start - Remove stream usage
+ // Leaf start - Remove stream in BehaviorUtils
+ SectionPos closestSection = center;
+ int closestDistance = j;

Expand All @@ -30,7 +30,7 @@ index 74fe07a3737728b67987ef794103346c1e428109..e2de86d7a86fde183c3ece4de0a1f899
+ }
+ }
+ return closestSection;
+ // Leaf end - Remove stream usage
+ // Leaf end - Remove stream in BehaviorUtils
}

public static boolean isWithinAttackRange(Mob mob, LivingEntity target, int rangedWeaponReachReduction) {
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Replace brain maps with optimized collection


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 f6439fb10031a8f17ce3a5bee9f6e89275edebc7..2e0859cd62fd064d43aaf3ac24269d159a6bd49e 100644
index 88ba505fc5ca084317aaf6be402472ccd42413d8..481cdef8550129983dba2532225b9798fb98c88f 100644
--- a/src/main/java/net/minecraft/world/entity/ai/Brain.java
+++ b/src/main/java/net/minecraft/world/entity/ai/Brain.java
@@ -45,14 +45,18 @@ public class Brain<E extends LivingEntity> {
Expand All @@ -15,22 +15,22 @@ index f6439fb10031a8f17ce3a5bee9f6e89275edebc7..2e0859cd62fd064d43aaf3ac24269d15
- private final Map<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> memories = Maps.newHashMap();
- private final Map<SensorType<? extends Sensor<? super E>>, Sensor<? super E>> sensors = Maps.newLinkedHashMap();
- private final Map<Integer, Map<Activity, Set<BehaviorControl<? super E>>>> availableBehaviorsByPriority = Maps.newTreeMap();
+ // Leaf start - Optimize Brain
+ // Leaf start - Replace brain maps with optimized collection
+ private final Map<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> memories = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>();
+ private final Map<SensorType<? extends Sensor<? super E>>, Sensor<? super E>> sensors = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap<>();
+ private final Map<Integer, Map<Activity, Set<BehaviorControl<? super E>>>> availableBehaviorsByPriority = new it.unimi.dsi.fastutil.objects.Object2ObjectRBTreeMap<>();
+ // Leaf end - Optimize Brain
+ // Leaf end - Replace brain maps with optimized collection
private Schedule schedule = Schedule.EMPTY;
- private final Map<Activity, Set<Pair<MemoryModuleType<?>, MemoryStatus>>> activityRequirements = Maps.newHashMap();
- private final Map<Activity, Set<MemoryModuleType<?>>> activityMemoriesToEraseWhenStopped = Maps.newHashMap();
- private Set<Activity> coreActivities = Sets.newHashSet();
- private final Set<Activity> activeActivities = Sets.newHashSet();
+ // Leaf start - Optimize Brain
+ // Leaf start - Replace brain maps with optimized collection
+ private final Map<Activity, Set<Pair<MemoryModuleType<?>, MemoryStatus>>> activityRequirements = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>();
+ private final Map<Activity, Set<MemoryModuleType<?>>> activityMemoriesToEraseWhenStopped = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>();
+ private Set<Activity> coreActivities = new it.unimi.dsi.fastutil.objects.ObjectOpenHashSet<>();
+ private final Set<Activity> activeActivities = new it.unimi.dsi.fastutil.objects.ObjectOpenHashSet<>();
+ // Leaf end - Optimize Brain
+ // Leaf end - Replace brain maps with optimized collection
private Activity defaultActivity = Activity.IDLE;
private long lastScheduleUpdate = -9999L;

0 comments on commit e8bc348

Please sign in to comment.