From 89310cdb22156c335b90a1823410c1e29c89fc81 Mon Sep 17 00:00:00 2001 From: aromaa Date: Mon, 11 Nov 2024 21:34:34 +0200 Subject: [PATCH] Fix Chunk#entities returning all entities of the world --- .../common/world/volume/VolumeStreamUtils.java | 4 ++-- .../world/level/chunk/LevelChunkMixin_API.java | 14 +++++++++----- .../entityactivation/EntityActivationRange.java | 3 +-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/spongepowered/common/world/volume/VolumeStreamUtils.java b/src/main/java/org/spongepowered/common/world/volume/VolumeStreamUtils.java index b6f2f0b20dd..acd61727b3a 100644 --- a/src/main/java/org/spongepowered/common/world/volume/VolumeStreamUtils.java +++ b/src/main/java/org/spongepowered/common/world/volume/VolumeStreamUtils.java @@ -219,14 +219,14 @@ public static void validateStreamArgs(final Vector3i min, final Vector3i max, fi ) { if (chunk.getLevel() instanceof ServerLevel) { return ((PersistentEntitySectionManagerAccessor) ((ServerLevelAccessor) chunk.getLevel()).accessor$getEntityManager()).accessor$sectionStorage() - .getExistingSectionsInChunk(SectionPos.of(chunk.getPos(), 0).asLong()) + .getExistingSectionsInChunk(chunk.getPos().toLong()) .flatMap(EntitySection::getEntities) .filter(entity -> VecHelper.inBounds(entity.blockPosition(), min, max)) .map(entity -> new AbstractMap.SimpleEntry<>(entity.blockPosition(), entity)); } else if (Sponge.isClientAvailable() && chunk.getLevel() instanceof ClientLevel) { return ((TransientEntitySectionManagerAccessor) ((ClientLevelAccessor) chunk.getLevel()).accessor$getEntityStorage()) .accessor$sectionStorage() - .getExistingSectionsInChunk(SectionPos.of(chunk.getPos(), 0).asLong()) + .getExistingSectionsInChunk(chunk.getPos().toLong()) .flatMap(EntitySection::getEntities) .filter(entity -> VecHelper.inBounds(entity.blockPosition(), min, max)) .map(entity -> new AbstractMap.SimpleEntry<>(entity.blockPosition(), entity)); diff --git a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/chunk/LevelChunkMixin_API.java b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/chunk/LevelChunkMixin_API.java index 29080211aac..e8bb8cbfce8 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/chunk/LevelChunkMixin_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/chunk/LevelChunkMixin_API.java @@ -39,6 +39,9 @@ import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.chunk.LevelChunkSection; import net.minecraft.world.level.chunk.UpgradeData; +import net.minecraft.world.level.entity.EntitySection; +import net.minecraft.world.level.entity.EntitySectionStorage; +import net.minecraft.world.level.entity.PersistentEntitySectionManager; import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.level.levelgen.blending.BlendingData; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -71,7 +74,9 @@ import org.spongepowered.asm.mixin.Intrinsic; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.common.accessor.server.level.ServerLevelAccessor; import org.spongepowered.common.accessor.world.level.LevelAccessor; +import org.spongepowered.common.accessor.world.level.entity.PersistentEntitySectionManagerAccessor; import org.spongepowered.common.bridge.world.level.LevelBridge; import org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge; import org.spongepowered.common.data.holder.SpongeServerLocationBaseDataHolder; @@ -100,7 +105,6 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; -import java.util.stream.StreamSupport; @Mixin(net.minecraft.world.level.chunk.LevelChunk.class) @Implements(@Interface(iface = WorldChunk.class, prefix = "worldChunk$", remap = Interface.Remap.NONE)) @@ -418,12 +422,12 @@ public Optional entity(final UUID uuid) { .filter(x -> x.chunkPosition().equals(this.chunkPos)); } - @SuppressWarnings("unchecked") + @SuppressWarnings({"rawtypes", "unchecked"}) @Override public Collection entities() { - return (Collection) (Object) StreamSupport.stream( - ((LevelAccessor) this.level).invoker$getEntities().getAll().spliterator(), false) - .collect(Collectors.toList()); + final PersistentEntitySectionManager entityManager = ((ServerLevelAccessor) this.level).accessor$getEntityManager(); + final EntitySectionStorage entitySectionStorage = ((PersistentEntitySectionManagerAccessor) entityManager).accessor$sectionStorage(); + return (Collection) entitySectionStorage.getExistingSectionsInChunk(this.chunkPos.toLong()).flatMap(EntitySection::getEntities).toList(); } @SuppressWarnings({"rawtypes", "unchecked"}) diff --git a/src/mixins/java/org/spongepowered/common/mixin/plugin/entityactivation/EntityActivationRange.java b/src/mixins/java/org/spongepowered/common/mixin/plugin/entityactivation/EntityActivationRange.java index 0e1337201d6..6f123326cde 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/plugin/entityactivation/EntityActivationRange.java +++ b/src/mixins/java/org/spongepowered/common/mixin/plugin/entityactivation/EntityActivationRange.java @@ -25,7 +25,6 @@ package org.spongepowered.common.mixin.plugin.entityactivation; import com.google.common.collect.ImmutableMap; -import net.minecraft.core.SectionPos; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; @@ -262,7 +261,7 @@ public static void activateEntities(final ServerLevel world) { private static void activateChunkEntities(final ServerPlayer player, final LevelChunk chunk) { final PersistentEntitySectionManager entityManager = ((ServerLevelAccessor) chunk.getLevel()).accessor$getEntityManager(); final EntitySectionStorage entitySectionStorage = ((PersistentEntitySectionManagerAccessor) entityManager).accessor$sectionStorage(); - entitySectionStorage.getExistingSectionsInChunk(SectionPos.of(chunk.getPos(), 0).asLong()).flatMap(EntitySection::getEntities).forEach(entity -> { + entitySectionStorage.getExistingSectionsInChunk(chunk.getPos().toLong()).flatMap(EntitySection::getEntities).forEach(entity -> { if (!entity.chunkPosition().equals(chunk.getPos())) { return; }