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