NaturesAura/src/main/java/de/ellpeck/naturesaura/gen/WorldGenNetherWartMushroom.java
2020-01-30 16:04:40 +01:00

61 lines
2.4 KiB
Java

package de.ellpeck.naturesaura.gen;
import de.ellpeck.naturesaura.blocks.ModBlocks;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.GenerationSettings;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.NoFeatureConfig;
import java.util.Random;
public class WorldGenNetherWartMushroom extends Feature<NoFeatureConfig> {
public WorldGenNetherWartMushroom() {
super(d -> NoFeatureConfig.NO_FEATURE_CONFIG);
}
@Override
public boolean place(IWorld worldIn, ChunkGenerator<? extends GenerationSettings> generator, Random rand, BlockPos pos, NoFeatureConfig config) {
int height = rand.nextInt(5) + 4;
if (rand.nextInt(10) == 0)
height += 5;
// Check if the stem has space
for (int i = 1; i < height; i++) {
BlockPos offset = pos.up(i);
if (worldIn.hasBlockState(offset, s -> !s.canBeReplacedByLogs(worldIn, offset)))
return false;
}
// Place stem
this.setBlockState(worldIn, pos, Blocks.AIR.getDefaultState());
for (int i = 0; i < height; i++)
this.placeIfPossible(worldIn, pos.up(i), Blocks.NETHER_WART_BLOCK);
// Place hat
int rad = 3;
for (int x = -rad; x <= rad; x++) {
for (int z = -rad; z <= rad; z++) {
int absX = Math.abs(x);
int absZ = Math.abs(z);
if (absX <= 1 && absZ <= 1) {
this.placeIfPossible(worldIn, pos.add(x, height, z), ModBlocks.NETHER_WART_MUSHROOM);
} else if (absX <= 2 && absZ <= 2 && absX != absZ) {
this.placeIfPossible(worldIn, pos.add(x, height - 1, z), ModBlocks.NETHER_WART_MUSHROOM);
} else if (absX < rad - 1 || absZ < rad - 1 || absX == rad - 1 && absZ == rad - 1) {
this.placeIfPossible(worldIn, pos.add(x, height - 2, z), ModBlocks.NETHER_WART_MUSHROOM);
}
}
}
return true;
}
private void placeIfPossible(IWorld world, BlockPos pos, Block block) {
if (world.hasBlockState(pos, s -> s.canBeReplacedByLogs(world, pos)))
world.setBlockState(pos, block.getDefaultState(), 19);
}
}