Solved Load plugin after world loads

Discussion in 'Spigot Plugin Development' started by Plochem, Aug 21, 2018.

  1. Short question: How can I load my plugin after MultiverseCore loads all my worlds?

    Edit: in my onenable, I read location objects from file, but it’ll throw an error since the world hasn’t been loaded yet.
     
    #1 Plochem, Aug 21, 2018
    Last edited: Aug 21, 2018
  2. Use the scheduler to run your plugin enable 1 tick after
     
    • Winner Winner x 1
  3. Add it as a softdepend: [MultiverseCore] if your plugin can work without MultiverseCore then if it's found your plugin will load after it, if your plugin won't work without MultiverseCore just add depends: [MultiverseCore] to your plugin.yml
     
  4. Am I better off loading the worlds myself?

    I put a scheduler in my onEnable?

    Doesn't work.
     
    #4 Plochem, Aug 21, 2018
    Last edited: Aug 21, 2018
  5. Why not use onWorldLoadEvent to listen for world's to load after your plugin has been enabled?
    You can use onWorldUnloadEvent to listen for when world's are unloaded.
     
  6. I have to read the location objects from the file after the worlds have been loaded, not when the worlds started loading.
     
  7. You put a task in the onEnable, if I recall correctly, even 0 ticks might work. I remember having an issue with my plugin not broadcasting in the onEnable because not everything was loaded. Making the scheduler take this responsibility forces everything else to load, and for the broadcast to be put on the back burner.

    I'm not saying this will work for your case, but it is definitely worth a shot.

    Also, wouldn't call myself thorough if I didn't mention, of course, the Plugin YAML documentation. There is a "load" field that you can set to POSTWORLD. As for whether or not this means all worlds, or just the first one, dunno.

    Cheers buddy
     
    • Like Like x 1
  8. FWIW according to the Bukkit plugin YAML docs load: defaults to postworld....
    Code (Text):
    Explicitly state when the plugin should be loaded. if not supplied will default to postworld
    I had a situation similar to the OP that I had to do some trickery to get it to kick in after a specific world was loaded. When I get home i'll dig out the code as I can't remember right off how I did it.
     
  9. I have multiple location objects from different worlds. if i use this listener, I would have to get what world is being loaded, then go through all my locations and check if the location’s world matches what is being loaded. That has an efficiency of O(# of worlds * # of locations). I’ll only use this way if I can’t figure out a more efficient method.
     
  10. ah found what I did! I just delayed registering events and such anything my plugin needed to set up by x ticks.

    Code (Text):
       
        @Override
        public void onEnable()
        {
            plugin = this;
            System.out.println("[MegaMachines] - Machine Plugin by dNiym Enabled!");
            new BukkitRunnable() {
               
                @Override
                public void run() {
                    System.out.println("Delayed load of elevator config.");
                    loadConfig();
                    plugin.getServer().getPluginManager().registerEvents(plugin,plugin);


                }
               
            }.runTaskLater(this.plugin, 200);
       
           
        }
       
    This plugin was having problems because some worlds wouldn't load as fast as the plugin tried to read locations from the config, thus the world didn't quite exist yet. Delaying reading the config in and registering the events solved it!
     
  11. I just have one problem with that: how do you know you should wait for 200 ticks? Doesn’t the number of worlds affect the length of time?
     
  12. No, again as I said before, you only need 1 tick
     
  13. I waited 200 ticks, it was an arbitrary delay. One tick may work as suggested above but this was for a modded server that took quite some time to load all the worlds. I just went with 200 and it worked fine, despite the worlds taking a long time to load.
     
  14. Well, if the functionality of your plugin is vital to the server (stuff will break without registered listeners etc), I recommend setting the whitelist on and turning it off after the delay happens. A lot can happen in 10 seconds. This way, the server owner or only people who are whitelisted can break the functionality.

    The other option is to see if 1 tick will work just fine.
     
  15. That's not how it works, the scheduler runs only when the server finishes setting everything up
     
  16. Why doesn't it work with load: POSTWORLD and a softdepend on MultiWorld? It should toposort all world loading in front of (ie before) your plugin.

    Scheduling something to run on the first tick is bad imho, because the game is already running at that point and you're essentially introducing lag. also, it forces other plugins depending on you to run on tick 1, and so on.

    I never had issues loading locations from different worlds, and I don't lag the server, cause dependency issues and ignore all sane practices.
     
  17. It was more to just delay a while to read the config and set up my listener. Nothing in my plugin was mission critical enough that a 200tick delay would cause a problem.




    Sent from my iPhone using Tapatalk
     
  18. Again, unless you are asyncing your plugin, there is nothing you have to wait for, as the scheduler only starts running when all plugins, all worlds and all anything have finished initializing, ie, when the "for help type ?" line is printed.

    But don't use the scheduler!
     
  19. If the bukkit yaml documentation is correct then the default is post world, but I ran into the same issue as the OP couldn’t get locations because the world wasn’t ready, putting my plugin on a 200tick delay solved it. Again 1 tick may be fine but nothing I was doing was that critical.

    It may very well be a bug of sorts but literally all I was doing was reading a couple locations in from a config file and it could not find them as the world was not ready,


    Sent from my iPhone using Tapatalk