Get placeholders working

This commit is contained in:
Hannes Greule 2020-07-16 18:04:17 +02:00
parent e8c155763b
commit 2d9cf8b759
6 changed files with 46 additions and 9 deletions

View File

@ -31,12 +31,12 @@ import com.plotsquared.bukkit.listener.ChunkListener;
import com.plotsquared.bukkit.listener.EntitySpawnListener;
import com.plotsquared.bukkit.listener.PaperListener;
import com.plotsquared.bukkit.listener.PlayerEvents;
import com.plotsquared.bukkit.listener.ServerListener;
import com.plotsquared.bukkit.listener.SingleWorldListener;
import com.plotsquared.bukkit.listener.WorldEvents;
import com.plotsquared.bukkit.managers.BukkitWorldManager;
import com.plotsquared.bukkit.managers.HyperverseWorldManager;
import com.plotsquared.bukkit.managers.MultiverseWorldManager;
import com.plotsquared.bukkit.placeholder.MVdWPlaceholders;
import com.plotsquared.bukkit.placeholder.PlaceholderFormatter;
import com.plotsquared.bukkit.placeholder.PAPIPlaceholders;
import com.plotsquared.bukkit.player.BukkitPlayerManager;
@ -371,11 +371,6 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain<
PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into PlaceholderAPI");
}
if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) {
new MVdWPlaceholders(this, PlotSquared.get().getPlaceholderRegistry());
PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI");
}
this.startMetrics();
if (Settings.Enabled_Components.WORLDS) {
TaskManager.IMP.taskRepeat(this::unload, 20);
@ -1052,6 +1047,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain<
getServer().getPluginManager().registerEvents(new WorldEvents(), this);
}
@Override
public void registerServerEvents() {
getServer().getPluginManager().registerEvents(new ServerListener(this), this);
}
@NotNull @Override public IndependentPlotGenerator getDefaultGenerator() {
return new HybridGen();
}

View File

@ -0,0 +1,29 @@
package com.plotsquared.bukkit.listener;
import com.plotsquared.bukkit.BukkitMain;
import com.plotsquared.bukkit.placeholder.MVdWPlaceholders;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Captions;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerLoadEvent;
public class ServerListener implements Listener {
private final BukkitMain plugin;
public ServerListener(BukkitMain plugin) {
this.plugin = plugin;
}
@EventHandler public void onServerLoad(ServerLoadEvent event) {
if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) {
new MVdWPlaceholders(this.plugin, PlotSquared.get().getPlaceholderRegistry());
PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI");
} else {
System.out.println("Nope");
}
}
}

View File

@ -41,6 +41,7 @@ import org.jetbrains.annotations.NotNull;
*/
public class MVdWPlaceholders {
private static final String PREFIX = "plotsquared_";
private final Plugin plugin;
private final PlaceholderRegistry registry;
@ -60,7 +61,7 @@ public class MVdWPlaceholders {
}
private void addPlaceholder(@NotNull final Placeholder placeholder) {
PlaceholderAPI.registerPlaceholder(plugin, String.format("plotsquared_%s", placeholder.getKey()),
PlaceholderAPI.registerPlaceholder(plugin, PREFIX + String.format("%s", placeholder.getKey()),
placeholderReplaceEvent -> {
if (!placeholderReplaceEvent.isOnline() || placeholderReplaceEvent.getPlayer() == null) {
return "";
@ -69,7 +70,8 @@ public class MVdWPlaceholders {
if (player == null) {
return "";
}
return registry.getPlaceholderValue(placeholderReplaceEvent.getPlaceholder(), player);
String key = placeholderReplaceEvent.getPlaceholder().substring(PREFIX.length());
return registry.getPlaceholderValue(key, player);
});
}

View File

@ -270,6 +270,11 @@ public interface IPlotMain<P> extends ILogger {
*/
void registerWorldEvents();
/**
* Register events related to the server
*/
void registerServerEvents();
/**
* Usually HybridGen
*

View File

@ -262,6 +262,7 @@ public class PlotSquared {
this.IMP.registerPlayerEvents();
}
// Required
this.IMP.registerServerEvents();
this.IMP.registerWorldEvents();
if (Settings.Enabled_Components.CHUNK_PROCESSOR) {
this.IMP.registerChunkProcessor();

View File

@ -159,7 +159,7 @@ public final class PlaceholderRegistry {
final Placeholder previous = this.placeholders
.put(placeholder.getKey().toLowerCase(Locale.ENGLISH),
Preconditions.checkNotNull(placeholder, "Placeholder may not be null"));
if (previous != null) {
if (previous == null) {
this.eventDispatcher.callGenericEvent(new PlaceholderAddedEvent(placeholder));
}
}