Tutorial Checking For New Version

Discussion in 'Resources' started by Gingerbreadman, Feb 25, 2015.

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

    Gingerbreadman

    Hello everyone and in this tutorial you will be learning on making your plugin detect if there is a new version available then do whatever you like xD

    We will start of by making a new class called "VersionThread" that extends Thread
    Code:
    public class VersionThread extends Thread {
    }
    
    Next we will add a constructor and some variables
    Code:
    public class VersionThread extends Thread {
        private JavaPlugin plugin;
        private String url;
        private double version = Double.parseDouble(plugin.getDescription().getVersion());
    
        public VersionThread(JavaPlugin plugin, String url) {
            this.plugin = plugin;
            this.url = url;
        }
    }
    
    The 3 variables are needed later on the tutorial, the "plugin" variable is for getting the version of the installed plugin, the "url" variable is for downloading the latest plugin version, and finally the version double is the current version installed.

    We will override a method called "run" now,
    We made a BufferedReader to download the up to date version and check if its the same as the one that is currently installed.
    I added a method called updateAvailable, and it will execute when there is a new version available, I just made it say "New version available" but you can do alot of thing like auto updating, etc xD
    Code:
    public class VersionThread extends Thread {
        private JavaPlugin plugin;
        private String url;
        private double version = Double.parseDouble(plugin.getDescription().getVersion());
     
        public VersionThread(JavaPlugin plugin, String url) {
            this.plugin = plugin;
            this.url = url;
        }
     
        @Override
        public void run() {
            try{
                BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
                double response = Double.parseDouble(br.readLine());
                if(response > version) {
                    updateAvailable();
                    br.close();
                }
            }catch(Exception e) {
                System.out.println("Version File Invalid!");
            }
        }
     
        private void updateAvailable() {
            System.out.println("New Version Available!");
        }
    }
    
    To use this thread on start up you will have to add
    Code:
    new VersionThread(this, "url").start();
    to your onEnable method! (Example of url format
    URL (open)
     
    Last edited: Feb 26, 2015
  2. Offline

    Totom3

    Code:
     public void run() throws NumberFormatException
    Throws NumberFormatException? Why not just catch it? Most server admins aren't able to read stack-traces, better tell them explicitly the file version is invalid.

    Code:
    private double version = Double.parseDouble(plugin.getDescription().getVersion());
    You should mention that you need the version to be a valid number. I've seen multiple times plugin versions such as "1.0.2", or even "0.5.1 - SNAPSHOT #346".

    Code:
    while(true) {
    // Stuff
    }
    Server's offline: method loops indefinitely. Error 404, no such file? Method loops indefinitely. Error 403, no access? Method loops indefinitely. Everything goes all right, but no new version is available? Method loops indefinitely. Not only that, if there's an IOException, the logs will be ABSOLUTELY spammed. You are going to spam many server admins with irrelevant information (for them). Also while you're there, why not let the plugin do something when a new version is available? Something like a "NewVersionAvailableHandler" interface with a handle() method.

    Code:
    private void updateAvailable() {
        Logger logger = Bukkit.getLogger();
        logger.log(Level.INFO, "New version available!");
    }
    Oh, please. What version? What plugin? Which new version is available? Which one is the current one? Why are you stealing Minecraft's logger?
     
    nlthijs48, Gingerbreadman and Goblom like this.
  3. Offline

    Gingerbreadman

    @Totom3 thank you for your response, I will update my work to make it better, the only thing is the updateAvailable method was an example xD

    EDIT: btw at the end of the tutorial I give a format to use -_-
     
    Last edited: Feb 26, 2015
Thread Status:
Not open for further replies.

Share This Page