| 1 | package net.minecraft.world.level.entity;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.Long2ObjectFunction;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 6 | import it.unimi.dsi.fastutil.longs.LongAVLTreeSet;
|
| 7 | import it.unimi.dsi.fastutil.longs.LongIterator;
|
| 8 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 9 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 10 | import it.unimi.dsi.fastutil.longs.LongSortedSet;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Spliterators;
|
| 13 | import java.util.PrimitiveIterator.OfLong;
|
| 14 | import java.util.stream.LongStream;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import java.util.stream.StreamSupport;
|
| 17 | import net.minecraft.core.SectionPos;
|
| 18 | import net.minecraft.util.AbortableIterationConsumer;
|
| 19 | import net.minecraft.util.VisibleForDebug;
|
| 20 | import net.minecraft.world.level.ChunkPos;
|
| 21 | import net.minecraft.world.phys.AABB;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | public class EntitySectionStorage<T extends EntityAccess> {
|
| 25 | public static final int CHONKY_ENTITY_SEARCH_GRACE = 2;
|
| 26 | public static final int MAX_NON_CHONKY_ENTITY_SIZE = 4;
|
| 27 | private final Class<T> entityClass;
|
| 28 | private final Long2ObjectFunction<Visibility> intialSectionVisibility;
|
| 29 | private final Long2ObjectMap<EntitySection<T>> sections = new Long2ObjectOpenHashMap<>();
|
| 30 | private final LongSortedSet sectionIds = new LongAVLTreeSet();
|
| 31 |
|
| 32 | public EntitySectionStorage(final Class<T> entityClass, final Long2ObjectFunction<Visibility> intialSectionVisibility) {
|
| 33 | this.entityClass = entityClass;
|
| 34 | this.intialSectionVisibility = intialSectionVisibility;
|
| 35 | }
|
| 36 |
|
| 37 | public void forEachAccessibleNonEmptySection(final AABB bb, final AbortableIterationConsumer<EntitySection<T>> output) {
|
| 38 | int xMin = SectionPos.posToSectionCoord(bb.minX - 2.0);
|
| 39 | int yMin = SectionPos.posToSectionCoord(bb.minY - 4.0);
|
| 40 | int zMin = SectionPos.posToSectionCoord(bb.minZ - 2.0);
|
| 41 | int xMax = SectionPos.posToSectionCoord(bb.maxX + 2.0);
|
| 42 | int yMax = SectionPos.posToSectionCoord(bb.maxY + 0.0);
|
| 43 | int zMax = SectionPos.posToSectionCoord(bb.maxZ + 2.0);
|
| 44 |
|
| 45 | for (int x = xMin; x <= xMax; x++) {
|
| 46 | long lowestAbsoluteSectionKey = SectionPos.asLong(x, 0, 0);
|
| 47 | long highestAbsoluteSectionKey = SectionPos.asLong(x, -1, -1);
|
| 48 | LongIterator it = this.sectionIds.subSet(lowestAbsoluteSectionKey, highestAbsoluteSectionKey + 1L).iterator();
|
| 49 |
|
| 50 | while (it.hasNext()) {
|
| 51 | long sectionKey = it.nextLong();
|
| 52 | int y = SectionPos.y(sectionKey);
|
| 53 | int z = SectionPos.z(sectionKey);
|
| 54 | if (y >= yMin && y <= yMax && z >= zMin && z <= zMax) {
|
| 55 | EntitySection<T> entitySection = this.sections.get(sectionKey);
|
| 56 | if (entitySection != null && !entitySection.isEmpty() && entitySection.getStatus().isAccessible() && output.accept(entitySection).shouldAbort()) {
|
| 57 | return;
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | public LongStream getExistingSectionPositionsInChunk(final long chunkKey) {
|
| 65 | int x = ChunkPos.getX(chunkKey);
|
| 66 | int z = ChunkPos.getZ(chunkKey);
|
| 67 | LongSortedSet chunkSections = this.getChunkSections(x, z);
|
| 68 | if (chunkSections.isEmpty()) {
|
| 69 | return LongStream.empty();
|
| 70 | }
|
| 71 |
|
| 72 | OfLong iterator = chunkSections.iterator();
|
| 73 | return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(iterator, 1301), false);
|
| 74 | }
|
| 75 |
|
| 76 | private LongSortedSet getChunkSections(final int x, final int z) {
|
| 77 | long lowestAbsoluteSectionKey = SectionPos.asLong(x, 0, z);
|
| 78 | long highestAbsoluteSectionKey = SectionPos.asLong(x, -1, z);
|
| 79 | return this.sectionIds.subSet(lowestAbsoluteSectionKey, highestAbsoluteSectionKey + 1L);
|
| 80 | }
|
| 81 |
|
| 82 | public Stream<EntitySection<T>> getExistingSectionsInChunk(final long chunkKey) {
|
| 83 | return this.getExistingSectionPositionsInChunk(chunkKey).mapToObj(this.sections::get).filter(Objects::nonNull);
|
| 84 | }
|
| 85 |
|
| 86 | private static long getChunkKeyFromSectionKey(final long sectionPos) {
|
| 87 | return ChunkPos.pack(SectionPos.x(sectionPos), SectionPos.z(sectionPos));
|
| 88 | }
|
| 89 |
|
| 90 | public EntitySection<T> getOrCreateSection(final long key) {
|
| 91 | return this.sections.computeIfAbsent(key, this::createSection);
|
| 92 | }
|
| 93 |
|
| 94 | public @Nullable EntitySection<T> getSection(final long key) {
|
| 95 | return this.sections.get(key);
|
| 96 | }
|
| 97 |
|
| 98 | private EntitySection<T> createSection(final long sectionPos) {
|
| 99 | long chunkPos = getChunkKeyFromSectionKey(sectionPos);
|
| 100 | Visibility chunkStatus = this.intialSectionVisibility.get(chunkPos);
|
| 101 | this.sectionIds.add(sectionPos);
|
| 102 | return new EntitySection<>(this.entityClass, chunkStatus);
|
| 103 | }
|
| 104 |
|
| 105 | public LongSet getAllChunksWithExistingSections() {
|
| 106 | LongSet chunks = new LongOpenHashSet();
|
| 107 | this.sections.keySet().forEach(sectionKey -> chunks.add(getChunkKeyFromSectionKey(sectionKey)));
|
| 108 | return chunks;
|
| 109 | }
|
| 110 |
|
| 111 | public void getEntities(final AABB bb, final AbortableIterationConsumer<T> output) {
|
| 112 | this.forEachAccessibleNonEmptySection(bb, section -> section.getEntities(bb, output));
|
| 113 | }
|
| 114 |
|
| 115 | public <U extends T> void getEntities(final EntityTypeTest<T, U> type, final AABB bb, final AbortableIterationConsumer<U> consumer) {
|
| 116 | this.forEachAccessibleNonEmptySection(bb, section -> section.getEntities(type, bb, consumer));
|
| 117 | }
|
| 118 |
|
| 119 | public void remove(final long sectionKey) {
|
| 120 | this.sections.remove(sectionKey);
|
| 121 | this.sectionIds.remove(sectionKey);
|
| 122 | }
|
| 123 |
|
| 124 | @VisibleForDebug
|
| 125 | public int count() {
|
| 126 | return this.sectionIds.size();
|
| 127 | }
|
| 128 | }
|
| 129 |
|