package com.alttd.custommobs.utility; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.util.BlockIterator; import java.util.Optional; public class SpawnableTargetBlock { public Optional getSpawnableTargetBlock(Player player, int maxDistance) { BlockIterator iter = new BlockIterator(player, maxDistance); while (iter.hasNext()) { Block block = iter.next(); if (isSpawnable(block)) { return Optional.of(block.getLocation().add(0, 1, 0)); } } return Optional.empty(); } private boolean isSpawnable(Block block) { Material material = block.getType(); if (material == Material.AIR) { return false; } Block blockAbove = block.getRelative(BlockFace.UP); if (blockAbove.getType() != Material.AIR) { return false; } return block.getType().isSolid(); } }