Modified the spawnable target block location by adding 1 to the Y-coordinate. This change ensures that entities spawn above the target block, preventing them from spawning inside it or in obstructed areas.
39 lines
1.0 KiB
Java
39 lines
1.0 KiB
Java
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<Location> 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();
|
|
}
|
|
} |