Search in sources :

Example 1 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class LevelChunkMixin_API method blockStateStream.

@Override
public VolumeStream<WorldChunk, BlockState> blockStateStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"), Objects.requireNonNull(options, "options"));
    final boolean shouldCarbonCopy = options.carbonCopy();
    final Vector3i size = max.sub(min).add(1, 1, 1);
    @MonotonicNonNull final ArrayMutableBlockBuffer backingVolume;
    if (shouldCarbonCopy) {
        backingVolume = new ArrayMutableBlockBuffer(min, size);
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<WorldChunk, BlockState, net.minecraft.world.level.block.state.BlockState, ChunkAccess, BlockPos>generateStream(options, // Ref
    (WorldChunk) this, (LevelChunk) (Object) this, // Entity Accessor
    VolumeStreamUtils.getBlockStatesForSections(min, max), // IdentityFunction
    (pos, blockState) -> {
        if (shouldCarbonCopy) {
            backingVolume.setBlock(pos, blockState);
        }
    }, // Biome by block position
    (key, biome) -> key, // Filtered Position Entity Accessor
    (blockPos, world) -> {
        final net.minecraft.world.level.block.state.BlockState tileEntity = shouldCarbonCopy ? backingVolume.getBlock(blockPos) : ((LevelReader) world).getBlockState(blockPos);
        return new Tuple<>(blockPos, tileEntity);
    });
}
Also used : ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) BlockState(org.spongepowered.api.block.BlockState) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) BlockPos(net.minecraft.core.BlockPos) Tuple(net.minecraft.util.Tuple)

Example 2 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class LevelChunkMixin_API method entityStream.

@Override
public VolumeStream<WorldChunk, Entity> entityStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"), Objects.requireNonNull(options, "options"));
    final boolean shouldCarbonCopy = options.carbonCopy();
    final Vector3i size = max.sub(min).add(1, 1, 1);
    @MonotonicNonNull final ObjectArrayMutableEntityBuffer backingVolume;
    if (shouldCarbonCopy) {
        backingVolume = new ObjectArrayMutableEntityBuffer(min, size);
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<WorldChunk, Entity, net.minecraft.world.entity.Entity, LevelChunk, UUID>generateStream(options, this, (LevelChunk) (Object) this, // Entity Accessor
    (chunk) -> VolumeStreamUtils.getEntitiesFromChunk(min, max, chunk), // Entity Identity Function
    VolumeStreamUtils.getOrCloneEntityWithVolume(shouldCarbonCopy, backingVolume, this.level), (key, entity) -> entity.getUUID(), // Filtered Position Entity Accessor
    (entityUuid, chunk) -> {
        final net.minecraft.world.entity.@Nullable Entity entity = shouldCarbonCopy ? (net.minecraft.world.entity.Entity) backingVolume.entity(entityUuid).orElse(null) : (net.minecraft.world.entity.Entity) chunk.world().entity(entityUuid).orElse(null);
        if (entity == null) {
            return null;
        }
        return new Tuple<>(entity.blockPosition(), entity);
    });
}
Also used : BlockEntity(org.spongepowered.api.block.entity.BlockEntity) Entity(org.spongepowered.api.entity.Entity) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) ObjectArrayMutableEntityBuffer(org.spongepowered.common.world.volume.buffer.entity.ObjectArrayMutableEntityBuffer) UUID(java.util.UUID) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Tuple(net.minecraft.util.Tuple)

Example 3 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class LevelChunkMixin_API method biomeStream.

