Skip to content

Commit

Permalink
change config management, improve stability and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 13, 2023
1 parent 6f67c70 commit fd8f2b3
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 105 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.xGinko</groupId>
<artifactId>BetterWorldStats</artifactId>
<version>1.3.1</version>
<version>1.4</version>
<packaging>jar</packaging>

<name>BetterWorldStats</name>
Expand Down Expand Up @@ -76,6 +76,10 @@
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>configmaster-repo</id>
<url>https://ci.pluginwiki.us/plugin/repository/everything/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -92,6 +96,12 @@
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<!-- Configuration Master for clean config file management -->
<dependency>
<groupId>com.github.thatsmusic99</groupId>
<artifactId>ConfigurationMaster-API</artifactId>
<version>v2.0.0-BETA-3</version>
</dependency>
<!-- reflections used for auto-lang feature -->
<dependency>
<groupId>org.reflections</groupId>
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/me/xGinko/betterworldstats/BetterWorldStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@
import java.util.regex.Pattern;

public final class BetterWorldStats extends JavaPlugin implements Listener {

private static BetterWorldStats instance;
private static ConfigCache configCache;
private static HashMap<String, LanguageCache> languageCacheMap;
private static Logger logger;
private static boolean PapiIsEnabled = false;
private static double fileSize;
private static int uniquePlayers;

@Override
public void onEnable() {
instance = this;
Logger logger = getLogger();
uniquePlayers = getServer().getOfflinePlayers().length;
logger = getLogger();
logger.info(ChatColor.AQUA + "Reading config");
reloadBetterWorldStats();
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
PapiIsEnabled = true;
logger.info(ChatColor.AQUA + "Found PlaceholderAPI, registering placeholders");
logger.info(ChatColor.AQUA + "Found PlaceholderAPI, registering placeholders...");
new PAPIExpansion().register();
}
logger.info(ChatColor.AQUA + "Registering commands");
Expand All @@ -69,22 +72,20 @@ private void onPlayerJoinEvent(PlayerJoinEvent event) {

public void reloadBetterWorldStats() {
reloadLang();
saveDefaultConfig();
reloadConfig();
configCache = new ConfigCache();
configCache.saveConfig();

HandlerList.unregisterAll((Plugin) this);
BukkitScheduler scheduler = getServer().getScheduler();
scheduler.cancelTasks(this);

uniquePlayers = getServer().getOfflinePlayers().length;
scheduler.runTaskTimerAsynchronously(this, () -> {
fileSize = count() / 1048576.0D / 1000.0D;
uniquePlayers = getServer().getOfflinePlayers().length;
if (configCache.logIsEnabled) {
getLogger().info("Updated filesize (" + configCache.fileSizeFormat.format(fileSize) + " GB) and unique players (" + uniquePlayers + ") asynchronously.");
if (configCache.log_is_enabled) {
logger.info("Updated filesize (" + configCache.filesize_display_format.format(fileSize) + "GB) asynchronously. Unique player joins: " + uniquePlayers);
}
}, 0L, configCache.fileSizeUpdateDelay);
}, 0L, configCache.filesize_update_period);

getServer().getPluginManager().registerEvents(this, this);
}
Expand All @@ -96,7 +97,7 @@ public void reloadLang() {
Files.createDirectories(langDirectory.toPath());
for (String fileName : getDefaultLanguageFiles()) {
String localeString = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.'));
getLogger().info(String.format("Found language file for %s", localeString));
logger.info(String.format("Found language file for %s", localeString));
LanguageCache langCache = new LanguageCache(localeString);
languageCacheMap.put(localeString, langCache);
}
Expand All @@ -106,21 +107,21 @@ public void reloadLang() {
if (langMatcher.find()) {
String localeString = langMatcher.group(1).toLowerCase();
if(!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded
getLogger().info(String.format("Found language file for %s", localeString));
logger.info(String.format("Found language file for %s", localeString));
LanguageCache langCache = new LanguageCache(localeString);
languageCacheMap.put(localeString, langCache);
}
}
}
} catch (IOException e) {
e.printStackTrace();
getLogger().severe("Error loading language files! Language files will not reload to avoid errors, make sure to correct this before restarting the server!");
logger.severe("Error loading language files! Language files will not reload to avoid errors, make sure to correct this before restarting the server!");
}
}

