Auto-Updater! Say wat!

Discussion in 'Resources' started by Jayjay110, Jun 4, 2011.

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

    Jayjay110

    Aight so I was looking for a more efficient way to handle updates for my plugin and I thought the best way would be to handle updating it myself. SO, I created a class that everyone can use to auto update there files, SICK AS Ey! :p

    Aight so I made a sort-of tutorial for how it works so yeah, here it is :):

    1. Goto ur main class and create a new Updater object like so, place it in your main class, while were at it, also initialize 2 variables, one is the path to your plugin in the plugin folder, and the other is the download path (For this ill use my mysterybox plugin as an example):
    PHP:
    Updater upd = new Updater(this);
    public 
    String address "http://cloud.github.com/downloads/xXN1pplefaceXx/MysteryBox/MysteryBox.jar";
        public 
    String updatepath "plugins"File.separator "MysteryBox.jar";
    2.Now we must add a check in our onenable to check if we need to update, you can do this by entering:
    PHP:
            log.info("Checking for updates...");
            if(
    upd.updateCheck()){
                
    getServer().getPluginManager().disablePlugin(this);
                for (
    int i 020i++){
                    
    log.info("THIS PLUGIN HAS BEEN UPDATED, RESTART THE SERVER TO ENABLE IT!");
                }
            }else{
                            
    //Put the rest of your code in onEnable in here!
                    
    }
    3. Now we have initialized everything you can go add the class I have made (name it "Updater"):
    PHP:
    package com.myaddress.woot.MyPlugin;

    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    public class 
    Updater {
        private static 
    MyPlugin plugin;
        public 
    Updater(MyPlugin instance) {
            
    plugin instance;                
        }
        public 
    Boolean updateCheck(){
            
    URLConnection conn null;
            try {
                
    URL url = new URL (plugin.address);
                
    conn url.openConnection();
                
    File localfile = new File(plugin.updatepath);
                
    long lastmodifiedurl conn.getLastModified();
                
    long lastmodifiedfile localfile.lastModified();
                if (
    lastmodifiedurl lastmodifiedfile){
                    
    plugin.log.info("Updating...");
                    
    download();
                    return 
    true;
                }else{
                    
    plugin.log.info("No updates available :)");
                    return 
    false;
                }
            } catch (
    MalformedURLException e) {
                
    e.printStackTrace();
            } catch (
    IOException e) {
                
    e.printStackTrace();
            }
            
    plugin.getServer().notify();
            return 
    false;
        }
        public 
    void download(){
            
    OutputStream out null;
            
    URLConnection conn null;
            
    InputStream in null;
            try{
                
    URL url = new URL (plugin.address);
                
    out = new BufferedOutputStream(new FileOutputStream(plugin.updatepath));
                
    conn url.openConnection();
                
    in conn.getInputStream();
                
    byte[] buffer = new byte[1024];
                
    int numRead;
                
    long numWritten 0;
                while ((
    numRead in.read(buffer)) != -1){
                    
    out.write(buffer,0,numRead);
                    
    numWritten += numRead;
                }
            } catch (
    Exception e){
                
    e.printStackTrace();
            } finally {
                try{
                    if (
    in != null){
                        
    in.close();
                    }
                    if (
    out != null){
                        
    out.close();
                    }
                } catch (
    IOException ioe){
                    
    ioe.printStackTrace();
                }
            }
        }
    }
    4. Now I havnt really explained this very well, mainly because it 12:42am, but if you have any questions, ask away, and btw I dont know how to auto restart the server...yet, I believe its easy but I just wanted to give this to people fast so I didnt have time to figure it out :p

    Btw step 4 means it works :p :D good luck using this :)
     
    Lolmewn and Connor Mahaffey like this.
  2. Offline

    Connor Mahaffey


    This is actually pretty cool! Especially since there is no central way to download bukkit plugin updates (yet).

    This part is a little unnecessary tho XD

    Code:
    for (int i = 0; i < 20; i++){
         log.info("THIS PLUGIN HAS BEEN UPDATED, RESTART THE SERVER TO ENABLE IT!");
    }
     
  3. Offline

    alta189

    I have a question, I tried to make something similiar to this, but if I reloaded the server it threw errors... Have you tested that?
     
  4. Offline

    Jayjay110

    Lol I used that because I cant figure out how to reload the server after everythings loaded :3 lol :p

    Nah, I cant figure out how to reload it after its loaded, once you figure out tell me :p
     
  5. Offline

    alta189

    Use a command sender to reload it
     
  6. Offline

    Shamebot

    there's a Server.reload()
     
  7. Offline

    alta189

    Didnt know that thanks
     
  8. Offline

    Sethcran

    The idea is neat, but given that not all plugins have full backwards compatibility nor is it even always possible, may want to add in something to determine versions and which builds it is compatible with.
     
  9. Offline

    alta189

    I agree
     
  10. Offline

    Jayjay110

    Well to do that I believe you would have to remotely download and edit the file all the time, or just add a check for a txt file displaying the version, rather than a direct link, I guess :p And server.reload() wont work until the servers loaded, which I cant figure out how to run when it is loaded :3
     
  11. Offline

    Shamebot

    Do you want to delay the execution of reload()?
     
  12. Offline

    Jayjay110

    Yes so it restarts after the server goes done(1.wajtever secomds
     
  13. Offline

    WMisiedjan

    This code is all for in the plugin right? How does it download the file to the same folder the plugin is in when it's already in use? And you can add a event hanlder for when server is done right?
     
  14. Offline

    Shamebot

    Easiest would be to use the BukkitScheduler and a sync delayed task and wait a certain amount of time.
    Or you could listen on the Logger for "Done"
     
  15. Offline

    Lolmewn Retired Staff

    Cool <3
     
  16. Offline

    Jayjay110

    Yes it does and you can change it if you want just look at the source

    Yes but what if another plugin logs done, it will break the server badly

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

    TAT

  18. Offline

    Jayjay110

  19. Offline

    Darkman2412

    Sorry for bump, but you can reload single plugins after disabling it with
    Code:
    server.getPluginManager().loadPlugin(new File("./plugins/{namehere}.jar"));
     
  20. Offline

    jeffadkins51

    For plugins that cannot use this, you could simply just load a file lets just say "version.txt" And in that file only being the latest version: e.g 1.1
    Now have the plugin simply check that file, if the double INT is higher then the current version, it would then read the other files e.g "Main.java", "test.java" .. Etc. By simply checking all the files by the extension .java
     
  21. Offline

    desmin88

    Or just download the plugin and put it in the root/updates folder which CB will automatically update any plugins in the updates folder on next reload
     
  22. Offline

    Jayjay110

    really is there a folder for that? I didnt know lol
     
  23. Offline

    Lithixium

    @Jayjay110 Says 'The Constructor Updater(PvPlus) is undefined' when I try Updater upd = new Updater(this); I have the class named Updater and the constructor (
    Code:
    private static PvPlus plugin;
        public Updater(PvPlus instance) {
            plugin = instance;
        }
    ) and still gives me this error in Eclipse.
    EDIT:
    Fixed it! Turns out I imported org.bukkit.fillr.Updater :p
     
    Jayjay110 likes this.
  24. Offline

    Jayjay110

    Does anyone still use this?
     
  25. Offline

    Lithixium

    Yup! Is very useful, but I modified it to get a version file and to not update whenever its avaliable, instead just ask an admin everytime they login to update by typing /<plugin name> update
     
  26. Offline

    Addramyr

    Just started using somthing like it for updating my plugins, thanks for the intro dude.
     
  27. Offline

    oyasunadev

  28. Offline

    ludo0777

    How does it know when there is a later version?
     
  29. Offline

    Jayjay110

    I dont use this anymore, but it checks the files last modified time and if it is newer than the old, it downloads it, you should make it make a temp file and on reload install it, dont use this!
     
  30. Offline

    ludo0777

    Well then can you explain that way instead because I don't know how :(

    So does this not work?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 16, 2016
Thread Status:
Not open for further replies.

Share This Page