Add a second placeholder to support only local flag of current plot

This commit is contained in:
EpiCanard 2020-06-24 00:51:27 +02:00
parent 14baead342
commit 7fbac4f286

View File

@ -38,13 +38,9 @@ import org.bukkit.entity.Player;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Placeholders extends PlaceholderExpansion { public class Placeholders extends PlaceholderExpansion {
private Pattern flagPattern = Pattern.compile("currentplot_flag_(.+)");
public Placeholders() { public Placeholders() {
} }
@ -130,8 +126,8 @@ public class Placeholders extends PlaceholderExpansion {
return ""; return "";
} }
String name = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(uid, String name = PlotSquared.get().getImpromptuUUIDPipeline()
Settings.UUID.BLOCKING_TIMEOUT); .getSingle(uid, Settings.UUID.BLOCKING_TIMEOUT);
if (name != null) { if (name != null) {
return name; return name;
@ -187,31 +183,40 @@ public class Placeholders extends PlaceholderExpansion {
default: default:
break; break;
} }
final Matcher matcher = this.flagPattern.matcher(identifier); if (identifier.startsWith("currentplot_localflag_")) {
if (matcher.find()) { final String[] splitValues = identifier.split("currentplot_localflag_");
return getFlag(plot, matcher.group(1)); 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 "";
} }
/** /**
* Return the flag value from its name on the current plot. * 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 doesn't exist it returns an empty string.
* If the flag exists but it is not set on current plot it return the default value. * 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 plot Current plot where the player is
* @param flagName Name of flag to get from current plot * @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 * @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); final PlotFlag<?, ?> flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName);
if (flag != null) { if (flag == null) {
return plot.getFlags().stream() return "";
.filter(pflag -> pflag.getName().equals(flagName)).findFirst()
.map(PlotFlag::getValue)
.map(Object::toString)
.orElseGet(() -> plot.getFlagContainer().getFlagErased(flag.getClass()).toString());
} }
return "";
if (inherit) {
return plot.getFlag(flag).toString();
} else {
final PlotFlag<?, ?> plotFlag = plot.getFlagContainer().queryLocal(flag.getClass());
return (plotFlag != null) ? plotFlag.getValue().toString() : "";
}
} }
} }