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 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.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class CloudExpansion { 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) { private double average_rating;
this.name = name;
this.author = author; private List<String> placeholders;
this.version = version;
this.description = description; private List<Version> versions;
this.link = link;
public CloudExpansion() {
}
public String getTimeSinceLastUpdate() {
int time = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - getLastUpdate());
return TimeUtil.getTime(time);
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
public String getAuthor() { public String getAuthor() {
return author; return author;
} }
public String getVersion() { public void setAuthor(String author) {
return version; 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() { public String getDescription() {
return description; return description;
} }
public String getLink() { public void setDescription(String description) {
return link; 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() { public boolean hasExpansion() {
@ -77,24 +138,67 @@ public class CloudExpansion {
this.shouldUpdate = shouldUpdate; this.shouldUpdate = shouldUpdate;
} }
public long getLastUpdate() { public boolean isVerified() {
return lastUpdate; return verified;
} }
public void setLastUpdate(long lastUpdate) { public long getLastUpdate() {
this.lastUpdate = lastUpdate; 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() { public String getReleaseNotes() {
return releaseNotes; return release_notes;
} }
public void setReleaseNotes(String releaseNotes) { public void setReleaseNotes(String release_notes) {
this.releaseNotes = releaseNotes; 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; package me.clip.placeholderapi.expansion.cloud;
import com.google.gson.Gson;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
@ -42,13 +43,15 @@ import java.util.stream.IntStream;
public class ExpansionCloudManager { public class ExpansionCloudManager {
private PlaceholderAPIPlugin plugin; 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 File dir;
private final TreeMap<Integer, CloudExpansion> remote = new TreeMap<>(); private final TreeMap<Integer, CloudExpansion> remote = new TreeMap<>();
private final List<String> downloading = new ArrayList<>(); private final List<String> downloading = new ArrayList<>();
private Gson gson;
public ExpansionCloudManager(PlaceholderAPIPlugin instance) { public ExpansionCloudManager(PlaceholderAPIPlugin instance) {
plugin = instance; plugin = instance;
gson = new Gson();
dir = new File(instance.getDataFolder() + File.separator + "expansions"); dir = new File(instance.getDataFolder() + File.separator + "expansions");
if (!dir.exists()) { if (!dir.exists()) {
try { try {
@ -62,6 +65,7 @@ public class ExpansionCloudManager {
public void clean() { public void clean() {
remote.clear(); remote.clear();
downloading.clear(); downloading.clear();
gson = null;
} }
public boolean isDownloading(String expansion) { public boolean isDownloading(String expansion) {
@ -219,38 +223,20 @@ public class ExpansionCloudManager {
for (Object o : jo.keySet()) { for (Object o : jo.keySet()) {
JSONObject sub = (JSONObject) jo.get(o); 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) { CloudExpansion ce = gson.fromJson(sub.toJSONString(), CloudExpansion.class);
notes = (String) sub.get("release_notes");
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"); PlaceholderExpansion ex = plugin.getExpansionManager().getRegisteredExpansion(ce.getName());
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);
if (ex != null && ex.isRegistered()) { if (ex != null && ex.isRegistered()) {
ce.setHasExpansion(true); ce.setHasExpansion(true);
if (!ex.getVersion().equals(version)) { if (!ex.getVersion().equals(ce.getLatestVersion())) {
ce.setShouldUpdate(true); ce.setShouldUpdate(true);
} }
} }
@ -314,30 +300,41 @@ public class ExpansionCloudManager {
} }
public void downloadExpansion(final String player, final CloudExpansion ex) { 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())) { if (downloading.contains(ex.getName())) {
return; 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; return;
} }
downloading.add(ex.getName()); 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, () -> { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try { try {
download(new URL(ex.getLink()), ex.getName()); download(new URL(ver.getUrl()), ex.getName());
plugin.getLogger().info("Download of expansion: " + ex.getName() + " complete!"); plugin.getLogger().info("Download of expansion: " + ex.getName() + " complete!");
} catch (Exception e) { } 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, () -> { Bukkit.getScheduler().runTask(plugin, () -> {