Skip to content

Commit

Permalink
kick folialib
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 24, 2024
1 parent 6724e8d commit 127b32e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 121 deletions.
24 changes: 9 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@
<id>configmaster-repo</id>
<url>https://ci.pluginwiki.us/plugin/repository/everything/</url>
</repository>
<repository>
<id>folialib-repo</id>
<url>https://nexuslite.gcnt.net/repos/other/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -131,41 +127,39 @@
<version>24.1.0</version>
</dependency>
<!-- Adventure for more configuration freedom in lang files -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-plain</artifactId>
<version>4.16.0</version>
</dependency>
<!-- Bukkit Implementation -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.3.2</version>
</dependency>
<!-- MiniMessage. This is what does the lang file to fancy text conversion -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.16.0</version>
</dependency>
<!-- Component serializers. -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-ansi</artifactId>
<version>4.16.0</version>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-logger-slf4j</artifactId>
<artifactId>adventure-text-serializer-legacy</artifactId>
<version>4.16.0</version>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId>
<artifactId>adventure-text-serializer-plain</artifactId>
<version>4.16.0</version>
</dependency>
<!-- Folia compatibility -->
<!-- Logger that supports printing stylized components to console -->
<dependency>
<groupId>com.tcoded</groupId>
<artifactId>FoliaLib</artifactId>
<version>0.3.1</version>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-logger-slf4j</artifactId>
<version>4.16.0</version>
</dependency>
<!-- ConfigurationMaster for easy config file management -->
<dependency>
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/me/xginko/betterworldstats/BetterWorldStats.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String, LanguageCache> languageCacheMap;
private static Config config;
private static Statistics statistics;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -97,10 +90,6 @@ public void onDisable() {
return statistics;
}

public static @NotNull FoliaLib getFoliaLib() {
return foliaLib;
}

public static @NotNull BukkitAudiences getAudiences() {
return audiences;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/me/xginko/betterworldstats/PAPIExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
148 changes: 67 additions & 81 deletions src/main/java/me/xginko/betterworldstats/stats/FileStats.java
Original file line number Diff line number Diff line change
@@ -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<ScanResult> 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<String> 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;
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/me/xginko/betterworldstats/stats/Players.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void onPlayerJoin(PlayerJoinEvent event) {
}
}

public int getUniqueJoins() {
return this.uniquePlayers.get();
public String getUniqueJoins() {
return uniquePlayers.toString();
}
}

0 comments on commit 127b32e

Please # to comment.