This commit is contained in:
YuTian 2024-07-23 21:33:54 +08:00
parent 7d069aea91
commit 1e27c60abe
2 changed files with 17 additions and 16 deletions

View File

@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ExecutionException;
public class GameData { public class GameData {
@ -94,7 +95,11 @@ public class GameData {
tasks.add(new BukkitRunnable() { tasks.add(new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
totalBlockAmount = ComputeBlock.getCompleteBlockAmount(blockRegionList); // 每秒刷新一次矿区方块数量 try {
totalBlockAmount = ComputeBlock.getCompleteBlockAmount(blockRegionList).get(); // 每秒刷新一次矿区方块数量
} catch (Exception e) {
e.printStackTrace();
}
updateBossBar(); updateBossBar();
if (!checking) { if (!checking) {
checkComplete(); // 检测玩家当前挑战完成进度 checkComplete(); // 检测玩家当前挑战完成进度

View File

@ -18,25 +18,21 @@ import java.util.concurrent.atomic.AtomicInteger;
public class ComputeBlock { public class ComputeBlock {
public static int getCompleteBlockAmount(List<BlockRegion> blockRegionList){ public static CompletableFuture<Integer> getCompleteBlockAmount(List<BlockRegion> blockRegionList) {
System.out.println("[调试 - 统计] 本次统计拆分异步区块 " + blockRegionList.size() + ""); CompletableFuture completableFuture = CompletableFuture.supplyAsync(()->{
AtomicInteger totalBlockAmount = new AtomicInteger(0); int amount = 0;
for (BlockRegion blockRegion : blockRegionList) { for (BlockRegion blockRegion : blockRegionList) {
CompletableFuture.runAsync(() -> {
int minX = blockRegion.getMinX(); int minX = blockRegion.getMinX();
int maxX = blockRegion.getMaxX(); int maxX = blockRegion.getMaxX();
CuboidRegion cuboidRegion = getCuboidRegion(minX, maxX); CuboidRegion cuboidRegion = getCuboidRegion(minX, maxX);
int airCount = countAirBlocks(cuboidRegion); // 计算 AIR 方块数量 amount += countBlocks(cuboidRegion);
totalBlockAmount.addAndGet(airCount); // 线程安全地累加 AIR 方块数量
System.out.println("Air count for region: " + airCount); // 调试信息
System.out.println("[调试 - 统计] 编号区块方块数量: "+totalBlockAmount.toString());
});
} }
System.out.println("[调试 - 统计] 总计方块数量: "+totalBlockAmount.toString()); return amount;
return Integer.parseInt(totalBlockAmount.toString()); });
return completableFuture;
} }
public static int countAirBlocks(CuboidRegion cuboidRegion) { public static int countBlocks(CuboidRegion cuboidRegion) {
World world = Main.gameData.getWorld(); World world = Main.gameData.getWorld();
int blockCount = 0; int blockCount = 0;
for (int y = cuboidRegion.getMinimumPoint().getBlockY(); y <= cuboidRegion.getMaximumPoint().getBlockY(); y++) { for (int y = cuboidRegion.getMinimumPoint().getBlockY(); y <= cuboidRegion.getMaximumPoint().getBlockY(); y++) {