Search in sources :

Example 1 with Biome

use of org.spongepowered.api.world.biome.Biome in project SpongeCommon by SpongePowered.

the class SchematicUpdater2_to_3 method update.

@Override
public DataView update(final DataView content) {
    // Move BlockData, BlockPalette, BlockEntities -> Blocks.Data, Blocks.Palette, and Blocks.BlockEntities
    content.getView(Constants.Sponge.Schematic.Versions.V2_BLOCK_PALETTE).ifPresent(dataView -> {
        content.remove(Constants.Sponge.Schematic.Versions.V2_BLOCK_PALETTE);
        final byte[] blockData = (byte[]) content.get(Constants.Sponge.Schematic.BLOCK_DATA).orElseThrow(() -> new InvalidDataException("Missing BlockData for Schematic"));
        content.remove(Constants.Sponge.Schematic.Versions.V2_BLOCK_DATA);
        final List<DataView> blockEntities = content.getViewList(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER).orElse(Collections.emptyList());
        content.remove(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER);
        final DataView blockContainer = content.createView(Constants.Sponge.Schematic.BLOCK_CONTAINER);
        blockContainer.set(Constants.Sponge.Schematic.BLOCK_DATA, blockData);
        blockContainer.set(Constants.Sponge.Schematic.BLOCK_PALETTE, dataView);
        blockContainer.set(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER, blockEntities);
    });
    // Move BiomeData, BiomePalette -> Biomes.Data and Biomes.Palette
    content.get(Constants.Sponge.Schematic.Versions.V2_BIOME_DATA).ifPresent(biomeData -> {
        content.remove(Constants.Sponge.Schematic.Versions.V2_BIOME_DATA);
        // But first, convert from a 2D array to a 3D array, which basically means almost fully deserializing
        // the entirety of the biome into a new buffer.
        final int[] offset = (int[]) content.get(Constants.Sponge.Schematic.OFFSET).orElse(new int[3]);
        if (offset.length != 3) {
            throw new InvalidDataException("Schematic offset was not of length 3");
        }
        final int xOffset = offset[0];
        final int yOffset = offset[1];
        final int zOffset = offset[2];
        final DataView palette = content.getView(Constants.Sponge.Schematic.Versions.V2_BIOME_PALETTE).orElseThrow(() -> new InvalidDataException("Missing Biome Palette for schematic"));
        final int width = content.getShort(Constants.Sponge.Schematic.WIDTH).orElseThrow(() -> new InvalidDataException("Missing value for: " + Constants.Sponge.Schematic.WIDTH));
        final int height = content.getShort(Constants.Sponge.Schematic.HEIGHT).orElseThrow(() -> new InvalidDataException("Missing value for: " + Constants.Sponge.Schematic.HEIGHT));
        final int length = content.getShort(Constants.Sponge.Schematic.LENGTH).orElseThrow(() -> new InvalidDataException("Missing value for: " + Constants.Sponge.Schematic.LENGTH));
        final Set<DataQuery> biomeKeys = palette.keys(false);
        final Registry<Biome> biomeRegistry = VolumeStreamUtils.nativeToSpongeRegistry(BuiltinRegistries.BIOME);
        final MutableBimapPalette<Biome, Biome> biomePalette = new MutableBimapPalette<>(PaletteTypes.BIOME_PALETTE.get(), biomeRegistry, RegistryTypes.BIOME, biomeKeys.size());
        final ByteArrayMutableBiomeBuffer biomeBuffer = new ByteArrayMutableBiomeBuffer(biomePalette, new Vector3i(-xOffset, -yOffset, -zOffset), new Vector3i(width, height, length));
        final DataView biomeView = content.createView(Constants.Sponge.Schematic.BIOME_CONTAINER);
        biomeView.set(Constants.Sponge.Schematic.BLOCK_PALETTE, palette);
        final byte[] biomes = (byte[]) biomeData;
        int biomeIndex = 0;
        int biomeJ = 0;
        int bVal = 0;
        int varIntLength = 0;
        final int yMin = biomeBuffer.min().y();
        final int yMax = biomeBuffer.max().y();
        while (biomeJ < biomes.length) {
            bVal = 0;
            varIntLength = 0;
            while (true) {
                bVal |= (biomes[biomeJ] & 127) << (varIntLength++ * 7);
                if (varIntLength > 5) {
                    throw new RuntimeException("VarInt too big (probably corrupted data)");
                }
                if (((biomes[biomeJ] & 128) != 128)) {
                    biomeJ++;
                    break;
                }
                biomeJ++;
            }
            final int z = (biomeIndex % (width * length)) / width;
            final int x = (biomeIndex % (width * length)) % width;
            final Biome type = biomePalette.get(bVal, Sponge.server()).get();
            for (int y = yMin; y <= yMax; y++) {
                biomeBuffer.setBiome(x - xOffset, y, z - zOffset, type);
            }
            biomeIndex++;
        }
        try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length)) {
            final int xMin = biomeBuffer.min().x();
            final int zMin = biomeBuffer.min().z();
            for (int y = 0; y < height; y++) {
                final int y0 = yMin + y;
                for (int z = 0; z < length; z++) {
                    final int z0 = zMin + z;
                    for (int x = 0; x < width; x++) {
                        final int x0 = xMin + x;
                        final Biome biome = biomeBuffer.biome(x0, y0, z0);
                        SchematicTranslator.writeIdToBuffer(buffer, biomePalette.orAssign(biome));
                    }
                }
            }
            content.set(Constants.Sponge.Schematic.BIOME_DATA, buffer.toByteArray());
        } catch (final IOException e) {
        // Should never reach here.
        }
        content.remove(Constants.Sponge.Schematic.Versions.V2_BIOME_PALETTE);
    });
    content.set(Constants.Sponge.Schematic.VERSION, 3);
    return content;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataView(org.spongepowered.api.data.persistence.DataView) Biome(org.spongepowered.api.world.biome.Biome) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) Vector3i(org.spongepowered.math.vector.Vector3i) MutableBimapPalette(org.spongepowered.common.world.schematic.MutableBimapPalette) DataQuery(org.spongepowered.api.data.persistence.DataQuery) ByteArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ByteArrayMutableBiomeBuffer)

