Skip to content

Commit 08efcca

Browse files
authored
Merge pull request InsightsPlugin#246 from Euphillya/feat/add-register-addon-instance
The ability to save or remove add-ons without adding a .jar file to the addons folder
2 parents 8ee86be + 769d014 commit 08efcca

1 file changed

Lines changed: 45 additions & 12 deletions

File tree

Insights-API/src/main/java/dev/frankheijden/insights/api/addons/AddonManager.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.frankheijden.insights.api.InsightsPlugin;
44
import org.bukkit.Bukkit;
55
import org.bukkit.Location;
6+
import org.bukkit.event.HandlerList;
67
import org.bukkit.event.Listener;
78
import java.io.IOException;
89
import java.lang.reflect.Constructor;
@@ -65,21 +66,53 @@ public void loadAddons() throws IOException {
6566
);
6667
continue;
6768
}
69+
registerAddon(addon);
70+
}
71+
}
72+
}
6873

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+
}
7386

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+
}
7891

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() + "'");
82112
}
113+
114+
plugin.getLogger().info("Unloaded addon '" + addon.getPluginName() + "' v" + addon.getVersion());
115+
return addon;
83116
}
84117

85118
public InsightsAddon loadAddon(Path path) throws AddonException, MalformedURLException {
@@ -166,7 +199,7 @@ public Optional<Region> getRegion(Location location) {
166199
}
167200

168201
for (String pluginName : addonsToRemove) {
169-
InsightsAddon addon = addons.remove(pluginName);
202+
InsightsAddon addon = unregisterAddon(pluginName);
170203
if (addon != null) {
171204
plugin.getLogger().warning("Unloaded addon '" + addon.getPluginName() + "' v" + addon.getVersion()
172205
+ ", because the plugin disappeared.");

0 commit comments

Comments
 (0)