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.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
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;
|
|
|
|
int startX = pos.getX() + rand.nextInt(16);
|
|
|
|
int startZ = pos.getZ() + rand.nextInt(16);
|
|
|
|
boolean any = false;
|
2021-12-04 19:17:21 +01:00
|
|
|
for (int i = Mth.nextInt(rand, 3, 8); i > 0; i--) {
|
|
|
|
int offX = startX + Mth.nextInt(rand, -5, 5);
|
|
|
|
int offZ = startZ + Mth.nextInt(rand, -5, 5);
|
2020-10-19 03:05:13 +02:00
|
|
|
if (this.nether) {
|
2021-12-04 19:17:21 +01:00
|
|
|
int y = Mth.nextInt(rand, 0, 128);
|
2020-10-19 03:05:13 +02:00
|
|
|
for (int off = 0; off < 64; off++) {
|
|
|
|
// 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 14:26:42 +01:00
|
|
|
BlockPos 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) {
|
|
|
|
BlockState state = this.block.defaultBlockState();
|
|
|
|
if (this.block.canSurvive(state, level, pos)) {
|
|
|
|
level.setBlock(pos, state, 3);
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockEntity 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
|
|
|
}
|