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;
|
2021-12-15 14:26:42 +01:00
|
|
|
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
|
|
|
|
2021-12-15 14:26:42 +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) {
|
2021-12-15 14:26:42 +01:00
|
|
|
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
|
2021-12-15 14:26:42 +01:00
|
|
|
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
|
|
|
|
2021-12-15 14:26:42 +01:00
|
|
|
private boolean tryPlace(WorldGenLevel level, BlockPos pos) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var state = this.block.defaultBlockState();
|
2023-02-15 23:45:50 +01:00
|
|
|
if (state.canSurvive(level, pos)) {
|
2021-12-15 14:26:42 +01:00
|
|
|
level.setBlock(pos, state, 3);
|
2021-12-15 16:30:22 +01:00
|
|
|
var tile = level.getBlockEntity(pos);
|
2021-12-15 14:26:42 +01:00
|
|
|
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
|
|
|
}
|