Skip to content

Commit

Permalink
structural improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 27, 2024
1 parent 24f83a4 commit 0ec9560
Show file tree
Hide file tree
Showing 22 changed files with 264 additions and 209 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ repositories {
}

dependencies {
compileOnly("com.destroystokyo.paper:paper-api:1.12.2-R0.1-SNAPSHOT")
compileOnly("org.jetbrains:annotations:24.1.0")
compileOnly("com.destroystokyo.paper:paper-api:1.12.2-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.5")

implementation("net.kyori:adventure-platform-bukkit:4.3.2")
Expand Down
112 changes: 57 additions & 55 deletions src/main/java/me/xginko/betterworldstats/BetterWorldStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import me.xginko.betterworldstats.commands.BWSCmd;
import me.xginko.betterworldstats.config.Config;
import me.xginko.betterworldstats.config.LanguageCache;
import me.xginko.betterworldstats.hooks.BWSHook;
import me.xginko.betterworldstats.hooks.PAPIExpansion;
import me.xginko.betterworldstats.utils.KyoriUtil;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import org.bstats.bukkit.Metrics;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

Expand All @@ -24,36 +22,34 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;

public final class BetterWorldStats extends JavaPlugin {
public static final TextColor COLOR = TextColor.color(0,204,34);
public static final Style STYLE = Style.style(COLOR, TextDecoration.BOLD);

private static final Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE);
private static BetterWorldStats instance;
private static Map<String, LanguageCache> languageCacheMap;
private static Config config;
private static Statistics statistics;
private static PAPIExpansion papiExpansion;
private static BukkitAudiences audiences;
private static ComponentLogger logger;
private static Metrics metrics;
private static Metrics bStats;

@Override
public void onEnable() {
instance = this;
audiences = BukkitAudiences.create(this);
logger = ComponentLogger.logger(getLogger().getName());
metrics = new Metrics(this, 17204);
logger.info(Component.text(" ").style(STYLE));
logger.info(Component.text(" ___ _ _ ").style(STYLE));
logger.info(Component.text(" | _ ) ___| |_| |_ ___ _ _ ").style(STYLE));
logger.info(Component.text(" | _ \\/ -_) _| _/ -_) '_| ").style(STYLE));
logger.info(Component.text(" __|___/\\___|\\__|\\__\\___|_|_ _ _ ").style(STYLE));
logger.info(Component.text(" \\ \\ / /__ _ _| |__| / __| |_ __ _| |_ ___").style(STYLE));
logger.info(Component.text(" \\ \\/\\/ / _ \\ '_| / _` \\__ \\ _/ _` | _(_-<").style(STYLE));
logger.info(Component.text(" \\_/\\_/\\___/_| |_\\__,_|___/\\__\\__,_|\\__/__/").style(STYLE));
logger.info(Component.text(" ").style(STYLE));
bStats = new Metrics(this, 17204);

logger.info(Component.text(" ").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" ___ _ _ ").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" | _ ) ___| |_| |_ ___ _ _ ").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" | _ \\/ -_) _| _/ -_) '_| ").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" __|___/\\___|\\__|\\__\\___|_|_ _ _ ").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" \\ \\ / /__ _ _| |__| / __| |_ __ _| |_ ___").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" \\ \\/\\/ / _ \\ '_| / _` \\__ \\ _/ _` | _(_-<").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" \\_/\\_/\\___/_| |_\\__,_|___/\\__\\__,_|\\__/__/").style(KyoriUtil.GUPPIE_GREEN_BOLD));
logger.info(Component.text(" ").style(KyoriUtil.GUPPIE_GREEN_BOLD));

