mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-26 10:29:13 +00:00
Originally vanilla logic is to use stream, and Mojang switched it to Guava's Collections2 since 1.21.4. It is much faster than using stream or manually adding to a new ArrayList. Manually adding to a new ArrayList requires allocating a new object array. However, the Collections2 lazy handles filter condition on iteration, so much better.
37 lines
2.3 KiB
Diff
37 lines
2.3 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Taiyou06 <kaandindar21@gmail.com>
|
|
Date: Sat, 10 May 2025 22:04:42 +0200
|
|
Subject: [PATCH] Smart sort items in NearestItemSensor
|
|
|
|
|
|
diff --git a/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java b/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
|
|
index 09fd13e2d958da8326276c4dadf25bf488aff5ac..651797720f7fc6ff9dc614f4de56053c304b1170 100644
|
|
--- a/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
|
|
+++ b/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
|
|
@@ -16,6 +16,12 @@ public class NearestItemSensor extends Sensor<Mob> {
|
|
private static final long Y_RANGE = 16L;
|
|
public static final int MAX_DISTANCE_TO_WANTED_ITEM = 32;
|
|
|
|
+ // Leaf start - Smart sort items in NearestItemSensor
|
|
+ private final org.dreeam.leaf.util.FastBitRadixSort itemSorter;
|
|
+ public NearestItemSensor() {
|
|
+ this.itemSorter = new org.dreeam.leaf.util.FastBitRadixSort();
|
|
+ }
|
|
+ // Leaf end - Smart sort items in NearestItemSensor
|
|
@Override
|
|
public Set<MemoryModuleType<?>> requires() {
|
|
return ImmutableSet.of(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM);
|
|
@@ -25,10 +31,10 @@ public class NearestItemSensor extends Sensor<Mob> {
|
|
protected void doTick(ServerLevel level, Mob entity) {
|
|
Brain<?> brain = entity.getBrain();
|
|
List<ItemEntity> entitiesOfClass = level.getEntitiesOfClass(ItemEntity.class, entity.getBoundingBox().inflate(32.0, 16.0, 32.0), itemEntity -> itemEntity.closerThan(entity, MAX_DISTANCE_TO_WANTED_ITEM) && entity.wantsToPickUp(level, itemEntity.getItem())); // Paper - Perf: Move predicate into getEntities
|
|
- entitiesOfClass.sort(Comparator.comparingDouble(entity::distanceToSqr));
|
|
+ ItemEntity[] sortedItems = this.itemSorter.sort(entitiesOfClass, entity, ItemEntity.class); // Leaf - Smart sort items in NearestItemSensor
|
|
// Paper start - Perf: remove streams from hot code
|
|
ItemEntity nearest = null;
|
|
- for (final ItemEntity itemEntity : entitiesOfClass) {
|
|
+ for (final ItemEntity itemEntity : sortedItems) { // Leaf - Smart sort items in NearestItemSensor
|
|
if (entity.hasLineOfSight(itemEntity)) { // Paper - Perf: Move predicate into getEntities
|
|
nearest = itemEntity;
|
|
break;
|