Load plugin before all others.

Discussion in 'Plugin Development' started by Cowboys1919, Jan 27, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Cowboys1919

    Long story short, I want to use onPluginEnable to link in dependencies. However, the plugins I depend on is dynamic, and is not found out until after enabling of my plugin.

    I tried setting "load: STARTUP" in plugin.yml, but, this is too early and Bukkit.getConsoleSender() and such are null.

    Any way to do this? Or any other workarounds in mind?

    -Thanks

    Also, a load after all others would work as well.

    Well, I made a class to get it easily, just make sure you check if it's null and generally don't use it when your plugins enabling, wait for an event you know will be called later.

    Code:
    import org.bukkit.Bukkit;
     
    public class GetPl<T>
    {
        @SuppressWarnings("unchecked")
        public T get(String name)
        {
            return (T) Bukkit.getPluginManager().getPlugin(name);
        }
    }
    Use, for example, to return if a player doesn't have access to a block in PlayerInteractEvent:

    Code:
    if (new GetPl<com.massivecraft.factions.P>().get("Factions") != null)
                        if (!FactionsPlayerListener.canPlayerUseBlock(e.getPlayer(), e.getClickedBlock(), true))
                            return;
    The question is, how efficient is Bukkit.getPluginManager().getPlugin().
    Is it ok to use this often?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    Woobie

    http://wiki.bukkit.org/Plugin_YAML
    loadbefore: [1Plugin, 2Plugin]

    Pretty sure bukkit has a method that triggers before onEnable, so you might be able to load the plugin by using that method.
    Edit: onLoad() {
     
  3. Offline

    Cowboys1919

    Woobie
    They're dynamic, so I don't know the names of the plugins before startup :)
     
  4. 2 options:
    1. onEnable of your plugin, look through all plugins that were loaded before yours and register your depenendies n stuff.
      Then, listen for PluginEnableEvent and PluginDisableEvent and handle the dependencies there as well.
      That way, it doesn't matter when your plugin enables, you will always catch all the others.
    2. Alternatively, simply delay your dependency setup by one tick so all plugins are guaranteed to load at that point. Use the bukkit scheduler.

    The first method is a bit "cleaner" and also takes manual disabling and enabling of plugins into account. The second one is a bit simpler to implement, more like the lazy way out ;)
     
    Cowboys1919 likes this.
  5. Offline

    perwin

    Third option:
    Learn about static blocks of Java code. You have for example this simple class:
    Code:java
    1.  
    2. import java.util.logging.Logger;
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class MyPlugin extends JavaPlugin {
    6. private static Logger console;
    7.  
    8. // Here the static block starts:
    9. static {
    10. MyPlugin.console = Logger.getLogger("Minecraft");
    11. MyPlugin.console.info("[MyPlugin] Message posted before bukkit's Loading message.");
    12. }
    13. // Here it ends
    14.  
    15. public void onEnable() {
    16. MyPlugin.console.info("[MyPlugin] Plugin has been enabled.");
    17. }
    18.  
    19. public void onDisable() {
    20. MyPlugin.console.info("[MyPlugin] Plugin has been disabled.");
    21. }
    22. }
    23.  

    Simply: everything you write into the static block is executed before onEnable method, when the bukkit notices your plugin in plugins folder.
     
Thread Status:
Not open for further replies.

Share This Page