Made EnumHandler static and ensured private lists function properly, implemented player-limit-functions (#30, #15)

This commit is contained in:
Artemis-the-gr8 2022-05-23 15:57:34 +02:00
parent 276c9c48ed
commit 23852f5864
6 changed files with 50 additions and 31 deletions

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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<String> blockNames;
private static List<String> entityTypeNames;
private static List<String> itemNames;
private static List<String> statNames;
private static List<String> entityStatNames;
private static List<String> subStatEntryNames;
private final static List<String> blockNames;
private final static List<String> entityTypeNames;
private final static List<String> itemNames;
private final static List<String> statNames;
private final static List<String> entityStatNames;
private final static List<String> 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

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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