mirror of
https://github.com/itHotL/PlayerStats.git
synced 2025-01-19 21:11:36 +01:00
Worked on transforming playerNames into UUIDs and vice versa for the ExcludeCommand (#88)
This commit is contained in:
parent
e158b4480d
commit
da49c46539
@ -106,11 +106,10 @@ public final class Main extends JavaPlugin implements PlayerStats {
|
||||
config = ConfigHandler.getInstance();
|
||||
languageKeyHandler = LanguageKeyHandler.getInstance();
|
||||
offlinePlayerHandler = OfflinePlayerHandler.getInstance();
|
||||
shareManager = ShareManager.getInstance();
|
||||
|
||||
outputManager = new OutputManager(adventure);
|
||||
shareManager = new ShareManager();
|
||||
|
||||
requestManager = new RequestManager(outputManager, shareManager);
|
||||
requestManager = new RequestManager(outputManager);
|
||||
threadManager = new ThreadManager(this, outputManager);
|
||||
}
|
||||
|
||||
@ -138,7 +137,7 @@ public final class Main extends JavaPlugin implements PlayerStats {
|
||||
}
|
||||
PluginCommand sharecmd = this.getCommand("statisticshare");
|
||||
if (sharecmd != null) {
|
||||
sharecmd.setExecutor(new ShareCommand(shareManager, outputManager));
|
||||
sharecmd.setExecutor(new ShareCommand(outputManager));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,16 @@
|
||||
package com.artemis.the.gr8.playerstats.commands;
|
||||
|
||||
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class ExcludeCommand implements CommandExecutor {
|
||||
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
@ -17,7 +21,27 @@ public final class ExcludeCommand implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length == 1 && args[0].equalsIgnoreCase("list")) {
|
||||
List<String> excludedPlayers = offlinePlayerHandler.getListOfExcludedPlayerNames();
|
||||
|
||||
for (String player : excludedPlayers) {
|
||||
MyLogger.logLowLevelMsg(player);
|
||||
}
|
||||
}
|
||||
//this is going to return false for all UUIDs in file at boot-up - that's an issue
|
||||
else if (args.length >= 2 && offlinePlayerHandler.isLoadedPlayer(args[1])) {
|
||||
String playerName = args[1];
|
||||
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName);
|
||||
|
||||
switch (args[0]) {
|
||||
case "add" -> offlinePlayerHandler.addPlayerToExcludeList(player.getUniqueId());
|
||||
case "remove" -> offlinePlayerHandler.removePlayerFromExcludeList(player.getUniqueId());
|
||||
case "info" -> {
|
||||
boolean isExcluded = offlinePlayerHandler.isExcluded(player.getUniqueId());
|
||||
MyLogger.logLowLevelMsg(player.getName() + " is excluded: " + isExcluded);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ public final class ReloadCommand implements CommandExecutor {
|
||||
|
||||
private static ThreadManager threadManager;
|
||||
|
||||
public ReloadCommand(ThreadManager t) {
|
||||
threadManager = t;
|
||||
public ReloadCommand(ThreadManager threadManager) {
|
||||
ReloadCommand.threadManager = threadManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -12,17 +12,17 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public final class ShareCommand implements CommandExecutor {
|
||||
|
||||
private static ShareManager shareManager;
|
||||
private static OutputManager outputManager;
|
||||
private static ShareManager shareManager;
|
||||
|
||||
public ShareCommand(ShareManager s, OutputManager m) {
|
||||
shareManager = s;
|
||||
outputManager = m;
|
||||
public ShareCommand(OutputManager outputManager) {
|
||||
ShareCommand.outputManager = outputManager;
|
||||
shareManager = ShareManager.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length == 1 && ShareManager.isEnabled()) {
|
||||
if (args.length == 1 && shareManager.isEnabled()) {
|
||||
int shareCode;
|
||||
try {
|
||||
shareCode = Integer.parseInt(args[0]);
|
||||
|
@ -243,7 +243,7 @@ public final class StatCommand implements CommandExecutor {
|
||||
OfflinePlayerHandler offlinePlayerHandler = OfflinePlayerHandler.getInstance();
|
||||
|
||||
for (String arg : args) {
|
||||
if (offlinePlayerHandler.isRelevantPlayer(arg)) {
|
||||
if (offlinePlayerHandler.isLoadedPlayer(arg)) {
|
||||
return arg;
|
||||
}
|
||||
}
|
||||
|
@ -14,5 +14,5 @@ public enum StandardMessage {
|
||||
STILL_ON_SHARE_COOLDOWN,
|
||||
RESULTS_ALREADY_SHARED,
|
||||
STAT_RESULTS_TOO_OLD,
|
||||
UNKNOWN_ERROR,
|
||||
UNKNOWN_ERROR
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.msg.msgutils.FormattingFunction;
|
||||
import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.artemis.the.gr8.playerstats.msg;
|
||||
package com.artemis.the.gr8.playerstats.msg.msgutils;
|
||||
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.command.CommandSender;
|
@ -25,6 +25,7 @@ import static java.time.temporal.ChronoUnit.SECONDS;
|
||||
*/
|
||||
public final class ShareManager {
|
||||
|
||||
private static volatile ShareManager instance;
|
||||
private static boolean isEnabled;
|
||||
private int waitingTime;
|
||||
|
||||
@ -33,11 +34,25 @@ public final class ShareManager {
|
||||
private ConcurrentHashMap<String, Instant> shareTimeStamp;
|
||||
private ArrayBlockingQueue<Integer> sharedResults;
|
||||
|
||||
public ShareManager() {
|
||||
private ShareManager() {
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
public static boolean isEnabled() {
|
||||
public static ShareManager getInstance() {
|
||||
ShareManager localVar = instance;
|
||||
if (localVar != null) {
|
||||
return localVar;
|
||||
}
|
||||
|
||||
synchronized (ShareManager.class) {
|
||||
if (instance == null) {
|
||||
instance = new ShareManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
|
||||
import com.artemis.the.gr8.playerstats.api.StatManager;
|
||||
import com.artemis.the.gr8.playerstats.msg.FormattingFunction;
|
||||
import com.artemis.the.gr8.playerstats.msg.msgutils.FormattingFunction;
|
||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.multithreading.ThreadManager;
|
||||
import com.artemis.the.gr8.playerstats.share.ShareManager;
|
||||
@ -28,9 +28,9 @@ public final class RequestManager implements StatManager {
|
||||
private static RequestProcessor processor;
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
|
||||
public RequestManager(OutputManager outputManager, ShareManager shareManager) {
|
||||
processor = new RequestProcessor(outputManager, shareManager);
|
||||
public RequestManager(OutputManager outputManager) {
|
||||
offlinePlayerHandler = OfflinePlayerHandler.getInstance();
|
||||
processor = new RequestProcessor(outputManager);
|
||||
}
|
||||
|
||||
public static StatResult<?> execute(@NotNull StatRequest<?> request) {
|
||||
@ -82,9 +82,9 @@ public final class RequestManager implements StatManager {
|
||||
private static OutputManager outputManager;
|
||||
private static ShareManager shareManager;
|
||||
|
||||
public RequestProcessor(OutputManager outputManager, ShareManager shareManager) {
|
||||
public RequestProcessor(OutputManager outputManager) {
|
||||
RequestProcessor.outputManager = outputManager;
|
||||
RequestProcessor.shareManager = shareManager;
|
||||
RequestProcessor.shareManager = ShareManager.getInstance();
|
||||
}
|
||||
|
||||
public @NotNull StatResult<Integer> processPlayerRequest(StatRequest.Settings requestSettings) {
|
||||
@ -149,7 +149,7 @@ public final class RequestManager implements StatManager {
|
||||
|
||||
private boolean outputShouldBeStored(CommandSender sender) {
|
||||
return !(sender instanceof ConsoleCommandSender) &&
|
||||
ShareManager.isEnabled() &&
|
||||
shareManager.isEnabled() &&
|
||||
shareManager.senderHasPermission(sender);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ import net.kyori.adventure.text.TextComponent;
|
||||
* and use the BukkitAudiences object can be found on
|
||||
* <a href="https://docs.adventure.kyori.net/platform/bukkit.html">Adventure's website</a>.
|
||||
*
|
||||
* <p>You can also use the provided {@link #formattedString ()} method to get the
|
||||
* <p>You can also use the provided {@link #formattedString()} method to get the
|
||||
* same information in String-format. Don't use Adventure's <code>#content()</code>
|
||||
* or <code>#toString()</code> methods on the Components - those won't get the actual
|
||||
* message. And finally, if you want the results to be formatted differently,
|
||||
|
@ -9,9 +9,10 @@ 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;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class FileHandler {
|
||||
|
||||
@ -48,7 +49,7 @@ public abstract class FileHandler {
|
||||
}
|
||||
|
||||
public void addValuesToFile(@NotNull Map<String, Object> keyValuePairs) {
|
||||
keyValuePairs.forEach(this::addValue);
|
||||
keyValuePairs.forEach(this::setValue);
|
||||
save();
|
||||
updateFile();
|
||||
}
|
||||
@ -58,23 +59,30 @@ public abstract class FileHandler {
|
||||
* (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);
|
||||
public void addEntryToListInFile(@NotNull String key, @NotNull String value) {
|
||||
List<String> existingList = fileConfiguration.getStringList(key);
|
||||
|
||||
List<Object> updatedValues;
|
||||
if (currentValues != null) {
|
||||
updatedValues = new ArrayList<>(currentValues);
|
||||
} else {
|
||||
updatedValues = new ArrayList<>();
|
||||
}
|
||||
updatedValues.add(value);
|
||||
List<String> updatedList = existingList.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
updatedList.add(value);
|
||||
|
||||
addValue(key, updatedValues);
|
||||
setValue(key, updatedList);
|
||||
save();
|
||||
updateFile();
|
||||
}
|
||||
|
||||
private void addValue(String key, Object value) {
|
||||
public void removeEntryFromListInFile(@NotNull String key, @NotNull String value) {
|
||||
List<String> currentValues = fileConfiguration.getStringList(key);
|
||||
|
||||
if (currentValues.remove(value)) {
|
||||
setValue(key, currentValues);
|
||||
save();
|
||||
updateFile();
|
||||
}
|
||||
}
|
||||
|
||||
private void setValue(String key, Object value) {
|
||||
fileConfiguration.set(key, value);
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* A utility class that deals with OfflinePlayers. It stores a list
|
||||
@ -64,23 +65,34 @@ public final class OfflinePlayerHandler extends FileHandler {
|
||||
* @param playerName String (case-sensitive)
|
||||
* @return true if this player is included
|
||||
*/
|
||||
public boolean isRelevantPlayer(String playerName) {
|
||||
public boolean isLoadedPlayer(String playerName) {
|
||||
return offlinePlayerUUIDs.containsKey(playerName);
|
||||
}
|
||||
|
||||
public void excludePlayer(UUID uniqueID) {
|
||||
super.addValueToListInFile("excluded", uniqueID);
|
||||
public void addPlayerToExcludeList(UUID uniqueID) {
|
||||
super.addEntryToListInFile("excluded", uniqueID.toString());
|
||||
}
|
||||
|
||||
public void removePlayerFromExcludeList(UUID uniqueID) {
|
||||
super.removeEntryFromListInFile("excluded", uniqueID.toString());
|
||||
}
|
||||
|
||||
public List<String> getListOfExcludedPlayerNames() {
|
||||
List<String> excludedUUIDs = excludedPlayers.getStringList("excluded");
|
||||
return excludedUUIDs.stream()
|
||||
.map(UUID::fromString)
|
||||
.map(Bukkit::getOfflinePlayer)
|
||||
.map(OfflinePlayer::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public boolean isExcluded(UUID uniqueID) {
|
||||
List<?> excluded = excludedPlayers.getList("excluded");
|
||||
if (excluded == null) {
|
||||
return false;
|
||||
}
|
||||
List<String> excluded = excludedPlayers.getStringList("excluded");
|
||||
|
||||
return excluded.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.anyMatch(obj -> obj.equals(uniqueID));
|
||||
.map(UUID::fromString)
|
||||
.anyMatch(uuid -> uuid.equals(uniqueID));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,5 +6,8 @@
|
||||
# This can be used for more fine-grained filtering, for example to exclude alt accounts.
|
||||
# For more general filtering settings, see the config.yml (section 'General')
|
||||
|
||||
# Format:
|
||||
# - playerUUID-1
|
||||
# - playerUUID-2
|
||||
excluded:
|
||||
-
|
Loading…
Reference in New Issue
Block a user