From 06889a221eb75b8671a0879200356f55e66b8641 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Thu, 4 Jul 2024 00:25:27 +0200 Subject: [PATCH] make npc property registry accessible during onLoad --- .../api/NpcPropertyRegistryProvider.java | 46 +++++++++++++++++++ .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 16 +++++-- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/lol/pyr/znpcsplus/api/NpcPropertyRegistryProvider.java diff --git a/api/src/main/java/lol/pyr/znpcsplus/api/NpcPropertyRegistryProvider.java b/api/src/main/java/lol/pyr/znpcsplus/api/NpcPropertyRegistryProvider.java new file mode 100644 index 0000000..5e8c113 --- /dev/null +++ b/api/src/main/java/lol/pyr/znpcsplus/api/NpcPropertyRegistryProvider.java @@ -0,0 +1,46 @@ +package lol.pyr.znpcsplus.api; + +import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry; +import org.bukkit.Bukkit; + +/** + * Provider for the registered entity property registry instance + */ +public class NpcPropertyRegistryProvider { + private static EntityPropertyRegistry registry = null; + + private NpcPropertyRegistryProvider() { + throw new UnsupportedOperationException(); + } + + /** + * Static method that returns the entity property registry instance of the plugin + * + * @return The instance of the entity property registry for the ZNPCsPlus plugin + */ + public static EntityPropertyRegistry get() { + if (registry == null) throw new IllegalStateException( + "ZNPCsPlus plugin isn't enabled yet!\n" + + "Please add it to your plugin.yml as a depend or softdepend." + ); + return registry; + } + + /** + * Internal method used to register the main instance of the plugin as the entity property registry provider + * You probably shouldn't call this method under any circumstances + * + * @param api Instance of the ZNPCsPlus entity property registry + */ + public static void register(EntityPropertyRegistry api) { + NpcPropertyRegistryProvider.registry = api; + } + + /** + * Internal method used to unregister the plugin from the provider when the plugin shuts down + * You probably shouldn't call this method under any circumstances + */ + public static void unregister() { + Bukkit.getServicesManager().unregister(registry); + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 4991b16..a27d874 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -14,6 +14,7 @@ import lol.pyr.director.adventure.parse.primitive.FloatParser; import lol.pyr.director.adventure.parse.primitive.IntegerParser; import lol.pyr.director.common.message.Message; import lol.pyr.znpcsplus.api.NpcApiProvider; +import lol.pyr.znpcsplus.api.NpcPropertyRegistryProvider; import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.commands.*; import lol.pyr.znpcsplus.commands.action.*; @@ -78,12 +79,23 @@ public class ZNpcsPlus { private final PacketEventsAPI packetEvents; private final ZNpcsPlusBootstrap bootstrap; + private final ConfigManager configManager; + private final MojangSkinCache skinCache; + private final EntityPropertyRegistryImpl propertyRegistry; + public ZNpcsPlus(ZNpcsPlusBootstrap bootstrap) { this.bootstrap = bootstrap; packetEvents = SpigotPacketEventsBuilder.build(bootstrap); PacketEvents.setAPI(packetEvents); packetEvents.getSettings().checkForUpdates(false); packetEvents.load(); + + configManager = new ConfigManager(getDataFolder()); + skinCache = new MojangSkinCache(configManager); + propertyRegistry = new EntityPropertyRegistryImpl(skinCache, configManager); + + NpcPropertyRegistryProvider.register(propertyRegistry); + shutdownTasks.add(NpcPropertyRegistryProvider::unregister); } private void log(String str) { @@ -113,9 +125,7 @@ public class ZNpcsPlus { TaskScheduler scheduler = FoliaUtil.isFolia() ? new FoliaScheduler(bootstrap) : new SpigotScheduler(bootstrap); shutdownTasks.add(scheduler::cancelAll); - ConfigManager configManager = new ConfigManager(getDataFolder()); - MojangSkinCache skinCache = new MojangSkinCache(configManager); - EntityPropertyRegistryImpl propertyRegistry = new EntityPropertyRegistryImpl(skinCache, configManager); + PacketFactory packetFactory = setupPacketFactory(scheduler, propertyRegistry, configManager); propertyRegistry.registerTypes(bootstrap, packetFactory, textSerializer);