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

72 lines
2.7 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;
2020-02-25 15:56:46 +01:00
import net.minecraft.block.Block;
2020-02-25 15:14:56 +01:00
import net.minecraft.block.BlockState;
2021-12-04 15:40:09 +01:00
import net.minecraft.tileentity.BlockEntity;
2020-02-25 15:14:56 +01:00
import net.minecraft.util.math.BlockPos;
2021-12-04 19:17:21 +01:00
import net.minecraft.util.math.Mth;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.ISeedReader;
import net.minecraft.level.gen.ChunkGenerator;
import net.minecraft.level.gen.Heightmap;
import net.minecraft.level.gen.feature.Feature;
import net.minecraft.level.gen.feature.IFeatureConfig;
import net.minecraft.level.gen.feature.NoFeatureConfig;
2020-02-25 15:14:56 +01:00
import java.util.Random;
2021-12-04 15:40:09 +01:00
public class LevelGenAuraBloom extends Feature<NoFeatureConfig> {
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) {
2020-09-22 03:17:02 +02:00
super(Codec.unit(IFeatureConfig.NO_FEATURE_CONFIG));
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-04 15:40:09 +01:00
public boolean func_241855_a(ISeedReader levelIn, ChunkGenerator gen, Random rand, BlockPos pos, NoFeatureConfig config) {
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-04 15:40:09 +01:00
BlockPos placePos = new BlockPos(offX, levelIn.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, offX, offZ), offZ);
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-04 15:40:09 +01:00
private boolean tryPlace(ISeedReader level, BlockPos pos) {
2020-10-19 03:05:13 +02:00
BlockState state = this.block.getDefaultState();
2021-12-04 15:40:09 +01:00
if (this.block.isValidPosition(state, level, pos)) {
level.setBlockState(pos, state, 3);
BlockEntity tile = level.getBlockEntity(pos);
if (tile instanceof BlockEntityAuraBloom)
((BlockEntityAuraBloom) tile).justGenerated = true;
2020-10-19 03:05:13 +02:00
return true;
}
return false;
}
2020-02-25 15:14:56 +01:00
}