Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7012

JavaPlugin.getProvidingPlugin(Class) does not work from the libraries section

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • None
    • Microsoft Windows 10 Home 10.0.19044 Build 19044
      OpenJDK 64-Bit Server VM Temurin-17.0.1+12 (build 17.0.1+12, mixed mode, sharing)

    • This server is running CraftBukkit version 3483-Spigot-42b6152-9cc7d76 (MC: 1.18.2) (Implementing API version 1.18.2-R0.1-SNAPSHOT)
    • Yes

      A plugin may specify a library in the libraries section in the config.yml. The server will then load this library on runtime for this plugin to use. However, if a library makes use of the JavaPlugin.getProvidingPlugin(Class) method, this method will fail with an exception: https://pastie.io/vwspkn/. The reasoning for this seems to be because libraries are loaded via a URLClassLoader and not a PluginClassLoader, causing the aforementioned method to not work properly. This behaviour is different from how it behaves when shading the library during compilation (e.g., via a build tool like Maven or Gradle). In this case, the plugin including the shaded library is loaded via the PluginClassLoader, which avoids this issue.

      A suggestion for a workaround for this is to create an additional class loader, made specifically for libraries, which contains some reference to the parent plugin. JavaPlugin.getProdivingPlugin(Class) could then be adapated to also check for this class loader and possibly return the plugin instance from this class loader. This is just a suggestion, however, any other resolution for this is of course also fine.

      While untested, I imagine the above also holds for JavaPlugin.getPlugin(Class).

      This bug occurs on both Spigot and CraftBukkit, but not Vanilla since it does not support plugins.

      A reproduction plugin is provided below. The following steps reproduce the issue:

      • Put this plugin in the plugins folder on the server and start the server.
      • Join the server as a player which has operator privileges.
      • Type the command '/gui' in chat and run it.
      • There should now be an exception in the server console.

      The source code for this plugin can be found in the attached zip file

            Unassigned Unassigned
            stefvanschie Stef van Schie
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: