Skip to content

Commit

Permalink
improve detection for still running filescan
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 27, 2024
1 parent 2fb638d commit 750c00a
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/main/java/me/xginko/betterworldstats/stats/WorldStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.kyori.adventure.text.Component;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.concurrent.CompletableFuture;
Expand All @@ -18,7 +19,7 @@ public class WorldStats {
private final @NotNull Config config;
private final @NotNull AtomicReference<FileScanResult> scan_result;
private final @NotNull AtomicInteger chunk_count, entity_count;
private boolean scanning = false;
private @Nullable CompletableFuture<Void> runningScan;

public WorldStats() {
this.config = BetterWorldStats.getConfiguration();
Expand All @@ -29,30 +30,29 @@ public WorldStats() {
}

private void refresh() {
if (scanning) {
if (scan_result.get() != null && scan_result.get().expiration_time_millis > System.currentTimeMillis()) {
return;
}

if (scan_result.get() != null && scan_result.get().expiration_time_millis > System.currentTimeMillis()) {
if (runningScan != null && !runningScan.isDone() && !runningScan.isCompletedExceptionally() && !runningScan.isCancelled()) {
return;
}

scanning = true; // Mark as scanning to avoid scheduling another check while there is already one running

CompletableFuture.supplyAsync(() -> new FileScanResult(config.paths_to_scan, config.filesize_update_period_millis)).thenAccept(result -> {
scan_result.set(result);
scanning = false;
if (config.log_is_enabled) {
BetterWorldStats.getLog().info(Component.text(
"Updated file stats asynchronously.").color(KyoriUtil.GUPPIE_GREEN));
BetterWorldStats.getLog().info(Component.text(
"Size: " + config.filesize_format.format(result.size_in_gb) + "GB, " +
"files: " + result.file_count + ", " +
"folders: " + result.folder_count + ", " +
"chunks: " + chunk_count + ", " +
"entities: " + entity_count).color(KyoriUtil.GUPPIE_GREEN));
}
});
this.runningScan = CompletableFuture
.supplyAsync(() -> new FileScanResult(config.paths_to_scan, config.filesize_update_period_millis))
.thenAccept(result -> {
scan_result.set(result);
if (config.log_is_enabled) {
BetterWorldStats.getLog().info(Component.text(
"Updated file stats asynchronously.").color(KyoriUtil.GUPPIE_GREEN));
BetterWorldStats.getLog().info(Component.text(
"Size: " + config.filesize_format.format(result.size_in_gb) + "GB, " +
"files: " + result.file_count + ", " +
"folders: " + result.folder_count + ", " +
"chunks: " + chunk_count + ", " +
"entities: " + entity_count).color(KyoriUtil.GUPPIE_GREEN));
}
});

if (PaperLib.isPaper()) {
for (final World world : BetterWorldStats.getInstance().getServer().getWorlds()) {
Expand Down

0 comments on commit 750c00a

Please sign in to comment.