From 1e27c60abe171d8b5efd036000c8f9dbac4af6a5 Mon Sep 17 00:00:00 2001 From: YuTian <2953516620@qq.com> Date: Tue, 23 Jul 2024 21:33:54 +0800 Subject: [PATCH] fix --- .../me/Demon/BlockWars/Game/GameData.java | 7 ++++- .../me/Demon/BlockWars/Util/ComputeBlock.java | 26 ++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/me/Demon/BlockWars/Game/GameData.java b/src/main/java/me/Demon/BlockWars/Game/GameData.java index 5f5bb34..755a59c 100644 --- a/src/main/java/me/Demon/BlockWars/Game/GameData.java +++ b/src/main/java/me/Demon/BlockWars/Game/GameData.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; +import java.util.concurrent.ExecutionException; public class GameData { @@ -94,7 +95,11 @@ public class GameData { tasks.add(new BukkitRunnable() { @Override public void run() { - totalBlockAmount = ComputeBlock.getCompleteBlockAmount(blockRegionList); // 每秒刷新一次矿区方块数量 + try { + totalBlockAmount = ComputeBlock.getCompleteBlockAmount(blockRegionList).get(); // 每秒刷新一次矿区方块数量 + } catch (Exception e) { + e.printStackTrace(); + } updateBossBar(); if (!checking) { checkComplete(); // 检测玩家当前挑战完成进度 diff --git a/src/main/java/me/Demon/BlockWars/Util/ComputeBlock.java b/src/main/java/me/Demon/BlockWars/Util/ComputeBlock.java index 25e4be3..048a65c 100644 --- a/src/main/java/me/Demon/BlockWars/Util/ComputeBlock.java +++ b/src/main/java/me/Demon/BlockWars/Util/ComputeBlock.java @@ -18,25 +18,21 @@ import java.util.concurrent.atomic.AtomicInteger; public class ComputeBlock { - public static int getCompleteBlockAmount(List blockRegionList){ - System.out.println("[调试 - 统计] 本次统计拆分异步区块 " + blockRegionList.size() + "个"); - AtomicInteger totalBlockAmount = new AtomicInteger(0); - for (BlockRegion blockRegion : blockRegionList) { - CompletableFuture.runAsync(() -> { + public static CompletableFuture getCompleteBlockAmount(List blockRegionList) { + CompletableFuture completableFuture = CompletableFuture.supplyAsync(()->{ + int amount = 0; + for (BlockRegion blockRegion : blockRegionList) { int minX = blockRegion.getMinX(); int maxX = blockRegion.getMaxX(); - CuboidRegion cuboidRegion = getCuboidRegion(minX,maxX); - int airCount = countAirBlocks(cuboidRegion); // 计算 AIR 方块数量 - totalBlockAmount.addAndGet(airCount); // 线程安全地累加 AIR 方块数量 - System.out.println("Air count for region: " + airCount); // 调试信息 - System.out.println("[调试 - 统计] 编号区块方块数量: "+totalBlockAmount.toString()); - }); - } - System.out.println("[调试 - 统计] 总计方块数量: "+totalBlockAmount.toString()); - return Integer.parseInt(totalBlockAmount.toString()); + CuboidRegion cuboidRegion = getCuboidRegion(minX, maxX); + amount += countBlocks(cuboidRegion); + } + return amount; + }); + return completableFuture; } - public static int countAirBlocks(CuboidRegion cuboidRegion) { + public static int countBlocks(CuboidRegion cuboidRegion) { World world = Main.gameData.getWorld(); int blockCount = 0; for (int y = cuboidRegion.getMinimumPoint().getBlockY(); y <= cuboidRegion.getMaximumPoint().getBlockY(); y++) {