Is there a reliable way to get the name of a Plugin's JAR file?
Question
Using the bukkit API, is it possible to make a call that would do something like the following pseudocode:
Bukkit.getPluginManager().getPlugin("PluginName").getJarFile();
Where for a plugin like MobBountyReloaded, it would return plugins/MobBountyReloaded_v235.jar
.
I tried just looking for files using well-known naming-schemes like just appending .jar
or <version>.jar
to the name of the plugin, but that doesn't always yield correct results. The above example fails because the version number in the plugin.yml
file is just 235
and not _v235
, and some plugins use schemes like <PluginName>-<Version>-<buildnumber>.jar
.
Is there a simple way to accomplish this? The File information for every loaded plugin must be somewhere in memory, no?
Solution
If you have a class loaded from the plugin, you can use the code source feature to get the jar from which the class was loaded.
Bukkit.getPluginManager().getPlugin("PluginName").getClass().getProtectionDomain().getCodeSource().getLocation();
OTHER TIPS
in your org.bukkit.plugin.java.JavaPlugin subclass,
this.getFile().getAbsoluteFile();
returns full jar file name with path where just this.getFile() returns something like this: "plugins\clj-minecraft-1.0.1-SNAPSHOT-standalone.jar"
in my case I needed this: "file:/S:/cb/plugins/memorystone-2.0.0-SNAPSHOT.jar" as an URL(not URI) which was: this.getFile().toURI().toURL();
tested in [org.bukkit/bukkit "1.4.5-R0.3-SNAPSHOT"]