Added way to get excluded player with corresponding config setting (#128, #129)

This commit is contained in:
Artemis-the-gr8 2022-12-06 20:29:04 +01:00
parent a6e15828c1
commit bf35582b13
6 changed files with 36 additions and 6 deletions

View File

@ -170,6 +170,7 @@ public final class StatCommand implements CommandExecutor {
switch (targetArg) {
case "me" -> {
if (sender instanceof Player) {
//TODO this is where an excluded player can sneak in
target = Target.PLAYER;
playerName = sender.getName();
} else {

View File

@ -21,7 +21,7 @@ public final class ConfigHandler extends FileHandler {
super("config.yml");
config = super.getFileConfiguration();
configVersion = 6;
configVersion = 7;
checkAndUpdateConfigVersion();
MyLogger.setDebugLevel(getDebugLevel());
}
@ -132,6 +132,14 @@ public final class ConfigHandler extends FileHandler {
return config.getInt("number-of-days-since-last-joined", 0);
}
/**
* Whether to allow the /stat player command for excluded players.
* @return the config setting (default: true)
*/
public boolean allowPlayerLookupsForExcludedPlayers() {
return config.getBoolean("allow-player-lookups-for-excluded-players", true);
}
/**
* Whether to use TranslatableComponents wherever possible.
*

View File

@ -64,7 +64,7 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
do {
String playerName = iterator.next();
MyLogger.actionRunning(Thread.currentThread().getName());
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName);
OfflinePlayer player = offlinePlayerHandler.getLoadedOfflinePlayer(playerName);
int statistic = 0;
switch (requestSettings.getStatistic().getType()) {
case UNTYPED -> statistic = player.getStatistic(requestSettings.getStatistic());

View File

@ -4,6 +4,7 @@ import com.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.artemis.the.gr8.playerstats.api.StatManager;
import com.artemis.the.gr8.playerstats.api.StatRequest;
import com.artemis.the.gr8.playerstats.api.StatResult;
import com.artemis.the.gr8.playerstats.core.config.ConfigHandler;
import com.artemis.the.gr8.playerstats.core.msg.msgutils.FormattingFunction;
import com.artemis.the.gr8.playerstats.core.msg.OutputManager;
import com.artemis.the.gr8.playerstats.core.multithreading.ThreadManager;
@ -85,10 +86,12 @@ public final class RequestManager implements StatManager {
private final class RequestProcessor {
private static ConfigHandler config;
private static OutputManager outputManager;
private static ShareManager shareManager;
public RequestProcessor(OutputManager outputManager) {
RequestProcessor.config = ConfigHandler.getInstance();
RequestProcessor.outputManager = outputManager;
RequestProcessor.shareManager = ShareManager.getInstance();
}
@ -121,7 +124,13 @@ public final class RequestManager implements StatManager {
}
private int getPlayerStat(@NotNull StatRequest.Settings requestSettings) {
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName());
OfflinePlayer player;
if (offlinePlayerHandler.isExcludedPlayer(requestSettings.getPlayerName()) &&
config.allowPlayerLookupsForExcludedPlayers()) {
player = offlinePlayerHandler.getExcludedOfflinePlayer(requestSettings.getPlayerName());
} else {
player = offlinePlayerHandler.getLoadedOfflinePlayer(requestSettings.getPlayerName());
}
return switch (requestSettings.getStatistic().getType()) {
case UNTYPED -> player.getStatistic(requestSettings.getStatistic());
case ENTITY -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity());

View File

@ -132,7 +132,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 @NotNull OfflinePlayer getOfflinePlayer(String playerName) throws IllegalArgumentException {
public @NotNull OfflinePlayer getLoadedOfflinePlayer(String playerName) throws IllegalArgumentException {
if (includedPlayerUUIDs.get(playerName) != null) {
return Bukkit.getOfflinePlayer(includedPlayerUUIDs.get(playerName));
}
@ -140,10 +140,17 @@ public final class OfflinePlayerHandler extends FileHandler {
MyLogger.logWarning("Cannot calculate statistics for player-name: " + playerName +
"! Double-check if the name is spelled correctly (including capital letters), " +
"or if any of your config settings exclude them");
throw new IllegalArgumentException("Cannot convert this player-name into a valid Player to calculate statistics for");
throw new IllegalArgumentException("PlayerStats does not know a player by this name");
}
}
public @NotNull OfflinePlayer getExcludedOfflinePlayer(String playerName) throws IllegalArgumentException {
if (excludedPlayerUUIDs.get(playerName) != null) {
return Bukkit.getOfflinePlayer(excludedPlayerUUIDs.get(playerName));
}
throw new IllegalArgumentException("There is no player on the exclude-list with this name");
}
private void loadOfflinePlayers() {
Executors.newSingleThreadExecutor().execute(() -> {
loadExcludedPlayerNames();

View File

@ -1,7 +1,7 @@
# ------------------------------------------------------------------------------------------------------ #
# PlayerStats Configuration #
# ------------------------------------------------------------------------------------------------------ #
config-version: 6
config-version: 7
# # ------------------------------- # #
@ -33,6 +33,11 @@ exclude-banned-players: false
# Leave this on 0 to include all players
number-of-days-since-last-joined: 0
# Players that are excluded through the previous settings or the excluded-players-file will not
# show up in top or server statistics. This setting controls whether you can still see their stats with
# the /stat player command
allow-player-lookups-for-excluded-players: true
# # ------------------------------- # #
# # Format & Display # #