Skip to content

Commit 0391de7

Browse files
committed
let users get size for any worlds, not just the main 3
1 parent b337b1a commit 0391de7

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

src/main/java/lol/hyper/anarchystats/AnarchyStats.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.File;
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
15+
import java.util.ArrayList;
1516
import java.util.logging.Logger;
1617

1718
public final class AnarchyStats extends JavaPlugin {
@@ -20,6 +21,7 @@ public final class AnarchyStats extends JavaPlugin {
2021
public final File configFile = new File(this.getDataFolder(), "config.yml");
2122
public FileConfiguration config;
2223
public final Logger logger = this.getLogger();
24+
public final ArrayList<Path> worldPaths = new ArrayList<>();
2325

2426
public CommandInfo commandInfo;
2527
public CommandReload commandReload;
@@ -42,7 +44,7 @@ public void onEnable() {
4244

4345
this.getCommand(config.getString("info-command-override")).setExecutor(commandInfo);
4446
this.getCommand("anarchystats").setExecutor(commandReload);
45-
Bukkit.getScheduler().runTaskAsynchronously(this, () -> this.updateWorldSize(WorldSize.world, WorldSize.nether, WorldSize.end));
47+
Bukkit.getScheduler().runTaskAsynchronously(this, this::updateWorldSize);
4648

4749
new Updater(this, 66089).getVersion(version -> {
4850
if (this.getDescription().getVersion().equalsIgnoreCase(version)) {
@@ -58,14 +60,22 @@ public void onEnable() {
5860
public void onDisable() {
5961
}
6062

61-
public void updateWorldSize(Path world, Path nether, Path end) {
62-
worldSize = WorldSize.readableFileSize(WorldSize.getWorldSize(world) + WorldSize.getWorldSize(nether) + WorldSize.getWorldSize(end));
63+
public void updateWorldSize() {
64+
worldSize = WorldSize.readableFileSize(WorldSize.getWorldSize(worldPaths));
6365
}
6466

6567
public void loadConfig() {
6668
config = YamlConfiguration.loadConfiguration(configFile);
67-
WorldSize.world = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + config.getString("world-files.overworld"));
68-
WorldSize.nether = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + config.getString("world-files.nether"));
69-
WorldSize.end = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + config.getString("world-files.end"));
69+
if (worldPaths.size() > 0) {
70+
worldPaths.clear();
71+
}
72+
for (String x : config.getStringList("worlds-to-use")) {
73+
Path currentPath = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + x);
74+
if (!currentPath.toFile().exists()) {
75+
logger.warning("World file " + x + " does not exist!");
76+
} else {
77+
worldPaths.add(currentPath);
78+
}
79+
}
7080
}
7181
}

src/main/java/lol/hyper/anarchystats/WorldSize.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.file.*;
77
import java.nio.file.attribute.BasicFileAttributes;
88
import java.text.DecimalFormat;
9+
import java.util.ArrayList;
910
import java.util.concurrent.atomic.AtomicLong;
1011

1112
public class WorldSize {
@@ -21,38 +22,40 @@ public class WorldSize {
2122
*
2223
* https://stackoverflow.com/a/19877372
2324
*/
24-
public static long getWorldSize(Path path) {
25+
public static long getWorldSize(ArrayList<Path> paths) {
2526

2627
final AtomicLong size = new AtomicLong(0);
2728

28-
try {
29-
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
30-
@Override
31-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
29+
for (Path p : paths) {
30+
try {
31+
Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
32+
@Override
33+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
3234

33-
size.addAndGet(attrs.size());
34-
return FileVisitResult.CONTINUE;
35-
}
35+
size.addAndGet(attrs.size());
36+
return FileVisitResult.CONTINUE;
37+
}
3638

37-
@Override
38-
public FileVisitResult visitFileFailed(Path file, IOException exc) {
39+
@Override
40+
public FileVisitResult visitFileFailed(Path file, IOException exc) {
3941

40-
Bukkit.getLogger().warning("File " + file + " doesn't exist. (" + exc + ")");
41-
// Skip folders that can't be traversed
42-
return FileVisitResult.CONTINUE;
43-
}
42+
Bukkit.getLogger().warning("File " + file + " doesn't exist. (" + exc + ")");
43+
// Skip folders that can't be traversed
44+
return FileVisitResult.CONTINUE;
45+
}
4446

45-
@Override
46-
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
47+
@Override
48+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
4749

48-
if (exc != null)
49-
Bukkit.getLogger().warning("Had trouble traversing: " + dir + " (" + exc + ")");
50-
// Ignore errors traversing a folder
51-
return FileVisitResult.CONTINUE;
52-
}
53-
});
54-
} catch (IOException e) {
55-
throw new AssertionError("walkFileTree will not throw IOException if the FileVisitor does not");
50+
if (exc != null)
51+
Bukkit.getLogger().warning("Had trouble traversing: " + dir + " (" + exc + ")");
52+
// Ignore errors traversing a folder
53+
return FileVisitResult.CONTINUE;
54+
}
55+
});
56+
} catch (IOException e) {
57+
throw new AssertionError("walkFileTree will not throw IOException if the FileVisitor does not");
58+
}
5659
}
5760

5861
return size.get();

src/main/java/lol/hyper/anarchystats/commands/CommandInfo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import lol.hyper.anarchystats.AnarchyStats;
44
import lol.hyper.anarchystats.MessageParser;
5-
import lol.hyper.anarchystats.WorldSize;
65
import org.bukkit.Bukkit;
76
import org.bukkit.command.Command;
87
import org.bukkit.command.CommandExecutor;
@@ -20,7 +19,7 @@ public CommandInfo(AnarchyStats anarchyStats, MessageParser messageParser) {
2019

2120
@Override
2221
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
23-
Bukkit.getScheduler().runTaskAsynchronously(anarchyStats, () -> anarchyStats.updateWorldSize(WorldSize.world, WorldSize.nether, WorldSize.end));
22+
Bukkit.getScheduler().runTaskAsynchronously(anarchyStats, anarchyStats::updateWorldSize);
2423
for (String x : messageParser.getCommandMessage()) {
2524
sender.sendMessage(x);
2625
}

src/main/resources/config.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Some plugins use /info for things. This let's you change the plugins command so they don't override.
22
# Default is /info.
3+
# This requires a whole server restart.
34
info-command-override: "info"
45

56
# This is the start date of your world.
@@ -11,12 +12,13 @@ date: 05/27/2019 # This date is May 27th, 2019.
1112
# You can find examples on how to custom this here: https://www.journaldev.com/17899/java-simpledateformat-java-date-format <- There is a chart at the bottom of the page.
1213
date-format: "M/dd/yyyy"
1314

14-
# Some servers have different folder names for the worlds. Please update these to match yours.
15-
# Leave default if you didn't change the names.
16-
world-files:
17-
overworld: "world"
18-
nether: "world_nether"
19-
end: "world_the_end"
15+
# This let's you set which worlds will be included in the size calculation.
16+
# By default, it uses the default worlds.
17+
# You can add more or change the names.
18+
worlds-to-use:
19+
- "world"
20+
- "world_nether"
21+
- "world_the_end"
2022

2123
# This is where you can change what message is on the command.
2224
# {{STARTDATE}} - Displays the start date for the server. Uses the date above.

0 commit comments

Comments
 (0)