From 127b32eaebb0f2b708a085918a8b433dd2126c96 Mon Sep 17 00:00:00 2001 From: xGinko Date: Sun, 24 Mar 2024 23:25:52 +0100 Subject: [PATCH] kick folialib --- pom.xml | 24 ++- .../betterworldstats/BetterWorldStats.java | 11 -- .../betterworldstats/PAPIExpansion.java | 12 +- .../commands/worldstats/WorldStatsCmd.java | 12 +- .../betterworldstats/stats/FileStats.java | 148 ++++++++---------- .../betterworldstats/stats/Players.java | 4 +- 6 files changed, 90 insertions(+), 121 deletions(-) diff --git a/pom.xml b/pom.xml index de3eed5..b763d07 100644 --- a/pom.xml +++ b/pom.xml @@ -99,10 +99,6 @@ configmaster-repo https://ci.pluginwiki.us/plugin/repository/everything/ - - folialib-repo - https://nexuslite.gcnt.net/repos/other/ - @@ -131,21 +127,19 @@ 24.1.0 - - net.kyori - adventure-text-serializer-plain - 4.16.0 - + net.kyori adventure-platform-bukkit 4.3.2 + net.kyori adventure-text-minimessage 4.16.0 + net.kyori adventure-text-serializer-ansi @@ -153,19 +147,19 @@ net.kyori - adventure-text-logger-slf4j + adventure-text-serializer-legacy 4.16.0 net.kyori - adventure-text-serializer-legacy + adventure-text-serializer-plain 4.16.0 - + - com.tcoded - FoliaLib - 0.3.1 + net.kyori + adventure-text-logger-slf4j + 4.16.0 diff --git a/src/main/java/me/xginko/betterworldstats/BetterWorldStats.java b/src/main/java/me/xginko/betterworldstats/BetterWorldStats.java index 022a7aa..04b56c3 100644 --- a/src/main/java/me/xginko/betterworldstats/BetterWorldStats.java +++ b/src/main/java/me/xginko/betterworldstats/BetterWorldStats.java @@ -1,6 +1,5 @@ package me.xginko.betterworldstats; -import com.tcoded.folialib.FoliaLib; import me.xginko.betterworldstats.commands.BWSCmd; import me.xginko.betterworldstats.config.Config; import me.xginko.betterworldstats.config.LanguageCache; @@ -32,7 +31,6 @@ public final class BetterWorldStats extends JavaPlugin { public static final Style STYLE = Style.style(COLOR, TextDecoration.BOLD); private static BetterWorldStats instance; - private static FoliaLib foliaLib; private static Map languageCacheMap; private static Config config; private static Statistics statistics; @@ -44,7 +42,6 @@ public final class BetterWorldStats extends JavaPlugin { @Override public void onEnable() { instance = this; - foliaLib = new FoliaLib(this); audiences = BukkitAudiences.create(this); logger = ComponentLogger.logger(getLogger().getName()); metrics = new Metrics(this, 17204); @@ -66,10 +63,6 @@ public void onEnable() { @Override public void onDisable() { HandlerList.unregisterAll(this); - if (foliaLib != null) { - foliaLib.getImpl().cancelAllTasks(); - foliaLib = null; - } if (audiences != null) { audiences.close(); audiences = null; @@ -97,10 +90,6 @@ public void onDisable() { return statistics; } - public static @NotNull FoliaLib getFoliaLib() { - return foliaLib; - } - public static @NotNull BukkitAudiences getAudiences() { return audiences; } diff --git a/src/main/java/me/xginko/betterworldstats/PAPIExpansion.java b/src/main/java/me/xginko/betterworldstats/PAPIExpansion.java index 54d7042..93241a2 100644 --- a/src/main/java/me/xginko/betterworldstats/PAPIExpansion.java +++ b/src/main/java/me/xginko/betterworldstats/PAPIExpansion.java @@ -51,17 +51,17 @@ public boolean canRegister() { public @Nullable String onPlaceholderRequest(Player player, @NotNull String identifier) { switch (identifier) { case "size": - return config.filesize_format.format(statistics.fileStats.getTrueSize()); + return statistics.fileStats.getSize(); case "spoofsize": - return config.filesize_format.format(statistics.fileStats.getSpoofedSize()); + return statistics.fileStats.getSpoofedSize(); case "file_count": - return Integer.toString(statistics.fileStats.getFileCount()); + return statistics.fileStats.getFileCount(); case "folder_count": - return Integer.toString(statistics.fileStats.getFolderCount()); + return statistics.fileStats.getFolderCount(); case "chunk_file_count": - return Integer.toString(statistics.fileStats.getChunkFileCount()); + return statistics.fileStats.getChunkFileCount(); case "players": - return Integer.toString(statistics.players.getUniqueJoins()); + return statistics.players.getUniqueJoins(); case "days": return statistics.mapAge.getDaysPart().toString(); case "months": diff --git a/src/main/java/me/xginko/betterworldstats/commands/worldstats/WorldStatsCmd.java b/src/main/java/me/xginko/betterworldstats/commands/worldstats/WorldStatsCmd.java index c8a5292..e4688e2 100644 --- a/src/main/java/me/xginko/betterworldstats/commands/worldstats/WorldStatsCmd.java +++ b/src/main/java/me/xginko/betterworldstats/commands/worldstats/WorldStatsCmd.java @@ -46,15 +46,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command statistics.mapAge.getYearsPart().toString(), statistics.mapAge.getMonthsPart().toString(), statistics.mapAge.getDaysPart().toString(), - Integer.toString(statistics.players.getUniqueJoins()), - config.filesize_format.format(statistics.fileStats.getTrueSize()), - config.filesize_format.format(statistics.fileStats.getSpoofedSize()), + statistics.players.getUniqueJoins(), + statistics.fileStats.getSize(), + statistics.fileStats.getSpoofedSize(), statistics.mapAge.asDays().toString(), statistics.mapAge.asMonths().toString(), statistics.mapAge.asYears().toString(), - Integer.toString(statistics.fileStats.getFileCount()), - Integer.toString(statistics.fileStats.getFolderCount()), - Integer.toString(statistics.fileStats.getChunkFileCount()) + statistics.fileStats.getFileCount(), + statistics.fileStats.getFolderCount(), + statistics.fileStats.getChunkFileCount() )) { KyoriUtil.sendMessage(sender, line); } diff --git a/src/main/java/me/xginko/betterworldstats/stats/FileStats.java b/src/main/java/me/xginko/betterworldstats/stats/FileStats.java index 12e8375..10342ed 100644 --- a/src/main/java/me/xginko/betterworldstats/stats/FileStats.java +++ b/src/main/java/me/xginko/betterworldstats/stats/FileStats.java @@ -1,112 +1,98 @@ package me.xginko.betterworldstats.stats; -import com.google.common.util.concurrent.AtomicDouble; -import com.tcoded.folialib.impl.ServerImplementation; import me.xginko.betterworldstats.BetterWorldStats; import me.xginko.betterworldstats.config.Config; import org.jetbrains.annotations.NotNull; import java.io.File; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; public class FileStats { - private final @NotNull ServerImplementation scheduler; private final @NotNull Config config; - private final @NotNull AtomicDouble sizeInGB; - private final @NotNull AtomicInteger filesTotal, chunkFiles, foldersTotal; - private long next_possible_check_time_millis = 0L; + private final @NotNull AtomicReference recentScan; + private final @NotNull AtomicLong cooldown; public FileStats() { - this.scheduler = BetterWorldStats.getFoliaLib().getImpl(); this.config = BetterWorldStats.getConfiguration(); - this.sizeInGB = new AtomicDouble(0.0); - this.filesTotal = this.chunkFiles = this.foldersTotal = new AtomicInteger(0); - this.updateAsync(); // Check on init so values aren't 0 on first request + this.recentScan = new AtomicReference<>(new ScanResult(config.paths_to_scan)); + this.cooldown = new AtomicLong(System.currentTimeMillis() + config.filesize_update_period_millis); } - private void updateAsync() { - final long current_time_millis = System.currentTimeMillis(); - // If on cooldown, do nothing - if (current_time_millis < next_possible_check_time_millis) return; - // Schedule check async - this.scheduler.runAsync(updateFileSize -> { - // Reset values so they can be updated next - this.filesTotal.set(0); - this.chunkFiles.set(0); - this.foldersTotal.set(0); - // Get total size, updating other stats in the process - final double totalSize = this.getTotalSizeInGB(); - this.sizeInGB.set(totalSize); - // Log - if (config.log_is_enabled) { - BetterWorldStats.getLog().info("Updated world size asynchronously " - + "(Real size: " + config.filesize_format.format(totalSize) + "GB, " - + "Spoofed size: " + config.filesize_format - .format(totalSize + config.additional_spoof_filesize) + "GB)."); - } - // Set cooldown - this.next_possible_check_time_millis = current_time_millis + config.filesize_update_period_millis; - }); + private void onRequest() { + if (cooldown.get() <= System.currentTimeMillis()) { + CompletableFuture.supplyAsync(() -> { + cooldown.set(System.currentTimeMillis() + config.filesize_update_period_millis); + return new ScanResult(config.paths_to_scan); + }).thenAccept(recentScan::set); + } } - public double getSpoofedSize() { - return this.getTrueSize() + config.additional_spoof_filesize; + public String getSize() { + onRequest(); + return config.filesize_format.format(recentScan.get().sizeInGB); } - public double getTrueSize() { - this.updateAsync(); - return this.sizeInGB.get(); + public String getSpoofedSize() { + onRequest(); + return config.filesize_format.format(recentScan.get().sizeInGB + config.additional_spoof_filesize); } - private double getTotalSizeInGB() { - long byteSize = 0L; - for (String path : config.paths_to_scan) { - byteSize += this.getByteSize(new File(path)); - } - return byteSize / 1048576.0D / 1000.0D; + public String getFolderCount() { + onRequest(); + return Integer.toString(recentScan.get().foldersTotal); } - private long getByteSize(File file) { - long bytes = 0L; - if (file.isFile()) { - this.filesTotal.getAndIncrement(); // Count file - if (file.getName().endsWith(".mca")) { // Check if is chunk file - final File parent = file.getParentFile(); - if (parent.isDirectory() && parent.getName().toLowerCase().contains("region")) { - this.chunkFiles.getAndIncrement(); - } - } - bytes += file.length(); - return bytes; - } - if (file.isDirectory()) { - this.foldersTotal.getAndIncrement(); // Count folder - try { - File[] subFiles = file.listFiles(); - assert subFiles != null; - for (File subFile : subFiles) { - bytes += this.getByteSize(subFile); - } - } catch (SecurityException e) { - BetterWorldStats.getLog().error("Could not read directory '"+file.getPath()+"' because access was denied.", e); - } - } - return bytes; + public String getFileCount() { + onRequest(); + return Integer.toString(recentScan.get().filesTotal); } - public int getFolderCount() { - this.updateAsync(); - return this.foldersTotal.get(); + public String getChunkFileCount() { + onRequest(); + return Integer.toString(recentScan.get().chunkFilesTotal); } - public int getFileCount() { - this.updateAsync(); - return this.filesTotal.get(); - } + private static class ScanResult { + public final double sizeInGB; + public int filesTotal, chunkFilesTotal, foldersTotal; + + protected ScanResult(@NotNull Iterable paths_to_scan) { + this.filesTotal = this.chunkFilesTotal = this.foldersTotal = 0; + long byteSize = 0L; + for (String path : paths_to_scan) + byteSize += this.getByteSize(new File(path)); + this.sizeInGB = byteSize / 1048576.0D / 1000.0D; + } - public int getChunkFileCount() { - this.updateAsync(); - return this.chunkFiles.get(); + private long getByteSize(File file) { + long bytes = 0L; + if (file.isFile()) { + this.filesTotal++; // Count file + if (file.getName().endsWith(".mca")) { // Check if is chunk file + final File parent = file.getParentFile(); + if (parent.isDirectory() && parent.getName().toLowerCase().contains("region")) { + this.chunkFilesTotal++; + } + } + bytes += file.length(); + return bytes; + } + if (file.isDirectory()) { + this.foldersTotal++; // Count folder + try { + File[] subFiles = file.listFiles(); + assert subFiles != null; + for (File subFile : subFiles) { + bytes += this.getByteSize(subFile); + } + } catch (SecurityException e) { + BetterWorldStats.getLog().error("Could not read directory '"+file.getPath()+"'.", e); + } + } + return bytes; + } } } \ No newline at end of file diff --git a/src/main/java/me/xginko/betterworldstats/stats/Players.java b/src/main/java/me/xginko/betterworldstats/stats/Players.java index 8126d49..5b48d36 100644 --- a/src/main/java/me/xginko/betterworldstats/stats/Players.java +++ b/src/main/java/me/xginko/betterworldstats/stats/Players.java @@ -26,7 +26,7 @@ private void onPlayerJoin(PlayerJoinEvent event) { } } - public int getUniqueJoins() { - return this.uniquePlayers.get(); + public String getUniqueJoins() { + return uniquePlayers.toString(); } }