private long count() {
final AtomicLong atomicLong = new AtomicLong(0L);
for (String path : configCache.directoriesToScan) {
for (String path : configCache.directories_to_scan) {
for (File file : Objects.requireNonNull(new File(path).listFiles())) {
if (file.isFile()) {
atomicLong.addAndGet(file.length());
Expand Down Expand Up @@ -172,4 +173,5 @@ public static int getUniquePlayers() {
public static boolean isPAPIEnabled() {
return PapiIsEnabled;
}

}
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 @@ -46,27 +46,27 @@ public boolean canRegister() {
@Override
public String onPlaceholderRequest(Player player, @NotNull String identifier) {
if (identifier.equals("size")) {
return String.valueOf(configCache.fileSizeFormat.format(BetterWorldStats.getFileSize()));
return String.valueOf(configCache.filesize_display_format.format(BetterWorldStats.getFileSize()));
}
if (identifier.equals("spoof")) {
return String.valueOf(configCache.fileSizeFormat.format(BetterWorldStats.getFileSize() + configCache.spoofSize));
return String.valueOf(configCache.filesize_display_format.format(BetterWorldStats.getFileSize() + configCache.additional_spoofed_filesize));
}
if (identifier.equals("players")) {
return String.valueOf(BetterWorldStats.getUniquePlayers());
}
if (identifier.equals("ageindays")) {
return String.valueOf(TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - configCache.serverBirthTime));
return String.valueOf(TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - configCache.server_birth_time));
}
if (identifier.equals("days")) {
calendar.setTimeInMillis(System.currentTimeMillis() - configCache.serverBirthTime);
calendar.setTimeInMillis(System.currentTimeMillis() - configCache.server_birth_time);
return String.valueOf(calendar.get(Calendar.DAY_OF_MONTH) - 1);
}
if (identifier.equals("months")) {
calendar.setTimeInMillis(System.currentTimeMillis() - configCache.serverBirthTime);
calendar.setTimeInMillis(System.currentTimeMillis() - configCache.server_birth_time);
return String.valueOf(calendar.get(Calendar.MONTH));
}
if (identifier.equals("years")) {
calendar.setTimeInMillis(System.currentTimeMillis() - configCache.serverBirthTime);
calendar.setTimeInMillis(System.currentTimeMillis() - configCache.server_birth_time);
return String.valueOf(calendar.get(Calendar.YEAR) - 1970);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ public class BetterWSCmd implements CommandExecutor, TabCompleter {

private final ArrayList<SubCommand> subcommands = new ArrayList<>();
private final List<String> tabCompletes = new ArrayList<>();

public BetterWSCmd() {
subcommands.add(new ReloadCmd());
subcommands.add(new VersionCmd());
for (int i=0; i<getSubcommands().size(); i++) {
tabCompletes.add(getSubcommands().get(i).getName());
}
}

public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (args.length > 0) {
return tabCompletes;
}
return null;
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (args.length > 0) {
Expand All @@ -48,5 +51,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}
return true;
}
public ArrayList<SubCommand> getSubcommands() {return subcommands;}

public ArrayList<SubCommand> getSubcommands() {
return subcommands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@

public class WorldStatsCmd implements CommandExecutor, Listener {

private final ConfigCache configCache;
private final Calendar calendar;
private final boolean isPapiEnabled;

public WorldStatsCmd() {
this.configCache = BetterWorldStats.getConfiguration();
this.calendar = Calendar.getInstance();
this.isPapiEnabled = BetterWorldStats.isPAPIEnabled();
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!sender.hasPermission("betterws.worldstats")) return true;
ConfigCache configCache = BetterWorldStats.getConfiguration();

calendar.setTimeInMillis(System.currentTimeMillis() - configCache.serverBirthTime);
calendar.setTimeInMillis(System.currentTimeMillis() - configCache.server_birth_time);
int year = calendar.get(Calendar.YEAR) - 1970;
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH) - 1;
Expand All @@ -42,20 +41,20 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
String monthAsString = String.valueOf(month);
String dayAsString = String.valueOf(day);

for (String line : BetterWorldStats.getLang(sender).worldStatsMessage) {
sender.sendMessage(format(line, yearAsString, monthAsString, dayAsString));
for (String line : BetterWorldStats.getLang(sender).world_stats_message) {
sender.sendMessage(format(line, yearAsString, monthAsString, dayAsString, configCache));
}

return true;
}

private String format(String line, String year, String month, String day) {
private String format(String line, String year, String month, String day, ConfigCache configCache) {
final String parsedLine = ChatColor.translateAlternateColorCodes('&', line)
.replace("%years%", year)
.replace("%months%", month)
.replace("%days%", day)
.replace("%size%", configCache.fileSizeFormat.format(BetterWorldStats.getFileSize()))
.replace("%spoof%", configCache.fileSizeFormat.format(BetterWorldStats.getFileSize() + configCache.spoofSize))
.replace("%size%", configCache.filesize_display_format.format(BetterWorldStats.getFileSize()))
.replace("%spoof%", configCache.filesize_display_format.format(BetterWorldStats.getFileSize() + configCache.additional_spoofed_filesize))
.replace("%players%", String.valueOf(BetterWorldStats.getUniquePlayers()));
return isPapiEnabled ? PlaceholderAPI.setPlaceholders(null, parsedLine) : parsedLine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void perform(@NotNull CommandSender sender, String[] args) {
BetterWorldStats.getInstance().reloadBetterWorldStats();
sender.sendMessage(ChatColor.GREEN + "Reload complete.");
} else {
sender.sendMessage(ChatColor.RED + BetterWorldStats.getLang(sender).noPermissions);
sender.sendMessage(ChatColor.RED + BetterWorldStats.getLang(sender).no_permissions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void perform(CommandSender sender, String[] args) {
if (sender.hasPermission("betterws.version")) {
sender.sendMessage(ChatColor.AQUA + "BetterWorldStats v" + BetterWorldStats.getInstance().getDescription().getVersion() + ChatColor.DARK_AQUA + " by xGinko");
} else {
sender.sendMessage(ChatColor.RED + BetterWorldStats.getLang(sender).noPermissions);
sender.sendMessage(ChatColor.RED + BetterWorldStats.getLang(sender).no_permissions);
}
}
}
Loading

0 comments on commit fd8f2b3

Please sign in to comment.