From 7011a8d8c5396618dd14c0a15fa937163def24d9 Mon Sep 17 00:00:00 2001 From: extendedclip Date: Sun, 8 Apr 2018 04:04:15 -0400 Subject: [PATCH] Supports ECloud API v2, added Gson parsing, expansion version history support --- .../expansion/cloud/CloudExpansion.java | 230 +++++++++++++----- .../cloud/ExpansionCloudManager.java | 63 +++-- 2 files changed, 197 insertions(+), 96 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/expansion/cloud/CloudExpansion.java b/src/main/java/me/clip/placeholderapi/expansion/cloud/CloudExpansion.java index 0f64de3..5416a11 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/cloud/CloudExpansion.java +++ b/src/main/java/me/clip/placeholderapi/expansion/cloud/CloudExpansion.java @@ -22,79 +22,183 @@ package me.clip.placeholderapi.expansion.cloud; import me.clip.placeholderapi.util.TimeUtil; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; public class CloudExpansion { - private String name, author, version, description, link, releaseNotes; - - private boolean hasExpansion, shouldUpdate; - - private long lastUpdate; + private String name, + author, + latest_version, + description, + source_url, + dependency_url; - public CloudExpansion(String name, String author, String version, String description, String link) { - this.name = name; - this.author = author; - this.version = version; - this.description = description; - this.link = link; - } - - public String getName() { - return name; + private boolean hasExpansion, + shouldUpdate, + verified; + + private long last_update, + ratings_count; + + private double average_rating; + + private List placeholders; + + private List versions; + + public CloudExpansion() { } - public String getAuthor() { - return author; - } - - public String getVersion() { - return version; - } - - public String getDescription() { - return description; - } - - public String getLink() { - return link; - } - - public boolean hasExpansion() { - return hasExpansion; - } - - public void setHasExpansion(boolean hasExpansion) { - this.hasExpansion = hasExpansion; - } - - public boolean shouldUpdate() { - return shouldUpdate; - } - - public void setShouldUpdate(boolean shouldUpdate) { - this.shouldUpdate = shouldUpdate; - } - - public long getLastUpdate() { - return lastUpdate; - } - - public void setLastUpdate(long lastUpdate) { - this.lastUpdate = lastUpdate; - } - - public String getReleaseNotes() { - return releaseNotes; - } - - public void setReleaseNotes(String releaseNotes) { - this.releaseNotes = releaseNotes; - } - public String getTimeSinceLastUpdate() { int time = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - getLastUpdate()); return TimeUtil.getTime(time); } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public Version getVersion() { + return getLatestVersion() == null ? null : getVersion(getLatestVersion()); + } + + public Version getVersion(String version) { + return versions == null ? null : versions.stream() + .filter(v -> v.getVersion().equals(version)) + .findFirst() + .orElse(null); + } + + public List getAvailableVersions() { + return versions.stream().map(Version::getVersion).collect(Collectors.toList()); + } + + public String getLatestVersion() { + return latest_version; + } + + public void setLatestVersion(String latest_version) { + this.latest_version = latest_version; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getSourceUrl() { + return source_url; + } + + public void setSourceUrl(String source_url) { + this.source_url = source_url; + } + + public String getDependencyUrl() { + return dependency_url; + } + + public void setDependencyUrl(String dependency_url) { + this.dependency_url = dependency_url; + } + + public boolean hasExpansion() { + return hasExpansion; + } + + public void setHasExpansion(boolean hasExpansion) { + this.hasExpansion = hasExpansion; + } + + public boolean shouldUpdate() { + return shouldUpdate; + } + + public void setShouldUpdate(boolean shouldUpdate) { + this.shouldUpdate = shouldUpdate; + } + + public boolean isVerified() { + return verified; + } + + public long getLastUpdate() { + return last_update; + } + + public void setLastUpdate(long last_update) { + this.last_update = last_update; + } + + public long getRatingsCount() { + return ratings_count; + } + + public double getAverage_rating() { + return average_rating; + } + + public List getPlaceholders() { + return placeholders; + } + + public void setPlaceholders(List placeholders) { + this.placeholders = placeholders; + } + + public List getVersions() { + return versions; + } + + public void setVersions(List versions) { + this.versions = versions; + } + + public class Version { + private String url, version, release_notes; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getReleaseNotes() { + return release_notes; + } + + public void setReleaseNotes(String release_notes) { + this.release_notes = release_notes; + } + } } diff --git a/src/main/java/me/clip/placeholderapi/expansion/cloud/ExpansionCloudManager.java b/src/main/java/me/clip/placeholderapi/expansion/cloud/ExpansionCloudManager.java index a492b54..eb249f9 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/cloud/ExpansionCloudManager.java +++ b/src/main/java/me/clip/placeholderapi/expansion/cloud/ExpansionCloudManager.java @@ -20,6 +20,7 @@ */ package me.clip.placeholderapi.expansion.cloud; +import com.google.gson.Gson; import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; @@ -42,13 +43,15 @@ import java.util.stream.IntStream; public class ExpansionCloudManager { private PlaceholderAPIPlugin plugin; - private final String API = "http://api.extendedclip.com/"; + private final String API = "http://api.extendedclip.com/v2/"; private final File dir; private final TreeMap remote = new TreeMap<>(); private final List downloading = new ArrayList<>(); + private Gson gson; public ExpansionCloudManager(PlaceholderAPIPlugin instance) { plugin = instance; + gson = new Gson(); dir = new File(instance.getDataFolder() + File.separator + "expansions"); if (!dir.exists()) { try { @@ -62,6 +65,7 @@ public class ExpansionCloudManager { public void clean() { remote.clear(); downloading.clear(); + gson = null; } public boolean isDownloading(String expansion) { @@ -157,7 +161,7 @@ public class ExpansionCloudManager { IntStream.range(start, end).forEach(n -> ex.put(n, map.get(n))); return ex; } - + public void fetch() { plugin.getLogger().info("Fetching available expansion information..."); @@ -218,39 +222,21 @@ public class ExpansionCloudManager { for (Object o : jo.keySet()) { - JSONObject sub = (JSONObject) jo.get(o); - String name = o.toString(); - String author = (String) sub.get("author"); - String version = (String) sub.get("version"); - String link = (String) sub.get("link"); - String description = (String) sub.get("description"); - String notes = ""; - long update = -1; + JSONObject sub = (JSONObject) jo.get(o); - if (sub.get("release_notes") != null) { - notes = (String) sub.get("release_notes"); + CloudExpansion ce = gson.fromJson(sub.toJSONString(), CloudExpansion.class); + + if (ce.getLatestVersion() == null || ce.getVersion(ce.getLatestVersion()) == null) { + continue; } - if (sub.get("last_update") != null) { + ce.setName(o.toString()); - Object u = sub.get("last_update"); - - if (u instanceof Long) { - update = (long) sub.get("last_update"); - } - } - - CloudExpansion ce = new CloudExpansion(name, author, version, description, link); - - ce.setReleaseNotes(notes); - - ce.setLastUpdate(update); - - PlaceholderExpansion ex = plugin.getExpansionManager().getRegisteredExpansion(name); + PlaceholderExpansion ex = plugin.getExpansionManager().getRegisteredExpansion(ce.getName()); if (ex != null && ex.isRegistered()) { ce.setHasExpansion(true); - if (!ex.getVersion().equals(version)) { + if (!ex.getVersion().equals(ce.getLatestVersion())) { ce.setShouldUpdate(true); } } @@ -312,32 +298,43 @@ public class ExpansionCloudManager { } } } - + public void downloadExpansion(final String player, final CloudExpansion ex) { + downloadExpansion(player, ex, ex.getLatestVersion()); + } + + public void downloadExpansion(final String player, final CloudExpansion ex, final String version) { if (downloading.contains(ex.getName())) { return; } + + final CloudExpansion.Version ver = ex.getVersions() + .stream() + .filter(v -> v.getVersion().equals(version)) + .findFirst() + .orElse(null); + - if (ex.getLink() == null) { + if (ver == null) { return; } downloading.add(ex.getName()); - plugin.getLogger().info("Attempting download of expansion: " + ex.getName() + (player != null ? " by user: " + player : "") + " from url: " + ex.getLink()); + plugin.getLogger().info("Attempting download of expansion: " + ex.getName() + (player != null ? " by user: " + player : "") + " from url: " + ver.getUrl()); Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try { - download(new URL(ex.getLink()), ex.getName()); + download(new URL(ver.getUrl()), ex.getName()); plugin.getLogger().info("Download of expansion: " + ex.getName() + " complete!"); } catch (Exception e) { - plugin.getLogger().warning("Failed to download expansion: " + ex.getName() + " from: " + ex.getLink()); + plugin.getLogger().warning("Failed to download expansion: " + ex.getName() + " from: " + ver.getUrl()); Bukkit.getScheduler().runTask(plugin, () -> {