2020-07-14 20:41:08 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package com.plotsquared.bukkit.placeholder;
|
|
|
|
|
|
|
|
import be.maximvdw.placeholderapi.PlaceholderAPI;
|
|
|
|
import com.google.common.eventbus.Subscribe;
|
|
|
|
import com.plotsquared.bukkit.util.BukkitUtil;
|
|
|
|
import com.plotsquared.core.PlotSquared;
|
|
|
|
import com.plotsquared.core.player.PlotPlayer;
|
|
|
|
import com.plotsquared.core.util.placeholders.Placeholder;
|
|
|
|
import com.plotsquared.core.util.placeholders.PlaceholderRegistry;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Placeholder support for MVdWPlaceholderAPI
|
|
|
|
*/
|
|
|
|
public class MVdWPlaceholders {
|
|
|
|
|
2020-07-16 18:04:17 +02:00
|
|
|
private static final String PREFIX = "plotsquared_";
|
2020-07-14 20:41:08 +02:00
|
|
|
private final Plugin plugin;
|
|
|
|
private final PlaceholderRegistry registry;
|
|
|
|
|
2020-07-24 18:52:05 +02:00
|
|
|
public MVdWPlaceholders(@NotNull final Plugin plugin, @NotNull final PlaceholderRegistry registry) {
|
2020-07-14 20:41:08 +02:00
|
|
|
this.plugin = plugin;
|
|
|
|
this.registry = registry;
|
|
|
|
for (final Placeholder placeholder : registry.getPlaceholders()) {
|
|
|
|
this.addPlaceholder(placeholder);
|
|
|
|
}
|
|
|
|
PlotSquared.get().getEventDispatcher().registerListener(this);
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:52:05 +02:00
|
|
|
@Subscribe public void onNewPlaceholder(@NotNull final PlaceholderRegistry.PlaceholderAddedEvent event) {
|
2020-07-14 20:41:08 +02:00
|
|
|
this.addPlaceholder(event.getPlaceholder());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addPlaceholder(@NotNull final Placeholder placeholder) {
|
2020-07-24 18:52:05 +02:00
|
|
|
PlaceholderAPI.registerPlaceholder(plugin, PREFIX + String.format("%s", placeholder.getKey()), placeholderReplaceEvent -> {
|
|
|
|
if (!placeholderReplaceEvent.isOnline() || placeholderReplaceEvent.getPlayer() == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
final PlotPlayer<Player> player = BukkitUtil.adapt(placeholderReplaceEvent.getPlayer());
|
|
|
|
String key = placeholderReplaceEvent.getPlaceholder().substring(PREFIX.length());
|
|
|
|
return registry.getPlaceholderValue(key, player);
|
|
|
|
});
|
2020-07-14 20:41:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|