@Override
public VolumeStream<WorldChunk, Biome> biomeStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"), Objects.requireNonNull(options, "options"));
    final boolean shouldCarbonCopy = options.carbonCopy();
    final Vector3i size = max.sub(min).add(1, 1, 1);
    @MonotonicNonNull final ObjectArrayMutableBiomeBuffer backingVolume;
    if (shouldCarbonCopy) {
        final Registry<net.minecraft.world.level.biome.Biome> biomeRegistry = this.level.registryAccess().registry(Registry.BIOME_REGISTRY).map(wr -> ((Registry<net.minecraft.world.level.biome.Biome>) wr)).orElse(BuiltinRegistries.BIOME);
        backingVolume = new ObjectArrayMutableBiomeBuffer(min, size, VolumeStreamUtils.nativeToSpongeRegistry(biomeRegistry));
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<WorldChunk, Biome, net.minecraft.world.level.biome.Biome, ChunkAccess, BlockPos>generateStream(options, // Ref
    (WorldChunk) this, (LevelChunk) (Object) this, // Entity Accessor
    VolumeStreamUtils.getBiomesForChunkByPos((LevelReader) (Object) this, min, max), // IdentityFunction
    (pos, biome) -> {
        if (shouldCarbonCopy) {
            backingVolume.setBiome(pos, biome);
        }
    }, // Biome by block position
    (key, biome) -> key, // Filtered Position Entity Accessor
    (blockPos, world) -> {
        final net.minecraft.world.level.biome.Biome biome = shouldCarbonCopy ? backingVolume.getNativeBiome(blockPos.getX(), blockPos.getY(), blockPos.getZ()) : ((LevelReader) world.world()).getBiome(blockPos);
        return new Tuple<>(blockPos, biome);
    });
}
Also used : Arrays(java.util.Arrays) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Registry(net.minecraft.core.Registry) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) Biome(org.spongepowered.api.world.biome.Biome) Mixin(org.spongepowered.asm.mixin.Mixin) ChunkBiomeContainerAccessor(org.spongepowered.common.accessor.world.level.chunk.ChunkBiomeContainerAccessor) Map(java.util.Map) DifficultyInstance(net.minecraft.world.DifficultyInstance) BuiltinRegistries(net.minecraft.data.BuiltinRegistries) ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) Predicate(java.util.function.Predicate) Collection(java.util.Collection) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) Remap(org.spongepowered.asm.mixin.Interface.Remap) UUID(java.util.UUID) LevelReader(net.minecraft.world.level.LevelReader) Final(org.spongepowered.asm.mixin.Final) Collectors(java.util.stream.Collectors) ObjectArrayMutableEntityBuffer(org.spongepowered.common.world.volume.buffer.entity.ObjectArrayMutableEntityBuffer) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) EntityUtil(org.spongepowered.common.entity.EntityUtil) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) BlockPos(net.minecraft.core.BlockPos) ChunkBiomeContainer(net.minecraft.world.level.chunk.ChunkBiomeContainer) VolumeStreamUtils(org.spongepowered.common.world.volume.VolumeStreamUtils) HeightTypes(org.spongepowered.api.world.HeightTypes) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Level(net.minecraft.world.level.Level) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) NonNull(org.checkerframework.checker.nullness.qual.NonNull) DataContainer(org.spongepowered.api.data.persistence.DataContainer) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) AABB(org.spongepowered.api.util.AABB) ClassInstanceMultiMap(net.minecraft.util.ClassInstanceMultiMap) ArrayList(java.util.ArrayList) BlockChangeFlag(org.spongepowered.api.world.BlockChangeFlag) ImmutableList(com.google.common.collect.ImmutableList) ObjectArrayMutableBlockEntityBuffer(org.spongepowered.common.world.volume.buffer.blockentity.ObjectArrayMutableBlockEntityBuffer) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Ticks(org.spongepowered.api.util.Ticks) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Tuple(net.minecraft.util.Tuple) Entity(org.spongepowered.api.entity.Entity) WorldLike(org.spongepowered.api.world.WorldLike) ChunkPos(net.minecraft.world.level.ChunkPos) Implements(org.spongepowered.asm.mixin.Implements) SpongeChunkLayout(org.spongepowered.common.world.storage.SpongeChunkLayout) Vector3d(org.spongepowered.math.vector.Vector3d) Heightmap(net.minecraft.world.level.levelgen.Heightmap) VecHelper(org.spongepowered.common.util.VecHelper) EntityType(org.spongepowered.api.entity.EntityType) SpongeTicks(org.spongepowered.common.util.SpongeTicks) Collections(java.util.Collections) Interface(org.spongepowered.asm.mixin.Interface) Intrinsic(org.spongepowered.asm.mixin.Intrinsic) Vector3i(org.spongepowered.math.vector.Vector3i) ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) LevelReader(net.minecraft.world.level.LevelReader) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) Biome(org.spongepowered.api.world.biome.Biome) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) BlockPos(net.minecraft.core.BlockPos) Tuple(net.minecraft.util.Tuple)

