Created
July 18, 2019 13:56
-
-
Save Corayzy/a31551ee53e137fed8c92a9a967852f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.plasmanetwork.Statistics; | |
import org.bukkit.Bukkit; | |
import org.bukkit.plugin.PluginManager; | |
import java.io.*; | |
import java.net.MalformedURLException; | |
import java.net.SocketTimeoutException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
public class PluginUpdater { | |
private static Main plugin; | |
private static String link = ("http://plasmanetwork.us/repo/statistics-plugin/statistics-plugin.txt"); | |
private static String jarLink = ("http://plasmanetwork.us/repo/statistics-plugin/Statistics.jar"); | |
public static void downloadFile(String location, String downloadLink, String name, String format) { | |
try { | |
java.net.URL url = new URL(downloadLink); | |
URLConnection connection = url.openConnection(); | |
connection.connect(); | |
InputStream in = connection.getInputStream(); | |
FileOutputStream out = new FileOutputStream(new File(location + File.separator + name + format)); | |
byte[] b = new byte[128]; | |
for (int len = in.read(b); len != -1; len = in.read(b)) { | |
out.write(b, 0, len); | |
} | |
in.close(); | |
out.flush(); | |
out.close(); | |
} catch (MalformedURLException ex) { | |
Main.logError("Could not connect to server to check for update. MalformedURLException."); | |
} catch (SocketTimeoutException ex) { | |
Main.logError("Could not connect to server to check for update. SocketTimeoutException."); | |
} catch (IOException ex) { | |
Main.logError("Error with checking for update. IOException."); | |
} | |
return; | |
} | |
public static void update() { | |
readTxt(); | |
if (!Main.latestVersion.equalsIgnoreCase(Main.currentVersion)) { | |
Main.log("Update required!"); | |
Main.log("Attempting to update!"); | |
downloadFile("plugins" + File.separator, jarLink, Main.pluginName, ".jar"); | |
Main.log("Downloaded " + Main.pluginName + ".jar"); | |
Main.log("New version: " + Main.latestVersion); | |
Main.log("-------------ATTENTION-------------"); | |
Main.log("Must reload/restart to take effect."); | |
Main.log("Plugin is disabled due to new version."); | |
Main.log("-------------ATTENTION-------------"); | |
Main.needsRestart = true; | |
} else { | |
Main.logInfo("Plugin is up to date."); | |
} | |
} | |
public static void readTxt() { | |
try { | |
URL news = new URL(link); | |
BufferedReader in = new BufferedReader(new InputStreamReader(news.openStream())); | |
String inputLine; | |
while ((inputLine = in.readLine()) != null) { | |
Main.latestVersion = inputLine; | |
Main.logInfo("Found latest version: " + Main.latestVersion); | |
} | |
in.close(); | |
return; | |
} catch (MalformedURLException ex) { | |
Main.logError("Eror reading version text/connecting to server. MalformedURLException."); | |
} catch (SocketTimeoutException ex) { | |
Main.logError("Error reading version text/connecting to server. SocketTimeoutException."); | |
} catch (IOException ex) { | |
Main.logError("Error with reading version text. IOException."); | |
} catch (Exception ex) { | |
Main.logError("There was an unknown error reading the version-text. Exception."); | |
} | |
Main.logInfo("Could not set version.."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment