From 14baead342b999dd4bff75a24d93fee5bf23ac5b Mon Sep 17 00:00:00 2001 From: EpiCanard Date: Tue, 23 Jun 2020 01:20:17 +0200 Subject: [PATCH 1/3] 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 ""; } } From 7fbac4f2868b331df7ab07414986ed5c099d8b01 Mon Sep 17 00:00:00 2001 From: EpiCanard Date: Wed, 24 Jun 2020 00:51:27 +0200 Subject: [PATCH 2/3] Add a second placeholder to support only local flag of current plot --- .../bukkit/placeholder/Placeholders.java | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 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 39de8a7fd..ce2445052 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -38,13 +38,9 @@ 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() { } @@ -130,8 +126,8 @@ public class Placeholders extends PlaceholderExpansion { return ""; } - String name = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(uid, - Settings.UUID.BLOCKING_TIMEOUT); + String name = PlotSquared.get().getImpromptuUUIDPipeline() + .getSingle(uid, Settings.UUID.BLOCKING_TIMEOUT); if (name != null) { return name; @@ -187,31 +183,40 @@ public class Placeholders extends PlaceholderExpansion { default: break; } - final Matcher matcher = this.flagPattern.matcher(identifier); - if (matcher.find()) { - return getFlag(plot, matcher.group(1)); + if (identifier.startsWith("currentplot_localflag_")) { + final String[] splitValues = identifier.split("currentplot_localflag_"); + return (splitValues.length >= 2) ? getFlagValue(plot, splitValues[1], false) : ""; + } + if (identifier.startsWith("currentplot_flag_")) { + final String[] splitValues = identifier.split("currentplot_flag_"); + return (splitValues.length >= 2) ? getFlagValue(plot, splitValues[1], true) : ""; } 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. + * If the flag doesn't exist it returns an empty string. + * If the flag exists but it is not set on current plot and the parameter inherit is set to true, + * it returns the default value. * * @param plot Current plot where the player is * @param flagName Name of flag to get from current plot + * @param inherit Define if it returns only the flag set on currentplot or also inherited flag * @return The value of flag serialized in string */ - private String getFlag(final Plot plot, final String flagName) { + private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { 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()); + if (flag == null) { + return ""; } - return ""; + + if (inherit) { + return plot.getFlag(flag).toString(); + } else { + final PlotFlag plotFlag = plot.getFlagContainer().queryLocal(flag.getClass()); + return (plotFlag != null) ? plotFlag.getValue().toString() : ""; + } + } } From e833403e3c473542ec3fe52573de59c23d38aaf4 Mon Sep 17 00:00:00 2001 From: EpiCanard Date: Thu, 25 Jun 2020 14:08:44 +0200 Subject: [PATCH 3/3] Replace split with substring for placeholders --- .../bukkit/placeholder/Placeholders.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 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 ce2445052..5457dbe0f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -72,20 +72,20 @@ public class Placeholders extends PlaceholderExpansion { } if (identifier.startsWith("has_plot_")) { - if (identifier.split("has_plot_").length != 2) + identifier = identifier.substring("has_plot_".length()); + if (identifier.isEmpty()) return ""; - identifier = identifier.split("has_plot_")[1]; return pl.getPlotCount(identifier) > 0 ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse(); } if (identifier.startsWith("plot_count_")) { - if (identifier.split("plot_count_").length != 2) + identifier = identifier.substring("plot_count_".length()); + if (identifier.isEmpty()) return ""; - identifier = identifier.split("plot_count_")[1]; return String.valueOf(pl.getPlotCount(identifier)); } @@ -184,12 +184,11 @@ public class Placeholders extends PlaceholderExpansion { break; } if (identifier.startsWith("currentplot_localflag_")) { - final String[] splitValues = identifier.split("currentplot_localflag_"); - return (splitValues.length >= 2) ? getFlagValue(plot, splitValues[1], false) : ""; + return getFlagValue(plot, identifier.substring("currentplot_localflag_".length()), + false); } if (identifier.startsWith("currentplot_flag_")) { - final String[] splitValues = identifier.split("currentplot_flag_"); - return (splitValues.length >= 2) ? getFlagValue(plot, splitValues[1], true) : ""; + return getFlagValue(plot, identifier.substring("currentplot_flag_".length()), true); } return ""; } @@ -206,10 +205,11 @@ public class Placeholders extends PlaceholderExpansion { * @return The value of flag serialized in string */ private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { - final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); - if (flag == null) { + if (flagName.isEmpty()) + return ""; + final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); + if (flag == null) return ""; - } if (inherit) { return plot.getFlag(flag).toString(); @@ -217,6 +217,5 @@ public class Placeholders extends PlaceholderExpansion { final PlotFlag plotFlag = plot.getFlagContainer().queryLocal(flag.getClass()); return (plotFlag != null) ? plotFlag.getValue().toString() : ""; } - } }