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            if (identifier.equals("this")) {
075                identifier = pl.getLocation().getWorldName();
076            }
077
078            return pl.getPlotCount(identifier) > 0 ?
079                    PlaceholderAPIPlugin.booleanTrue() :
080                    PlaceholderAPIPlugin.booleanFalse();
081        }
082
083        if (identifier.startsWith("plot_count_")) {
084            identifier = identifier.substring("plot_count_".length());
085            if (identifier.isEmpty()) {
086                return "";
087            }
088
089            if (identifier.equals("this")) {
090                identifier = pl.getLocation().getWorldName();
091            }
092
093            return String.valueOf(pl.getPlotCount(identifier));
094        }
095
096        if (identifier.startsWith("base_plot_count_")) {
097            identifier = identifier.substring("base_plot_count_".length());
098            if (identifier.isEmpty()) {
099                return "";
100            }
101
102            if (identifier.equals("this")) {
103                identifier = pl.getLocation().getWorldName();
104            }
105
106            return String.valueOf(PlotQuery.newQuery()
107                    .ownedBy(pl)
108                    .inWorld(identifier)
109                    .whereBasePlot()
110                    .thatPasses(plot -> !DoneFlag.isDone(plot))
111                    .count());
112        }
113
114        if (identifier.startsWith("server_plot_count_")) {
115            identifier = identifier.substring("server_plot_count_".length());
116            if (identifier.isEmpty()) {
117                return "";
118            }
119
120            if (identifier.equals("this")) {
121                identifier = pl.getLocation().getWorldName();
122            }
123
124            return String.valueOf(PlotQuery.newQuery()
125                    .allPlots()
126                    .inWorld(identifier)
127                    .count());
128        }
129
130        if (identifier.startsWith("server_base_plot_count_")) {
131            identifier = identifier.substring("server_base_plot_count_".length());
132            if (identifier.isEmpty()) {
133                return "";
134            }
135
136            if (identifier.equals("this")) {
137                identifier = pl.getLocation().getWorldName();
138            }
139
140            return String.valueOf(PlotQuery.newQuery()
141                    .allPlots()
142                    .inWorld(identifier)
143                    .whereBasePlot()
144                    .count());
145        }
146
147        // PlotSquared placeholders
148        return PlotSquared.platform().placeholderRegistry().getPlaceholderValue(identifier, pl);
149    }
150
151}