Checking for Plugin-Updates

Discussion in 'Resources' started by Uniclaw, Nov 8, 2012.

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

    Uniclaw

    Hi!

    here a little and easy code, how to check if a newer Version of the plugin exists :)

    Just create a new Class named "checkUpdate", and paste this Code in it: (Don't forget to add the package!)
    Show Spoiler
    Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class checkUpdate {
     
    public static void upCheck(JavaPlugin jp, Float version, String uRl, String upMsg, boolean stopOnUpdate, String stopMsg) throws IOException, MalformedURLException{
    URL url = new URL(uRl);
    url.openConnection();
    URLConnection yc = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
            String inputLine = in.readLine();
            
            Float cv = Float.parseFloat(inputLine);
            if(cv > version){
    System.out.println(upMsg);
    if(stopOnUpdate){
    jp.getServer().getPluginManager().disablePlugin(jp);
    System.out.println(stopMsg);
    }
    }
            
            in.close();
    }
     
    }
     
    }


    How to use?

    It's simple! In the Main Class in the onEnable:

    Code:
    try {
    checkUpdate.upCheck(theplugin, Float version, String "url", String updatemsg, boolean b, String shutdownmsg);
    } catch (MalformedURLException e) {
    System.out.println(String wrongUrl);
    } catch (IOException e) {
    System.out.println(String connectionError);
    }
    
    Explanation:

    theplugin - The Plugin-Mainclass. Just write "this"

    version - The Float of the current Version

    url - The url to a File, more Infos below.

    updatemsg - If a newer Version is available, this String is written in the console

    b - A boolean. If true, the plugin will shutdown if a update is available.

    shutdownmsg - The msg wich appears in the console if a newer version is available & b is true.

    Example in use:

    In the Main:
    Code:
     
    public void onEnable(){
     
    try {
    checkUpdate.upCheck(this, v, "http://dl.uniclaw.net/plugins/update.txt", uN, true, "[" + pluginname + version + "]Plugin shutting down..");
    } catch (MalformedURLException e) {
    System.out.println("[" + pluginname + version + "]Error while searching for updates");
    } catch (IOException e) {
    System.out.println("[" + pluginname + version + "]Connection Error.");
    }
    }
     
     
    //PLUGINNAME & VERSION & UPDATE
    public String pluginname = "Testing PLugin ", version = "v1.0", uN = "[" + pluginname + version + "]A newer Version is availabe. Please re-download & install this Plugin.";
    public float v = 1.0F;
    
    Okay, now the Output if the Float in "update.txt" is greater than the Version, and the shudown option is true:

    Code:
    [Testing PLugin v1.0]A newer Version is availabe. Please re-download & install this Plugin.
    [Testing PLugin v1.0]Plugin shutting down..
    
    Without the Shutdown option:
    Code:
    [Testing PLugin v1.0]A newer Version is availabe. Please re-download & install this Plugin.
    

    That it :) !

    Just note: The File given in the url MUST contains the Version in the first line.

    Greet
     
  2. Offline

    BeeJayMendrinos

    How am i supposed to put the version in the file? i get errors each time i try...
     
Thread Status:
Not open for further replies.

Share This Page