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.bukkit.placeholder;
020
021import com.plotsquared.core.PlotSquared;
022import com.plotsquared.core.player.PlotPlayer;
023import com.plotsquared.core.plot.flag.implementations.DoneFlag;
024import com.plotsquared.core.util.query.PlotQuery;
025import me.clip.placeholderapi.PlaceholderAPIPlugin;
026import me.clip.placeholderapi.expansion.PlaceholderExpansion;
027import org.bukkit.entity.Player;
028
029public class PAPIPlaceholders extends PlaceholderExpansion {
030
031    public PAPIPlaceholders() {
032    }
033
034    @Override
035    public boolean persist() {
036        return true;
037    }
038
039    @Override
040    public boolean canRegister() {
041        return true;
042    }
043
044    @Override
045    public String getAuthor() {
046        return "IntellectualSites";
047    }
048
049    @Override
050    public String getIdentifier() {
051        return "plotsquared";
052    }
053
054    @Override
055    public String getVersion() {
056        return "3";
057    }
058
059    @Override
060    public String onPlaceholderRequest(Player p, String identifier) {
061        final PlotPlayer<?> pl = PlotSquared.platform().playerManager().getPlayerIfExists(p.getUniqueId());
062
063        if (pl == null) {
064            return "";
065        }
066
067        // PAPI specific ones that don't translate well over into other placeholder APIs
068        if (identifier.startsWith("has_plot_")) {
069            identifier = identifier.substring("has_plot_".length());
070            if (identifier.isEmpty()) {
071                return "";
072            }
073
074            return pl.getPlotCount(identifier) > 0 ?
075                    PlaceholderAPIPlugin.booleanTrue() :
076                    PlaceholderAPIPlugin.booleanFalse();
077        }
078
079        if (identifier.startsWith("plot_count_")) {
080            identifier = identifier.substring("plot_count_".length());
081            if (identifier.isEmpty()) {
082                return "";
083            }
084
085            return String.valueOf(pl.getPlotCount(identifier));
086        }
087
088        if (identifier.startsWith("base_plot_count_")) {
089            identifier = identifier.substring("base_plot_count_".length());
090            if (identifier.isEmpty()) {
091                return "";
092            }
093
094            return String.valueOf(PlotQuery.newQuery()
095                    .ownedBy(pl)
096                    .inWorld(identifier)
097                    .whereBasePlot()
098                    .thatPasses(plot -> !DoneFlag.isDone(plot))
099                    .count());
100        }
101
102        // PlotSquared placeholders
103        return PlotSquared.platform().placeholderRegistry().getPlaceholderValue(identifier, pl);
104    }
105
106}