Search in sources :

Example 1 with BulkSectionAccess

use of net.minecraft.world.level.chunk.BulkSectionAccess in project Create by Creators-of-Create.

the class LayeredOreFeature method place.

public boolean place(FeaturePlaceContext<ConfigDrivenOreConfiguration> pContext) {
    Random random = pContext.random();
    BlockPos blockpos = pContext.origin();
    WorldGenLevel worldgenlevel = pContext.level();
    ConfigDrivenOreConfiguration config = pContext.config();
    List<LayerPattern> patternPool = config.getLayers();
    if (patternPool.isEmpty())
        return false;
    LayerPattern layerPattern = patternPool.get(random.nextInt(patternPool.size()));
    int placedAmount = 0;
    int size = config.getSize();
    int radius = Mth.ceil(config.getSize() / 2f);
    int x0 = blockpos.getX() - radius;
    int y0 = blockpos.getY() - radius;
    int z0 = blockpos.getZ() - radius;
    int width = size + 1;
    int length = size + 1;
    int height = size + 1;
    if (blockpos.getY() >= worldgenlevel.getHeight(Heightmap.Types.OCEAN_FLOOR_WG, blockpos.getX(), blockpos.getZ()))
        return false;
    List<LayerPattern.Layer> resolvedLayers = new ArrayList<>();
    List<Float> layerDiameterOffsets = new ArrayList<>();
    MutableBlockPos mutablePos = new MutableBlockPos();
    BulkSectionAccess bulksectionaccess = new BulkSectionAccess(worldgenlevel);
    int layerCoordinate = random.nextInt(4);
    int slantyCoordinate = random.nextInt(3);
    float slope = random.nextFloat() * .75f;
    try {
        for (int x = 0; x < width; x++) {
            float dx = x * 2f / width - 1;
            if (dx * dx > 1)
                continue;
            for (int y = 0; y < height; y++) {
                float dy = y * 2f / height - 1;
                if (dx * dx + dy * dy > 1)
                    continue;
                if (worldgenlevel.isOutsideBuildHeight(y0 + y))
                    continue;
                for (int z = 0; z < length; z++) {
                    float dz = z * 2f / height - 1;
                    int layerIndex = layerCoordinate == 0 ? z : layerCoordinate == 1 ? x : y;
                    if (slantyCoordinate != layerCoordinate)
                        layerIndex += Mth.floor(slantyCoordinate == 0 ? z : slantyCoordinate == 1 ? x : y) * slope;
                    while (layerIndex >= resolvedLayers.size()) {
                        Layer next = layerPattern.rollNext(resolvedLayers.isEmpty() ? null : resolvedLayers.get(resolvedLayers.size() - 1), random);
                        float offset = random.nextFloat() * .5f + .5f;
                        for (int i = 0; i < next.minSize + random.nextInt(1 + next.maxSize - next.minSize); i++) {
                            resolvedLayers.add(next);
                            layerDiameterOffsets.add(offset);
                        }
                    }
                    if (dx * dx + dy * dy + dz * dz > 1 * layerDiameterOffsets.get(layerIndex))
                        continue;
                    LayerPattern.Layer layer = resolvedLayers.get(layerIndex);
                    List<TargetBlockState> state = layer.rollBlock(random);
                    int currentX = x0 + x;
                    int currentY = y0 + y;
                    int currentZ = z0 + z;
                    mutablePos.set(currentX, currentY, currentZ);
                    if (!worldgenlevel.ensureCanWrite(mutablePos))
                        continue;
                    LevelChunkSection levelchunksection = bulksectionaccess.getSection(mutablePos);
                    if (levelchunksection == null)
                        continue;
                    int i3 = SectionPos.sectionRelative(currentX);
                    int j3 = SectionPos.sectionRelative(currentY);
                    int k3 = SectionPos.sectionRelative(currentZ);
                    BlockState blockstate = levelchunksection.getBlockState(i3, j3, k3);
                    for (OreConfiguration.TargetBlockState oreconfiguration$targetblockstate : state) {
                        if (!canPlaceOre(blockstate, bulksectionaccess::getBlockState, random, config, oreconfiguration$targetblockstate, mutablePos))
                            continue;
                        if (oreconfiguration$targetblockstate.state.isAir())
                            continue;
                        levelchunksection.setBlockState(i3, j3, k3, oreconfiguration$targetblockstate.state, false);
                        ++placedAmount;
                        break;
                    }
                }
            }
        }
    } catch (Throwable throwable1) {
        try {
            bulksectionaccess.close();
        } catch (Throwable throwable) {
            throwable1.addSuppressed(throwable);
        }
        throw throwable1;
    }
    bulksectionaccess.close();
    return placedAmount > 0;
}
Also used : BulkSectionAccess(net.minecraft.world.level.chunk.BulkSectionAccess) ArrayList(java.util.ArrayList) Layer(com.simibubi.create.foundation.worldgen.LayerPattern.Layer) Layer(com.simibubi.create.foundation.worldgen.LayerPattern.Layer) BlockState(net.minecraft.world.level.block.state.BlockState) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) Random(java.util.Random) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) BlockPos(net.minecraft.core.BlockPos) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) WorldGenLevel(net.minecraft.world.level.WorldGenLevel) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) OreConfiguration(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration)

