]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/SpawnableBlocksHelper.java
99d3371e3f7fd75eef64970cc2868e8268234adc
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / interop / SpawnableBlocksHelper.java
1 package com.irtimaled.bbor.client.interop;
2
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;
16
17 public class SpawnableBlocksHelper {
18     private static final EntityType entityType = EntityType.Builder.create(SpawnGroup.MONSTER)
19             .setDimensions(0f, 0f).disableSaving().build(null);
20
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;
25
26         int blockZ = coords.getZ();
27         int minZ = blockZ - width;
28         int maxZ = blockZ + width + 1;
29
30         int blockY = coords.getY();
31         int minY = Math.max(1, blockY - height);
32         int maxY = Math.min(255, blockY + height);
33
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);
44                     }
45                 }
46             }
47         }
48     }
49
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);
62     }
63 }