Example 4 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class LevelMixin_API method loadedChunks.

@Override
public Iterable<WorldChunk> loadedChunks() {
    final ChunkSource chunkProvider = ((LevelAccessor) this).getChunkSource();
    if (chunkProvider instanceof ServerChunkCache) {
        final ChunkMapAccessor chunkManager = (ChunkMapAccessor) ((ServerChunkCache) chunkProvider).chunkMap;
        final List<WorldChunk> chunks = new ArrayList<>();
        chunkManager.invoker$getChunks().forEach(holder -> {
            final WorldChunk chunk = (WorldChunk) holder.getTickingChunk();
            if (chunk != null) {
                chunks.add(chunk);
            }
        });
        return chunks;
    }
    return Collections.emptyList();
}
Also used : ChunkSource(net.minecraft.world.level.chunk.ChunkSource) LevelAccessor(net.minecraft.world.level.LevelAccessor) ChunkMapAccessor(org.spongepowered.common.accessor.server.level.ChunkMapAccessor) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) ArrayList(java.util.ArrayList) ServerChunkCache(net.minecraft.server.level.ServerChunkCache)

Example 5 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class ChunkMapMixin method impl$onLoad.

@Redirect(method = "*", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/chunk/LevelChunk;setLoaded(Z)V"), slice = @Slice(from = @At(value = "INVOKE", remap = false, target = "Lit/unimi/dsi/fastutil/longs/LongSet;add(J)Z"), to = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;addAllPendingBlockEntities(Ljava/util/Collection;)V")))
private void impl$onLoad(final LevelChunk levelChunk, final boolean loaded) {
    levelChunk.setLoaded(true);
    final Vector3i chunkPos = new Vector3i(levelChunk.getPos().x, 0, levelChunk.getPos().z);
    if (ShouldFire.CHUNK_EVENT_LOAD) {
        final ChunkEvent.Load loadEvent = SpongeEventFactory.createChunkEventLoad(PhaseTracker.getInstance().currentCause(), ((WorldChunk) levelChunk), chunkPos, (ResourceKey) (Object) this.level.dimension().location());
        SpongeCommon.post(loadEvent);
    }
    for (final Direction dir : Constants.Chunk.CARDINAL_DIRECTIONS) {
        final Vector3i neighborPos = chunkPos.add(dir.asBlockOffset());
        ChunkAccess neighbor = this.level.getChunk(neighborPos.x(), neighborPos.z(), ChunkStatus.EMPTY, false);
        if (neighbor instanceof ImposterProtoChunk) {
            neighbor = ((ImposterProtoChunk) neighbor).getWrapped();
        }
        if (neighbor instanceof LevelChunk) {
            final int index = DirectionUtil.directionToIndex(dir);
            final int oppositeIndex = DirectionUtil.directionToIndex(dir.opposite());
            ((LevelChunkBridge) levelChunk).bridge$setNeighborChunk(index, (LevelChunk) neighbor);
            ((LevelChunkBridge) neighbor).bridge$setNeighborChunk(oppositeIndex, levelChunk);
        }
    }
}
Also used : ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) ImposterProtoChunk(net.minecraft.world.level.chunk.ImposterProtoChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) ChunkEvent(org.spongepowered.api.event.world.chunk.ChunkEvent) Direction(org.spongepowered.api.util.Direction) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

WorldChunk (org.spongepowered.api.world.chunk.WorldChunk)8 Vector3i (org.spongepowered.math.vector.Vector3i)6 Tuple (net.minecraft.util.Tuple)4 ChunkAccess (net.minecraft.world.level.chunk.ChunkAccess)4 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)4 BlockPos (net.minecraft.core.BlockPos)3 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)3 Entity (org.spongepowered.api.entity.Entity)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 UUID (java.util.UUID)2 BlockState (org.spongepowered.api.block.BlockState)2 AABB (org.spongepowered.api.util.AABB)2 ArrayMutableBlockBuffer (org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer)2 ObjectArrayMutableEntityBuffer (org.spongepowered.common.world.volume.buffer.entity.ObjectArrayMutableEntityBuffer)2 ImmutableList (com.google.common.collect.ImmutableList)1 Inject (com.google.inject.Inject)1 Arrays (java.util.Arrays)1