Search in sources :

Example 1 with TargetBlockState

use of net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState in project Electrodynamics by aurilisdev.

the class OreGeneration method registerOres.

public static void registerOres() {
    // called in onCommonSetup(FMLCommonSetupEvent event)
    for (SubtypeOre ore : SubtypeOre.values()) {
        if (OreConfig.oresToSpawn.contains(ore.name())) {
            BlockState oreDefault = DeferredRegisters.SUBTYPEBLOCK_MAPPINGS.get(ore).defaultBlockState();
            BlockState oreDeepslateDefault = DeferredRegisters.SUBTYPEBLOCK_MAPPINGS.get(SubtypeOreDeepslate.values()[ore.ordinal()]).defaultBlockState();
            List<TargetBlockState> targetBlockStates = List.of(OreConfiguration.target(OreFeatures.STONE_ORE_REPLACEABLES, oreDefault), OreConfiguration.target(OreFeatures.DEEPSLATE_ORE_REPLACEABLES, oreDeepslateDefault));
            ConfiguredFeature<?, ?> feature = FeatureUtils.register(ore.tag(), Feature.ORE.configured(new OreConfiguration(targetBlockStates, ore.veinSize)));
            PlacedFeature placed = PlacementUtils.register(ore.tag(), feature.placed(List.of(CountPlacement.of((int) (ore.veinsPerChunk * OreConfig.OREGENERATIONMULTIPLIER)), InSquarePlacement.spread(), HeightRangePlacement.triangle(VerticalAnchor.absolute(ore.minY), VerticalAnchor.absolute(ore.maxY)), BiomeFilter.biome())));
            FEATURES.add(placed);
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) SubtypeOre(electrodynamics.common.block.subtype.SubtypeOre) PlacedFeature(net.minecraft.world.level.levelgen.placement.PlacedFeature) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) OreConfiguration(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration)

Example 2 with TargetBlockState

use of net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState 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)

Aggregations

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