1.16.5 How to check if a plugin is installed (before it is enabled)?

Discussion in 'Spigot Plugin Development' started by BoBoBalloon, Oct 11, 2021.

Thread Status:
Not open for further replies.
  1. Hey guys quick question, is it possible for me to check if a plugin is installed before it is enabled? I know that you can check if a plugin is enabled with a plugin manager like so but that only works if a plugin is enabled. I am looking if this is possible while on the onLoad() method. The reason I am asking is because I have made a custom item plugin and I would like to add api support to register custom objects in my plugin's cache if a certain plugin is installed. Because of this, I cant add the dependency to my plugin.yml and just check on the onEnable() method due to the fact that I don't know what plugins will be depended on by compile time.

    Thanks for the insight,
    BoBoBalloon
     
  2. First, add the plugin you wish to add support to your plugin's "softdepend" section in "plugin.yml".
    Then, you can check if the plugin named "PluginX" (whatever its called) is installed with this:
    Code (Java):
    if (getServer().getPluginManager().getPlugin("PluginX") != null) {
      // Do stuff
    }
     
    • Useful Useful x 1
  3. Thank you for your response but given the answer you gave I feel like it is safe to assume you didn't bother reading the content of my post. As I explained above, that method will not work given my present circumstance
     
    • Funny Funny x 1
  4. I think you didn't read what I wrote - it's different from what you wrote completely and does exactly what you need. The "if" statement will let you know if "PluginX" is present or not regardless of dependency.
     
    • Like Like x 1
    • Informative Informative x 1
  5. It will check if the plugin is enabled, I would like to do this on the onLoad method, by that point ZERO plugins are enabled, I just need to check if they are installed
     
    • Creative Creative x 1
  6. no, it'll check if the plugin is present
    if you wanna see if its enabled, you would do Plugin#isEnabled
     
    • Like Like x 1
    • Agree Agree x 1
  7. Decompiled isPluginEnabled method:

    Code (Java):
        /**
         * Checks if the given plugin is enabled or not
         *
         * @param plugin Plugin to check
         * @return true if the plugin is enabled, otherwise false
         */

        @Override
        public boolean isPluginEnabled(@Nullable Plugin plugin) {
            if ((plugin != null) && (plugins.contains(plugin))) {
                return plugin.isEnabled();
            } else {
                return false;
            }
        }
    So unless IntelliJ is messing with me, it looks like it calls the isEnabled method
     
    • Optimistic Optimistic x 1
    • Creative Creative x 1
  8. for like the 3rd time...
    Code (Java):
    if (getServer().getPluginManager().getPlugin("PluginX") != null) {
    DOES NOT check if the plugin is enabled, it checks if the plugin is present on the server
     
    • Winner Winner x 2
    • Like Like x 1
  9. I just did a little test using Essentials and one of my plugins, Indeed you can check if a plugin is present (enabled or not) in onLoad

    My test:
    Code (Text):
       
        @Override
        public void onLoad() {
            if(Bukkit.getPluginManager().getPlugin("Essentials") != null) {
                getLogger().log(Level.INFO, "Essentials is installed");
            }
        }
    [00:34:09] [Server thread/INFO]: [Wands] Loading Wands v0.1
    [00:34:09] [Server thread/INFO]: Essentials is installed
     
    • Winner Winner x 2
  10. The issue is, if my plugin loads before the plugin you are looking for, I cant check if it is in the cache. As I cant put the plugin I am looking for in my soft-depends or depends in my plugin.yml
     
  11. If you want to support a different plugin you'll need to put it in the "depends" or "softdepends" section anyway, so that solves that problem.
    If some other plugin will want to use yours then they will need to take care of it by themselves by being dependant on yours.
     
  12. The whole point of using soft-depends is in case the plugin isn't installed. There's no reason why you shouldn't be able to "put the plugin I am looking for in my soft-depends"
     
    • Like Like x 1
    • Agree Agree x 1
  13. im super confused with your plan here
    If you are able to check in your CODE whether a plugin is installed or not, you should be able to plop it in your plugin.yml as a soft depend
    incase you didnt know, basically all soft depend does is forces your plugin to load AFTER all soft depends, if they're not existent on the server, they're ignored, no errors, no issues.

    IF you dont know which plugins you're depending on, then what's the point of checking if they're available, you can't do anything with said plugin if you dont know what its about. This is all very confusing.

    Keep in mind, in the onEnable, all plugins would be loaded at this point, just they havent enabled yet, so you WOULD be able to check if they're present on the server

    If you've ever watched the server load, you will notice:
    - server starts
    - plugins load (are added to a list of plugins on the server)
    - worlds load
    - plugins enable (actually load the plugin running the plugin's onEnable method)
     

  14. This is also not true, if your plugin loads before another plugin you can still check if it's installed or not

    Test (with telling Essentials to load after my plugin Wands):
    Code (Text):
       
        @Override
        public void onLoad() {
            if(Bukkit.getPluginManager().getPlugin("Essentials") != null) {
                getLogger().log(Level.INFO, "Essentials is installed");
            }
        }
    [18:34:05] [Server thread/INFO]: [Wands] Loading Wands v0.1
    [18:34:05] [Server thread/INFO]: Essentials is installed
    [18:34:05] [Server thread/INFO]: [Essentials] Loading Essentials v2.19.0-dev+57-89c1e3b
    [18:34:05] [Server thread/INFO]: [EssentialsChat] Loading EssentialsChat v2.19.0-dev+57-89c1e3b

    As you can see my plugin started loading before Essentials and it was still able to tell me if it's installed or not.
     
    • Winner Winner x 1
  15. Really, in that case it is exactly what I am looking for. I didn't think that bukkit would add every plugin to the cache and then load them all, I would think it would just load up right after it adds each one to the cache. Also I reread the code you guys put and you are 100% right. Thanks for all the help and I apologize that you had to deal with my dumbass.
     
Thread Status:
Not open for further replies.