Example 2 with Biome

use of org.spongepowered.api.world.biome.Biome in project SpongeCommon by SpongePowered.

the class ObjectArrayImmutableBiomeBuffer method getNativeBiome.

/**
 * Gets the native biome for the position, resolving virtual biomes to
 * persisted types if needed.
 *
 * @param x The X position
 * @param y The Y position
 * @param z The X position
 * @return The native biome
 */
@SuppressWarnings("ConstantConditions")
public net.minecraft.world.level.biome.Biome getNativeBiome(final int x, final int y, final int z) {
    this.checkRange(x, y, z);
    final Biome type = this.biomes[this.getIndex(x, y, z)];
    return (net.minecraft.world.level.biome.Biome) (Object) type;
}
Also used : Biome(org.spongepowered.api.world.biome.Biome)

Example 3 with Biome

use of org.spongepowered.api.world.biome.Biome in project SpongeCommon by SpongePowered.

the class ByteArrayMutableBiomeBuffer method biomeStream.

@Override
public VolumeStream<BiomeVolume.Mutable, Biome> biomeStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    final Vector3i blockMin = this.min();
    final Vector3i blockMax = this.max();
    VolumeStreamUtils.validateStreamArgs(min, max, blockMin, blockMax, options);
    final byte[] biomes;
    if (options.carbonCopy()) {
        biomes = Arrays.copyOf(this.biomes, this.biomes.length);
    } else {
        biomes = this.biomes;
    }
    final Stream<VolumeElement<BiomeVolume.Mutable, Biome>> stateStream = IntStream.range(min.x(), max.x() + 1).mapToObj(x -> IntStream.range(min.z(), max.z() + 1).mapToObj(z -> IntStream.range(min.y(), max.y() + 1).mapToObj(y -> VolumeElement.of((BiomeVolume.Mutable) this, () -> {
        final byte biomeId = biomes[this.getIndex(x, y, z)];
        return this.palette.get(biomeId & 255, Sponge.server()).orElseGet(() -> Sponge.server().registry(RegistryTypes.BIOME).value(Biomes.OCEAN));
    }, new Vector3d(x, y, z)))).flatMap(Function.identity())).flatMap(Function.identity());
    return new SpongeVolumeStream<>(stateStream, () -> this);
}
Also used : BiomeVolume(org.spongepowered.api.world.volume.biome.BiomeVolume) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) Sponge(org.spongepowered.api.Sponge) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) Palette(org.spongepowered.api.world.schematic.Palette) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) Function(java.util.function.Function) Biome(org.spongepowered.api.world.biome.Biome) Objects(java.util.Objects) Biomes(org.spongepowered.api.world.biome.Biomes) Stream(java.util.stream.Stream) Vector3d(org.spongepowered.math.vector.Vector3d) VolumeStreamUtils(org.spongepowered.common.world.volume.VolumeStreamUtils) SpongeVolumeStream(org.spongepowered.common.world.volume.SpongeVolumeStream) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Vector3i(org.spongepowered.math.vector.Vector3i) Vector3d(org.spongepowered.math.vector.Vector3d) Vector3i(org.spongepowered.math.vector.Vector3i) BiomeVolume(org.spongepowered.api.world.volume.biome.BiomeVolume) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) SpongeVolumeStream(org.spongepowered.common.world.volume.SpongeVolumeStream)

