Supports ECloud API v2, added Gson parsing, expansion version history support

This commit is contained in:
extendedclip 2018-04-08 04:04:15 -04:00
parent 92d30ea4cb
commit 7011a8d8c5
2 changed files with 197 additions and 96 deletions

View File

@ -22,43 +22,104 @@ 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 String name,
author,
latest_version,
description,
source_url,
dependency_url;
private boolean hasExpansion, shouldUpdate;
private boolean hasExpansion,
shouldUpdate,
verified;
private long lastUpdate;
private long last_update,
ratings_count;
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;
private double average_rating;
private List<String> placeholders;
private List<Version> versions;
public CloudExpansion() {
}
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 String getVersion() {
return version;
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<String> 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 String getLink() {
return link;
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() {
@ -77,24 +138,67 @@ public class CloudExpansion {
this.shouldUpdate = shouldUpdate;
}
public long getLastUpdate() {
return lastUpdate;
public boolean isVerified() {
return verified;
}
public void setLastUpdate(long lastUpdate) {
this.lastUpdate = lastUpdate;
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<String> getPlaceholders() {
return placeholders;
}
public void setPlaceholders(List<String> placeholders) {
this.placeholders = placeholders;
}
public List<Version> getVersions() {
return versions;
}
public void setVersions(List<Version> 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 releaseNotes;
return release_notes;
}
public void setReleaseNotes(String releaseNotes) {
this.releaseNotes = releaseNotes;
public void setReleaseNotes(String release_notes) {
this.release_notes = release_notes;
}
public String getTimeSinceLastUpdate() {
int time = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - getLastUpdate());
return TimeUtil.getTime(time);
}
}

View File

@ -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<Integer, CloudExpansion> remote = new TreeMap<>();
private final List<String> 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) {
@ -219,38 +223,20 @@ 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;
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);
}
}
@ -314,30 +300,41 @@ 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;
}
if (ex.getLink() == null) {
final CloudExpansion.Version ver = ex.getVersions()
.stream()
.filter(v -> v.getVersion().equals(version))
.findFirst()
.orElse(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, () -> {