|
3 | 3 | import dev.frankheijden.insights.api.InsightsPlugin; |
4 | 4 | import org.bukkit.Bukkit; |
5 | 5 | import org.bukkit.Location; |
| 6 | +import org.bukkit.event.HandlerList; |
6 | 7 | import org.bukkit.event.Listener; |
7 | 8 | import java.io.IOException; |
8 | 9 | import java.lang.reflect.Constructor; |
@@ -65,21 +66,53 @@ public void loadAddons() throws IOException { |
65 | 66 | ); |
66 | 67 | continue; |
67 | 68 | } |
| 69 | + registerAddon(addon); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
68 | 73 |
|
69 | | - if (!InsightsPlugin.getInstance().isAvailable(addon.getPluginName())) { |
70 | | - plugin.getLogger().severe("Error loading addon: " + addon.getPluginName() + " is not enabled!"); |
71 | | - continue; |
72 | | - } |
| 74 | + /** |
| 75 | + * Registers an addon instance directly, without loading it from the addons folder. |
| 76 | + * Useful for addons bundled in another plugin / already on the classpath. |
| 77 | + * |
| 78 | + * @return true if the addon was registered, false otherwise. |
| 79 | + */ |
| 80 | + @SuppressWarnings("UnusedReturnValue") |
| 81 | + public boolean registerAddon(InsightsAddon addon) { |
| 82 | + if (!InsightsPlugin.getInstance().isAvailable(addon.getPluginName())) { |
| 83 | + plugin.getLogger().severe("Error loading addon: " + addon.getPluginName() + " is not enabled!"); |
| 84 | + return false; |
| 85 | + } |
73 | 86 |
|
74 | | - if (addon instanceof Listener) { |
75 | | - Bukkit.getPluginManager().registerEvents((Listener) addon, plugin); |
76 | | - plugin.getLogger().info("Registered listener of addon '" + addon.getPluginName() + "'"); |
77 | | - } |
| 87 | + if (addon instanceof Listener) { |
| 88 | + Bukkit.getPluginManager().registerEvents((Listener) addon, plugin); |
| 89 | + plugin.getLogger().info("Registered listener of addon '" + addon.getPluginName() + "'"); |
| 90 | + } |
78 | 91 |
|
79 | | - this.addons.put(addon.getPluginName(), addon); |
80 | | - plugin.getLogger().info("Loaded addon '" + addon.getPluginName() + "' v" + addon.getVersion()); |
81 | | - } |
| 92 | + this.addons.put(addon.getPluginName(), addon); |
| 93 | + plugin.getLogger().info("Loaded addon '" + addon.getPluginName() + "' v" + addon.getVersion()); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Unregisters a previously registered addon by its plugin name. |
| 99 | + * If the addon is a Listener, its events are unregistered as well. |
| 100 | + * |
| 101 | + * @return the removed addon, or null if no addon was registered under that name. |
| 102 | + */ |
| 103 | + public InsightsAddon unregisterAddon(String pluginName) { |
| 104 | + InsightsAddon addon = addons.remove(pluginName); |
| 105 | + if (addon == null) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + if (addon instanceof Listener) { |
| 110 | + HandlerList.unregisterAll((Listener) addon); |
| 111 | + plugin.getLogger().info("Unregistered listener of addon '" + addon.getPluginName() + "'"); |
82 | 112 | } |
| 113 | + |
| 114 | + plugin.getLogger().info("Unloaded addon '" + addon.getPluginName() + "' v" + addon.getVersion()); |
| 115 | + return addon; |
83 | 116 | } |
84 | 117 |
|
85 | 118 | public InsightsAddon loadAddon(Path path) throws AddonException, MalformedURLException { |
@@ -166,7 +199,7 @@ public Optional<Region> getRegion(Location location) { |
166 | 199 | } |
167 | 200 |
|
168 | 201 | for (String pluginName : addonsToRemove) { |
169 | | - InsightsAddon addon = addons.remove(pluginName); |
| 202 | + InsightsAddon addon = unregisterAddon(pluginName); |
170 | 203 | if (addon != null) { |
171 | 204 | plugin.getLogger().warning("Unloaded addon '" + addon.getPluginName() + "' v" + addon.getVersion() |
172 | 205 | + ", because the plugin disappeared."); |
|
0 commit comments