Example 4 with Biome

use of org.spongepowered.api.world.biome.Biome 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 5 with Biome

use of org.spongepowered.api.world.biome.Biome in project SpongeCommon by SpongePowered.

the class SpongeBiomeProviderFactory method layered.

@Override
public <T extends LayeredBiomeConfig> ConfigurableBiomeProvider<T> layered(final T config) {
    final WritableRegistry<net.minecraft.world.level.biome.Biome> biomeRegistry = BootstrapProperties.registries.registryOrThrow(Registry.BIOME_REGISTRY);
    final OverworldBiomeSource layeredBiomeProvider = new OverworldBiomeSource(config.seed(), config.largeBiomes(), false, biomeRegistry);
    final List<net.minecraft.world.level.biome.Biome> biomes = new ArrayList<>();
    for (final RegistryReference<Biome> biome : config.biomes()) {
        biomes.add(biomeRegistry.get((ResourceLocation) (Object) biome.location()));
    }
    ((BiomeSourceAccessor) layeredBiomeProvider).accessor$possibleBiomes(biomes);
    return (ConfigurableBiomeProvider<T>) layeredBiomeProvider;
}
Also used : AttributedBiome(org.spongepowered.api.world.biome.AttributedBiome) Biome(org.spongepowered.api.world.biome.Biome) BiomeSourceAccessor(org.spongepowered.common.accessor.world.level.biome.BiomeSourceAccessor) MultiNoiseBiomeSourceAccessor(org.spongepowered.common.accessor.world.level.biome.MultiNoiseBiomeSourceAccessor) TheEndBiomeSourceAccessor(org.spongepowered.common.accessor.world.level.biome.TheEndBiomeSourceAccessor) OverworldBiomeSource(net.minecraft.world.level.biome.OverworldBiomeSource) ResourceLocation(net.minecraft.resources.ResourceLocation) ConfigurableBiomeProvider(org.spongepowered.api.world.biome.provider.ConfigurableBiomeProvider) ArrayList(java.util.ArrayList)

Aggregations

Biome (org.spongepowered.api.world.biome.Biome)11 Sponge (org.spongepowered.api.Sponge)5 RegistryTypes (org.spongepowered.api.registry.RegistryTypes)5 Collectors (java.util.stream.Collectors)4 ResourceKey (org.spongepowered.api.ResourceKey)4 StreamOptions (org.spongepowered.api.world.volume.stream.StreamOptions)4 Vector3d (org.spongepowered.math.vector.Vector3d)4 Vector3i (org.spongepowered.math.vector.Vector3i)4 Inject (com.google.inject.Inject)3 TypeToken (io.leangen.geantyref.TypeToken)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Stream (java.util.stream.Stream)3 ImmutableList (com.google.common.collect.ImmutableList)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Objects (java.util.Objects)2