9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-23 17:09:29 +00:00

Optimize LivingEntity sensor and YieldJobSite (#139)

* Remove stream in YieldJobSite

* Use parallelQuickSort in NearestLivingEntitySensor
This commit is contained in:
Kobe ⑧
2024-10-26 21:03:36 +08:00
committed by GitHub
parent 84820324d1
commit f75d4f1cdb
30 changed files with 83 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
Date: Sat, 26 Oct 2024 14:04:54 +0800
Subject: [PATCH] Remove stream in YieldJobSite
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/YieldJobSite.java b/src/main/java/net/minecraft/world/entity/ai/behavior/YieldJobSite.java
index d1a9b62d3304916275dd6b4c4e783cf1563b5e21..572d266c3af268a5dbbc31272b5aa15d46442408 100644
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/YieldJobSite.java
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/YieldJobSite.java
@@ -38,24 +38,25 @@ public class YieldJobSite {
if (optional.isEmpty()) {
return true;
} else {
- context.<List<LivingEntity>>get(mobs)
- .stream()
- .filter(mob -> mob instanceof Villager && mob != entity)
- .map(villager -> (Villager)villager)
- .filter(LivingEntity::isAlive)
- .filter(villager -> nearbyWantsJobsite(optional.get(), villager, blockPos))
- .findFirst()
- .ifPresent(villager -> {
- walkTarget.erase();
- lookTarget.erase();
- potentialJobSite.erase();
- if (villager.getBrain().getMemory(MemoryModuleType.JOB_SITE).isEmpty()) {
- BehaviorUtils.setWalkAndLookTargetMemories(villager, blockPos, speed, 1);
- villager.getBrain()
- .setMemory(MemoryModuleType.POTENTIAL_JOB_SITE, GlobalPos.of(world.dimension(), blockPos));
- DebugPackets.sendPoiTicketCountPacket(world, blockPos);
+ // Leaf start - Remove stream in YieldJobSite
+ List<LivingEntity> mobsList = context.get(mobs);
+ for (LivingEntity mob : mobsList) {
+ if (mob instanceof Villager villager && mob != entity && villager.isAlive()) {
+ if (nearbyWantsJobsite(optional.get(), villager, blockPos)) {
+ walkTarget.erase();
+ lookTarget.erase();
+ potentialJobSite.erase();
+
+ if (villager.getBrain().getMemory(MemoryModuleType.JOB_SITE).isEmpty()) {
+ BehaviorUtils.setWalkAndLookTargetMemories(villager, blockPos, speed, 1);
+ villager.getBrain().setMemory(MemoryModuleType.POTENTIAL_JOB_SITE, GlobalPos.of(world.dimension(), blockPos));
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
+ }
+ break;
}
- });
+ }
+ }
+ // Leaf end - Remove stream in YieldJobSite
return true;
}
}

View File

@@ -0,0 +1,30 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
Date: Sat, 26 Oct 2024 15:00:21 +0800
Subject: [PATCH] Use QuickSort in NearestLivingEntitySensor
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
index 5a059e1ec232d82e8e891ae78fea962bec2f878e..490c5322f8286f92f9ba4583f59ed60618ad33e5 100644
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
@@ -16,10 +16,16 @@ public class NearestLivingEntitySensor<T extends LivingEntity> extends Sensor<T>
protected void doTick(ServerLevel world, T entity) {
AABB aABB = entity.getBoundingBox().inflate((double)this.radiusXZ(), (double)this.radiusY(), (double)this.radiusXZ());
List<LivingEntity> list = world.getEntitiesOfClass(LivingEntity.class, aABB, e -> e != entity && e.isAlive());
- list.sort(Comparator.comparingDouble(entity::distanceToSqr));
+ // Leaf start - Use QuickSort in NearestLivingEntitySensor
+ LivingEntity[] sortArray = list.toArray(new LivingEntity[]{});
+ it.unimi.dsi.fastutil.objects.ObjectArrays.quickSort(sortArray, Comparator.comparingDouble(entity::distanceToSqr));
+ List<LivingEntity> sortedList = java.util.Arrays.asList(sortArray);
+ // Leaf end - Use QuickSort in NearestLivingEntitySensor
Brain<?> brain = entity.getBrain();
- brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, list);
- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(entity, list));
+ // Leaf start - Use QuickSort in NearestLivingEntitySensor
+ brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, sortedList);
+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(entity, sortedList));
+ // Leaf end - Use QuickSort in NearestLivingEntitySensor
}
protected int radiusXZ() {