Add placeholder to get the value of a plot flag

This commit is contained in:
EpiCanard 2020-06-23 01:20:17 +02:00
parent d07d32e28b
commit 14baead342

View File

@ -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) {
@ -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 "";
}
}