New way of update checking

This commit is contained in:
gomeow
2013-05-05 21:23:24 -07:00
parent f41114fce4
commit 457aad54f9
3 changed files with 37 additions and 45 deletions
@@ -10,16 +10,16 @@ import java.util.TreeMap;
import java.util.logging.Level;
import org.bukkit.configuration.file.YamlConfiguration;
import org.json.JSONException;
import org.json.JSONObject;
import com.drtshock.playervaults.PlayerVaults;
public class Updater extends PlayerVaults {
SortedMap<String, String> lang = new TreeMap<String, String>();
String version;
public Updater(String version) {
this.version = version;
public Updater() {
YamlConfiguration langConf = super.getLang();
for(Lang item:Lang.values()) {
if(langConf.getString(item.getPath()) == null) {
@@ -35,45 +35,37 @@ public class Updater extends PlayerVaults {
}
}
String newVersion = "";
public String getNewVersion() {
return this.newVersion;
}
public boolean getUpdate() throws Exception {
String version = this.version;
URL url = new URL("http://dev.bukkit.org/server-mods/playervaults/files.rss");
InputStreamReader isr = null;
public boolean getUpdate() throws IOException {
JSONObject json;
try {
isr = new InputStreamReader(url.openStream());
} catch(UnknownHostException e) {
return false;
}
BufferedReader in = new BufferedReader(isr);
String line;
int lineNum = 0;
while ((line = in.readLine()) != null) {
if(line.length() != line.replace("<title>", "").length()) {
line = line.replaceAll("<title>", "").replaceAll("</title>", "").replaceAll(" ", "").substring(1);
if(lineNum == 1) {
this.newVersion = line;
Integer newVer = Integer.parseInt(line.replace(".", ""));
Integer oldVer = Integer.parseInt(version.replace(".", ""));
if(oldVer < newVer) {
return true;
}
else if(oldVer > newVer) {
return false;
}
else {
return false;
}
}
lineNum = lineNum + 1;
json = getInfo();
String version = json.getString("dbo_version");
String link = json.getString("link");
PlayerVaults.LINK = link;
if(!version.equalsIgnoreCase(super.getDescription().getVersion())) {
return true;
}
} catch(JSONException e) {
throw new IOException();
}
in.close();
return false;
}
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) {
}
return null;
}
}