Search in sources :

Example 1 with EntityTrackerSection

use of me.jellysquid.mods.lithium.common.entity.tracker.EntityTrackerSection in project lithium-fabric by CaffeineMC.

the class NearbyEntityListener method forEachChunkInRangeChange.

/**
 * Calls the callbacks for the chunk coordinates that this listener is leaving and entering
 */
default void forEachChunkInRangeChange(SectionedEntityCache<? extends EntityLike> entityCache, ChunkSectionPos prevCenterPos, ChunkSectionPos newCenterPos) {
    Range6Int chunkRange = this.getChunkRange();
    if (chunkRange == EMPTY_RANGE) {
        return;
    }
    BlockPos.Mutable pos = new BlockPos.Mutable();
    BlockBox after = newCenterPos == null ? null : new BlockBox(newCenterPos.getX() - chunkRange.negativeX(), newCenterPos.getY() - chunkRange.negativeY(), newCenterPos.getZ() - chunkRange.negativeZ(), newCenterPos.getX() + chunkRange.positiveX(), newCenterPos.getY() + chunkRange.positiveY(), newCenterPos.getZ() + chunkRange.positiveZ());
    BlockBox before = prevCenterPos == null ? null : new BlockBox(prevCenterPos.getX() - chunkRange.negativeX(), prevCenterPos.getY() - chunkRange.negativeY(), prevCenterPos.getZ() - chunkRange.negativeZ(), prevCenterPos.getX() + chunkRange.positiveX(), prevCenterPos.getY() + chunkRange.positiveY(), prevCenterPos.getZ() + chunkRange.positiveZ());
    if (before != null) {
        for (int x = before.getMinX(); x <= before.getMaxX(); x++) {
            for (int y = before.getMinY(); y <= before.getMaxY(); y++) {
                for (int z = before.getMinZ(); z <= before.getMaxZ(); z++) {
                    if (after == null || !after.contains(pos.set(x, y, z))) {
                        long sectionPos = ChunkSectionPos.asLong(x, y, z);
                        EntityTrackingSection<? extends EntityLike> trackingSection = entityCache.getTrackingSection(sectionPos);
                        ((EntityTrackerSection) trackingSection).removeListener(entityCache, this);
                        if (trackingSection.isEmpty()) {
                            entityCache.removeSection(sectionPos);
                        }
                    }
                }
            }
        }
    }
    if (after != null) {
        for (int x = after.getMinX(); x <= after.getMaxX(); x++) {
            for (int y = after.getMinY(); y <= after.getMaxY(); y++) {
                for (int z = after.getMinZ(); z <= after.getMaxZ(); z++) {
                    if (before == null || !before.contains(pos.set(x, y, z))) {
                        ((EntityTrackerSection) entityCache.getTrackingSection(ChunkSectionPos.asLong(x, y, z))).addListener(this);
                    }
                }
            }
        }
    }
}
Also used : Range6Int(me.jellysquid.mods.lithium.common.util.tuples.Range6Int) BlockBox(net.minecraft.util.math.BlockBox) EntityTrackerSection(me.jellysquid.mods.lithium.common.entity.tracker.EntityTrackerSection) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with EntityTrackerSection

use of me.jellysquid.mods.lithium.common.entity.tracker.EntityTrackerSection in project lithium-fabric by CaffeineMC.

the class SectionedEntityMovementTracker method register.

public void register(ServerWorld world) {
    assert world == this.trackedWorldSections.world();
    if (this.timesRegistered == 0) {
        // noinspection unchecked
        SectionedEntityCache<E> cache = ((ServerEntityManagerAccessor<E>) ((ServerWorldAccessor) world).getEntityManager()).getCache();
        this.sectionChangeCounters = new ArrayList<>();
        WorldSectionBox trackedSections = this.trackedWorldSections;
        int size = trackedSections.numSections();
        assert size > 0;
        this.sortedSections = new ArrayList<>(size);
        this.sectionVisible = new boolean[size];
        // WorldSectionBox upper coordinates are exclusive
        for (int x = trackedSections.chunkX1(); x < trackedSections.chunkX2(); x++) {
            for (int z = trackedSections.chunkZ1(); z < trackedSections.chunkZ2(); z++) {
                for (int y = trackedSections.chunkY1(); y < trackedSections.chunkY2(); y++) {
                    EntityTrackingSection<E> section = cache.getTrackingSection(ChunkSectionPos.asLong(x, y, z));
                    EntityTrackerSection sectionAccess = (EntityTrackerSection) section;
                    this.sortedSections.add(section);
                    sectionAccess.addListener(this);
                }
            }
        }
        this.setChanged(world.getTime());
    }
    this.timesRegistered++;
}
Also used : ServerEntityManagerAccessor(me.jellysquid.mods.lithium.mixin.ai.nearby_entity_tracking.ServerEntityManagerAccessor) WorldSectionBox(me.jellysquid.mods.lithium.common.util.tuples.WorldSectionBox) EntityTrackerSection(me.jellysquid.mods.lithium.common.entity.tracker.EntityTrackerSection)

Example 3 with EntityTrackerSection

use of me.jellysquid.mods.lithium.common.entity.tracker.EntityTrackerSection in project lithium-fabric by CaffeineMC.

the class SectionedEntityMovementTracker method unRegister.

public void unRegister(ServerWorld world) {
    assert world == this.trackedWorldSections.world();
    if (--this.timesRegistered > 0) {
        return;
    }
    assert this.timesRegistered == 0;
    // noinspection unchecked
    SectionedEntityCache<E> cache = ((ServerEntityManagerAccessor<E>) ((ServerWorldAccessor) world).getEntityManager()).getCache();
    MovementTrackerCache storage = (MovementTrackerCache) cache;
    storage.remove(this);
    ArrayList<EntityTrackingSection<E>> sections = this.sortedSections;
    for (int i = sections.size() - 1; i >= 0; i--) {
        EntityTrackingSection<E> section = sections.get(i);
        EntityTrackerSection sectionAccess = (EntityTrackerSection) section;
        sectionAccess.removeListener(cache, this);
    }
    this.setChanged(world.getTime());
}
Also used : ServerEntityManagerAccessor(me.jellysquid.mods.lithium.mixin.ai.nearby_entity_tracking.ServerEntityManagerAccessor) EntityTrackingSection(net.minecraft.world.entity.EntityTrackingSection) EntityTrackerSection(me.jellysquid.mods.lithium.common.entity.tracker.EntityTrackerSection)

Aggregations

EntityTrackerSection (me.jellysquid.mods.lithium.common.entity.tracker.EntityTrackerSection)3 ServerEntityManagerAccessor (me.jellysquid.mods.lithium.mixin.ai.nearby_entity_tracking.ServerEntityManagerAccessor)2 Range6Int (me.jellysquid.mods.lithium.common.util.tuples.Range6Int)1 WorldSectionBox (me.jellysquid.mods.lithium.common.util.tuples.WorldSectionBox)1 BlockBox (net.minecraft.util.math.BlockBox)1 BlockPos (net.minecraft.util.math.BlockPos)1 EntityTrackingSection (net.minecraft.world.entity.EntityTrackingSection)1