001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util.placeholders; 020 021import com.plotsquared.core.player.PlotPlayer; 022import com.plotsquared.core.plot.Plot; 023import com.plotsquared.core.plot.flag.GlobalFlagContainer; 024import com.plotsquared.core.plot.flag.PlotFlag; 025import org.checkerframework.checker.nullness.qual.NonNull; 026 027public final class PlotFlagPlaceholder extends PlotSpecificPlaceholder { 028 029 private final PlotFlag<?, ?> flag; 030 private final boolean local; 031 032 public PlotFlagPlaceholder(final @NonNull PlotFlag<?, ?> flag, final boolean local) { 033 super(String.format("currentplot_%sflag_%s", local ? "local" : "", flag.getName())); 034 this.flag = flag; 035 this.local = local; 036 } 037 038 @Override 039 public @NonNull String getValue(final @NonNull PlotPlayer<?> player, final @NonNull Plot plot) { 040 return this.getFlagValue(plot, this.flag.getName(), !this.local); 041 } 042 043 /** 044 * Return the flag value from its name on the current plot. 045 * If the flag doesn't exist it returns an empty string. 046 * If the flag exists but it is not set on current plot and the parameter inherit is set to true, 047 * it returns the default value. 048 * 049 * @param plot Current plot where the player is 050 * @param flagName Name of flag to get from current plot 051 * @param inherit Define if it returns only the flag set on the current plot or also inherited flags 052 * @return The value of flag serialized in string 053 */ 054 @NonNull 055 private String getFlagValue(final @NonNull Plot plot, final @NonNull String flagName, final boolean inherit) { 056 if (flagName.isEmpty()) { 057 return ""; 058 } 059 final PlotFlag<?, ?> flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); 060 if (flag == null) { 061 return ""; 062 } 063 if (inherit) { 064 return plot.getFlag(flag).toString(); 065 } else { 066 final PlotFlag<?, ?> plotFlag = plot.getFlagContainer().queryLocal(flag.getClass()); 067 return (plotFlag != null) ? plotFlag.getValue().toString() : ""; 068 } 069 } 070 071}