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);
});
}
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);
});
}
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);
});
}
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();
}
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);
}
}
}
Aggregations