updated server load listener to be final, and to automatically unregister itself when done

This commit is contained in:
Sxtanna 2020-07-22 20:41:27 -04:00
parent d7b947ddad
commit 9631b087b3

View File

@ -20,23 +20,25 @@
*/ */
package me.clip.placeholderapi.listeners; package me.clip.placeholderapi.listeners;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.PlaceholderHook;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerLoadEvent; import org.bukkit.event.server.ServerLoadEvent;
import org.jetbrains.annotations.NotNull;
import java.util.Map; public final class ServerLoadEventListener implements Listener
{
public class ServerLoadEventListener implements Listener {
@NotNull
private final PlaceholderAPIPlugin plugin; private final PlaceholderAPIPlugin plugin;
public ServerLoadEventListener(PlaceholderAPIPlugin instance) { public ServerLoadEventListener(@NotNull final PlaceholderAPIPlugin plugin)
plugin = instance; {
Bukkit.getPluginManager().registerEvents(this, instance); this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
} }
/** /**
@ -45,18 +47,15 @@ public class ServerLoadEventListener implements Listener {
* The goal of the method is to register all the expansions as soon as possible * The goal of the method is to register all the expansions as soon as possible
* especially before players can join * especially before players can join
* <p> * <p>
* This will ensure no issues with expanions and hooks. * This will ensure no issues with expansions and hooks.
* *
* @param e the server load event * @param event the server load event
*/ */
@EventHandler @EventHandler
public void onServerLoad(ServerLoadEvent e) { public void onServerLoad(@NotNull final ServerLoadEvent event)
plugin.getLogger().info("Placeholder expansion registration initializing..."); {
final Map<String, PlaceholderHook> alreadyRegistered = PlaceholderAPI.getPlaceholders(); HandlerList.unregisterAll(this);
plugin.getExpansionManager().registerAllExpansions(); plugin.getExpansionManager().initializeExpansions();
}
if (alreadyRegistered != null && !alreadyRegistered.isEmpty()) {
alreadyRegistered.forEach(PlaceholderAPI::registerPlaceholderHook);
}
}
} }