Plugin Reloading Itself.

Discussion in 'Plugin Development' started by jebbo, Feb 25, 2015.

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

    jebbo

    So a 'friend' of my who is also a developer told me that there is a way to reload the plugin itself without lagging the server at all. I'm wondering how this works since disabling and enabling the plugin won't work, it doesnt load up the updated file. Maybe you guys know if it is possible at all and if so help me a bit to get on track.

    Thanks!
     
  2. you can load a class from a jar-file and create an object of it. this way, you can load the class which extends javaplugin and is registered in the plugin.yml and create an object of the new version of your plugin. then, i think you can add this object somehow to the pluginlist of bukkit, call onDisable() of your old one and remove your old one. after that you call the onEnable() method on your new plugin. i was about to look for you, how to add a plugin to bukkit manually, but i was to lazy to search through the documentation and i don't have a bukkit.jar anymore, so i can not figure it out by trial-error
     
  3. Offline

    MajorSkillage

    onEnable(){
    saveConfig();
    }
    onDisable(){
    saveConfig();
    }
    plugin you can setEnabled() or disabled()
     
  4. @MajorSkillage as far as i understood, he has a plugin.jar in his plugin folder. server is started already. now he made a new version of his plugin. plugin-2.jar if you want so :) now he wants to reload the plugin so the new jar is loaded. but he does not want to call the reload-command of the server to do this. so he wants to load the new jar and the new pluginclass from the folder, initialize it and replace it with the old plugin while runtime. but you showed me a point i didn't think about. safing all the values and the current state of the plugin and loading it after reloading it.
     
  5. Offline

    MajorSkillage

    onCommand(){
    if(cmd.getName().equalsIgnoreCase("pluginname reload") && player.hasPermission("my.permission.node")){
    saveConfig();
    }
     
  6. Offline

    Gingerbreadman

    @jebbo so you have 2 plugins? and you want to unload one and load one?
    you can do this by adding the second plugin into the build path of the first one, you do all your stuff in the first one and when you want to move all your stuff to the second one, just invoke a method in the second one like "gatherData" from the first one, before you send the data to the second one remember to load it using Bukkit.getServer().getPluginManager().load("SecondPlugin");
    and make the "gatherData" do Bukkit.getServer().getPluginManager().unload("FirstPlugin");
     
  7. Offline

    MajorSkillage

    Oh wow i didn't notice it is 2 commands, well that is easy just register the command in one plugin.yml only and onCommandPreprocessEvent just simply reload both of them if the same reload command is executed similar to my first example. It depends how your plugin is being stored which depends the best way for this to happen.
     
  8. Offline

    jebbo

    I don't want to reload the Config. I want in my Plugin Named Plugin1 for example a command /reloadthis. With this command, the Plugin Named Plugin1 should reload itself. Its not possible with unloading and loading the plugin. Again, I'm not 100% sure if this is possible, I was told there is a way that this is possible to be able to reload only the plugin itself to reduce lag on a server. thanks
     
  9. Offline

    MajorSkillage

    Plugin pluginname = getServer().getPluginManager().getPlugin("pluginname").saveResource("you wot m8", true);
    there is also getServer().getPluginManager().getPlugin("name").getPluginLoader().loadPlugin(fileName);
    if that helps :)
     
  10. Offline

    jebbo

    I have this so far:

    Code:
    public void reload(){
            File file = new File("plugins", "Plugin.jar");
            try {
                getServer().getPluginManager().disablePlugin(this);
                Plugin pl = getServer().getPluginManager().getPlugin("Plugin").getPluginLoader().loadPlugin(file);
                getServer().getPluginManager().enablePlugin(pl);
            } catch (UnknownDependencyException | InvalidPluginException e) {
                e.printStackTrace();
            }
        }
    After a 'reload' no commands work anymore :/
     
  11. Offline

    mythbusterma

    @jebbo

    Why do you need to unload the plugin anyway? There's pretty much no reason to need to reload your plugin. When most plugins "reload," all they actually do is read their configuration files for updates, which is usually all that is required.
     
    Datdenkikniet likes this.
  12. Offline

    jebbo

    @mythbusterma
    What if I want to quick update my Plugin and don't want to reload the whole server with a lot of people on it?
     
  13. Offline

    mythbusterma

    @jebbo

    You should maybe reconsider a development cycle that doesn't require updates so frequently that becomes an issue? Also, last I checked, doing /reload only takes ~5 seconds on most servers anyway.

    Anyway, to do it properly you would need to create a separate plugin that unloads your plugin, waits for you to replace the file, then loads it again. Not like anybody has already written a plugin to do this, but....
     
Thread Status:
Not open for further replies.

Share This Page