From 1671a52bc532addc847857ab2ef8f479c044aaf8 Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Sat, 2 Jul 2022 13:13:10 +0300 Subject: [PATCH] Added support for location based placeholders with the custom placeholders registered using the PlaceholdersService --- .../placeholders/PlaceholdersServiceImpl.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/bgsoftware/superiorskyblock/service/placeholders/PlaceholdersServiceImpl.java b/src/main/java/com/bgsoftware/superiorskyblock/service/placeholders/PlaceholdersServiceImpl.java index 6d63894c1..1541e3c50 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/service/placeholders/PlaceholdersServiceImpl.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/service/placeholders/PlaceholdersServiceImpl.java @@ -41,6 +41,7 @@ public class PlaceholdersServiceImpl implements PlaceholdersService { private static final Pattern ISLAND_PLACEHOLDER_PATTERN = Pattern.compile("island_(.+)"); private static final Pattern PLAYER_PLACEHOLDER_PATTERN = Pattern.compile("player_(.+)"); + private static final Pattern LOCATION_PLACEHOLDER_PATTERN = Pattern.compile("location_(.+)"); private static final Pattern PERMISSION_PLACEHOLDER_PATTERN = Pattern.compile("island_permission_(.+)"); private static final Pattern UPGRADE_PLACEHOLDER_PATTERN = Pattern.compile("island_upgrade_(.+)"); private static final Pattern COUNT_PLACEHOLDER_PATTERN = Pattern.compile("island_count_(.+)"); @@ -236,20 +237,25 @@ public String handlePluginPlaceholder(@Nullable OfflinePlayer offlinePlayer, Str Optional placeholderResult = Optional.empty(); + Matcher matcher; + if (superiorPlayer != null) { PlayerPlaceholderParser customPlayerParser = CUSTOM_PLAYER_PARSERS.get(placeholder); if (customPlayerParser != null) { placeholderResult = Optional.ofNullable(customPlayerParser.apply(superiorPlayer)); } else { - IslandPlaceholderParser customIslandParser = CUSTOM_ISLAND_PARSERS.get(placeholder); - if (customIslandParser != null) - placeholderResult = Optional.ofNullable(customIslandParser.apply(superiorPlayer.getIsland(), superiorPlayer)); + boolean isLocationPlaceholder = placeholder.startsWith("location_"); + IslandPlaceholderParser customIslandParser = CUSTOM_ISLAND_PARSERS.get( + isLocationPlaceholder ? placeholder.substring(9) : placeholder); + if (customIslandParser != null) { + Island island = isLocationPlaceholder ? plugin.getGrid().getIslandAt(superiorPlayer.getLocation()) : + superiorPlayer.getIsland(); + placeholderResult = Optional.ofNullable(customIslandParser.apply(island, superiorPlayer)); + } } } if (!placeholderResult.isPresent()) { - Matcher matcher; - if ((matcher = PLAYER_PLACEHOLDER_PATTERN.matcher(placeholder)).matches()) { String subPlaceholder = matcher.group(1).toLowerCase(Locale.ENGLISH); placeholderResult = parsePlaceholdersForPlayer(superiorPlayer, subPlaceholder);