make shutdown tasks register right after the component they are meant to shutdown finishes initializing

This commit is contained in:
Pyrbu 2023-06-26 17:26:31 +02:00
parent 37e8b42997
commit a56096d4d6

@ -73,7 +73,6 @@ public class ZNpcsPlus extends JavaPlugin {
.hexColors().build(); .hexColors().build();
private final List<Runnable> shutdownTasks = new ArrayList<>(); private final List<Runnable> shutdownTasks = new ArrayList<>();
private boolean enabled = false;
private PacketEventsAPI<Plugin> packetEvents; private PacketEventsAPI<Plugin> packetEvents;
@Override @Override
@ -111,27 +110,40 @@ public class ZNpcsPlus extends JavaPlugin {
} }
log(ChatColor.WHITE + " * Initializing libraries..."); log(ChatColor.WHITE + " * Initializing libraries...");
packetEvents.init(); packetEvents.init();
BukkitAudiences adventure = BukkitAudiences.create(this); BukkitAudiences adventure = BukkitAudiences.create(this);
shutdownTasks.add(adventure::close);
log(ChatColor.WHITE + " * Initializing components..."); log(ChatColor.WHITE + " * Initializing components...");
TaskScheduler scheduler = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this); TaskScheduler scheduler = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this);
MetadataFactory metadataFactory = setupMetadataFactory(); shutdownTasks.add(scheduler::cancelAll);
ConfigManager configManager = new ConfigManager(getDataFolder()); ConfigManager configManager = new ConfigManager(getDataFolder());
SkinCache skinCache = new SkinCache(configManager); SkinCache skinCache = new SkinCache(configManager);
EntityPropertyRegistryImpl propertyRegistry = new EntityPropertyRegistryImpl(skinCache); EntityPropertyRegistryImpl propertyRegistry = new EntityPropertyRegistryImpl(skinCache);
MetadataFactory metadataFactory = setupMetadataFactory();
PacketFactory packetFactory = setupPacketFactory(scheduler, metadataFactory, propertyRegistry); PacketFactory packetFactory = setupPacketFactory(scheduler, metadataFactory, propertyRegistry);
BungeeConnector bungeeConnector = new BungeeConnector(this); BungeeConnector bungeeConnector = new BungeeConnector(this);
ActionRegistry actionRegistry = new ActionRegistry(); ActionRegistry actionRegistry = new ActionRegistry();
NpcTypeRegistryImpl typeRegistry = new NpcTypeRegistryImpl(); NpcTypeRegistryImpl typeRegistry = new NpcTypeRegistryImpl();
NpcRegistryImpl npcRegistry = new NpcRegistryImpl(configManager, this, packetFactory, actionRegistry, NpcRegistryImpl npcRegistry = new NpcRegistryImpl(configManager, this, packetFactory, actionRegistry,
scheduler, typeRegistry, propertyRegistry, textSerializer); scheduler, typeRegistry, propertyRegistry, textSerializer);
if (configManager.getConfig().autoSaveEnabled()) shutdownTasks.add(npcRegistry::save);
shutdownTasks.add(npcRegistry::unload);
UserManager userManager = new UserManager(); UserManager userManager = new UserManager();
shutdownTasks.add(userManager::shutdown);
DataImporterRegistry importerRegistry = new DataImporterRegistry(configManager, adventure, bungeeConnector, DataImporterRegistry importerRegistry = new DataImporterRegistry(configManager, adventure, bungeeConnector,
scheduler, packetFactory, textSerializer, typeRegistry, getDataFolder().getParentFile(), scheduler, packetFactory, textSerializer, typeRegistry, getDataFolder().getParentFile(),
propertyRegistry, skinCache); propertyRegistry, skinCache);
log(ChatColor.WHITE + " * Registerring components..."); log(ChatColor.WHITE + " * Registerring components...");
typeRegistry.registerDefault(packetEvents, propertyRegistry); typeRegistry.registerDefault(packetEvents, propertyRegistry);
actionRegistry.registerTypes(scheduler, adventure, bungeeConnector, textSerializer); actionRegistry.registerTypes(scheduler, adventure, bungeeConnector, textSerializer);
packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry), PacketListenerPriority.MONITOR); packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry), PacketListenerPriority.MONITOR);
@ -170,14 +182,7 @@ public class ZNpcsPlus extends JavaPlugin {
} }
} }
shutdownTasks.add(npcRegistry::unload);
shutdownTasks.add(scheduler::cancelAll);
shutdownTasks.add(userManager::shutdown);
shutdownTasks.add(adventure::close);
if (configManager.getConfig().autoSaveEnabled()) shutdownTasks.add(npcRegistry::save);
NpcApiProvider.register(this, new ZNpcsPlusApi(npcRegistry, typeRegistry, propertyRegistry, skinCache)); NpcApiProvider.register(this, new ZNpcsPlusApi(npcRegistry, typeRegistry, propertyRegistry, skinCache));
enabled = true;
log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)"); log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)");
log(""); log("");
@ -198,7 +203,6 @@ public class ZNpcsPlus extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
if (!enabled) return;
NpcApiProvider.unregister(); NpcApiProvider.unregister();
for (Runnable runnable : shutdownTasks) runnable.run(); for (Runnable runnable : shutdownTasks) runnable.run();
PacketEvents.getAPI().terminate(); PacketEvents.getAPI().terminate();