Merge pull request #69 from potatoru/regex-pstat

Pstat placeholder parse improvement
This commit is contained in:
Kiran Hart 2023-08-15 15:49:37 -04:00 committed by GitHub
commit 9f9a3bd3f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,8 @@ import java.time.temporal.ChronoUnit;
import java.util.List; import java.util.List;
import java.util.OptionalInt; import java.util.OptionalInt;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -120,14 +122,33 @@ public class PlaceholderAPIHook extends PlaceholderExpansion {
} }
if (paramSplit[0].equalsIgnoreCase("pstat")) { if (paramSplit[0].equalsIgnoreCase("pstat")) {
if (paramSplit.length < 3) return null; if (paramSplit.length < 2) return null;
final AuctionStatisticType auctionStatisticType = getStatTypeFromParam(paramSplit[1]); final AuctionStatisticType auctionStatisticType = getStatTypeFromParam(paramSplit[1]);
final OfflinePlayer targetUser = getPlayerMaybe(paramSplit[2]);
if (paramSplit.length == 4) { // default values for placeholders without player name or time
final int duration = Integer.parseInt(splitString(paramSplit[3])[0]); OfflinePlayer targetUser = player;
final ChronoUnit unit = getChronoUnitFromParam(splitString(paramSplit[3])[1]); String[] chronoSplit = null;
if (paramSplit.length > 2) {
String timePattern = "";
// check if param has time at the end
if (paramSplit[paramSplit.length - 1].matches("[1-9]+[smhdw]")) {
chronoSplit = splitString(paramSplit[paramSplit.length - 1]);
timePattern = "_" + paramSplit[paramSplit.length - 1];
}
// Match player name
Matcher playerName = Pattern.compile("pstat_[a-z]+_(.+)" + timePattern).matcher(params);
if (playerName.matches()) {
targetUser = getPlayerMaybe(playerName.group(1));
}
}
if (chronoSplit != null) {
final int duration = Integer.parseInt(chronoSplit[0]);
final ChronoUnit unit = getChronoUnitFromParam(chronoSplit[1]);
return String.valueOf(AuctionHouse.getInstance().getAuctionStatisticManager().getStatisticByPlayer(targetUser.getUniqueId(), auctionStatisticType, unit, duration)); return String.valueOf(AuctionHouse.getInstance().getAuctionStatisticManager().getStatisticByPlayer(targetUser.getUniqueId(), auctionStatisticType, unit, duration));
} }