Do a lot of work on the updater, make it compatible with dev builds

This commit is contained in:
gomeow
2013-06-08 16:09:07 -04:00
parent 62f4646a1b
commit e4def202f5
2 changed files with 46 additions and 62 deletions
@@ -101,16 +101,19 @@ public class PlayerVaults extends JavaPlugin {
new BukkitRunnable() { new BukkitRunnable() {
public void run() { public void run() {
Updater u = new Updater();
if (getConfig().getBoolean("check-update")) { if (getConfig().getBoolean("check-update")) {
try { if (getConfig().getBoolean("check-update")) {
if (u.getUpdate(getDescription().getVersion())) { try {
UPDATE = true; Updater u = new Updater(getDescription().getVersion());
if (UPDATE = u.getUpdate()) {
LINK = u.getLink();
NEWVERSION = u.getNewVersion();
}
} catch(Exception e) {
getLogger().log(Level.WARNING, "Failed to check for updates.");
getLogger().log(Level.WARNING, "Report this stack trace to gomeow.");
e.printStackTrace();
} }
} catch(IOException e) {
log.log(Level.WARNING, "PlayerVaults: Failed to check for updates.");
log.log(Level.WARNING, "PlayerVaults: Report this stack trace to drtshock and gomeow.");
e.printStackTrace();
} }
} }
} }
@@ -3,67 +3,48 @@ package com.drtshock.playervaults.util;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException;
import java.util.SortedMap;
import java.util.TreeMap;
import org.json.JSONException; import javax.xml.parsers.DocumentBuilderFactory;
import org.json.JSONObject; import javax.xml.parsers.ParserConfigurationException;
import com.drtshock.playervaults.PlayerVaults; import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/** public class Updater {
* A class for updating the lang.yml and checking for updates at DBO.
*/
public class Updater extends PlayerVaults {
SortedMap<String, String> lang = new TreeMap<String, String>(); public Updater(String v) throws SAXException, IOException, ParserConfigurationException {
oldVersion = v.substring(0, 5);
/** HttpURLConnection connection = (HttpURLConnection) new URL("http://dev.bukkit.org/projects/playervaults/files.rss").openConnection();
* Check whether or not there is a new update. connection.setConnectTimeout(10000);
* @param currentVersion The current running version. connection.setReadTimeout(10000);
* @return Whether or not an update is available. connection.setUseCaches(false);
* @throws IOException Oh no! Document feed = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(connection.getInputStream());
*/ newVersion = feed.getElementsByTagName("title").item(1).getTextContent().substring(1);
public boolean getUpdate(String currentVersion) throws IOException { String link = feed.getElementsByTagName("link").item(1).getTextContent();
JSONObject json; this.link = new BufferedReader(new InputStreamReader(new URL("http://is.gd/create.php?format=simple&url=" + link).openStream())).readLine();
try { if(v.contains("SNAPSHOT") && !newVersion.equals(oldVersion)) {
json = getInfo(); update = false;
String version = json.getString("dbo_version"); return;
String link = json.getString("link");
PlayerVaults.NEWVERSION = version;
String goodLink = new BufferedReader(new InputStreamReader(new URL("http://is.gd/create.php?format=simple&url=" + link).openStream())).readLine();
PlayerVaults.LINK = goodLink;
if (!version.equalsIgnoreCase(currentVersion)) {
return true;
}
} catch(JSONException e) {
throw new IOException();
} }
return false; update = !newVersion.equals(oldVersion);
} }
/** private String oldVersion;
* Get the information about versions from DBO. private String newVersion;
* @return The information in JSON. private String link;
* @throws IOException Oh no! private boolean update;
*/
public JSONObject getInfo() throws IOException { public boolean getUpdate() {
URL url = new URL("http://api.bukget.org/3/plugins/bukkit/playervaults/latest"); return update;
BufferedReader in = null; }
try {
in = new BufferedReader(new InputStreamReader(url.openStream())); public String getNewVersion() {
} catch(UnknownHostException e) { return newVersion;
throw new IOException(); }
}
JSONObject json; public String getLink() {
try { return link;
json = new JSONObject(in.readLine()).getJSONArray("versions").getJSONObject(0);
in.close();
return json;
} catch(JSONException e) {
throw new IOException("Oh no!");
}
} }
} }