From 6cef9f71812c3ec7516e1b5eba0865428e169255 Mon Sep 17 00:00:00 2001 From: Alexander Morozov <9192541+potatoru@users.noreply.github.com> Date: Tue, 15 Aug 2023 10:57:14 +1100 Subject: [PATCH] Use regex to match pstat player name --- .../api/hook/PlaceholderAPIHook.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/ca/tweetzy/auctionhouse/api/hook/PlaceholderAPIHook.java b/src/main/java/ca/tweetzy/auctionhouse/api/hook/PlaceholderAPIHook.java index 7f38628..559d097 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/api/hook/PlaceholderAPIHook.java +++ b/src/main/java/ca/tweetzy/auctionhouse/api/hook/PlaceholderAPIHook.java @@ -33,6 +33,8 @@ import java.time.temporal.ChronoUnit; import java.util.List; import java.util.OptionalInt; import java.util.concurrent.CompletableFuture; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -120,14 +122,33 @@ public class PlaceholderAPIHook extends PlaceholderExpansion { } if (paramSplit[0].equalsIgnoreCase("pstat")) { - if (paramSplit.length < 3) return null; + if (paramSplit.length < 2) return null; final AuctionStatisticType auctionStatisticType = getStatTypeFromParam(paramSplit[1]); - final OfflinePlayer targetUser = getPlayerMaybe(paramSplit[2]); - if (paramSplit.length == 4) { - final int duration = Integer.parseInt(splitString(paramSplit[3])[0]); - final ChronoUnit unit = getChronoUnitFromParam(splitString(paramSplit[3])[1]); + // default values for placeholders without player name or time + OfflinePlayer targetUser = player; + 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)); }