diff --git a/src/main/java/ml/volder/transporter/classes/api/TransporterPriceApi.java b/src/main/java/ml/volder/transporter/classes/api/TransporterPriceApi.java index 90da6d5..bc3410c 100644 --- a/src/main/java/ml/volder/transporter/classes/api/TransporterPriceApi.java +++ b/src/main/java/ml/volder/transporter/classes/api/TransporterPriceApi.java @@ -40,34 +40,24 @@ private JsonObject getJsonFromUrl(String stringURL) { } reader.close(); - // Parse JSON response - JsonElement jsonElement = JsonParser.parseString(response.toString()); - return jsonElement.getAsJsonObject(); + //JsonElement jsonElement = JsonParser.parseString(response.toString()); + return new JsonParser().parse(response.toString()).getAsJsonObject(); } catch (Exception e) { + UnikAPI.LOGGER.printStackTrace(Logger.LOG_LEVEL.FINER, e); UnikAPI.LOGGER.debug("Failed to get JSON from URL: " + stringURL, Logger.DEBUG_LEVEL.MEDIUM); } return null; } private String getUrl() { - if(url == null) { - JsonObject jsonObject = getJsonFromUrl("https://raw.githubusercontent.com/TransporterAddon/TransporterPriceAPI/main/info.json"); + return "https://transporter-price-api-479a42778d13.herokuapp.com"; - String url = (jsonObject != null && jsonObject.get("url") != null) ? jsonObject.get("url").getAsString() : null; - - if(url != null) { - this.url = url; - } else { - this.url = "https://transporter-price-api-479a42778d13.herokuapp.com"; - } - - UnikAPI.LOGGER.debug("Transporter Price API URL: " + url, Logger.DEBUG_LEVEL.MEDIUM); - } - return url; } public int getSellValueFromPriceServer(String itemName) { + UnikAPI.LOGGER.debug("Hent item pris: " + itemName, Logger.DEBUG_LEVEL.LOW); JsonObject jsonObject = getJsonFromUrl(getUrl() + "/items/" + itemName); + UnikAPI.LOGGER.debug("Hent item pris (response): " + jsonObject, Logger.DEBUG_LEVEL.TESTING); if(jsonObject == null) { return 0; } diff --git a/src/main/java/ml/volder/transporter/updater/UpdateManager.java b/src/main/java/ml/volder/transporter/updater/UpdateManager.java index 7eb6554..52ad83f 100644 --- a/src/main/java/ml/volder/transporter/updater/UpdateManager.java +++ b/src/main/java/ml/volder/transporter/updater/UpdateManager.java @@ -1,5 +1,8 @@ package ml.volder.transporter.updater; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import ml.volder.transporter.TransporterAddon; import ml.volder.transporter.gui.UpdateFailedScreen; import ml.volder.unikapi.UnikAPI; @@ -33,8 +36,35 @@ private static class UpdateInfoJson { } public static boolean isUpToDate() { - //Updated through FlintUpdater. - return true; + InputStream transporterInputStream = UpdateManager.class.getClassLoader().getResourceAsStream("transporter.json"); + if(transporterInputStream != null){ + Reader r = null; + try { + r = new InputStreamReader(transporterInputStream, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return true; + } + JsonObject transporterJson = new JsonParser().parse(r).getAsJsonObject(); + if(transporterJson.has("autoUpdate") && !transporterJson.get("autoUpdate").getAsBoolean()) + return true; + } + Gson gson = new Gson(); + UpdateInfoJson localUpdateInfo; + UpdateInfoJson remoteUpdateInfo; + try { + InputStream updateInfoSteam = UpdateManager.class.getClassLoader().getResourceAsStream("transporter/updateInfo.json"); + Reader reader = new InputStreamReader(updateInfoSteam, "UTF-8"); + localUpdateInfo = gson.fromJson(reader, UpdateInfoJson.class); + + InputStream remoteInputStream = new URL("https://github.com/TFSMads/transporter/releases/latest/download/updateInfo.json").openStream(); + reader = new InputStreamReader(remoteInputStream, "UTF-8"); + remoteUpdateInfo = gson.fromJson(reader, UpdateInfoJson.class); + } catch (IOException ignored) { + return true; + } + + currentVersion = localUpdateInfo.version; + return localUpdateInfo.version.equals(remoteUpdateInfo.version); } diff --git a/src/main/resources/transporter.json b/src/main/resources/transporter.json new file mode 100644 index 0000000..80dfa3b --- /dev/null +++ b/src/main/resources/transporter.json @@ -0,0 +1,3 @@ +{ + "autoUpdate": true +} \ No newline at end of file