Skip to content

Commit

Permalink
Fix Chunk#entities returning all entities of the world
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Nov 14, 2024
1 parent 9b83c93 commit 89310cd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ public static void validateStreamArgs(final Vector3i min, final Vector3i max, fi
) {
if (chunk.getLevel() instanceof ServerLevel) {
return ((PersistentEntitySectionManagerAccessor<Entity>) ((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<Entity>) ((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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -418,12 +422,12 @@ public Optional<Entity> entity(final UUID uuid) {
.filter(x -> x.chunkPosition().equals(this.chunkPos));
}

@SuppressWarnings("unchecked")
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public Collection<? extends Entity> entities() {
return (Collection<? extends Entity>) (Object) StreamSupport.stream(
((LevelAccessor) this.level).invoker$getEntities().getAll().spliterator(), false)
.collect(Collectors.toList());
final PersistentEntitySectionManager<net.minecraft.world.entity.Entity> entityManager = ((ServerLevelAccessor) this.level).accessor$getEntityManager();
final EntitySectionStorage<net.minecraft.world.entity.Entity> entitySectionStorage = ((PersistentEntitySectionManagerAccessor<net.minecraft.world.entity.Entity>) entityManager).accessor$sectionStorage();
return (Collection) entitySectionStorage.getExistingSectionsInChunk(this.chunkPos.toLong()).flatMap(EntitySection::getEntities).toList();
}

@SuppressWarnings({"rawtypes", "unchecked"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -262,7 +261,7 @@ public static void activateEntities(final ServerLevel world) {
private static void activateChunkEntities(final ServerPlayer player, final LevelChunk chunk) {
final PersistentEntitySectionManager<Entity> entityManager = ((ServerLevelAccessor) chunk.getLevel()).accessor$getEntityManager();
final EntitySectionStorage<Entity> entitySectionStorage = ((PersistentEntitySectionManagerAccessor<Entity>) 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;
}
Expand Down

0 comments on commit 89310cd

Please sign in to comment.