Example 2 with BulkSectionAccess

use of net.minecraft.world.level.chunk.BulkSectionAccess in project Create by Creators-of-Create.

the class VanillaStyleOreFeature method doPlace.

protected boolean doPlace(WorldGenLevel pLevel, Random pRandom, ConfigDrivenOreConfiguration pConfig, double pMinX, double pMaxX, double pMinZ, double pMaxZ, double pMinY, double pMaxY, int pX, int pY, int pZ, int pWidth, int pHeight) {
    int i = 0;
    BitSet bitset = new BitSet(pWidth * pHeight * pWidth);
    BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
    int j = pConfig.getSize();
    double[] adouble = new double[j * 4];
    for (int k = 0; k < j; ++k) {
        float f = (float) k / (float) j;
        double d0 = Mth.lerp(f, pMinX, pMaxX);
        double d1 = Mth.lerp(f, pMinY, pMaxY);
        double d2 = Mth.lerp(f, pMinZ, pMaxZ);
        double d3 = pRandom.nextDouble() * j / 16.0D;
        double d4 = ((Mth.sin((float) Math.PI * f) + 1.0F) * d3 + 1.0D) / 2.0D;
        adouble[k * 4 + 0] = d0;
        adouble[k * 4 + 1] = d1;
        adouble[k * 4 + 2] = d2;
        adouble[k * 4 + 3] = d4;
    }
    for (int l3 = 0; l3 < j - 1; ++l3) {
        if (adouble[l3 * 4 + 3] <= 0.0D)
            continue;
        for (int i4 = l3 + 1; i4 < j; ++i4) {
            if (adouble[i4 * 4 + 3] <= 0.0D)
                continue;
            double d8 = adouble[l3 * 4 + 0] - adouble[i4 * 4 + 0];
            double d10 = adouble[l3 * 4 + 1] - adouble[i4 * 4 + 1];
            double d12 = adouble[l3 * 4 + 2] - adouble[i4 * 4 + 2];
            double d14 = adouble[l3 * 4 + 3] - adouble[i4 * 4 + 3];
            if (d14 * d14 <= d8 * d8 + d10 * d10 + d12 * d12)
                continue;
            if (d14 > 0.0D)
                adouble[i4 * 4 + 3] = -1.0D;
            else
                adouble[l3 * 4 + 3] = -1.0D;
        }
    }
    BulkSectionAccess bulksectionaccess = new BulkSectionAccess(pLevel);
    try {
        for (int j4 = 0; j4 < j; ++j4) {
            double d9 = adouble[j4 * 4 + 3];
            if (d9 < 0.0D)
                continue;
            double d11 = adouble[j4 * 4 + 0];
            double d13 = adouble[j4 * 4 + 1];
            double d15 = adouble[j4 * 4 + 2];
            int k4 = Math.max(Mth.floor(d11 - d9), pX);
            int l = Math.max(Mth.floor(d13 - d9), pY);
            int i1 = Math.max(Mth.floor(d15 - d9), pZ);
            int j1 = Math.max(Mth.floor(d11 + d9), k4);
            int k1 = Math.max(Mth.floor(d13 + d9), l);
            int l1 = Math.max(Mth.floor(d15 + d9), i1);
            for (int i2 = k4; i2 <= j1; ++i2) {
                double d5 = (i2 + 0.5D - d11) / d9;
                if (d5 * d5 >= 1.0D)
                    continue;
                for (int j2 = l; j2 <= k1; ++j2) {
                    double d6 = (j2 + 0.5D - d13) / d9;
                    if (d5 * d5 + d6 * d6 >= 1.0D)
                        continue;
                    for (int k2 = i1; k2 <= l1; ++k2) {
                        double d7 = (k2 + 0.5D - d15) / d9;
                        if (d5 * d5 + d6 * d6 + d7 * d7 >= 1.0D || pLevel.isOutsideBuildHeight(j2))
                            continue;
                        int l2 = i2 - pX + (j2 - pY) * pWidth + (k2 - pZ) * pWidth * pHeight;
                        if (bitset.get(l2))
                            continue;
                        bitset.set(l2);
                        blockpos$mutableblockpos.set(i2, j2, k2);
                        if (!pLevel.ensureCanWrite(blockpos$mutableblockpos))
                            continue;
                        LevelChunkSection levelchunksection = bulksectionaccess.getSection(blockpos$mutableblockpos);
                        if (levelchunksection == null)
                            continue;
                        int i3 = SectionPos.sectionRelative(i2);
                        int j3 = SectionPos.sectionRelative(j2);
                        int k3 = SectionPos.sectionRelative(k2);
                        BlockState blockstate = levelchunksection.getBlockState(i3, j3, k3);
                        for (OreConfiguration.TargetBlockState oreconfiguration$targetblockstate : pConfig.targetStates) {
                            if (!canPlaceOre(blockstate, bulksectionaccess::getBlockState, pRandom, pConfig, oreconfiguration$targetblockstate, blockpos$mutableblockpos))
                                continue;
                            levelchunksection.setBlockState(i3, j3, k3, oreconfiguration$targetblockstate.state, false);
                            ++i;
                            break;
                        }
                    }
                }
            }
        }
    } catch (Throwable throwable1) {
        try {
            bulksectionaccess.close();
        } catch (Throwable throwable) {
            throwable1.addSuppressed(throwable);
        }
        throw throwable1;
    }
    bulksectionaccess.close();
    return i > 0;
}
Also used : BulkSectionAccess(net.minecraft.world.level.chunk.BulkSectionAccess) BitSet(java.util.BitSet) BlockState(net.minecraft.world.level.block.state.BlockState) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) BlockPos(net.minecraft.core.BlockPos) OreConfiguration(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration)

Aggregations

BlockPos (net.minecraft.core.BlockPos)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 BulkSectionAccess (net.minecraft.world.level.chunk.BulkSectionAccess)2 LevelChunkSection (net.minecraft.world.level.chunk.LevelChunkSection)2 OreConfiguration (net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration)2 Layer (com.simibubi.create.foundation.worldgen.LayerPattern.Layer)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Random (java.util.Random)1 MutableBlockPos (net.minecraft.core.BlockPos.MutableBlockPos)1 WorldGenLevel (net.minecraft.world.level.WorldGenLevel)1 TargetBlockState (net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState)1