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() {
public void run() {
Updater u = new Updater();
if (getConfig().getBoolean("check-update")) {
try {
if (u.getUpdate(getDescription().getVersion())) {
UPDATE = true;
if (getConfig().getBoolean("check-update")) {
try {
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.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.SortedMap;
import java.util.TreeMap;
import org.json.JSONException;
import org.json.JSONObject;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import com.drtshock.playervaults.PlayerVaults;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
* A class for updating the lang.yml and checking for updates at DBO.
*/
public class Updater extends PlayerVaults {
public class Updater {
SortedMap<String, String> lang = new TreeMap<String, String>();
/**
* Check whether or not there is a new update.
* @param currentVersion The current running version.
* @return Whether or not an update is available.
* @throws IOException Oh no!
*/
public boolean getUpdate(String currentVersion) throws IOException {
JSONObject json;
try {
json = getInfo();
String version = json.getString("dbo_version");
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();
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();
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setUseCaches(false);
Document feed = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(connection.getInputStream());
newVersion = feed.getElementsByTagName("title").item(1).getTextContent().substring(1);
String link = feed.getElementsByTagName("link").item(1).getTextContent();
this.link = new BufferedReader(new InputStreamReader(new URL("http://is.gd/create.php?format=simple&url=" + link).openStream())).readLine();
if(v.contains("SNAPSHOT") && !newVersion.equals(oldVersion)) {
update = false;
return;
}
return false;
update = !newVersion.equals(oldVersion);
}
/**
* Get the information about versions from DBO.
* @return The information in JSON.
* @throws IOException Oh no!
*/
public JSONObject getInfo() throws IOException {
URL url = new URL("http://api.bukget.org/3/plugins/bukkit/playervaults/latest");
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(url.openStream()));
} catch(UnknownHostException e) {
throw new IOException();
}
JSONObject json;
try {
json = new JSONObject(in.readLine()).getJSONArray("versions").getJSONObject(0);
in.close();
return json;
} catch(JSONException e) {
throw new IOException("Oh no!");
}
private String oldVersion;
private String newVersion;
private String link;
private boolean update;
public boolean getUpdate() {
return update;
}
public String getNewVersion() {
return newVersion;
}
public String getLink() {
return link;
}
}