mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2024-11-18 00:46:55 +01:00
Supports ECloud API v2, added Gson parsing, expansion version history support
This commit is contained in:
parent
92d30ea4cb
commit
7011a8d8c5
@ -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 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;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
private List<String> placeholders;
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
private List<Version> versions;
|
||||
|
||||
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 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 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 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<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 release_notes;
|
||||
}
|
||||
|
||||
public void setReleaseNotes(String release_notes) {
|
||||
this.release_notes = release_notes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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, () -> {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user