mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-10-07 11:45:26 +02:00
@@ -26,8 +26,11 @@ import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
public class CloudExpansion {
|
||||
private String name, author,
|
||||
|
||||
private String name,
|
||||
author,
|
||||
latest_version,
|
||||
description,
|
||||
source_url,
|
||||
@@ -71,12 +74,14 @@ public class CloudExpansion {
|
||||
}
|
||||
|
||||
public Version getVersion() {
|
||||
return latest_version == null ? null : getVersion(latest_version);
|
||||
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);
|
||||
.filter(v -> v.getVersion().equals(version))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public List<String> getAvailableVersions() {
|
||||
@@ -135,10 +140,6 @@ public class CloudExpansion {
|
||||
return verified;
|
||||
}
|
||||
|
||||
public void setVerified(boolean verified) {
|
||||
this.verified = verified;
|
||||
}
|
||||
|
||||
public long getLastUpdate() {
|
||||
return last_update;
|
||||
}
|
||||
@@ -155,10 +156,6 @@ public class CloudExpansion {
|
||||
return average_rating;
|
||||
}
|
||||
|
||||
public void setAverage_rating(double average_rating) {
|
||||
this.average_rating = average_rating;
|
||||
}
|
||||
|
||||
public List<String> getPlaceholders() {
|
||||
return placeholders;
|
||||
}
|
||||
@@ -175,11 +172,7 @@ public class CloudExpansion {
|
||||
this.versions = versions;
|
||||
}
|
||||
|
||||
public void setRatings_count(long ratings_count) {
|
||||
this.ratings_count = ratings_count;
|
||||
}
|
||||
|
||||
public static class Version {
|
||||
public class Version {
|
||||
private String url, version, release_notes;
|
||||
|
||||
public String getUrl() {
|
||||
|
@@ -37,29 +37,37 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ExpansionCloudManager {
|
||||
|
||||
private static final String API_URL = "http://api.extendedclip.com/v2/";
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
|
||||
private final PlaceholderAPIPlugin plugin;
|
||||
private final File expansionsDir;
|
||||
|
||||
private final Set<String> downloading = new HashSet<>();
|
||||
private final List<String> downloading = new ArrayList<>();
|
||||
private final Map<Integer, CloudExpansion> remote = new TreeMap<>();
|
||||
|
||||
|
||||
public ExpansionCloudManager(PlaceholderAPIPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
expansionsDir = new File(plugin.getDataFolder(), "expansions");
|
||||
|
||||
if (expansionsDir.mkdirs()) {
|
||||
final boolean result = expansionsDir.mkdirs();
|
||||
if (result) {
|
||||
plugin.getLogger().info("Created Expansions Directory");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void clean() {
|
||||
remote.clear();
|
||||
downloading.clear();
|
||||
}
|
||||
|
||||
|
||||
public Map<Integer, CloudExpansion> getCloudExpansions() {
|
||||
return remote;
|
||||
}
|
||||
@@ -72,6 +80,7 @@ public class ExpansionCloudManager {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
|
||||
public int getCloudAuthorCount() {
|
||||
return remote.values()
|
||||
.stream()
|
||||
@@ -117,10 +126,14 @@ public class ExpansionCloudManager {
|
||||
|
||||
|
||||
public int getPagesAvailable(Map<Integer, CloudExpansion> map, int amount) {
|
||||
if (map == null) return 0;
|
||||
if (map == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pages = map.size() > 0 ? 1 : 0;
|
||||
if (pages == 0) return 0;
|
||||
if (pages == 0) {
|
||||
return pages;
|
||||
}
|
||||
|
||||
if (map.size() > amount) {
|
||||
pages = map.size() / amount;
|
||||
@@ -146,11 +159,12 @@ public class ExpansionCloudManager {
|
||||
return ex;
|
||||
}
|
||||
|
||||
|
||||
public void fetch(boolean allowUnverified) {
|
||||
plugin.getLogger().info("Fetching available expansion information...");
|
||||
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
Map<String, CloudExpansion> data = new HashMap<>();
|
||||
final Map<String, CloudExpansion> data = new HashMap<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(API_URL).openStream()))) {
|
||||
data.putAll(GSON.fromJson(reader, new TypeToken<Map<String, CloudExpansion>>() {
|
||||
@@ -159,12 +173,11 @@ public class ExpansionCloudManager {
|
||||
if (plugin.getPlaceholderAPIConfig().isDebugMode()) {
|
||||
ex.printStackTrace();
|
||||
} else {
|
||||
plugin.getLogger().warning("Unable to fetch expansions!\nThere was an error with the server host connecting to the PlaceholderAPI eCloud (https://api" +
|
||||
".extendedclip.com/v2/)");
|
||||
plugin.getLogger().warning("Unable to fetch expansions!\nThere was an error with the server host connecting to the PlaceholderAPI eCloud (https://api.extendedclip.com/v2/)");
|
||||
}
|
||||
}
|
||||
|
||||
List<CloudExpansion> unsorted = new ArrayList<>();
|
||||
final List<CloudExpansion> unsorted = new ArrayList<>();
|
||||
|
||||
data.forEach((name, cexp) -> {
|
||||
if ((allowUnverified || cexp.isVerified()) && cexp.getLatestVersion() != null && cexp.getVersion(cexp.getLatestVersion()) != null) {
|
||||
@@ -191,6 +204,7 @@ public class ExpansionCloudManager {
|
||||
}
|
||||
|
||||
plugin.getLogger().info(count + " placeholder expansions are available on the cloud.");
|
||||
|
||||
long updates = getToUpdateCount();
|
||||
|
||||
if (updates > 0) {
|
||||
@@ -206,15 +220,19 @@ public class ExpansionCloudManager {
|
||||
|
||||
private void download(URL url, String name) throws IOException {
|
||||
InputStream is = null;
|
||||
|
||||
FileOutputStream fos = null;
|
||||
|
||||
try {
|
||||
URLConnection urlConn = url.openConnection();
|
||||
|
||||
is = urlConn.getInputStream();
|
||||
|
||||
fos = new FileOutputStream(
|
||||
expansionsDir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar");
|
||||
|
||||
byte[] buffer = new byte[is.available()];
|
||||
|
||||
int l;
|
||||
|
||||
while ((l = is.read(buffer)) > 0) {
|
||||
@@ -234,35 +252,42 @@ public class ExpansionCloudManager {
|
||||
}
|
||||
|
||||
|
||||
public void downloadExpansion(String player, CloudExpansion ex) {
|
||||
public void downloadExpansion(final String player, final CloudExpansion ex) {
|
||||
downloadExpansion(player, ex, ex.getLatestVersion());
|
||||
}
|
||||
|
||||
public void downloadExpansion(String player, CloudExpansion ex, String version) {
|
||||
public void downloadExpansion(final String player, final CloudExpansion ex, final String version) {
|
||||
if (downloading.contains(ex.getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
CloudExpansion.Version ver = ex.getVersions()
|
||||
final CloudExpansion.Version ver = ex.getVersions()
|
||||
.stream()
|
||||
.filter(v -> v.getVersion().equals(version))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
if (ver == null) return;
|
||||
if (ver == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
downloading.add(ex.getName());
|
||||
|
||||
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(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: " + ver.getUrl());
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
|
||||
downloading.remove(ex.getName());
|
||||
|
||||
if (player != null) {
|
||||
@@ -289,6 +314,7 @@ public class ExpansionCloudManager {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user