修改方块生成范围集中的问题

This commit is contained in:
xiaobai
2026-02-13 22:45:17 +08:00
parent 2fbf5cfd7d
commit b268a74eeb
8 changed files with 118 additions and 20 deletions

View File

@@ -92,24 +92,28 @@ public class PlayerBlockManager {
}
/**
* 寻找地表位置(上方无方块覆盖
* 寻找地表位置(放宽条件,只要是露天地面就可以
*/
private Location findSurfaceLocation(Location center, int spreadRange, boolean requireOpenSky) {
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 20; i++) { // 增加尝试次数
int x = random.nextInt(spreadRange * 2 + 1) - spreadRange;
int z = random.nextInt(spreadRange * 2 + 1) - spreadRange;
// 从中心点上方开始向下寻找地表
Location testLoc = center.clone().add(x, 10, z);
// 以玩家坐标为中心,但高度从世界最高点开始寻找
Location testLoc = center.clone().add(x, 0, z);
World world = testLoc.getWorld();
if (world == null) continue;
// 向下寻找第一个非空气方块
// 从世界最高点向下寻找第一个固体方块
int maxHeight = world.getMaxHeight();
Block groundBlock = null;
for (int y = 10; y > world.getMinHeight(); y--) {
for (int y = maxHeight; y > world.getMinHeight(); y--) {
testLoc.setY(y);
Block block = testLoc.getBlock();
if (!block.getType().isAir()) {
Material type = block.getType();
// 检查是否是固体方块(可以作为支撑)
if (type.isSolid() && type.isBlock() && !type.isTransparent()) {
groundBlock = block;
break;
}
@@ -117,24 +121,23 @@ public class PlayerBlockManager {
if (groundBlock == null) continue;
// 检查地表方块上方位置
// 地表位置 = 固体方块上方一格
Location surfaceLoc = groundBlock.getLocation().add(0, 1, 0);
Block surfaceBlock = surfaceLoc.getBlock();
// 检查是否已有方块
if (blockOwners.containsKey(surfaceLoc)) {
continue;
}
// 检查地表方块是否合适
if (!isSuitableLocation(surfaceLoc)) {
// 检查位置是否合适(放宽条件)
if (!isSuitableLocationRelaxed(surfaceLoc)) {
continue;
}
// 如果需要上方无方块覆盖,检查上方
// 如果需要上方无方块覆盖,检查上方3格
if (requireOpenSky) {
boolean hasCover = false;
for (int y = 1; y <= 5; y++) {
for (int y = 1; y <= 3; y++) {
Block aboveBlock = surfaceLoc.clone().add(0, y, 0).getBlock();
if (!aboveBlock.getType().isAir()) {
hasCover = true;
@@ -234,6 +237,43 @@ public class PlayerBlockManager {
return true;
}
/**
* 放宽条件的检查方法(用于新的生成逻辑)
*/
private boolean isSuitableLocationRelaxed(Location location) {
Block block = location.getBlock();
// 检查是否已有方块
if (blockOwners.containsKey(location)) {
return false;
}
// 检查方块是否可替换(放宽条件)
Material type = block.getType();
if (!type.isAir()) {
// 固体方块不能替换
if (type.isSolid()) {
return false;
}
// 液体方块不能替换
if (type == Material.WATER || type == Material.LAVA) {
return false;
}
}
// 检查下方是否有支撑方块(放宽条件)
Block below = location.clone().add(0, -1, 0).getBlock();
Material belowType = below.getType();
// 只要下方不是空气或液体就可以
if (belowType.isAir() || belowType == Material.WATER || belowType == Material.LAVA) {
return false;
}
return true;
}
/**
* 放置玩家头颅方块
*/