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.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(); // 检测玩家当前挑战完成进度

View File

@ -18,25 +18,21 @@ import java.util.concurrent.atomic.AtomicInteger;
public class ComputeBlock {
public static int getCompleteBlockAmount(List<BlockRegion> blockRegionList){
System.out.println("[调试 - 统计] 本次统计拆分异步区块 " + blockRegionList.size() + "");
AtomicInteger totalBlockAmount = new AtomicInteger(0);
public static CompletableFuture<Integer> getCompleteBlockAmount(List<BlockRegion> blockRegionList) {
CompletableFuture completableFuture = CompletableFuture.supplyAsync(()->{
int amount = 0;
for (BlockRegion blockRegion : blockRegionList) {
CompletableFuture.runAsync(() -> {
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());
});
CuboidRegion cuboidRegion = getCuboidRegion(minX, maxX);
amount += countBlocks(cuboidRegion);
}
System.out.println("[调试 - 统计] 总计方块数量: "+totalBlockAmount.toString());
return Integer.parseInt(totalBlockAmount.toString());
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++) {