diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java index 2864fd3..0387a4c 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java @@ -37,7 +37,7 @@ public class Main extends JavaPlugin { getLogger().info("Amount of offline players: " + offlinePlayerHandler.getOfflinePlayerCount()); //get private lists ready with item/material/entity/stat names - EnumHandler.prepareLists(); + //EnumHandler.prepareLists(); //register the commands PluginCommand statcmd = this.getCommand("statistic"); @@ -46,7 +46,7 @@ public class Main extends JavaPlugin { statcmd.setTabCompleter(new TabCompleter(offlinePlayerHandler)); } PluginCommand reloadcmd = this.getCommand("statisticreload"); - if (reloadcmd != null) reloadcmd.setExecutor(new ReloadCommand(config, offlinePlayerHandler, outputFormatter)); + if (reloadcmd != null) reloadcmd.setExecutor(new ReloadCommand(config, offlinePlayerHandler, outputFormatter, this)); //register the listener Bukkit.getPluginManager().registerEvents(new JoinListener(offlinePlayerHandler), this); @@ -60,7 +60,7 @@ public class Main extends JavaPlugin { } public long logTimeTaken(String className, String methodName, long previousTime) { - getLogger().info(className + " " + methodName + " " + ": " + (System.currentTimeMillis() - previousTime)); + getLogger().info(className + " " + methodName + ": " + (System.currentTimeMillis() - previousTime)); return System.currentTimeMillis(); } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ReloadCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ReloadCommand.java index 6b56535..fa998af 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ReloadCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ReloadCommand.java @@ -1,5 +1,6 @@ package com.gmail.artemis.the.gr8.playerstats.commands; +import com.gmail.artemis.the.gr8.playerstats.Main; import com.gmail.artemis.the.gr8.playerstats.filehandlers.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.gmail.artemis.the.gr8.playerstats.utils.OutputFormatter; @@ -14,13 +15,14 @@ public class ReloadCommand implements CommandExecutor { private final ConfigHandler config; private final OfflinePlayerHandler offlinePlayerHandler; private final OutputFormatter outputFormatter; + private final Main plugin; - public ReloadCommand(ConfigHandler c, OfflinePlayerHandler of, OutputFormatter o) { + public ReloadCommand(ConfigHandler c, OfflinePlayerHandler of, OutputFormatter o, Main p) { offlinePlayerHandler = of; outputFormatter = o; config = c; - + plugin = p; } @Override @@ -29,6 +31,7 @@ public class ReloadCommand implements CommandExecutor { outputFormatter.updateOutputColors(); offlinePlayerHandler.updateOfflinePlayerList(); + plugin.getLogger().info("Amount of players: " + offlinePlayerHandler.getOfflinePlayerCount()); sender.sendMessage(outputFormatter.getPluginPrefix() + ChatColor.GREEN + "Config reloaded!"); return true; } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java index ece4180..b40da4a 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java @@ -5,24 +5,22 @@ import org.bukkit.Statistic; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; public class EnumHandler { - private static List blockNames; - private static List entityTypeNames; - private static List itemNames; - private static List statNames; - private static List entityStatNames; - private static List subStatEntryNames; + private final static List blockNames; + private final static List entityTypeNames; + private final static List itemNames; + private final static List statNames; + private final static List entityStatNames; + private final static List subStatEntryNames; - private EnumHandler() { - } - - public static void prepareLists() { + static{ blockNames = Arrays.stream(Material.values()).filter( Material::isBlock).map(Material::toString).map(String::toLowerCase).toList(); entityTypeNames = Arrays.stream(EntityType.values()).map( @@ -31,15 +29,14 @@ public class EnumHandler { Material::isItem).map(Material::toString).map(String::toLowerCase).toList(); statNames = Arrays.stream(Statistic.values()).map( Statistic::toString).map(String::toLowerCase).toList(); - entityStatNames = Arrays.stream(Statistic.values()).filter(statistic -> statistic.getType().equals(Statistic.Type.ENTITY)).map( Statistic::toString).map(String::toLowerCase).collect(Collectors.toList()); - subStatEntryNames = new ArrayList<>(); - subStatEntryNames.addAll(getBlockNames()); - subStatEntryNames.addAll(getEntityTypeNames()); - subStatEntryNames.addAll(getItemNames()); + subStatEntryNames = Stream.of(blockNames, entityTypeNames, itemNames).flatMap(Collection::stream).toList(); + } + + private EnumHandler() { } //checks whether the provided string is a valid item diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java index 23a60c8..c85e963 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java @@ -33,7 +33,7 @@ public class OfflinePlayerHandler { updateOfflinePlayerList(config.whitelistOnly(), config.excludeBanned(), config.lastPlayedLimit()); } - //stores a private HashMap with keys:playerName and values:UUID, and a private list of the names for easy access + //stores a private HashMap of all relevant offline players with keys:playerName and values:UUID private void updateOfflinePlayerList(boolean whitelistOnly, boolean excludeBanned, int lastPlayedLimit) { if (offlinePlayerUUIDs == null) offlinePlayerUUIDs = new HashMap<>(); else if (!offlinePlayerUUIDs.isEmpty()) { @@ -41,8 +41,11 @@ public class OfflinePlayerHandler { } Arrays.stream(Bukkit.getOfflinePlayers()).filter(offlinePlayer -> - offlinePlayer.getName() != null && offlinePlayer.hasPlayedBefore()).forEach(offlinePlayer -> - offlinePlayerUUIDs.put(offlinePlayer.getName(), offlinePlayer.getUniqueId())); + offlinePlayer.getName() != null && + (!excludeBanned || !offlinePlayer.isBanned()) && + (!whitelistOnly || offlinePlayer.isWhitelisted()) && + (lastPlayedLimit == 0 || UnixTimeHandler.hasPlayedSince(lastPlayedLimit, offlinePlayer.getLastPlayed()))) + .forEach(offlinePlayer -> offlinePlayerUUIDs.put(offlinePlayer.getName(), offlinePlayer.getUniqueId())); } public OfflinePlayer getOfflinePlayer(String playerName) { diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/UnixTimeHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/UnixTimeHandler.java new file mode 100644 index 0000000..cd8a409 --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/UnixTimeHandler.java @@ -0,0 +1,17 @@ +package com.gmail.artemis.the.gr8.playerstats.utils; + +import java.time.Instant; + +public class UnixTimeHandler { + + //calculates whether a player has played recently enough to fall within the lastPlayedLimit + //if lastPlayedLimit == 0, this always returns true (since there is no limit) + public static boolean hasPlayedSince(long lastPlayedLimit, long lastPlayed) { + long maxLastPlayed = System.currentTimeMillis() - lastPlayedLimit * 24 * 60 * 60 * 1000; + + System.out.println("maxLastPlayed: " + maxLastPlayed); + System.out.println("Player last played: " + lastPlayed); + + return lastPlayedLimit == 0 || lastPlayed >= maxLastPlayed; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4fe548f..b9e724c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -3,23 +3,22 @@ # ------------------------------ # ------ General Options ------- -# The following are filtering options to control which players are included in statistic results +# The following are filtering options to control which players are included when calculating statistics include-whitelist-only: false exclude-banned-players: false # To only show statistics from players that have been online in the last x days, specify the number of days below -# 0 means no time-constraint will be used +# Leave this on 0 if you want to include everyone number-of-days-since-last-joined: 0 -# The maximum number of entries displayed in the top list +# ------ Format Options -------- +# The maximum number of results displayed in the top list top-list-max-size: 10 -# --- Format & Color Options --- -# --------- Format ------------- # If true, the top list will be aligned with lines of dots so that the stat numbers are all underneath each other use-dots: true -# ---------- Color ------------- +# ------ Color Options --------- # The colors below can be chat colors or hex codes if your server supports those (Bukkit does not, Spigot and further forks do) # Make sure to put hex codes between quotation marks! (format: '#xxxxxx') individual-statistics: @@ -43,7 +42,7 @@ individual-statistics-style: sub-stat-names: none stat-numbers: none -# Note that making the player-names bold will cause the alignment of the stat numbers to shift slightly +# Note that making the player-names bold could cause the alignment of the stat numbers to shift slightly top-list-style: player-names: none stat-names: none