logger.info("Loading languages");
reloadLang();
logger.info("Loading config");
Expand All @@ -65,20 +61,19 @@ public void onEnable() {

@Override
public void onDisable() {
HandlerList.unregisterAll(this);
BWSHook.HOOKS.forEach(BWSHook::unHook);
if (statistics != null) {
statistics.shutdown();
statistics = null;
}
if (audiences != null) {
audiences.close();
audiences = null;
}
if (papiExpansion != null) {
papiExpansion.unregister();
papiExpansion = null;
}
if (metrics != null) {
metrics.shutdown();
metrics = null;
if (bStats != null) {
bStats.shutdown();
bStats = null;
}
statistics = null;
config = null;
languageCacheMap = null;
logger = null;
Expand Down Expand Up @@ -126,13 +121,11 @@ public void reloadPlugin() {

private void reloadConfiguration() {
try {
if (statistics != null) statistics.shutdown();
if (statistics != null)
statistics.shutdown();
config = new Config();
statistics = new Statistics();
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
if (papiExpansion != null) papiExpansion.unregister();
papiExpansion = new PAPIExpansion();
}
BWSHook.reloadHooks();
config.saveConfig();
} catch (Exception e) {
logger.error("Failed loading config!", e);
Expand All @@ -144,35 +137,44 @@ public void reloadLang() {
try {
File langDirectory = new File(getDataFolder() + "/lang");
Files.createDirectories(langDirectory.toPath());
for (String fileName : getDefaultLanguageFiles()) {
final String localeString = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.'));
Set<String> locales = new HashSet<>();
locales.addAll(getDefaultLocales(getFile()));
locales.addAll(getPresentLocales(langDirectory));
for (String localeString : locales) {
logger.info("Found language file for " + localeString);
languageCacheMap.put(localeString, new LanguageCache(localeString));
}
final Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE);
for (File langFile : langDirectory.listFiles()) {
final Matcher langMatcher = langPattern.matcher(langFile.getName());
if (langMatcher.find()) {
final String localeString = langMatcher.group(1).toLowerCase();
if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded
logger.info("Found language file for " + localeString);
languageCacheMap.put(localeString, new LanguageCache(localeString));
}
}
}
} catch (Exception e) {
logger.error("Error loading language files!", e);
} catch (Throwable t) {
logger.error("Error loading language files!", t);
}
}

private @NotNull Set<String> getDefaultLanguageFiles() {
try (final JarFile pluginJarFile = new JarFile(this.getFile())) {
private @NotNull Set<String> getDefaultLocales(File jarFile) {
try (final JarFile pluginJarFile = new JarFile(jarFile)) {
return pluginJarFile.stream()
.map(ZipEntry::getName)
.filter(name -> name.startsWith("lang/") && name.endsWith(".yml"))
.map(zipEntry -> {
Matcher matcher = langPattern.matcher(zipEntry.getName());
return matcher.find() ? matcher.group(1) : null;
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
} catch (Throwable t) {
logger.error("Failed getting default lang files!", t);
return Collections.emptySet();
}
}

private @NotNull Set<String> getPresentLocales(File folder) {
try {
return Arrays.stream(Objects.requireNonNull(folder.listFiles()))
.map(file -> {
Matcher matcher = langPattern.matcher(file.getName());
return matcher.find() ? matcher.group(1) : null;
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
} catch (IOException e) {
logger.error("Failed getting default lang files!", e);
} catch (Throwable t) {
logger.error("Failed getting lang files from plugin folder!", t);
return Collections.emptySet();
}
}
Expand Down
76 changes: 0 additions & 76 deletions src/main/java/me/xginko/betterworldstats/PAPIExpansion.java

This file was deleted.

23 changes: 11 additions & 12 deletions src/main/java/me/xginko/betterworldstats/Statistics.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package me.xginko.betterworldstats;

import me.xginko.betterworldstats.stats.FileStats;
import me.xginko.betterworldstats.stats.MapAge;
import me.xginko.betterworldstats.stats.Players;
import org.bukkit.event.HandlerList;
import me.xginko.betterworldstats.stats.WorldStats;
import me.xginko.betterworldstats.stats.BirthCalendar;
import me.xginko.betterworldstats.stats.PlayerStats;
import org.jetbrains.annotations.NotNull;

public class Statistics {
public final class Statistics {

public final @NotNull MapAge mapAge;
public final @NotNull FileStats fileStats;
public final @NotNull Players players;
public final @NotNull BirthCalendar birthCalendar;
public final @NotNull WorldStats worldStats;
public final @NotNull PlayerStats playerStats;

public Statistics() {
this.fileStats = new FileStats();
this.mapAge = new MapAge();
this.players = new Players();
this.worldStats = new WorldStats();
this.birthCalendar = new BirthCalendar();
this.playerStats = new PlayerStats();
}

public void shutdown() {
HandlerList.unregisterAll(players);
playerStats.disable();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.xginko.betterworldstats.commands.betterworldstats;

import me.xginko.betterworldstats.BetterWorldStats;
import me.xginko.betterworldstats.commands.BWSCmd;
import me.xginko.betterworldstats.commands.SubCmd;
import me.xginko.betterworldstats.commands.betterworldstats.subcommands.ReloadSubCmd;
Expand Down Expand Up @@ -58,11 +57,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

private void sendCommandOverview(CommandSender sender) {
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(BetterWorldStats.COLOR));
KyoriUtil.sendMessage(sender, Component.text("BetterWorldStats Commands").color(BetterWorldStats.COLOR));
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(BetterWorldStats.COLOR));
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(KyoriUtil.GUPPIE_GREEN));
KyoriUtil.sendMessage(sender, Component.text("BetterWorldStats Commands").color(KyoriUtil.GUPPIE_GREEN));
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(KyoriUtil.GUPPIE_GREEN));
for (SubCmd subCmd : subCmds) KyoriUtil.sendMessage(sender,
subCmd.getSyntax().append(Component.text(" - ").color(NamedTextColor.DARK_GRAY)).append(subCmd.getDescription()));
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(BetterWorldStats.COLOR));
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(KyoriUtil.GUPPIE_GREEN));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public TextComponent getDescription() {

@Override
public TextComponent getSyntax() {
return Component.text("/bws reload").color(BetterWorldStats.COLOR);
return Component.text("/bws reload").color(KyoriUtil.GUPPIE_GREEN);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public TextComponent getDescription() {

@Override
public TextComponent getSyntax() {
return Component.text("/bws version").color(BetterWorldStats.COLOR);
return Component.text("/bws version").color(KyoriUtil.GUPPIE_GREEN);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

for (final Component line : BetterWorldStats.getLang(sender).worldStatsMsg(
sender,
statistics.mapAge.getYearsPart().toString(),
statistics.mapAge.getMonthsPart().toString(),
statistics.mapAge.getDaysPart().toString(),
statistics.players.getUniqueJoins(),
statistics.fileStats.getSize(),
statistics.fileStats.getSpoofedSize(),
statistics.mapAge.asDays().toString(),
statistics.mapAge.asMonths().toString(),
statistics.mapAge.asYears().toString(),
statistics.fileStats.getFileCount(),
statistics.fileStats.getFolderCount(),
statistics.fileStats.getChunkCount(),
statistics.fileStats.getEntityCount()
statistics.birthCalendar.getYearsPart().toString(),
statistics.birthCalendar.getMonthsPart().toString(),
statistics.birthCalendar.getDaysPart().toString(),
statistics.playerStats.getUniqueJoins(),
statistics.worldStats.getSize(),
statistics.worldStats.getSpoofedSize(),
statistics.birthCalendar.asDays().toString(),
statistics.birthCalendar.asMonths().toString(),
statistics.birthCalendar.asYears().toString(),
statistics.worldStats.getFileCount(),
statistics.worldStats.getFolderCount(),
statistics.worldStats.getChunkCount(),
statistics.worldStats.getEntityCount()
)) {
KyoriUtil.sendMessage(sender, line);
}
Expand Down
Loading

0 comments on commit 0ec9560

Please # to comment.