NaturesAura/src/main/java/de/ellpeck/naturesaura/gen/LevelGenAuraBloom.java

71 lines
2.8 KiB
Java
Raw Normal View History

2020-02-25 15:14:56 +01:00
package de.ellpeck.naturesaura.gen;
2020-09-22 03:17:02 +02:00
import com.mojang.serialization.Codec;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityAuraBloom;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Mth;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
2020-02-25 15:14:56 +01:00
public class LevelGenAuraBloom extends Feature<NoneFeatureConfiguration> {
2020-02-25 15:56:46 +01:00
private final Block block;
2020-10-19 03:05:13 +02:00
private final int chance;
private final boolean nether;
2020-02-25 15:56:46 +01:00
2021-12-04 15:40:09 +01:00
public LevelGenAuraBloom(Block block, int chance, boolean nether) {
super(Codec.unit(FeatureConfiguration.NONE));
2020-02-25 15:56:46 +01:00
this.block = block;
2020-10-19 03:05:13 +02:00
this.chance = chance;
this.nether = nether;
2020-02-25 15:14:56 +01:00
}
@Override
public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> ctx) {
var levelIn = ctx.level();
var pos = ctx.origin();
var rand = ctx.random();
2020-10-19 03:05:13 +02:00
if (rand.nextInt(this.chance) != 0)
2020-02-25 15:14:56 +01:00
return false;
2021-12-15 16:30:22 +01:00
var startX = pos.getX() + rand.nextInt(16);
var startZ = pos.getZ() + rand.nextInt(16);
var any = false;
for (var i = Mth.nextInt(rand, 3, 8); i > 0; i--) {
var offX = startX + Mth.nextInt(rand, -5, 5);
var offZ = startZ + Mth.nextInt(rand, -5, 5);
2020-10-19 03:05:13 +02:00
if (this.nether) {
2021-12-15 16:30:22 +01:00
var y = Mth.nextInt(rand, 0, 128);
for (var off = 0; off < 64; off++) {
2020-10-19 03:05:13 +02:00
// try to find a good location in both directions of the random pos
2021-12-04 15:40:09 +01:00
if (this.tryPlace(levelIn, new BlockPos(offX, y - off, offZ)) || this.tryPlace(levelIn, new BlockPos(offX, y + off, offZ))) {
2020-10-19 03:05:13 +02:00
any = true;
break;
}
}
} else {
2021-12-15 16:30:22 +01:00
var placePos = new BlockPos(offX, levelIn.getHeight(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, offX, offZ), offZ);
2021-12-04 15:40:09 +01:00
if (this.tryPlace(levelIn, placePos))
2020-10-19 03:05:13 +02:00
any = true;
2020-02-25 15:14:56 +01:00
}
}
return any;
}
2020-10-19 03:05:13 +02:00
private boolean tryPlace(WorldGenLevel level, BlockPos pos) {
2021-12-15 16:30:22 +01:00
var state = this.block.defaultBlockState();
if (this.block.canSurvive(state, level, pos)) {
level.setBlock(pos, state, 3);
2021-12-15 16:30:22 +01:00
var tile = level.getBlockEntity(pos);
if (tile instanceof BlockEntityAuraBloom bloom)
bloom.justGenerated = true;
2020-10-19 03:05:13 +02:00
return true;
}
return false;
}
2020-02-25 15:14:56 +01:00
}