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 be.maximvdw.placeholderapi.PlaceholderAPI;
022import com.google.common.eventbus.Subscribe;
023import com.plotsquared.bukkit.util.BukkitUtil;
024import com.plotsquared.core.PlotSquared;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.util.placeholders.Placeholder;
027import com.plotsquared.core.util.placeholders.PlaceholderRegistry;
028import org.bukkit.entity.Player;
029import org.bukkit.plugin.Plugin;
030import org.checkerframework.checker.nullness.qual.NonNull;
031
032/**
033 * Placeholder support for MVdWPlaceholderAPI
034 */
035public class MVdWPlaceholders {
036
037    private static final String PREFIX = "plotsquared_";
038    private final Plugin plugin;
039    private final PlaceholderRegistry registry;
040
041    public MVdWPlaceholders(
042            final @NonNull Plugin plugin,
043            final @NonNull PlaceholderRegistry registry
044    ) {
045        this.plugin = plugin;
046        this.registry = registry;
047        for (final Placeholder placeholder : registry.getPlaceholders()) {
048            this.addPlaceholder(placeholder);
049        }
050        PlotSquared.get().getEventDispatcher().registerListener(this);
051    }
052
053    @Subscribe
054    public void onNewPlaceholder(final PlaceholderRegistry.@NonNull PlaceholderAddedEvent event) {
055        this.addPlaceholder(event.placeholder());
056    }
057
058    private void addPlaceholder(final @NonNull Placeholder placeholder) {
059        PlaceholderAPI.registerPlaceholder(
060                plugin,
061                PREFIX + String.format("%s", placeholder.getKey()),
062                placeholderReplaceEvent -> {
063                    if (!placeholderReplaceEvent.isOnline() || placeholderReplaceEvent.getPlayer() == null) {
064                        return "";
065                    }
066                    final PlotPlayer<Player> player = BukkitUtil.adapt(placeholderReplaceEvent.getPlayer());
067                    String key = placeholderReplaceEvent.getPlaceholder().substring(PREFIX.length());
068                    return registry.getPlaceholderValue(key, player);
069                }
070        );
071    }
072
073}