mirror of
https://github.com/itHotL/PlayerStats.git
synced 2025-01-22 21:41:19 +01:00
Fixed bug in entity/material-related Requests, added IllegalArgumentException for wrong player-name
This commit is contained in:
parent
86e9a7827e
commit
9c1298794d
@ -3,6 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.api;
|
||||
import com.gmail.artemis.the.gr8.playerstats.enums.Unit;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.NumberFormatter;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.StringUtils;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Statistic;
|
||||
@ -32,6 +33,13 @@ public interface ApiFormatter {
|
||||
return new NumberFormatter();
|
||||
}
|
||||
|
||||
/** Replace "_" with " " and capitalize each first letter of the input.
|
||||
@param rawEnumString String to prettify, case-insensitive
|
||||
@return the transformed String. For example: animals_bred becomes Animals Bred*/
|
||||
default String BukkitEnumToString(String rawEnumString) {
|
||||
return StringUtils.prettify(rawEnumString);
|
||||
}
|
||||
|
||||
/** Gets the default prefix PlayerStats uses.
|
||||
@return [PlayerStats]*/
|
||||
TextComponent getPluginPrefix();
|
||||
|
@ -63,17 +63,15 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
|
||||
String playerName = iterator.next();
|
||||
MyLogger.actionRunning(Thread.currentThread().getName(), playerName, 2);
|
||||
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName);
|
||||
if (player != null) {
|
||||
int statistic = 0;
|
||||
switch (requestSettings.getStatistic().getType()) {
|
||||
case UNTYPED -> statistic = player.getStatistic(requestSettings.getStatistic());
|
||||
case ENTITY -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity());
|
||||
case BLOCK -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock());
|
||||
case ITEM -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem());
|
||||
}
|
||||
if (statistic > 0) {
|
||||
allStats.put(playerName, statistic);
|
||||
}
|
||||
int statistic = 0;
|
||||
switch (requestSettings.getStatistic().getType()) {
|
||||
case UNTYPED -> statistic = player.getStatistic(requestSettings.getStatistic());
|
||||
case ENTITY -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity());
|
||||
case BLOCK -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock());
|
||||
case ITEM -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem());
|
||||
}
|
||||
if (statistic > 0) {
|
||||
allStats.put(playerName, statistic);
|
||||
}
|
||||
} while (iterator.hasNext());
|
||||
}
|
||||
|
@ -22,18 +22,14 @@ public final class StatCalculator {
|
||||
this.offlinePlayerHandler = offlinePlayerHandler;
|
||||
}
|
||||
|
||||
|
||||
public int getPlayerStat(RequestSettings requestSettings) {
|
||||
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName());
|
||||
if (player != null) {
|
||||
return switch (requestSettings.getStatistic().getType()) {
|
||||
case UNTYPED -> player.getStatistic(requestSettings.getStatistic());
|
||||
case ENTITY -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity());
|
||||
case BLOCK -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock());
|
||||
case ITEM -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem());
|
||||
};
|
||||
}
|
||||
return 0;
|
||||
return switch (requestSettings.getStatistic().getType()) {
|
||||
case UNTYPED -> player.getStatistic(requestSettings.getStatistic());
|
||||
case ENTITY -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity());
|
||||
case BLOCK -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock());
|
||||
case ITEM -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem());
|
||||
};
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Integer> getTopStats(RequestSettings requestSettings) {
|
||||
|
@ -68,12 +68,14 @@ public final class RequestHandler {
|
||||
else {
|
||||
throw new IllegalArgumentException("Either this statistic is not of Type.Block or Type.Item, or no valid block or item has been provided");
|
||||
}
|
||||
requestSettings.setStatistic(statistic);
|
||||
requestSettings.setSubStatEntryName(material.toString());
|
||||
return requestSettings;
|
||||
}
|
||||
|
||||
public RequestSettings entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
|
||||
if (statistic.getType() == Statistic.Type.ENTITY) {
|
||||
requestSettings.setStatistic(statistic);
|
||||
requestSettings.setSubStatEntryName(entityType.toString());
|
||||
requestSettings.setEntity(entityType);
|
||||
return requestSettings;
|
||||
|
@ -2,7 +2,6 @@ package com.gmail.artemis.the.gr8.playerstats.utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -48,16 +47,19 @@ public final class OfflinePlayerHandler {
|
||||
|
||||
/**
|
||||
* Uses the playerName to get the player's UUID from a private HashMap, and uses the UUID to get the corresponding OfflinePlayer Object.
|
||||
* @param playerName name of the target player
|
||||
* @return OfflinePlayer (if this player is on the list, otherwise null)
|
||||
* @param playerName name of the target player (case-sensitive)
|
||||
* @return OfflinePlayer
|
||||
* @throws IllegalArgumentException if this player is not on the list of players that should be included in statistic calculations
|
||||
*/
|
||||
public @Nullable OfflinePlayer getOfflinePlayer(String playerName) {
|
||||
public OfflinePlayer getOfflinePlayer(String playerName) throws IllegalArgumentException {
|
||||
if (offlinePlayerUUIDs.get(playerName) != null) {
|
||||
return Bukkit.getOfflinePlayer(offlinePlayerUUIDs.get(playerName));
|
||||
}
|
||||
else {
|
||||
MyLogger.logMsg("Cannot calculate statistics for player-name: " + playerName, true);
|
||||
return null;
|
||||
MyLogger.logMsg("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", true);
|
||||
throw new IllegalArgumentException("Cannot convert this player-name into a valid Player to calculate statistics for");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user