From 14baead342b999dd4bff75a24d93fee5bf23ac5b Mon Sep 17 00:00:00 2001 From: EpiCanard Date: Tue, 23 Jun 2020 01:20:17 +0200 Subject: [PATCH] Add placeholder to get the value of a plot flag --- .../bukkit/placeholder/Placeholders.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java index 0ae44fbcb..39de8a7fd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -29,6 +29,8 @@ import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; +import com.plotsquared.core.plot.flag.PlotFlag; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.Bukkit; @@ -36,9 +38,13 @@ import org.bukkit.entity.Player; import java.util.Set; import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Placeholders extends PlaceholderExpansion { + private Pattern flagPattern = Pattern.compile("currentplot_flag_(.+)"); + public Placeholders() { } @@ -59,7 +65,7 @@ public class Placeholders extends PlaceholderExpansion { } @Override public String getVersion() { - return "2.4"; + return "2.5"; } @Override public String onPlaceholderRequest(Player p, String identifier) { @@ -124,7 +130,7 @@ public class Placeholders extends PlaceholderExpansion { return ""; } - String name = PlotSquared.get().getImpromptuUUIDPipeline() .getSingle(uid, + String name = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(uid, Settings.UUID.BLOCKING_TIMEOUT); if (name != null) { @@ -181,6 +187,31 @@ public class Placeholders extends PlaceholderExpansion { default: break; } + final Matcher matcher = this.flagPattern.matcher(identifier); + if (matcher.find()) { + return getFlag(plot, matcher.group(1)); + } + return ""; + } + + /** + * Return the flag value from its name on the current plot. + * If the flag doesn't exist it return an empty string. + * If the flag exists but it is not set on current plot it return the default value. + * + * @param plot Current plot where the player is + * @param flagName Name of flag to get from current plot + * @return The value of flag serialized in string + */ + private String getFlag(final Plot plot, final String flagName) { + final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); + if (flag != null) { + return plot.getFlags().stream() + .filter(pflag -> pflag.getName().equals(flagName)).findFirst() + .map(PlotFlag::getValue) + .map(Object::toString) + .orElseGet(() -> plot.getFlagContainer().getFlagErased(flag.getClass()).toString()); + } return ""; } }