Paper/patches/server/0684-Optimise-general-POI-access.patch
Jake Potrebic 3fa4ea6668
Updated Upstream (Bukkit/CraftBukkit/Spigot)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
6680169e [#660/Enum] Merge remote-tracking branch 'origin/pr/660' into experimental
8b97f215 Add missing AbstractTestingBase extension
9f21f42b [#660/Enum] Merge remote-tracking branch 'origin/pr/660' into experimental
fb59a4a0 Create experimental version
a7c1393b Merge branch 'master' into enums-to-registers
1af01165 Merge branch 'master' into enums-to-registers
4ee82e4e Implement feedback
af8ffd60 Merge branch 'master' into enums-to-registers
6a8ea63f Updated to 1.20
22ae9ebc Merge branch 'master' into enums-to-registers
b1d669be Some clean up
685d812e Merge branch 'master' into enums-to-registers
00d778c7 Convert MusicInstrument
bba2eb5f Convert GameEvent
ffbf67a1 Convert PotionType
eacaa45d Convert Particle
d08d21d1 Convert PatternType
c6b51f7c Convert Cat type
af6c2987 Make missing Frog variant abstract
a67a5f5c Add missing Annotation
6ab21c50 Change how converting from / to BlockType and ItemType work
e3e84e69 Add Objects.requireNonNull so that there are not marked as nullable
fceddab5 Add missing deprecation
e91906f5 Don't convert legacy in register instead, only in required method
2200b334 Use static constructors for ItemStack
b5f483b0 Deprecated Material
a995df2a Fix typo getItemTyp
9cedb664 import ItemType
27e282b2 getSteerItemType -> getSteerItem
d8d0e43b Better Piglin method names
3a2ab399 BLOCK_TYPE -> BLOCK, ITEM_TYPE -> ITEM
a0eb63ac Interface it is
4bb0b646 Split Material into BlockType and ItemType
b6bfcff5 Merge branch 'master' into enums-to-registers
1f86c847 Updated to Mockito 5.3.1
280ee1f7 Fix merge, updated to 1.19.4
9e0c7ad5 Merge branch 'master' into enums-to-registers
fdbed698 Updated to 1.19.3
85c3e2d3 Merge branch 'master' into enums-to-registers
b2c390af Fix merge
4e405647 Merge branch 'master' into enums-to-registers
d01b4c90 Fix copy/paste
ba2c8cb1 Update to 1.19
7e4f2db2 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
a1a974f0 Fix merge
7d3a91d3 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
499e22d9 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
a0cf419f Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
d5bd36a2 Fix / Implement merge changes
fe643952 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
cf1d2005 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
f18dce93 Make Statistic abstract
bbe3f791 Fix 1.18 merge Handle comment out test cases
0988647e Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
6e4f2c50 Populated BlockType and ItemType
7a58144d Convert Material enum, midpoint push, it compiles and runs
4771132c Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/bukkit into enums-to-registers
e6b179ff Convert Material enum, midpoint push to merge BlockState changes
f33b85a0 Change other enums / classes.
8b0d5418 Create seperat OldEnum class, which holds common enum methods.
45544426 Change Enums to classes to easier handle none standart minecraft values

CraftBukkit Changes:
8969b32d0 [#931/Enum] Merge remote-tracking branch 'origin/pr/931' into experimental
222257a67 Add missing AbstractTestingBase extension
0480af399 [#931/Enum] Merge remote-tracking branch 'origin/pr/931' into experimental
1afa1ddc2 Create experimental version
00780ea51 Ignore FactoryItemMaterialTest test
676969d01 SPIGOT-7389: Handle setting null items in ChiseledBookshelf Inventory
84f10cc36 Fix merge
9e114e13e Merge branch 'master' into enums-to-registers
941787e24 Add missing Commodore for 1.20 updated
6dac9a12d Updated tests
737426398 Only allow reference holder
aaaa5fa88 Merge branch 'master' into enums-to-registers
74957eb99 Merge branch 'master' into enums-to-registers
a1ca4e870 Merge branch 'master' into enums-to-registers
f293f4a61 Updated to 1.20
b434b3d15 Merge branch 'master' into enums-to-registers
e99dcbda7 Some clean up
fcead8aed Use correct primitive class
e955d9c50 Fix some Commodore errors
af5526ebb Allow Material to support older plugins
b83afd643 Add rewrite for Google enum set methods
067323765 Add missing method replacement in dynamic invocation
63e17e631 Merge branch 'master' into enums-to-registers
26dfcacf8 Bad copy and past
d50c9bd6a Convert MusicInstrument
c0c5312db Convert GameEvent
39daffe2c Convert PotionType
9b974f832 Convert Particle
f528fca63 Convert PatternType
525c65006 Convert Cat type
6832b8fbb More consistent to / from bukkit / minecraft methods
d31e38e16 Make missing Frog variant abstract
e4f0e7d8e This shouldn't be committed
6fee81baa Add Commodore for EnumSet
82a668683 Fix hasItemType / getItemType
f70162d66 Change how converting from / to BlockType and ItemType work
c3f7c7886 Don't convert legacy in register instead, only in required method
2039e05fa Use static constructors for ItemStack
fe221578b More Commodore
2b70bd171 More Commodore
70f4a89f5 Fix some Commodore
06544ed4b Fix typo getItemTyp
6269d2e42 getSteerItemType -> getSteerItem
a19ac46c0 Better Piglin method names
eef5f52c6 BLOCK_TYPE -> BLOCK, ITEM_TYPE -> ITEM
bbaff1348 Interface it is
c39e1316c Finish Commodore action for Material split
dd8552105 Work on Commodore
1d4ef8bf2 Split Material into BlockType and ItemType
6c5a98220 Merge branch 'master' into enums-to-registers
869658a96 Handle Material calls in lambda expression
523ac4ac0 Add reroute for Class#getEnumConstants
0a4463279 Use extra method for getting registry
602d9b404 Updated to Mockito 5.3.1
8ff87b77d Fix merge, updated to 1.19.4
9d739d313 Merge branch 'master' into enums-to-registers
eb6f702ff Reduce usage of BuiltInRegistries
b6f667cac Some more asm compatibility changes, add config option
87c931d38 Handle enum maps
a2c6699db Updated to 1.19.3
f7c27584f Merge branch 'master' into enums-to-registers
2f95b9951 Fix merge
184b05740 Merge branch 'master' into enums-to-registers
12bd8de26 Updated to 1.19
9c57831b7 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
4ed8eb402 Fix merge
a9faac8e4 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
0d2988603 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
3f8f9557d Fix merge, updated to 1.18.2
1560490c6 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
a0e4eb12c Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
8b59f682d Move getType method to CraftEntity class
b849c0147 Add missing patches
4644ba79f Fix / Implement merge changes
cf9ee732e Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
0c9125b67 Use Tag where possible
cc05153d9 Cache interactable call
ab5cc36de Use getHandle
83ebf4114 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
bc20aea0c Make Statistic abstract
3faa7e135 Add Tests for Material BlockType and ItemType
e10f74365 Fix 1.18 merge Handle comment out test cases
f72f70ec4 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
dbf4f5b7e Populated BlockType and ItemType
015afc1bc Convert Material enum, midpoint push, it compiles and runs
cc0112866 Merge branch 'master' of https://hub.spigotmc.org/stash/scm/~derfrzocker/craftbukkit into enums-to-registers
e26742c59 Convert Material enum, midpoint push to merge BlockState changes
796ad9295 Fix bug in legacy naming converting.
199c8278c Change other enums / classes.
fd513652a Seperated custom biome value handling. Fix compareTo call.
60c71ce07 Change Enums to classes to easier handle none standart minecraft values

Spigot Changes:
addcf45f [Enum] Rebuild patches
2023-06-20 09:50:05 -07:00

1054 lines
57 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Sun, 31 Jan 2021 02:29:24 -0800
Subject: [PATCH] Optimise general POI access
There are a couple of problems with mojang's POI code.
Firstly, it's all streams. Unsurprisingly, stacking
streams on top of each other is horrible for performance
and ultimately took up half of a villager's tick!
Secondly, sometime's the search radius is large and there are
a significant number of poi entries per chunk section. Even
removing streams at this point doesn't help much. The only solution
is to start at the search point and iterate outwards. This
type of approach shows massive gains for portals, simply because
we can avoid sync loading a large area of chunks. I also tested
a massive farm I found in JellySquid's discord, which showed
to benefit significantly simply because the farm had so many
portal blocks that searching through them all was very slow.
Great care has been taken so that behavior remains identical to
vanilla, however I cannot account for oddball Stream API
implementations, if they even exist (streams can technically
be loose with iteration order in a sorted stream given its
source stream is not tagged with ordered, and mojang does not
tag the source stream as ordered). However in my testing on openjdk
there showed no difference, as expected.
This patch also specifically optimises other areas of code to
use PoiAccess. For example, some villager AI and portaling code
had to be specifically modified.
diff --git a/src/main/java/io/papermc/paper/util/PoiAccess.java b/src/main/java/io/papermc/paper/util/PoiAccess.java
new file mode 100644
index 0000000000000000000000000000000000000000..69be1761b3b5ba7b496c1c10a4db897e6212d671
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/PoiAccess.java
@@ -0,0 +1,804 @@
+package io.papermc.paper.util;
+
+import com.mojang.datafixers.util.Pair;
+import it.unimi.dsi.fastutil.doubles.Double2ObjectMap;
+import it.unimi.dsi.fastutil.doubles.Double2ObjectRBTreeMap;
+import it.unimi.dsi.fastutil.longs.LongArrayFIFOQueue;
+import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
+import java.util.function.BiPredicate;
+import net.minecraft.core.BlockPos;
+import net.minecraft.core.Holder;
+import net.minecraft.util.Mth;
+import net.minecraft.world.entity.ai.village.poi.PoiManager;
+import net.minecraft.world.entity.ai.village.poi.PoiRecord;
+import net.minecraft.world.entity.ai.village.poi.PoiSection;
+import net.minecraft.world.entity.ai.village.poi.PoiType;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Predicate;
+
+/**
+ * Provides optimised access to POI data. All returned values will be identical to vanilla.
+ */
+public final class PoiAccess {
+
+ protected static double clamp(final double val, final double min, final double max) {
+ return (val < min ? min : (val > max ? max : val));
+ }
+
+ protected static double getSmallestDistanceSquared(final double boxMinX, final double boxMinY, final double boxMinZ,
+ final double boxMaxX, final double boxMaxY, final double boxMaxZ,
+
+ final double circleX, final double circleY, final double circleZ) {
+ // is the circle center inside the box?
+ if (circleX >= boxMinX && circleX <= boxMaxX && circleY >= boxMinY && circleY <= boxMaxY && circleZ >= boxMinZ && circleZ <= boxMaxZ) {
+ return 0.0;
+ }
+
+ final double boxWidthX = (boxMaxX - boxMinX) / 2.0;
+ final double boxWidthY = (boxMaxY - boxMinY) / 2.0;
+ final double boxWidthZ = (boxMaxZ - boxMinZ) / 2.0;
+
+ final double boxCenterX = (boxMinX + boxMaxX) / 2.0;
+ final double boxCenterY = (boxMinY + boxMaxY) / 2.0;
+ final double boxCenterZ = (boxMinZ + boxMaxZ) / 2.0;
+
+ double centerDiffX = circleX - boxCenterX;
+ double centerDiffY = circleY - boxCenterY;
+ double centerDiffZ = circleZ - boxCenterZ;
+
+ centerDiffX = circleX - (clamp(centerDiffX, -boxWidthX, boxWidthX) + boxCenterX);
+ centerDiffY = circleY - (clamp(centerDiffY, -boxWidthY, boxWidthY) + boxCenterY);
+ centerDiffZ = circleZ - (clamp(centerDiffZ, -boxWidthZ, boxWidthZ) + boxCenterZ);
+
+ return (centerDiffX * centerDiffX) + (centerDiffY * centerDiffY) + (centerDiffZ * centerDiffZ);
+ }
+
+
+ // key is:
+ // upper 32 bits:
+ // upper 16 bits: max y section
+ // lower 16 bits: min y section
+ // lower 32 bits:
+ // upper 16 bits: section
+ // lower 16 bits: radius
+ protected static long getKey(final int minSection, final int maxSection, final int section, final int radius) {
+ return (
+ (maxSection & 0xFFFFL) << (64 - 16)
+ | (minSection & 0xFFFFL) << (64 - 32)
+ | (section & 0xFFFFL) << (64 - 48)
+ | (radius & 0xFFFFL) << (64 - 64)
+ );
+ }
+
+ // only includes x/z axis
+ // finds the closest poi data by distance.
+ public static BlockPos findClosestPoiDataPosition(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final PoiRecord ret = findClosestPoiDataRecord(
+ poiStorage, villagePlaceType, positionPredicate, sourcePosition, range, maxDistanceSquared, occupancy, load
+ );
+
+ return ret == null ? null : ret.getPos();
+ }
+
+ // only includes x/z axis
+ // finds the closest poi data by distance.
+ public static Pair<Holder<PoiType>, BlockPos> findClosestPoiDataTypeAndPosition(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final PoiRecord ret = findClosestPoiDataRecord(
+ poiStorage, villagePlaceType, positionPredicate, sourcePosition, range, maxDistanceSquared, occupancy, load
+ );
+
+ return ret == null ? null : Pair.of(ret.getPoiType(), ret.getPos());
+ }
+
+ // only includes x/z axis
+ // finds the closest poi data by distance. if multiple match the same distance, then they all are returned.
+ public static void findClosestPoiDataPositions(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load,
+ final Set<BlockPos> ret) {
+ final Set<BlockPos> positions = new HashSet<>();
+ // pos predicate is last thing that runs before adding to ret.
+ final Predicate<BlockPos> newPredicate = (final BlockPos pos) -> {
+ if (positionPredicate != null && !positionPredicate.test(pos)) {
+ return false;
+ }
+ return positions.add(pos.immutable());
+ };
+
+ final List<PoiRecord> toConvert = new ArrayList<>();
+ findClosestPoiDataRecords(
+ poiStorage, villagePlaceType, newPredicate, sourcePosition, range, maxDistanceSquared, occupancy, load, toConvert
+ );
+
+ for (final PoiRecord record : toConvert) {
+ ret.add(record.getPos());
+ }
+ }
+
+ // only includes x/z axis
+ // finds the closest poi data by distance.
+ public static PoiRecord findClosestPoiDataRecord(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final List<PoiRecord> ret = new ArrayList<>();
+ findClosestPoiDataRecords(
+ poiStorage, villagePlaceType, positionPredicate, sourcePosition, range, maxDistanceSquared, occupancy, load, ret
+ );
+ return ret.isEmpty() ? null : ret.get(0);
+ }
+
+ // only includes x/z axis
+ // finds the closest poi data by distance.
+ public static PoiRecord findClosestPoiDataRecord(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final BiPredicate<Holder<PoiType>, BlockPos> predicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final List<PoiRecord> ret = new ArrayList<>();
+ findClosestPoiDataRecords(
+ poiStorage, villagePlaceType, predicate, sourcePosition, range, maxDistanceSquared, occupancy, load, ret
+ );
+ return ret.isEmpty() ? null : ret.get(0);
+ }
+
+ // only includes x/z axis
+ // finds the closest poi data by distance. if multiple match the same distance, then they all are returned.
+ public static void findClosestPoiDataRecords(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load,
+ final List<PoiRecord> ret) {
+ final BiPredicate<Holder<PoiType>, BlockPos> predicate = positionPredicate != null ? (type, pos) -> positionPredicate.test(pos) : null;
+ findClosestPoiDataRecords(poiStorage, villagePlaceType, predicate, sourcePosition, range, maxDistanceSquared, occupancy, load, ret);
+ }
+
+ public static void findClosestPoiDataRecords(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final BiPredicate<Holder<PoiType>, BlockPos> predicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load,
+ final List<PoiRecord> ret) {
+ final Predicate<? super PoiRecord> occupancyFilter = occupancy.getTest();
+
+ final List<PoiRecord> closestRecords = new ArrayList<>();
+ double closestDistanceSquared = maxDistanceSquared;
+
+ final int lowerX = Mth.floor(sourcePosition.getX() - range) >> 4;
+ final int lowerY = WorldUtil.getMinSection(poiStorage.world);
+ final int lowerZ = Mth.floor(sourcePosition.getZ() - range) >> 4;
+ final int upperX = Mth.floor(sourcePosition.getX() + range) >> 4;
+ final int upperY = WorldUtil.getMaxSection(poiStorage.world);
+ final int upperZ = Mth.floor(sourcePosition.getZ() + range) >> 4;
+
+ final int centerX = sourcePosition.getX() >> 4;
+ final int centerY = Mth.clamp(sourcePosition.getY() >> 4, lowerY, upperY);
+ final int centerZ = sourcePosition.getZ() >> 4;
+ final long centerKey = CoordinateUtils.getChunkSectionKey(centerX, centerY, centerZ);
+
+ final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
+ final LongOpenHashSet seen = new LongOpenHashSet();
+ seen.add(centerKey);
+ queue.enqueue(centerKey);
+
+ while (!queue.isEmpty()) {
+ final long key = queue.dequeueLong();
+ final int sectionX = CoordinateUtils.getChunkSectionX(key);
+ final int sectionY = CoordinateUtils.getChunkSectionY(key);
+ final int sectionZ = CoordinateUtils.getChunkSectionZ(key);
+
+ if (sectionX < lowerX || sectionX > upperX || sectionY < lowerY || sectionY > upperY || sectionZ < lowerZ || sectionZ > upperZ) {
+ // out of bound chunk
+ continue;
+ }
+
+ final double sectionDistanceSquared = getSmallestDistanceSquared(
+ (sectionX << 4) + 0.5,
+ (sectionY << 4) + 0.5,
+ (sectionZ << 4) + 0.5,
+ (sectionX << 4) + 15.5,
+ (sectionY << 4) + 15.5,
+ (sectionZ << 4) + 15.5,
+ (double)sourcePosition.getX(), (double)sourcePosition.getY(), (double)sourcePosition.getZ()
+ );
+ if (sectionDistanceSquared > closestDistanceSquared) {
+ continue;
+ }
+
+ // queue all neighbours
+ for (int dz = -1; dz <= 1; ++dz) {
+ for (int dx = -1; dx <= 1; ++dx) {
+ for (int dy = -1; dy <= 1; ++dy) {
+ // -1 and 1 have the 1st bit set. so just add up the first bits, and it will tell us how many
+ // values are set. we only care about cardinal neighbours, so, we only care if one value is set
+ if ((dx & 1) + (dy & 1) + (dz & 1) != 1) {
+ continue;
+ }
+
+ final int neighbourX = sectionX + dx;
+ final int neighbourY = sectionY + dy;
+ final int neighbourZ = sectionZ + dz;
+
+ final long neighbourKey = CoordinateUtils.getChunkSectionKey(neighbourX, neighbourY, neighbourZ);
+ if (seen.add(neighbourKey)) {
+ queue.enqueue(neighbourKey);
+ }
+ }
+ }
+ }
+
+ final Optional<PoiSection> poiSectionOptional = load ? poiStorage.getOrLoad(key) : poiStorage.get(key);
+
+ if (poiSectionOptional == null || !poiSectionOptional.isPresent()) {
+ continue;
+ }
+
+ final PoiSection poiSection = poiSectionOptional.get();
+
+ final Map<Holder<PoiType>, Set<PoiRecord>> sectionData = poiSection.getData();
+ if (sectionData.isEmpty()) {
+ continue;
+ }
+
+ // now we search the section data
+ for (final Map.Entry<Holder<PoiType>, Set<PoiRecord>> entry : sectionData.entrySet()) {
+ if (!villagePlaceType.test(entry.getKey())) {
+ // filter out by poi type
+ continue;
+ }
+
+ // now we can look at the poi data
+ for (final PoiRecord poiData : entry.getValue()) {
+ if (!occupancyFilter.test(poiData)) {
+ // filter by occupancy
+ continue;
+ }
+
+ final BlockPos poiPosition = poiData.getPos();
+
+ if (Math.abs(poiPosition.getX() - sourcePosition.getX()) > range
+ || Math.abs(poiPosition.getZ() - sourcePosition.getZ()) > range) {
+ // out of range for square radius
+ continue;
+ }
+
+ // it's important that it's poiPosition.distSqr(source) : the value actually is different IF the values are swapped!
+ final double dataRange = poiPosition.distSqr(sourcePosition);
+
+ if (dataRange > closestDistanceSquared) {
+ // out of range for distance check
+ continue;
+ }
+
+ if (predicate != null && !predicate.test(poiData.getPoiType(), poiPosition)) {
+ // filter by position
+ continue;
+ }
+
+ if (dataRange < closestDistanceSquared) {
+ closestRecords.clear();
+ closestDistanceSquared = dataRange;
+ }
+ closestRecords.add(poiData);
+ }
+ }
+ }
+
+ // uh oh! we might have multiple records that match the distance sorting!
+ // we need to re-order our results by the way vanilla would have iterated over them.
+ closestRecords.sort((record1, record2) -> {
+ // vanilla iterates the same way we do for data inside sections, so we know the ordering inside a section
+ // is fine and should be preserved (this sort is stable so we're good there)
+ // but they iterate sections by x then by z (like the following)
+ // for (int x = -dx; x <= dx; ++x)
+ // for (int z = -dz; z <= dz; ++z)
+ // ....
+ // so we need to reorder such that records with lower chunk z, then lower chunk x come first
+ final BlockPos pos1 = record1.getPos();
+ final BlockPos pos2 = record2.getPos();
+
+ final int cx1 = pos1.getX() >> 4;
+ final int cz1 = pos1.getZ() >> 4;
+
+ final int cx2 = pos2.getX() >> 4;
+ final int cz2 = pos2.getZ() >> 4;
+
+ if (cz2 != cz1) {
+ // want smaller z
+ return Integer.compare(cz1, cz2);
+ }
+
+ if (cx2 != cx1) {
+ // want smaller x
+ return Integer.compare(cx1, cx2);
+ }
+
+ // same chunk
+ // once vanilla has the chunk, it will iterate from all of the chunk sections starting from smaller y
+ // so now we just compare section y, wanting smaller y
+
+ return Integer.compare(pos1.getY() >> 4, pos2.getY() >> 4);
+ });
+
+ // now we match perfectly what vanilla would have outputted, without having to search the whole radius (hopefully).
+ ret.addAll(closestRecords);
+ }
+
+ // finds the closest poi entry pos.
+ public static BlockPos findNearestPoiPosition(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final PoiRecord ret = findNearestPoiRecord(
+ poiStorage, villagePlaceType, positionPredicate, sourcePosition, range, maxDistanceSquared, occupancy, load
+ );
+ return ret == null ? null : ret.getPos();
+ }
+
+ // finds the closest `max` poi entry positions.
+ public static void findNearestPoiPositions(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load,
+ final int max,
+ final List<Pair<Holder<PoiType>, BlockPos>> ret) {
+ final Set<BlockPos> positions = new HashSet<>();
+ // pos predicate is last thing that runs before adding to ret.
+ final Predicate<BlockPos> newPredicate = (final BlockPos pos) -> {
+ if (positionPredicate != null && !positionPredicate.test(pos)) {
+ return false;
+ }
+ return positions.add(pos.immutable());
+ };
+
+ final List<PoiRecord> toConvert = new ArrayList<>();
+ findNearestPoiRecords(
+ poiStorage, villagePlaceType, newPredicate, sourcePosition, range, maxDistanceSquared, occupancy, load, max, toConvert
+ );
+
+ for (final PoiRecord record : toConvert) {
+ ret.add(Pair.of(record.getPoiType(), record.getPos()));
+ }
+ }
+
+ // finds the closest poi entry.
+ public static PoiRecord findNearestPoiRecord(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final List<PoiRecord> ret = new ArrayList<>();
+ findNearestPoiRecords(
+ poiStorage, villagePlaceType, positionPredicate, sourcePosition, range, maxDistanceSquared, occupancy, load,
+ 1, ret
+ );
+ return ret.isEmpty() ? null : ret.get(0);
+ }
+
+ // finds the closest `max` poi entries.
+ public static void findNearestPoiRecords(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ // position predicate must not modify chunk POI
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final double maxDistanceSquared,
+ final PoiManager.Occupancy occupancy,
+ final boolean load,
+ final int max,
+ final List<PoiRecord> ret) {
+ final Predicate<? super PoiRecord> occupancyFilter = occupancy.getTest();
+
+ final Double2ObjectRBTreeMap<List<PoiRecord>> closestRecords = new Double2ObjectRBTreeMap<>();
+ int totalRecords = 0;
+ double furthestDistanceSquared = maxDistanceSquared;
+
+ final int lowerX = Mth.floor(sourcePosition.getX() - range) >> 4;
+ final int lowerY = WorldUtil.getMinSection(poiStorage.world);
+ final int lowerZ = Mth.floor(sourcePosition.getZ() - range) >> 4;
+ final int upperX = Mth.floor(sourcePosition.getX() + range) >> 4;
+ final int upperY = WorldUtil.getMaxSection(poiStorage.world);
+ final int upperZ = Mth.floor(sourcePosition.getZ() + range) >> 4;
+
+ final int centerX = sourcePosition.getX() >> 4;
+ final int centerY = Mth.clamp(sourcePosition.getY() >> 4, lowerY, upperY);
+ final int centerZ = sourcePosition.getZ() >> 4;
+ final long centerKey = CoordinateUtils.getChunkSectionKey(centerX, centerY, centerZ);
+
+ final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
+ final LongOpenHashSet seen = new LongOpenHashSet();
+ seen.add(centerKey);
+ queue.enqueue(centerKey);
+
+ while (!queue.isEmpty()) {
+ final long key = queue.dequeueLong();
+ final int sectionX = CoordinateUtils.getChunkSectionX(key);
+ final int sectionY = CoordinateUtils.getChunkSectionY(key);
+ final int sectionZ = CoordinateUtils.getChunkSectionZ(key);
+
+ if (sectionX < lowerX || sectionX > upperX || sectionY < lowerY || sectionY > upperY || sectionZ < lowerZ || sectionZ > upperZ) {
+ // out of bound chunk
+ continue;
+ }
+
+ final double sectionDistanceSquared = getSmallestDistanceSquared(
+ (sectionX << 4) + 0.5,
+ (sectionY << 4) + 0.5,
+ (sectionZ << 4) + 0.5,
+ (sectionX << 4) + 15.5,
+ (sectionY << 4) + 15.5,
+ (sectionZ << 4) + 15.5,
+ (double) sourcePosition.getX(), (double) sourcePosition.getY(), (double) sourcePosition.getZ()
+ );
+
+ if (sectionDistanceSquared > (totalRecords >= max ? furthestDistanceSquared : maxDistanceSquared)) {
+ continue;
+ }
+
+ // queue all neighbours
+ for (int dz = -1; dz <= 1; ++dz) {
+ for (int dx = -1; dx <= 1; ++dx) {
+ for (int dy = -1; dy <= 1; ++dy) {
+ // -1 and 1 have the 1st bit set. so just add up the first bits, and it will tell us how many
+ // values are set. we only care about cardinal neighbours, so, we only care if one value is set
+ if ((dx & 1) + (dy & 1) + (dz & 1) != 1) {
+ continue;
+ }
+
+ final int neighbourX = sectionX + dx;
+ final int neighbourY = sectionY + dy;
+ final int neighbourZ = sectionZ + dz;
+
+ final long neighbourKey = CoordinateUtils.getChunkSectionKey(neighbourX, neighbourY, neighbourZ);
+ if (seen.add(neighbourKey)) {
+ queue.enqueue(neighbourKey);
+ }
+ }
+ }
+ }
+
+ final Optional<PoiSection> poiSectionOptional = load ? poiStorage.getOrLoad(key) : poiStorage.get(key);
+
+ if (poiSectionOptional == null || !poiSectionOptional.isPresent()) {
+ continue;
+ }
+
+ final PoiSection poiSection = poiSectionOptional.get();
+
+ final Map<Holder<PoiType>, Set<PoiRecord>> sectionData = poiSection.getData();
+ if (sectionData.isEmpty()) {
+ continue;
+ }
+
+ // now we search the section data
+ for (final Map.Entry<Holder<PoiType>, Set<PoiRecord>> entry : sectionData.entrySet()) {
+ if (!villagePlaceType.test(entry.getKey())) {
+ // filter out by poi type
+ continue;
+ }
+
+ // now we can look at the poi data
+ for (final PoiRecord poiData : entry.getValue()) {
+ if (!occupancyFilter.test(poiData)) {
+ // filter by occupancy
+ continue;
+ }
+
+ final BlockPos poiPosition = poiData.getPos();
+
+ if (Math.abs(poiPosition.getX() - sourcePosition.getX()) > range
+ || Math.abs(poiPosition.getZ() - sourcePosition.getZ()) > range) {
+ // out of range for square radius
+ continue;
+ }
+
+ // it's important that it's poiPosition.distSqr(source) : the value actually is different IF the values are swapped!
+ final double dataRange = poiPosition.distSqr(sourcePosition);
+
+ if (dataRange > maxDistanceSquared) {
+ // out of range for distance check
+ continue;
+ }
+
+ if (dataRange > furthestDistanceSquared && totalRecords >= max) {
+ // out of range for distance check
+ continue;
+ }
+
+ if (positionPredicate != null && !positionPredicate.test(poiPosition)) {
+ // filter by position
+ continue;
+ }
+
+ if (dataRange > furthestDistanceSquared) {
+ // we know totalRecords < max, so this entry is now our furthest
+ furthestDistanceSquared = dataRange;
+ }
+
+ closestRecords.computeIfAbsent(dataRange, (final double unused) -> {
+ return new ArrayList<>();
+ }).add(poiData);
+
+ if (++totalRecords >= max) {
+ if (closestRecords.size() >= 2) {
+ int entriesInClosest = 0;
+ final Iterator<Double2ObjectMap.Entry<List<PoiRecord>>> iterator = closestRecords.double2ObjectEntrySet().iterator();
+ double nextFurthestDistanceSquared = 0.0;
+
+ for (int i = 0, len = closestRecords.size() - 1; i < len; ++i) {
+ final Double2ObjectMap.Entry<List<PoiRecord>> recordEntry = iterator.next();
+ entriesInClosest += recordEntry.getValue().size();
+ nextFurthestDistanceSquared = recordEntry.getDoubleKey();
+ }
+
+ if (entriesInClosest >= max) {
+ // the last set of entries at range wont even be considered for sure... nuke em
+ final Double2ObjectMap.Entry<List<PoiRecord>> recordEntry = iterator.next();
+ totalRecords -= recordEntry.getValue().size();
+ iterator.remove();
+
+ furthestDistanceSquared = nextFurthestDistanceSquared;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ final List<PoiRecord> closestRecordsUnsorted = new ArrayList<>();
+
+ // we're done here, so now just flatten the map and sort it.
+
+ for (final List<PoiRecord> records : closestRecords.values()) {
+ closestRecordsUnsorted.addAll(records);
+ }
+
+ // uh oh! we might have multiple records that match the distance sorting!
+ // we need to re-order our results by the way vanilla would have iterated over them.
+ closestRecordsUnsorted.sort((record1, record2) -> {
+ // vanilla iterates the same way we do for data inside sections, so we know the ordering inside a section
+ // is fine and should be preserved (this sort is stable so we're good there)
+ // but they iterate sections by x then by z (like the following)
+ // for (int x = -dx; x <= dx; ++x)
+ // for (int z = -dz; z <= dz; ++z)
+ // ....
+ // so we need to reorder such that records with lower chunk z, then lower chunk x come first
+ final BlockPos pos1 = record1.getPos();
+ final BlockPos pos2 = record2.getPos();
+
+ final int cx1 = pos1.getX() >> 4;
+ final int cz1 = pos1.getZ() >> 4;
+
+ final int cx2 = pos2.getX() >> 4;
+ final int cz2 = pos2.getZ() >> 4;
+
+ if (cz2 != cz1) {
+ // want smaller z
+ return Integer.compare(cz1, cz2);
+ }
+
+ if (cx2 != cx1) {
+ // want smaller x
+ return Integer.compare(cx1, cx2);
+ }
+
+ // same chunk
+ // once vanilla has the chunk, it will iterate from all of the chunk sections starting from smaller y
+ // so now we just compare section y, wanting smaller section y
+
+ return Integer.compare(pos1.getY() >> 4, pos2.getY() >> 4);
+ });
+
+ // trim out any entries exceeding our maximum
+ for (int i = closestRecordsUnsorted.size() - 1; i >= max; --i) {
+ closestRecordsUnsorted.remove(i);
+ }
+
+ // now we match perfectly what vanilla would have outputted, without having to search the whole radius (hopefully).
+ ret.addAll(closestRecordsUnsorted);
+ }
+
+ public static BlockPos findAnyPoiPosition(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final PoiRecord ret = findAnyPoiRecord(
+ poiStorage, villagePlaceType, positionPredicate, sourcePosition, range, occupancy, load
+ );
+
+ return ret == null ? null : ret.getPos();
+ }
+
+ public static void findAnyPoiPositions(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final PoiManager.Occupancy occupancy,
+ final boolean load,
+ final int max,
+ final List<Pair<Holder<PoiType>, BlockPos>> ret) {
+ final Set<BlockPos> positions = new HashSet<>();
+ // pos predicate is last thing that runs before adding to ret.
+ final Predicate<BlockPos> newPredicate = (final BlockPos pos) -> {
+ if (positionPredicate != null && !positionPredicate.test(pos)) {
+ return false;
+ }
+ return positions.add(pos.immutable());
+ };
+
+ final List<PoiRecord> toConvert = new ArrayList<>();
+ findAnyPoiRecords(
+ poiStorage, villagePlaceType, newPredicate, sourcePosition, range, occupancy, load, max, toConvert
+ );
+
+ for (final PoiRecord record : toConvert) {
+ ret.add(Pair.of(record.getPoiType(), record.getPos()));
+ }
+ }
+
+ public static PoiRecord findAnyPoiRecord(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final PoiManager.Occupancy occupancy,
+ final boolean load) {
+ final List<PoiRecord> ret = new ArrayList<>();
+ findAnyPoiRecords(poiStorage, villagePlaceType, positionPredicate, sourcePosition, range, occupancy, load, 1, ret);
+ return ret.isEmpty() ? null : ret.get(0);
+ }
+
+ public static void findAnyPoiRecords(final PoiManager poiStorage,
+ final Predicate<Holder<PoiType>> villagePlaceType,
+ final Predicate<BlockPos> positionPredicate,
+ final BlockPos sourcePosition,
+ final int range, // distance on x y z axis
+ final PoiManager.Occupancy occupancy,
+ final boolean load,
+ final int max,
+ final List<PoiRecord> ret) {
+ // the biggest issue with the original mojang implementation is that they chain so many streams together
+ // the amount of streams chained just rolls performance, even if nothing is iterated over
+ final Predicate<? super PoiRecord> occupancyFilter = occupancy.getTest();
+ final double rangeSquared = range * range;
+
+ int added = 0;
+
+ // First up, we need to iterate the chunks
+ // all the values here are in chunk sections
+ final int lowerX = Mth.floor(sourcePosition.getX() - range) >> 4;
+ final int lowerY = Math.max(WorldUtil.getMinSection(poiStorage.world), Mth.floor(sourcePosition.getY() - range) >> 4);
+ final int lowerZ = Mth.floor(sourcePosition.getZ() - range) >> 4;
+ final int upperX = Mth.floor(sourcePosition.getX() + range) >> 4;
+ final int upperY = Math.min(WorldUtil.getMaxSection(poiStorage.world), Mth.floor(sourcePosition.getY() + range) >> 4);
+ final int upperZ = Mth.floor(sourcePosition.getZ() + range) >> 4;
+
+ // Vanilla iterates by x until max is reached then increases z
+ // vanilla also searches by increasing Y section value
+ for (int currZ = lowerZ; currZ <= upperZ; ++currZ) {
+ for (int currX = lowerX; currX <= upperX; ++currX) {
+ for (int currY = lowerY; currY <= upperY; ++currY) { // vanilla searches the entire chunk because they're actually stupid. just search the sections we need
+ final Optional<PoiSection> poiSectionOptional = load ? poiStorage.getOrLoad(CoordinateUtils.getChunkSectionKey(currX, currY, currZ)) :
+ poiStorage.get(CoordinateUtils.getChunkSectionKey(currX, currY, currZ));
+ final PoiSection poiSection = poiSectionOptional == null ? null : poiSectionOptional.orElse(null);
+ if (poiSection == null) {
+ continue;
+ }
+
+ final Map<Holder<PoiType>, Set<PoiRecord>> sectionData = poiSection.getData();
+ if (sectionData.isEmpty()) {
+ continue;
+ }
+
+ // now we search the section data
+ for (final Map.Entry<Holder<PoiType>, Set<PoiRecord>> entry : sectionData.entrySet()) {
+ if (!villagePlaceType.test(entry.getKey())) {
+ // filter out by poi type
+ continue;
+ }
+
+ // now we can look at the poi data
+ for (final PoiRecord poiData : entry.getValue()) {
+ if (!occupancyFilter.test(poiData)) {
+ // filter by occupancy
+ continue;
+ }
+
+ final BlockPos poiPosition = poiData.getPos();
+
+ if (Math.abs(poiPosition.getX() - sourcePosition.getX()) > range
+ || Math.abs(poiPosition.getZ() - sourcePosition.getZ()) > range) {
+ // out of range for square radius
+ continue;
+ }
+
+ if (poiPosition.distSqr(sourcePosition) > rangeSquared) {
+ // out of range for distance check
+ continue;
+ }
+
+ if (positionPredicate != null && !positionPredicate.test(poiPosition)) {
+ // filter by position
+ continue;
+ }
+
+ // found one!
+ ret.add(poiData);
+ if (++added >= max) {
+ return;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private PoiAccess() {
+ throw new RuntimeException();
+ }
+}
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java b/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
index 9fd6cf0c98cbf6300ef23d4aab32a6110aeba665..d4c91e0a0c64fcb7f1145de3f30134cb1f1f8ee6 100644
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
@@ -62,7 +62,11 @@ public class AcquirePoi {
return true;
}
};
- Set<Pair<Holder<PoiType>, BlockPos>> set = poiManager.findAllClosestFirstWithType(poiPredicate, predicate2, entity.blockPosition(), 48, PoiManager.Occupancy.HAS_SPACE).limit(5L).collect(Collectors.toSet());
+ // Paper start - optimise POI access
+ java.util.List<Pair<Holder<PoiType>, BlockPos>> poiposes = new java.util.ArrayList<>();
+ io.papermc.paper.util.PoiAccess.findNearestPoiPositions(poiManager, poiPredicate, predicate2, entity.blockPosition(), 48, 48*48, PoiManager.Occupancy.HAS_SPACE, false, 5, poiposes);
+ Set<Pair<Holder<PoiType>, BlockPos>> set = new java.util.HashSet<>(poiposes);
+ // Paper end - optimise POI access
Path path = findPathToPois(entity, set);
if (path != null && path.canReach()) {
BlockPos blockPos = path.getTarget();
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
index 33fbf72b440e0d164ecd4fb0fdec72e2394d0a1e..8db20db72cd51046213625fac46c35854c59ec5d 100644
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
@@ -53,10 +53,12 @@ public class NearestBedSensor extends Sensor<Mob> {
return true;
}
};
- Set<Pair<Holder<PoiType>, BlockPos>> set = poiManager.findAllWithType((holder) -> {
- return holder.is(PoiTypes.HOME);
- }, predicate, entity.blockPosition(), 48, PoiManager.Occupancy.ANY).collect(Collectors.toSet());
- Path path = AcquirePoi.findPathToPois(entity, set);
+ // Paper start - optimise POI access
+ java.util.List<Pair<Holder<PoiType>, BlockPos>> poiposes = new java.util.ArrayList<>();
+ // don't ask me why it's unbounded. ask mojang.
+ io.papermc.paper.util.PoiAccess.findAnyPoiPositions(poiManager, type -> type.is(PoiTypes.HOME), predicate, entity.blockPosition(), 48, PoiManager.Occupancy.ANY, false, Integer.MAX_VALUE, poiposes);
+ Path path = AcquirePoi.findPathToPois(entity, new java.util.HashSet<>(poiposes));
+ // Paper end - optimise POI access
if (path != null && path.canReach()) {
BlockPos blockPos = path.getTarget();
Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
diff --git a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java
index 6f2c7baea0d1ac7813c7b85e1f5558573745762c..12a7aaeaa8b4b788b620b1985591c3b93253ccd5 100644
--- a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java
+++ b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java
@@ -127,43 +127,62 @@ public class PoiManager extends SectionStorage<PoiSection> {
}
public Optional<BlockPos> find(Predicate<Holder<PoiType>> typePredicate, Predicate<BlockPos> posPredicate, BlockPos pos, int radius, PoiManager.Occupancy occupationStatus) {
- return this.findAll(typePredicate, posPredicate, pos, radius, occupationStatus).findFirst();
+ // Paper start - re-route to faster logic
+ BlockPos ret = io.papermc.paper.util.PoiAccess.findAnyPoiPosition(this, typePredicate, posPredicate, pos, radius, occupationStatus, false);
+ return Optional.ofNullable(ret);
+ // Paper end
}
public Optional<BlockPos> findClosest(Predicate<Holder<PoiType>> typePredicate, BlockPos pos, int radius, PoiManager.Occupancy occupationStatus) {
- return this.getInRange(typePredicate, pos, radius, occupationStatus).map(PoiRecord::getPos).min(Comparator.comparingDouble((blockPos2) -> {
- return blockPos2.distSqr(pos);
- }));
+ // Paper start - re-route to faster logic
+ BlockPos ret = io.papermc.paper.util.PoiAccess.findClosestPoiDataPosition(this, typePredicate, null, pos, radius, radius * radius, occupationStatus, false);
+ return Optional.ofNullable(ret);
+ // Paper end - re-route to faster logic
}
public Optional<Pair<Holder<PoiType>, BlockPos>> findClosestWithType(Predicate<Holder<PoiType>> typePredicate, BlockPos pos, int radius, PoiManager.Occupancy occupationStatus) {
- return this.getInRange(typePredicate, pos, radius, occupationStatus).min(Comparator.comparingDouble((poi) -> {
- return poi.getPos().distSqr(pos);
- })).map((poi) -> {
- return Pair.of(poi.getPoiType(), poi.getPos());
- });
+ // Paper start - re-route to faster logic
+ return Optional.ofNullable(io.papermc.paper.util.PoiAccess.findClosestPoiDataTypeAndPosition(
+ this, typePredicate, null, pos, radius, radius * radius, occupationStatus, false
+ ));
+ // Paper end - re-route to faster logic
}
public Optional<BlockPos> findClosest(Predicate<Holder<PoiType>> typePredicate, Predicate<BlockPos> posPredicate, BlockPos pos, int radius, PoiManager.Occupancy occupationStatus) {
- return this.getInRange(typePredicate, pos, radius, occupationStatus).map(PoiRecord::getPos).filter(posPredicate).min(Comparator.comparingDouble((blockPos2) -> {
- return blockPos2.distSqr(pos);
- }));
+ // Paper start - re-route to faster logic
+ BlockPos ret = io.papermc.paper.util.PoiAccess.findClosestPoiDataPosition(this, typePredicate, posPredicate, pos, radius, radius * radius, occupationStatus, false);
+ return Optional.ofNullable(ret);
+ // Paper end - re-route to faster logic
}
public Optional<BlockPos> take(Predicate<Holder<PoiType>> typePredicate, BiPredicate<Holder<PoiType>, BlockPos> biPredicate, BlockPos pos, int radius) {
- return this.getInRange(typePredicate, pos, radius, PoiManager.Occupancy.HAS_SPACE).filter((poi) -> {
- return biPredicate.test(poi.getPoiType(), poi.getPos());
- }).findFirst().map((poi) -> {
+ // Paper start - re-route to faster logic
+ final @javax.annotation.Nullable PoiRecord closest = io.papermc.paper.util.PoiAccess.findClosestPoiDataRecord(
+ this, typePredicate, biPredicate, pos, radius, radius * radius, Occupancy.HAS_SPACE, false
+ );
+ return Optional.ofNullable(closest).map(poi -> {
+ // Paper end - re-route to faster logic
poi.acquireTicket();
return poi.getPos();
});
}
public Optional<BlockPos> getRandom(Predicate<Holder<PoiType>> typePredicate, Predicate<BlockPos> positionPredicate, PoiManager.Occupancy occupationStatus, BlockPos pos, int radius, RandomSource random) {
- List<PoiRecord> list = Util.toShuffledList(this.getInRange(typePredicate, pos, radius, occupationStatus), random);
- return list.stream().filter((poi) -> {
- return positionPredicate.test(poi.getPos());
- }).findFirst().map(PoiRecord::getPos);
+ // Paper start - re-route to faster logic
+ List<PoiRecord> list = new java.util.ArrayList<>();
+ io.papermc.paper.util.PoiAccess.findAnyPoiRecords(
+ this, typePredicate, positionPredicate, pos, radius, occupationStatus, false, Integer.MAX_VALUE, list
+ );
+
+ // the old method shuffled the list and then tried to find the first element in it that
+ // matched positionPredicate, however we moved positionPredicate into the poi search. This means we can avoid a
+ // shuffle entirely, and just pick a random element from list
+ if (list.isEmpty()) {
+ return Optional.empty();
+ }
+
+ return Optional.of(list.get(random.nextInt(list.size())).getPos());
+ // Paper end - re-route to faster logic
}
public boolean release(BlockPos pos) {
diff --git a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiSection.java b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiSection.java
index 3fc17817906876e83f040f908b8b1ba6cfa37b8b..9f138bc471b5c2a4fa813ff943dbe34018b8df74 100644
--- a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiSection.java
+++ b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiSection.java
@@ -26,7 +26,7 @@ import org.slf4j.Logger;
public class PoiSection {
private static final Logger LOGGER = LogUtils.getLogger();
private final Short2ObjectMap<PoiRecord> records = new Short2ObjectOpenHashMap<>();
- private final Map<Holder<PoiType>, Set<PoiRecord>> byType = Maps.newHashMap();
+ private final Map<Holder<PoiType>, Set<PoiRecord>> byType = Maps.newHashMap(); public final Map<Holder<PoiType>, Set<PoiRecord>> getData() { return this.byType; } // Paper - public accessor
private final Runnable setDirty;
private boolean isValid;
public final Optional<PoiSection> noAllocateOptional = Optional.of(this); // Paper - rewrite chunk system
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java
index 0887cba39bfc4279abec21c6c316abab28beb0a3..5561b8499a0503b850974b1dc309edfb80219549 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java
@@ -71,11 +71,11 @@ public class SectionStorage<R> extends RegionFileStorage implements AutoCloseabl
}
@Nullable
- protected Optional<R> get(long pos) {
+ public Optional<R> get(long pos) { // Paper - public
return this.storage.get(pos);
}
- protected Optional<R> getOrLoad(long pos) {
+ public Optional<R> getOrLoad(long pos) { // Paper - public
if (this.outsideStoredRange(pos)) {
return Optional.empty();
} else {
diff --git a/src/main/java/net/minecraft/world/level/portal/PortalForcer.java b/src/main/java/net/minecraft/world/level/portal/PortalForcer.java
index 33a6fda1e8f2df61b44f0e3ddda356ffa386f2df..9b38f4c81ca9ef0f91f9d59fc2be4eecc1afc165 100644
--- a/src/main/java/net/minecraft/world/level/portal/PortalForcer.java
+++ b/src/main/java/net/minecraft/world/level/portal/PortalForcer.java
@@ -51,18 +51,39 @@ public class PortalForcer {
// int i = flag ? 16 : 128;
// CraftBukkit end
- villageplace.ensureLoadedAndValid(this.level, blockposition, i);
- Optional<PoiRecord> optional = villageplace.getInSquare((holder) -> {
- return holder.is(PoiTypes.NETHER_PORTAL);
- }, blockposition, i, PoiManager.Occupancy.ANY).filter((villageplacerecord) -> {
- return worldborder.isWithinBounds(villageplacerecord.getPos()) && !this.level.paperConfig().environment.netherCeilingVoidDamageHeight.test(v -> villageplacerecord.getPos().getY() >= v); // Paper - don't teleport into void damage
- }).sorted(Comparator.comparingDouble((PoiRecord villageplacerecord) -> { // CraftBukkit - decompile error
- return villageplacerecord.getPos().distSqr(blockposition);
- }).thenComparingInt((villageplacerecord) -> {
- return villageplacerecord.getPos().getY();
- })).filter((villageplacerecord) -> {
- return this.level.getBlockState(villageplacerecord.getPos()).hasProperty(BlockStateProperties.HORIZONTAL_AXIS);
- }).findFirst();
+ // Paper start - optimise portals
+ Optional<PoiRecord> optional;
+ java.util.List<PoiRecord> records = new java.util.ArrayList<>();
+ io.papermc.paper.util.PoiAccess.findClosestPoiDataRecords(
+ villageplace,
+ type -> type.is(PoiTypes.NETHER_PORTAL),
+ (BlockPos pos) -> {
+ net.minecraft.world.level.chunk.ChunkAccess lowest = this.level.getChunk(pos.getX() >> 4, pos.getZ() >> 4, net.minecraft.world.level.chunk.ChunkStatus.EMPTY);
+ if (!lowest.getStatus().isOrAfter(net.minecraft.world.level.chunk.ChunkStatus.FULL)
+ && (lowest.getBelowZeroRetrogen() == null || !lowest.getBelowZeroRetrogen().targetStatus().isOrAfter(net.minecraft.world.level.chunk.ChunkStatus.SPAWN))) {
+ // why would we generate the chunk?
+ return false;
+ }
+ if (!worldborder.isWithinBounds(pos) || this.level.paperConfig().environment.netherCeilingVoidDamageHeight.test(v -> pos.getY() >= v)) { // Paper - don't teleport into void damage
+ return false;
+ }
+ return lowest.getBlockState(pos).hasProperty(BlockStateProperties.HORIZONTAL_AXIS);
+ },
+ blockposition, i, Double.MAX_VALUE, PoiManager.Occupancy.ANY, true, records
+ );
+
+ // this gets us most of the way there, but we bias towards lower y values.
+ PoiRecord lowestYRecord = null;
+ for (PoiRecord record : records) {
+ if (lowestYRecord == null) {
+ lowestYRecord = record;
+ } else if (lowestYRecord.getPos().getY() > record.getPos().getY()) {
+ lowestYRecord = record;
+ }
+ }
+ // now we're done
+ optional = Optional.ofNullable(lowestYRecord);
+ // Paper end - optimise portals
return optional.map((villageplacerecord) -> {
BlockPos blockposition1 = villageplacerecord.getPos();