1 package com.irtimaled.bbor.client.interop;
3 import com.irtimaled.bbor.common.models.Coords;
4 import net.minecraft.block.Block;
5 import net.minecraft.block.BlockState;
6 import net.minecraft.client.MinecraftClient;
7 import net.minecraft.entity.EntityType;
8 import net.minecraft.entity.SpawnGroup;
9 import net.minecraft.tag.BlockTags;
10 import net.minecraft.util.math.BlockPos;
11 import net.minecraft.util.math.Direction;
12 import net.minecraft.util.shape.VoxelShape;
13 import net.minecraft.world.LightType;
14 import net.minecraft.world.World;
15 import net.minecraft.world.biome.Biome;
17 public class SpawnableBlocksHelper {
18 private static final EntityType entityType = EntityType.Builder.create(SpawnGroup.MONSTER)
19 .setDimensions(0f, 0f).disableSaving().build(null);
21 public static void findSpawnableBlocks(Coords coords, int width, int height, BlockProcessor blockProcessor) {
22 int blockX = coords.getX();
23 int minX = blockX - width;
24 int maxX = blockX + width + 1;
26 int blockZ = coords.getZ();
27 int minZ = blockZ - width;
28 int maxZ = blockZ + width + 1;
30 int blockY = coords.getY();
31 int minY = Math.max(1, blockY - height);
32 int maxY = Math.min(255, blockY + height);
34 World world = MinecraftClient.getInstance().world;
35 for (int x = minX; x < maxX; x++) {
36 for (int z = minZ; z < maxZ; z++) {
37 BlockState upperBlockState = world.getBlockState(new BlockPos(x, minY - 1, z));
38 for (int y = Math.max(1, minY); y < maxY; y++) {
39 BlockState spawnBlockState = upperBlockState;
40 BlockPos pos = new BlockPos(x, y, z);
41 upperBlockState = world.getBlockState(pos);
42 if (isSpawnable(world, pos, spawnBlockState, upperBlockState)) {
43 blockProcessor.process(pos);
50 static boolean isSpawnable(World world, BlockPos pos, BlockState spawnBlockState, BlockState upperBlockState) {
51 VoxelShape collisionShape = upperBlockState.getCollisionShape(world, pos);
52 Biome biome = world.getBiome(pos);
53 boolean isNether = biome.getCategory() == Biome.Category.NETHER;
54 return biome.getCategory() != Biome.Category.MUSHROOM &&
55 spawnBlockState.allowsSpawning(world, pos.down(), isNether ? EntityType.ZOMBIFIED_PIGLIN : entityType) &&
56 !Block.isFaceFullSquare(collisionShape, Direction.UP) &&
57 !upperBlockState.emitsRedstonePower() &&
58 !upperBlockState.isIn(BlockTags.RAILS) &&
59 collisionShape.getMax(Direction.Axis.Y) <= 0 &&
60 upperBlockState.getFluidState().isEmpty() &&
61 (isNether || world.getLightLevel(LightType.BLOCK, pos) <= 7);