Auto Plugin Updater

Discussion in 'BungeeCord Plugin Development' started by MeDen, Feb 3, 2020.

Thread Status:
Not open for further replies.
  1. There is a code, but it is for bukkit. Who can remake it under a bungeecord?

    Code (Java):
    package com.lenis0012.bukkit.waz.utils;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.lang.reflect.Method;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.logging.Level;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class WazUpdater {
          private final String dlLink = "http://myWebsite.com/myPlugin/latest.jar";
        private final String versionLink = "http://myWebsite.com/myPlugin/version.txt";
        private Plugin plugin;
     
        public WazUpdater(Plugin plugin) {
            this.plugin = plugin;
        }
     
        public void update() {
            int oldVersion = this.getVersionFromString(plugin.getDescription().getVersion());
            String path = this.getFilePath();
         
            try {
                URL url = new URL(versionLink);
                URLConnection con = url.openConnection();
                InputStreamReader isr = new InputStreamReader(con.getInputStream());
                BufferedReader reader = new BufferedReader(isr);
                int newVersion = this.getVersionFromString(reader.readLine());
                reader.close();
             
                if(newVersion > oldVersion) {
                    url = new URL(dlLink);
                    con = url.openConnection();
                    InputStream in = con.getInputStream();
                    FileOutputStream out = new FileOutputStream(path);
                    byte[] buffer = new byte[1024];
                    int size = 0;
                    while((size = in.read(buffer)) != -1) {
                        out.write(buffer, 0, size);
                    }
                 
                    out.close();
                    in.close();
                    plugin.getLogger().log(Level.INFO, "Succesfully updated plugin to v" + newVersion);
                    plugin.getLogger().log(Level.INFO, "Reload/restart server to enable changes");
                }
            } catch(IOException e) {
                plugin.getLogger().log(Level.SEVERE, "Failed to auto-update", e);
            }
        }
     
        private String getFilePath() {
            if(plugin instanceof JavaPlugin) {
                try {
                    Method method = JavaPlugin.class.getDeclaredMethod("getFile");
                    boolean wasAccessible = method.isAccessible();
                    method.setAccessible(true);
                    File file = (File) method.invoke(plugin);
                    method.setAccessible(wasAccessible);
                 
                    return file.getPath();
                } catch(Exception e) {
                    return "plugins" + File.separator + plugin.getName();
                }
            } else {
                return "plugins" + File.separator + plugin.getName();
            }
        }
     
        private int getVersionFromString(String from) {
            String result = "";
            Pattern pattern = Pattern.compile("d+");
            Matcher matcher = pattern.matcher(from);
         
            while(matcher.find()) {
                result += matcher.group();
            }
         
            return result.isEmpty() ? 0 : Integer.parseInt(result);
        }
    }
     
    #1 MeDen, Feb 3, 2020
    Last edited: Feb 4, 2020
  2. Here's a working one, using spiget. You can modify the download links and check links if you want. Works both with bukkit and spigot.
     
  3. Thanks
     
Thread Status:
Not open for further replies.