Added ExcludeCommand and added methods for excluding players/checking exclude status in OfflinePlayerHandler (#88)

This commit is contained in:
Artemis-the-gr8 2022-10-19 13:17:02 +02:00
parent ca63eca1a0
commit 51a9140a9a
15 changed files with 125 additions and 45 deletions

View File

@ -26,6 +26,7 @@ import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
/**
@ -85,8 +86,8 @@ public final class Main extends JavaPlugin implements PlayerStats {
MyLogger.setDebugLevel(config.getDebugLevel());
languageKeyHandler.reload();
offlinePlayerHandler.reload();
outputManager.update();
ShareManager.updateSettings(config);
outputManager.updateSettings();
shareManager.updateSettings(config);
}
/**
@ -146,8 +147,9 @@ public final class Main extends JavaPlugin implements PlayerStats {
}.runTaskLaterAsynchronously(this, 200);
}
@Contract(pure = true)
@Override
public String getVersion() {
public @NotNull String getVersion() {
return "1.8";
}

View File

@ -0,0 +1,23 @@
package com.artemis.the.gr8.playerstats.commands;
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public final class ExcludeCommand implements CommandExecutor {
private final OfflinePlayerHandler offlinePlayerHandler;
public ExcludeCommand(OfflinePlayerHandler offlinePlayerHandler) {
this.offlinePlayerHandler = offlinePlayerHandler;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return false;
}
}

View File

@ -16,7 +16,7 @@ public final class ReloadCommand implements CommandExecutor {
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
threadManager.startReloadThread(sender);
return true;
}

View File

@ -21,7 +21,7 @@ public final class ShareCommand implements CommandExecutor {
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
if (args.length == 1 && ShareManager.isEnabled()) {
int shareCode;
try {

View File

@ -26,7 +26,7 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StatCommand implements CommandExecutor {
public final class StatCommand implements CommandExecutor {
private static final Pattern pattern = Pattern.compile("top|server|me|player");
@ -45,7 +45,7 @@ public class StatCommand implements CommandExecutor {
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length == 0 || args[0].equalsIgnoreCase("help")) { //in case of less than 1 argument or "help", display the help message
outputManager.sendHelp(sender);
}

View File

@ -8,6 +8,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
@ -34,7 +35,7 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter {
//args[2] = playerName OR target (player/server/top) (length = 3)
//args[3] = playerName (length = 4)
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length >= 1) {
String currentArg = args[args.length-1];

View File

@ -48,8 +48,7 @@ public final class ConfigHandler extends FileHandler {
Map<String, Object> defaultValues = defaultValueGetter.getValuesToAdjust();
defaultValues.put("config-version", configVersion);
super.addValues(defaultValues);
super.updateFile();
super.addValuesToFile(defaultValues);
reload();
MyLogger.logLowLevelMsg("Your config has been updated to version " + configVersion +

View File

@ -5,7 +5,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import java.util.HashMap;
import java.util.Map;
public class DefaultValueGetter {
public final class DefaultValueGetter {
private final FileConfiguration config;
private final Map<String, Object> defaultValuesToAdjust;
@ -53,4 +53,4 @@ public class DefaultValueGetter {
defaultValuesToAdjust.put(path, newValue);
}
}
}
}

View File

@ -49,7 +49,7 @@ public final class OutputManager {
prepareFunctions();
}
public void update() {
public void updateSettings() {
getMessageBuilders();
}

View File

@ -28,8 +28,8 @@ public final class PlayerLoadAction extends RecursiveAction {
* Fills a ConcurrentHashMap with PlayerNames and UUIDs for all OfflinePlayers
* that should be included in statistic calculations.
*
* @param players array of all OfflinePlayers (straight from Bukkit)
* @param lastPlayedLimit whether to set a limit based on last-played-date
* @param players array of all OfflinePlayers to filter and load
* @param lastPlayedLimit optional limit for amount of days ago players last played
* @param offlinePlayerUUIDs the ConcurrentHashMap to put playerNames and UUIDs in
* @see OfflinePlayerHandler
*/
@ -76,8 +76,7 @@ public final class PlayerLoadAction extends RecursiveAction {
OfflinePlayer player = players[i];
String playerName = player.getName();
MyLogger.actionRunning(Thread.currentThread().getName());
if (playerName != null &&
(lastPlayedLimit == 0 || UnixTimeHandler.hasPlayedSince(lastPlayedLimit, player.getLastPlayed()))) {
if (playerName != null && UnixTimeHandler.hasPlayedSince(lastPlayedLimit, player.getLastPlayed())) {
offlinePlayerUUIDs.put(playerName, player.getUniqueId());
}
}

View File

@ -26,12 +26,12 @@ import static java.time.temporal.ChronoUnit.SECONDS;
public final class ShareManager {
private static boolean isEnabled;
private static int waitingTime;
private int waitingTime;
private static volatile AtomicInteger resultID;
private static ConcurrentHashMap<Integer, StoredResult> statResultQueue;
private static ConcurrentHashMap<String, Instant> shareTimeStamp;
private static ArrayBlockingQueue<Integer> sharedResults;
private volatile AtomicInteger NumberOfStoredResults;
private ConcurrentHashMap<Integer, StoredResult> statResultQueue;
private ConcurrentHashMap<String, Instant> shareTimeStamp;
private ArrayBlockingQueue<Integer> sharedResults;
public ShareManager(ConfigHandler config) {
updateSettings(config);
@ -41,14 +41,14 @@ public final class ShareManager {
return isEnabled;
}
public static synchronized void updateSettings(ConfigHandler config) {
public void updateSettings(ConfigHandler config) {
isEnabled = config.allowStatSharing() && config.useHoverText();
waitingTime = config.getStatShareWaitingTime();
if (isEnabled) {
sharedResults = new ArrayBlockingQueue<>(500); //reset the sharedResultsQueue
if (resultID == null) { //if we went from disabled to enabled, initialize
resultID = new AtomicInteger(); //always starts with value 0
if (NumberOfStoredResults == null) { //if we went from disabled to enabled, initialize
NumberOfStoredResults = new AtomicInteger(); //always starts with value 0
statResultQueue = new ConcurrentHashMap<>();
shareTimeStamp = new ConcurrentHashMap<>();
}
@ -148,6 +148,6 @@ public final class ShareManager {
}
private int getNextIDNumber() {
return resultID.incrementAndGet();
return NumberOfStoredResults.incrementAndGet();
}
}

View File

@ -19,7 +19,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
public class RequestProcessor {
public final class RequestProcessor {
private final OfflinePlayerHandler offlinePlayerHandler;
private static OutputManager outputManager;

View File

@ -9,6 +9,8 @@ import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public abstract class FileHandler {
@ -45,22 +47,31 @@ public abstract class FileHandler {
return fileConfiguration;
}
/**
* Add new key-value pairs to the config without losing comments,
* using <a href="https://github.com/tchristofferson/Config-Updater">tchristofferson's Config-Updater</a>
*/
public void updateFile() {
JavaPlugin plugin = Main.getPluginInstance();
try {
ConfigUpdater.update(plugin, fileName, file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void addValues(@NotNull Map<String, Object> keyValuePairs) {
public void addValuesToFile(@NotNull Map<String, Object> keyValuePairs) {
keyValuePairs.forEach(this::addValue);
save();
updateFile();
}
/**
* @param key the Key under which the List will be stored
* (or expanded if it already exists)
* @param value the value(s) to expand the List with
*/
public void addValueToListInFile(@NotNull String key, @NotNull Object value) {
List<?> currentValues = fileConfiguration.getList(key);
List<Object> updatedValues;
if (currentValues != null) {
updatedValues = new ArrayList<>(currentValues);
} else {
updatedValues = new ArrayList<>();
}
updatedValues.add(value);
addValue(key, updatedValues);
save();
updateFile();
}
private void addValue(String key, Object value) {
@ -74,4 +85,17 @@ public abstract class FileHandler {
e.printStackTrace();
}
}
/**
* Add new key-value pairs to the config without losing comments,
* using <a href="https://github.com/tchristofferson/Config-Updater">tchristofferson's Config-Updater</a>
*/
private void updateFile() {
JavaPlugin plugin = Main.getPluginInstance();
try {
ConfigUpdater.update(plugin, fileName, file);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -4,6 +4,8 @@ import com.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.artemis.the.gr8.playerstats.reload.PlayerLoadAction;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@ -20,11 +22,13 @@ import java.util.function.Predicate;
public final class OfflinePlayerHandler extends FileHandler {
private static ConfigHandler config;
private static FileConfiguration excludedPlayers;
private ConcurrentHashMap<String, UUID> offlinePlayerUUIDs;
private ArrayList<String> playerNames;
public OfflinePlayerHandler(ConfigHandler configHandler) {
super("excluded_players.yml");
excludedPlayers = super.getFileConfiguration();
config = configHandler;
loadOfflinePlayers();
}
@ -32,6 +36,7 @@ public final class OfflinePlayerHandler extends FileHandler {
@Override
public void reload() {
super.reload();
excludedPlayers = super.getFileConfiguration();
loadOfflinePlayers();
}
@ -46,6 +51,23 @@ public final class OfflinePlayerHandler extends FileHandler {
return offlinePlayerUUIDs.containsKey(playerName);
}
public void excludePlayer(UUID uniqueID) {
super.addValueToListInFile("excluded", uniqueID);
}
public static boolean isExcluded(UUID uniqueID) {
List<?> excluded = excludedPlayers.getList("excluded");
if (excluded == null) {
return false;
}
for (Object obj : excluded) {
if (obj.equals(uniqueID)) {
return true;
}
}
return false;
}
/**
* Gets the number of OfflinePlayers that are included in
* statistic calculations.
@ -75,7 +97,7 @@ public final class OfflinePlayerHandler extends FileHandler {
* @throws IllegalArgumentException if this player is not on the list
* of players that should be included in statistic calculations
*/
public OfflinePlayer getOfflinePlayer(String playerName) throws IllegalArgumentException {
public @NotNull OfflinePlayer getOfflinePlayer(String playerName) throws IllegalArgumentException {
if (offlinePlayerUUIDs.get(playerName) != null) {
return Bukkit.getOfflinePlayer(offlinePlayerUUIDs.get(playerName));
}
@ -119,7 +141,7 @@ public final class OfflinePlayerHandler extends FileHandler {
return Bukkit.getWhitelistedPlayers().toArray(OfflinePlayer[]::new);
}
private OfflinePlayer[] getNonBannedPlayers() {
private @NotNull OfflinePlayer[] getNonBannedPlayers() {
if (Bukkit.getPluginManager().isPluginEnabled("LiteBans")) {
return Arrays.stream(Bukkit.getOfflinePlayers())
.parallel()

View File

@ -11,13 +11,13 @@ commands:
aliases:
- stat
- stats
description: general statistic command
description: show player statistics in private chat
permission: playerstats.stat
statisticshare:
aliases:
- statshare
- statsshare
description: shares last stat lookup in chat
description: share last stat lookup in chat
usage: "§b/statshare"
permission: playerstats.share
statisticreload:
@ -27,6 +27,13 @@ commands:
description: reloads the config
usage: "§a/statreload"
permission: playerstats.reload
statisticexclude:
aliases:
- statexclude
- statsexclude
description: hide this player's statistics from /stat results
usage: "§c/statexclude"
permission: playerstats.exclude
permissions:
playerstats.stat:
description: allows usage of /statistic
@ -34,6 +41,9 @@ permissions:
playerstats.share:
description: allows sharing stats in chat
default: true
playerstats.exclude:
description: allows usage of /statexclude
default: op
playerstats.reload:
description: allows usage of /statreload
default: op